[6152] | 1 | /* |
---|
| 2 | Copyright 2006 by Sean Luke |
---|
| 3 | Licensed under the Academic Free License version 3.0 |
---|
| 4 | See the file "LICENSE" for more information |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | package ec.gp; |
---|
| 9 | import ec.*; |
---|
| 10 | import ec.util.*; |
---|
| 11 | |
---|
| 12 | /* GPNodeConstraints.java |
---|
| 13 | * |
---|
| 14 | * Created: Fri Aug 27 17:40:40 1999 |
---|
| 15 | * By: Sean Luke |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | /** |
---|
| 20 | * A GPNodeConstraints is a Clique which defines constraint information |
---|
| 21 | * common to many different GPNode functions, namely return types, |
---|
| 22 | * child types, and number of children. |
---|
| 23 | * GPNodeConstraints have unique names by which |
---|
| 24 | * they are identified. |
---|
| 25 | * |
---|
| 26 | * <p>In adding new things to GPNodeConstraints, you should ask yourself |
---|
| 27 | * the following questions: first, is this something that takes up too |
---|
| 28 | * much memory to store in GPNodes themselves? second, is this something |
---|
| 29 | * that needs to be accessed very rapidly, so cannot be implemented |
---|
| 30 | * as a method call in a GPNode? third, can this be shared among |
---|
| 31 | * different GPNodes, even ones representing different functions? |
---|
| 32 | |
---|
| 33 | <p><b>Parameters</b><br> |
---|
| 34 | <table> |
---|
| 35 | <tr><td valign=top><i>base</i>.<tt>size</tt><br> |
---|
| 36 | <font size=-1>int >= 1</font></td> |
---|
| 37 | <td valign=top>(number of node constraints)</td></tr> |
---|
| 38 | |
---|
| 39 | <tr><td valign=top><i>base.n</i>.<tt>name</tt><br> |
---|
| 40 | <font size=-1>String</font></td> |
---|
| 41 | <td valign=top>(name of node constraint <i>n</i>)</td></tr> |
---|
| 42 | |
---|
| 43 | <tr><td valign=top><i>base.n</i>.<tt>returns</tt><br> |
---|
| 44 | <font size=-1>String</font></td> |
---|
| 45 | <td valign=top>(return type for node constraint <i>n</i>)</td></tr> |
---|
| 46 | |
---|
| 47 | <tr><td valign=top><i>base.n</i>.<tt>size</tt><br> |
---|
| 48 | <font size=-1>int >= 1</font></td> |
---|
| 49 | <td valign=top>(number of child arguments for node constraint <i>n</i>)</td></tr> |
---|
| 50 | |
---|
| 51 | <tr><td valign=top><i>base.n</i>.<tt>child.</tt><i>m</i><br> |
---|
| 52 | <font size=-1>String</font></td> |
---|
| 53 | <td valign=top>(name of type for child argument <i>m</i> of node constraint <i>n</i>)</td></tr> |
---|
| 54 | |
---|
| 55 | <tr><td valign=top><i>base</i>.<tt>prob</tt><br> |
---|
| 56 | <font size=-1>float >= 0.0</font></td> |
---|
| 57 | <td valign=top>(auxillary probability of selection -- used by ec.gp.build.PTC1 and ec.gp.build.PTC2)</td></tr> |
---|
| 58 | |
---|
| 59 | </table> |
---|
| 60 | |
---|
| 61 | * @author Sean Luke |
---|
| 62 | * @version 1.0 |
---|
| 63 | */ |
---|
| 64 | |
---|
| 65 | public class GPNodeConstraints implements Clique |
---|
| 66 | { |
---|
| 67 | public static final int SIZE_OF_BYTE = 256; |
---|
| 68 | public final static String P_NAME = "name"; |
---|
| 69 | public final static String P_RETURNS = "returns"; |
---|
| 70 | public final static String P_CHILD = "child"; |
---|
| 71 | public final static String P_SIZE = "size"; |
---|
| 72 | public final static String P_PROBABILITY = "prob"; |
---|
| 73 | public final static float DEFAULT_PROBABILITY = 1.0f; |
---|
| 74 | |
---|
| 75 | /** Probability of selection -- an auxillary measure mostly used by PTC1/PTC2 |
---|
| 76 | right now */ |
---|
| 77 | public float probabilityOfSelection; |
---|
| 78 | |
---|
| 79 | /** The byte value of the constraints -- we can only have 256 of them */ |
---|
| 80 | public byte constraintNumber; |
---|
| 81 | |
---|
| 82 | /** The return type for a GPNode */ |
---|
| 83 | public GPType returntype; |
---|
| 84 | |
---|
| 85 | /** The children types for a GPNode */ |
---|
| 86 | public GPType[] childtypes; |
---|
| 87 | |
---|
| 88 | /** The name of the GPNodeConstraints object -- this is NOT the |
---|
| 89 | name of the GPNode */ |
---|
| 90 | public String name; |
---|
| 91 | |
---|
| 92 | public String toString() { return name; } |
---|
| 93 | |
---|
| 94 | /** A little memory optimization: if GPNodes have no children, they are welcome to |
---|
| 95 | use share this zero-sized array as their children array. */ |
---|
| 96 | public GPNode zeroChildren[] = new GPNode[0]; |
---|
| 97 | |
---|
| 98 | /** This must be called <i>after</i> the GPTypes have been set up. */ |
---|
| 99 | public final void setup(final EvolutionState state, final Parameter base) |
---|
| 100 | { |
---|
| 101 | // What's my name? |
---|
| 102 | name = state.parameters.getString(base.push(P_NAME),null); |
---|
| 103 | if (name==null) |
---|
| 104 | state.output.fatal("No name was given for this node constraints.", |
---|
| 105 | base.push(P_NAME)); |
---|
| 106 | |
---|
| 107 | // Register me |
---|
| 108 | GPNodeConstraints old_constraints = (GPNodeConstraints)(((GPInitializer)state.initializer).nodeConstraintRepository.put(name,this)); |
---|
| 109 | if (old_constraints != null) |
---|
| 110 | state.output.fatal("The GP node constraint \"" + name + "\" has been defined multiple times.", base.push(P_NAME)); |
---|
| 111 | |
---|
| 112 | // What's my return type? |
---|
| 113 | String s = state.parameters.getString(base.push(P_RETURNS),null); |
---|
| 114 | if (s==null) |
---|
| 115 | state.output.fatal("No return type given for the GPNodeConstraints " + name, base.push(P_RETURNS)); |
---|
| 116 | returntype = GPType.typeFor(s,state); |
---|
| 117 | |
---|
| 118 | // Load probability of selection |
---|
| 119 | |
---|
| 120 | if (state.parameters.exists(base.push(P_PROBABILITY),null)) |
---|
| 121 | { |
---|
| 122 | float f = state.parameters.getFloat(base.push(P_PROBABILITY),null,0); |
---|
| 123 | if (f < 0) |
---|
| 124 | state.output.fatal("The probability of selection is < 0, which is not valid.",base.push(P_PROBABILITY),null); |
---|
| 125 | probabilityOfSelection = f; |
---|
| 126 | } |
---|
| 127 | else probabilityOfSelection = DEFAULT_PROBABILITY; |
---|
| 128 | |
---|
| 129 | // How many child types do I have? |
---|
| 130 | |
---|
| 131 | int x = state.parameters.getInt(base.push(P_SIZE),null,0); |
---|
| 132 | if (x < 0) |
---|
| 133 | state.output.fatal("The number of children types for the GPNodeConstraints " + name + " must be >= 0.", base.push(P_SIZE)); |
---|
| 134 | |
---|
| 135 | childtypes = new GPType[x]; |
---|
| 136 | |
---|
| 137 | Parameter p = base.push(P_CHILD); |
---|
| 138 | |
---|
| 139 | // Load my children |
---|
| 140 | for (x=0;x<childtypes.length;x++) |
---|
| 141 | { |
---|
| 142 | s = state.parameters.getString(p.push(""+x),null); |
---|
| 143 | if (s==null) |
---|
| 144 | state.output.fatal("Type #" + x + " is not defined for the GPNodeConstraints " + name + ".", base.push(""+x)); |
---|
| 145 | childtypes[x] = GPType.typeFor(s,state); |
---|
| 146 | } |
---|
| 147 | // ...because I promised when I called typeFor(...) |
---|
| 148 | state.output.exitIfErrors(); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | /** You must guarantee that after calling constraintsFor(...) one or |
---|
| 153 | several times, you call state.output.exitIfErrors() once. */ |
---|
| 154 | |
---|
| 155 | public static GPNodeConstraints constraintsFor(final String constraintsName, |
---|
| 156 | final EvolutionState state) |
---|
| 157 | { |
---|
| 158 | GPNodeConstraints myConstraints = (GPNodeConstraints)(((GPInitializer)state.initializer).nodeConstraintRepository.get(constraintsName)); |
---|
| 159 | if (myConstraints==null) |
---|
| 160 | state.output.error("The GP node constraint \"" + constraintsName + "\" could not be found."); |
---|
| 161 | return myConstraints; |
---|
| 162 | } |
---|
| 163 | } |
---|