Class Terminal
- java.lang.Object
-
- ec.gp.GPNode
-
- ec.gp.ERC
-
- ec.gp.push.Terminal
-
- All Implemented Interfaces:
- GPNodeParent, Prototype, Setup, java.io.Serializable, java.lang.Cloneable
public class Terminal extends ERC
Terminal is the leaf node in Push trees and is used to represent Push instructions of all types.ECJ implements Push's s-expressions as trees of nonterminals and terminals. The nonterminals are all dummy instances of the Nonterminal class. Terminals are all instances of the Terminal class.
The nonterminals and terminals aren't actually evaluated. Instead, the tree is printed out as a lisp s-expression and sent to the Push interpreter.
Terminals are implemented as ERCs which hold the actual Push instruction or atom as a string ('value'). There are four kinds of instructions at present:
- Built-in Push instructions like float.* or integer.swap
- Floating-point ERCs (defined by "float.erc")
- Integer ERCs (defined by "integer.erc")
- Custom Push instructions
You specify your instructions like this:
push.op.size = 7 push.op.0 = float.erc push.op.1 = float.+ # This is a custom instruction push.op.2 = float.print push.op.2.func = ec.gp.push.example.MyPushInstruction push.op.3 = float.% push.op.4 = float.- push.op.5 = float.dup push.op.6 = float.swapFor the (at present) two kinds of ERCs, you can specify a minimum and a maximum value. Here are the defaults:
push.op.float.min = -10 push.op.float.max = 10 push.op.int.min = -10 push.op.int.max = 10The full list of Psh instructions is:integer.+
integer.-
integer./
integer.\%
integer.*
integer.pow
integer.log
integer.=
integer.>
integer.*lt;
integer.min
integer.max
integer.abs
integer.neg
integer.ln
integer.fromfloat
integer.fromboolean
integer.rand
float.+
float.-
float./
float.\%
float.*
float.pow
float.log
float.=
float.>
float.<
float.min
float.max
float.sin
float.cos
float.tan
float.exp
float.abs
float.neg
float.ln
float.frominteger
float.fromboolean
float.rand
boolean.=
boolean.not
boolean.and
boolean.or
boolean.xor
boolean.frominteger
boolean.fromfloat
boolean.rand
true
false
code.quote
code.fromboolean
code.frominteger
code.fromfloat
code.noop
code.do*times
code.do*count
code.do*range
code.=
code.if
code.rand
exec.k
exec.s
exec.y
exec.noop
exec.do*times
exec.do*count
exec.do*range
exec.=
exec.if
exec.rand
input.index
input.inall
input.inallrev
input.stackdepth
frame.push
frame.pop
Parameters
base.op.size
int >= 1(Number of instructions in Push's internal "instruction set") base.op.i
String(Name of instruction i) base.op.i.func
classname, inherits and != ec.gp.push.PushInstruction(PushInstruction corresponding to instruction i, if it is a custom instruction) base.op.float.min
float(Minimum value for a Push floating-point ERC) base.op.float.max
float(Maximum value for a Push floating-point ERC) base.op.int.min
int(Minimum value for a Push integer ERC) base.op.int.max
int(Maximum value for a Push integer ERC) Default Base
gp.push- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field and Description PushInstruction[]customInstructionsA list of custom PushInstructions I can be set to.static java.lang.String[]ERC_NAMESstatic intFLOAT_ERCint[]indicesFor each PushInstruction, a pointer into instructions which gives the name of that instruction.java.lang.String[]instructionsNames of all the Push instructions I can be set to.static intINTEGER_ERCstatic doublemaxFloatERCstatic intmaxIntegerERCstatic doubleminFloatERCstatic intminIntegerERCstatic java.lang.StringP_FLOATstatic java.lang.StringP_FUNCstatic java.lang.StringP_INSTRUCTIONstatic java.lang.StringP_INTEGERstatic java.lang.StringP_MAXstatic java.lang.StringP_MINstatic java.lang.StringP_NUM_INSTRUCTIONS-
Fields inherited from class ec.gp.GPNode
argposition, children, CHILDREN_UNKNOWN, constraints, GPNODEPRINTTAB, MAXPRINTBYTES, NODESEARCH_ALL, NODESEARCH_NONTERMINALS, NODESEARCH_TERMINALS, P_NODE, P_NODECONSTRAINTS, parent
-
-
Constructor Summary
Constructors Constructor and Description Terminal()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description booleandecode(DecodeReturn dret)Decodes data into the ERC from dret.ParameterdefaultBase()The default base for GPNodes -- defined even though GPNode is abstract so you don't have to in subclasses.java.lang.Stringencode()Encodes data from the ERC, using ec.util.Code.voideval(EvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, Problem problem)Evaluates the node with the given thread, state, individual, problem, and stack.intexpectedChildren()Usually ERCs don't have children, and this default implementation makes certain of it.java.lang.Stringname()Returns the lowercase "name" of this ERC function class, some simple, short name which distinguishes this class from other ERC function classes you're using.booleannodeEquals(GPNode other)Implement this to do ERC-to-ERC comparisons.voidresetNode(EvolutionState state, int thread)Remember to override this to randomize your ERC after it has been cloned.voidsetup(EvolutionState state, Parameter base)Sets up a prototypical GPNode with those features all nodes of that prototype share, and nothing more.java.lang.StringtoStringForHumans()You might want to override this to return a special human-readable version of the erc value; otherwise this defaults to toString(); This should be something that resembles a LISP atom.-
Methods inherited from class ec.gp.ERC
mutateERC, nodeHashCode, readNode, readNode, toString, writeNode
-
Methods inherited from class ec.gp.GPNode
atDepth, checkConstraints, clone, cloneReplacing, cloneReplacing, cloneReplacing, cloneReplacingAtomic, cloneReplacingAtomic, cloneReplacingNoSubclone, constraints, contains, depth, errorInfo, iterator, iterator, iterator, lightClone, makeCTree, makeGraphvizTree, makeLatexTree, makeLispTree, makeLispTree, nodeEquivalentTo, nodeInPosition, nodeInPosition, numNodes, numNodes, parentType, pathLength, printNode, printNode, printNode, printNodeForHumans, printNodeForHumans, printRootedTree, printRootedTree, printRootedTree, printRootedTreeForHumans, printRootedTreeForHumans, readRootedTree, readRootedTree, replaceWith, rootedTreeEquals, rootedTreeHashCode, rootParent, swapCompatibleWith, toStringForError, writeRootedTree
-
-
-
-
Field Detail
-
P_INSTRUCTION
public static final java.lang.String P_INSTRUCTION
- See Also:
- Constant Field Values
-
P_NUM_INSTRUCTIONS
public static final java.lang.String P_NUM_INSTRUCTIONS
- See Also:
- Constant Field Values
-
P_FUNC
public static final java.lang.String P_FUNC
- See Also:
- Constant Field Values
-
P_FLOAT
public static final java.lang.String P_FLOAT
- See Also:
- Constant Field Values
-
P_INTEGER
public static final java.lang.String P_INTEGER
- See Also:
- Constant Field Values
-
P_MIN
public static final java.lang.String P_MIN
- See Also:
- Constant Field Values
-
P_MAX
public static final java.lang.String P_MAX
- See Also:
- Constant Field Values
-
FLOAT_ERC
public static final int FLOAT_ERC
- See Also:
- Constant Field Values
-
INTEGER_ERC
public static final int INTEGER_ERC
- See Also:
- Constant Field Values
-
ERC_NAMES
public static final java.lang.String[] ERC_NAMES
-
minFloatERC
public static double minFloatERC
-
maxFloatERC
public static double maxFloatERC
-
minIntegerERC
public static int minIntegerERC
-
maxIntegerERC
public static int maxIntegerERC
-
instructions
public java.lang.String[] instructions
Names of all the Push instructions I can be set to. This includes names for custom PushInstructions.
-
customInstructions
public PushInstruction[] customInstructions
A list of custom PushInstructions I can be set to.
-
indices
public int[] indices
For each PushInstruction, a pointer into instructions which gives the name of that instruction. Note that some instructions in instructions are built-in Push instructions and will have nothing pointing to them.
-
-
Method Detail
-
name
public java.lang.String name()
Description copied from class:ERCReturns the lowercase "name" of this ERC function class, some simple, short name which distinguishes this class from other ERC function classes you're using. If you have more than one ERC function, you need to distinguish them here. By default the value is "ERC", which works fine for a single ERC function in the function set. Whatever the name is, it should generally only have letters, numbers, or hyphens or underscores in it. No whitespace or other characters.
-
expectedChildren
public int expectedChildren()
Description copied from class:ERCUsually ERCs don't have children, and this default implementation makes certain of it. But if you want to override this, you're welcome to.- Overrides:
expectedChildrenin classERC
-
toStringForHumans
public java.lang.String toStringForHumans()
Description copied from class:ERCYou might want to override this to return a special human-readable version of the erc value; otherwise this defaults to toString(); This should be something that resembles a LISP atom. If a simple number or other object won't suffice, you might use something that begins with name() + [ + ... + ]- Overrides:
toStringForHumansin classERC
-
defaultBase
public Parameter defaultBase()
Description copied from class:GPNodeThe default base for GPNodes -- defined even though GPNode is abstract so you don't have to in subclasses.- Specified by:
defaultBasein interfacePrototype- Overrides:
defaultBasein classGPNode
-
setup
public void setup(EvolutionState state, Parameter base)
Description copied from class:GPNodeSets up a prototypical GPNode with those features all nodes of that prototype share, and nothing more. So no filled-in children, no argposition, no parent. Yet. This must be called after the GPTypes and GPNodeConstraints have been set up. Presently they're set up in GPInitializer, which gets called before this does, so we're safe. You should override this if you need to load some special features on a per-function basis. Note that base hangs off of a function set, so this method may get called for different instances in the same GPNode class if they're being set up as prototypes for different GPFunctionSets. If you absolutely need some global base, then you should use something hanging off of GPDefaults.base(). The ultimate caller of this method must guarantee that he will eventually call state.output.exitIfErrors(), so you can freely use state.output.error instead of state.output.fatal(), which will help a lot.
-
nodeEquals
public boolean nodeEquals(GPNode other)
Description copied from class:ERCImplement this to do ERC-to-ERC comparisons.- Specified by:
nodeEqualsin classERC
-
encode
public java.lang.String encode()
Description copied from class:ERCEncodes data from the ERC, using ec.util.Code.
-
decode
public boolean decode(DecodeReturn dret)
Description copied from class:ERCDecodes data into the ERC from dret. Return true if you sucessfully decoded, false if you didn't. Don't increment dret.pos's value beyond exactly what was needed to decode your ERC. If you fail to decode, you should make sure that the position and data in the dret are exactly as they were originally.
-
resetNode
public void resetNode(EvolutionState state, int thread)
Description copied from class:ERCRemember to override this to randomize your ERC after it has been cloned. The prototype will not ever receive this method call.
-
eval
public void eval(EvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, Problem problem)
Description copied from class:GPNodeEvaluates the node with the given thread, state, individual, problem, and stack. Your random number generator will be state.random[thread]. The node should, as appropriate, evaluate child nodes with these same items passed to eval(...).About input: input is special; it is how data is passed between parent and child nodes. If children "receive" data from their parent node when it evaluates them, they should receive this data stored in input. If (more likely) the parent "receives" results from its children, it should pass them an input object, which they'll fill out, then it should check this object for the returned value.
A tree is typically evaluated by dropping a GPData into the root. When the root returns, the resultant input should hold the return value.
In general, you should not be creating new GPDatas. If you think about it, in most conditions (excepting ADFs and ADMs) you can use and reuse input for most communications purposes between parents and children.
So, let's say that your GPNode function implements the boolean AND function, and expects its children to return return boolean values (as it does itself). You've implemented your GPData subclass to be, uh, BooleanData, which looks like
public class BooleanData extends GPData { public boolean result; public GPData copyTo(GPData gpd) { ((BooleanData)gpd).result = result; } }...so, you might implement your eval(...) function as follows:
public void eval(final EvolutionState state, final int thread, final GPData input, final ADFStack stack, final GPIndividual individual, final Problem problem { BooleanData dat = (BooleanData)input; boolean x; // evaluate the first child children[0].eval(state,thread,input,stack,individual,problem); // store away its result x = dat.result; // evaluate the second child children[1].eval(state,thread,input,stack,individual,problem); // return (in input) the result of the two ANDed dat.result = dat.result && x; return; }
-
-
DMelt 3.0 © DataMelt by jWork.ORG