[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 | import java.io.*; |
---|
| 12 | |
---|
| 13 | /* |
---|
| 14 | * ADFArgument.java |
---|
| 15 | * |
---|
| 16 | * Created: Tue Oct 26 16:14:09 1999 |
---|
| 17 | * By: Sean Luke |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * An ADFArgument is a GPNode which represents an ADF's |
---|
| 22 | * <i>argument terminal</i>, its counterpart which returns argument |
---|
| 23 | * values in its associated function tree. In lil-gp this is called an |
---|
| 24 | * ARG node. |
---|
| 25 | * |
---|
| 26 | * <p>Obviously, if you have Argument Terminals in a tree, that tree must |
---|
| 27 | * be only callable by ADFs and ADMs, otherwise the Argument Terminals |
---|
| 28 | * won't have anything to return. Furthermore, you must make sure that |
---|
| 29 | * you don't have an Argument Terminal in a tree whose number is higher |
---|
| 30 | * than the smallest arity (number of arguments) of a calling ADF or ADM. |
---|
| 31 | * |
---|
| 32 | <p><b>Parameters</b><br> |
---|
| 33 | <table> |
---|
| 34 | <tr><td valign=top><i>base</i>.<tt>arg</tt><br> |
---|
| 35 | <font size=-1>int >= 0</font></td> |
---|
| 36 | <td valign=top>(The related argument position for the ADF Argument Node in the associated ADF)</td></tr> |
---|
| 37 | </table> |
---|
| 38 | |
---|
| 39 | <p><b>Default Base</b><br> |
---|
| 40 | gp.adf-argument |
---|
| 41 | |
---|
| 42 | * @see ec.gp.ADF |
---|
| 43 | * @author Sean Luke |
---|
| 44 | * @version 1.0 |
---|
| 45 | */ |
---|
| 46 | |
---|
| 47 | public class ADFArgument extends GPNode |
---|
| 48 | { |
---|
| 49 | public static final String P_ADFARGUMENT = "adf-argument"; |
---|
| 50 | public final static String P_ARGUMENT = "arg"; |
---|
| 51 | public static final String P_FUNCTIONNAME = "name"; |
---|
| 52 | public int argument; |
---|
| 53 | |
---|
| 54 | /** The "function name" of the ADFArgument, to distinguish it from other GP |
---|
| 55 | functions you might provide. */ |
---|
| 56 | public String name; |
---|
| 57 | public String name() { return name; } |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | public Parameter defaultBase() |
---|
| 61 | { |
---|
| 62 | return GPDefaults.base().push(P_ADFARGUMENT); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | public String toString() { return name(); } |
---|
| 66 | |
---|
| 67 | public void setup(final EvolutionState state, final Parameter base) |
---|
| 68 | { |
---|
| 69 | super.setup(state,base); |
---|
| 70 | |
---|
| 71 | Parameter def = defaultBase(); |
---|
| 72 | |
---|
| 73 | // make sure we don't have any children... |
---|
| 74 | if (children.length!= 0) |
---|
| 75 | state.output.error("Incorrect number of children for ADF Argument terminal -- should be 0. Check the constraints.",base,def); |
---|
| 76 | |
---|
| 77 | argument = state.parameters.getInt(base.push(P_ARGUMENT),def.push(P_ARGUMENT),0); |
---|
| 78 | if (argument < 0) |
---|
| 79 | state.output.fatal("Argument terminal must have a positive argument number.", |
---|
| 80 | base.push(P_ARGUMENT),def.push(P_ARGUMENT)); |
---|
| 81 | |
---|
| 82 | name = state.parameters.getString(base.push(P_FUNCTIONNAME),def.push(P_FUNCTIONNAME)); |
---|
| 83 | if (name == null || name.equals("")) |
---|
| 84 | { |
---|
| 85 | name = "ARG" + argument; |
---|
| 86 | state.output.warning("ADFArgument node for argument " + argument + " has no function name. Using the name " + name(), |
---|
| 87 | base.push(P_FUNCTIONNAME),def.push(P_FUNCTIONNAME)); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | public void writeNode(final EvolutionState state, final DataOutput dataOutput) throws IOException |
---|
| 92 | { |
---|
| 93 | dataOutput.writeInt(argument); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | public void readNode(final EvolutionState state, final DataInput dataInput) throws IOException |
---|
| 98 | { |
---|
| 99 | argument = dataInput.readInt(); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | public void eval(final EvolutionState state, |
---|
| 103 | final int thread, |
---|
| 104 | final GPData input, |
---|
| 105 | final ADFStack stack, |
---|
| 106 | final GPIndividual individual, |
---|
| 107 | final Problem problem) |
---|
| 108 | { |
---|
| 109 | // get the current context |
---|
| 110 | ADFContext c = stack.top(0); |
---|
| 111 | if (c==null) // uh oh |
---|
| 112 | state.output.fatal("No context with which to evaluate ADFArgument terminal " + toStringForError() + ". This often happens if you evaluate a tree by hand which is supposed to only be an ADF's associated tree."); |
---|
| 113 | c.evaluate(state,thread,input,stack,individual,problem,argument); |
---|
| 114 | } |
---|
| 115 | } |
---|