Class Terminal
- All Implemented Interfaces:
GPNodeParent,Prototype,Setup,Serializable,Cloneable
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.swap
For 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 = 10
The 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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionA list of custom PushInstructions I can be set to.static final String[]static final intint[]For each PushInstruction, a pointer into instructions which gives the name of that instruction.String[]Names of all the Push instructions I can be set to.static final intstatic doublestatic intstatic doublestatic intstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final StringFields 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 -
Method Summary
Modifier and TypeMethodDescriptionbooleandecode(DecodeReturn dret) Decodes data into the ERC from dret.The default base for GPNodes -- defined even though GPNode is abstract so you don't have to in subclasses.encode()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.intUsually ERCs don't have children, and this default implementation makes certain of it.name()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.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, writeNodeMethods inherited from class ec.gp.GPNode
atDepth, checkConstraints, clone, cloneReplacing, cloneReplacing, cloneReplacing, cloneReplacingAtomic, cloneReplacingAtomic, cloneReplacingNoSubclone, constraints, contains, depth, errorInfo, iterator, iterator, iterator, lightClone, makeCTree, makeGraphvizSubtree, 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 Details
-
P_INSTRUCTION
- See Also:
-
P_NUM_INSTRUCTIONS
- See Also:
-
P_FUNC
- See Also:
-
P_FLOAT
- See Also:
-
P_INTEGER
- See Also:
-
P_MIN
- See Also:
-
P_MAX
- See Also:
-
FLOAT_ERC
public static final int FLOAT_ERC- See Also:
-
INTEGER_ERC
public static final int INTEGER_ERC- See Also:
-
ERC_NAMES
-
minFloatERC
public static double minFloatERC -
maxFloatERC
public static double maxFloatERC -
minIntegerERC
public static int minIntegerERC -
maxIntegerERC
public static int maxIntegerERC -
instructions
Names of all the Push instructions I can be set to. This includes names for custom PushInstructions. -
customInstructions
A list of custom PushInstructions I can be set to. -
indices
public int[] indicesFor 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.
-
-
Constructor Details
-
Terminal
public Terminal()
-
-
Method Details
-
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
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
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
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
Description copied from class:ERCImplement this to do ERC-to-ERC comparisons.- Specified by:
nodeEqualsin classERC
-
encode
Description copied from class:ERCEncodes data from the ERC, using ec.util.Code. -
decode
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
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 invalid input: '&'invalid input: '&' x; return; }
-