[6152] | 1 | /* |
---|
| 2 | Copyright 2006 by Sean Luke and George Mason University |
---|
| 3 | Licensed under the Academic Free License version 3.0 |
---|
| 4 | See the file "LICENSE" for more information |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | package ec.rule; |
---|
| 9 | |
---|
| 10 | import java.util.Enumeration; |
---|
| 11 | import java.util.Hashtable; |
---|
| 12 | |
---|
| 13 | import ec.simple.SimpleInitializer; |
---|
| 14 | import ec.util.Parameter; |
---|
| 15 | import ec.EvolutionState; |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * RuleInitializer.java |
---|
| 19 | * |
---|
| 20 | * Created: Fri Sep 14 14:00:02 2001 |
---|
| 21 | * By: Liviu Panait |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | /** A SimpleInitializer subclass designed to be used with rules. Basically, |
---|
| 26 | the RuleInitializer sets up the RuleConstraints and RuleSetConstraints cliques |
---|
| 27 | at setup() time, and does nothing else different from SimpleInitializer. |
---|
| 28 | The RuleInitializer also specifies the parameter bases for the RuleSetConstraints |
---|
| 29 | and RuleConstraints objects. |
---|
| 30 | |
---|
| 31 | <p><b>Parameter bases</b><br> |
---|
| 32 | <table> |
---|
| 33 | <tr><td valign=top><tt>rule.rsc</tt></td> |
---|
| 34 | <td>RuleSetConstraints</td></tr> |
---|
| 35 | <tr><td valign=top><tt>rule.rc</tt></td> |
---|
| 36 | <td>RuleConstraints</td></tr> |
---|
| 37 | </table> |
---|
| 38 | */ |
---|
| 39 | |
---|
| 40 | public class RuleInitializer extends SimpleInitializer |
---|
| 41 | { |
---|
| 42 | // used just here, so far as I know :-) |
---|
| 43 | public static final int SIZE_OF_BYTE = 256; |
---|
| 44 | public final static String P_RULESETCONSTRAINTS = "rsc"; |
---|
| 45 | public final static String P_RULECONSTRAINTS = "rc"; |
---|
| 46 | public final static String P_SIZE = "size"; |
---|
| 47 | |
---|
| 48 | public Hashtable ruleConstraintRepository; |
---|
| 49 | public RuleConstraints[] ruleConstraints; |
---|
| 50 | public byte numRuleConstraints; |
---|
| 51 | |
---|
| 52 | public Hashtable ruleSetConstraintRepository; |
---|
| 53 | public RuleSetConstraints[] ruleSetConstraints; |
---|
| 54 | public byte numRuleSetConstraints; |
---|
| 55 | |
---|
| 56 | /** Sets up the RuleConstraints and RuleSetConstraints cliques. */ |
---|
| 57 | public void setup(final EvolutionState state, final Parameter base) |
---|
| 58 | { |
---|
| 59 | super.setup(state,base); |
---|
| 60 | |
---|
| 61 | ruleConstraintRepository = new Hashtable(); |
---|
| 62 | ruleConstraints = new RuleConstraints[SIZE_OF_BYTE]; |
---|
| 63 | numRuleConstraints = 0; |
---|
| 64 | |
---|
| 65 | ruleSetConstraintRepository = new Hashtable(); |
---|
| 66 | ruleSetConstraints = new RuleSetConstraints[SIZE_OF_BYTE]; |
---|
| 67 | numRuleSetConstraints = 0; |
---|
| 68 | |
---|
| 69 | // Now let's load our constraints and function sets also. |
---|
| 70 | // This is done in a very specific order, don't change it or things |
---|
| 71 | // will break. |
---|
| 72 | setupConstraints( |
---|
| 73 | state, RuleDefaults.base().push( P_RULECONSTRAINTS ) ); |
---|
| 74 | setupRuleSetConstraints( |
---|
| 75 | state, RuleDefaults.base().push( P_RULESETCONSTRAINTS ) ); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /** Sets up all the RuleConstraints, loading them from the parameter |
---|
| 79 | file. This must be called before anything is called which refers |
---|
| 80 | to a type by name. */ |
---|
| 81 | |
---|
| 82 | public void setupConstraints(final EvolutionState state, |
---|
| 83 | final Parameter base) |
---|
| 84 | { |
---|
| 85 | state.output.message("Processing Rule Constraints"); |
---|
| 86 | |
---|
| 87 | // How many RuleConstraints do we have? |
---|
| 88 | int x = state.parameters.getInt(base.push(P_SIZE),null,1); |
---|
| 89 | if (x<=0) |
---|
| 90 | state.output.fatal("The number of rule constraints must be at least 1.",base.push(P_SIZE)); |
---|
| 91 | |
---|
| 92 | // Load our constraints |
---|
| 93 | for (int y=0;y<x;y++) |
---|
| 94 | { |
---|
| 95 | RuleConstraints c; |
---|
| 96 | // Figure the constraints class |
---|
| 97 | if (state.parameters.exists(base.push(""+y), null)) |
---|
| 98 | c = (RuleConstraints)(state.parameters.getInstanceForParameterEq( |
---|
| 99 | base.push(""+y),null,RuleConstraints.class)); |
---|
| 100 | else |
---|
| 101 | { |
---|
| 102 | state.output.message("No Rule Constraints specified, assuming the default class: ec.rule.RuleConstraints for " + base.push(""+y)); |
---|
| 103 | c = new RuleConstraints(); |
---|
| 104 | } |
---|
| 105 | c.setup(state,base.push(""+y)); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | // set our constraints array up |
---|
| 109 | Enumeration e = ruleConstraintRepository.elements(); |
---|
| 110 | while(e.hasMoreElements()) |
---|
| 111 | { |
---|
| 112 | RuleConstraints c = (RuleConstraints)(e.nextElement()); |
---|
| 113 | c.constraintNumber = numRuleConstraints; |
---|
| 114 | ruleConstraints[numRuleConstraints] = c; |
---|
| 115 | numRuleConstraints++; |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | public void setupRuleSetConstraints(final EvolutionState state, |
---|
| 120 | final Parameter base) |
---|
| 121 | { |
---|
| 122 | state.output.message("Processing Ruleset Constraints"); |
---|
| 123 | // How many RuleSetConstraints do we have? |
---|
| 124 | int x = state.parameters.getInt(base.push(P_SIZE),null,1); |
---|
| 125 | if (x<=0) |
---|
| 126 | state.output.fatal("The number of RuleSetConstraints must be at least 1.",base.push(P_SIZE)); |
---|
| 127 | |
---|
| 128 | // Load our RuleSetConstraints |
---|
| 129 | for (int y=0;y<x;y++) |
---|
| 130 | { |
---|
| 131 | RuleSetConstraints c; |
---|
| 132 | // Figure the RuleSetConstraints class |
---|
| 133 | if (state.parameters.exists(base.push(""+y), null)) |
---|
| 134 | c = (RuleSetConstraints)(state.parameters.getInstanceForParameterEq( |
---|
| 135 | base.push(""+y),null,RuleSetConstraints.class)); |
---|
| 136 | else |
---|
| 137 | { |
---|
| 138 | state.output.message("No RuleSetConstraints specified, assuming the default class: ec.gp.RuleSetConstraints for " + base.push(""+y)); |
---|
| 139 | c = new RuleSetConstraints(); |
---|
| 140 | } |
---|
| 141 | c.setup(state,base.push(""+y)); |
---|
| 142 | ruleSetConstraints[y] = c; |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | } |
---|