Package ec.gp.push

# Class Terminal

java.lang.Object
ec.gp.GPNode
ec.gp.ERC
ec.gp.push.Terminal

All Implemented Interfaces:
  `GPNodeParent`, `Prototype`, `Setup`, `Serializable`, `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:

1. Built-in Push instructions like float.* or integer.swap
2. Floating-point ERCs (defined by "float.erc")
3. Integer ERCs (defined by "integer.erc")
4. 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:
  - Serialized Form

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `PushInstruction[]`
  `customInstructions`
  A list of custom PushInstructions I can be set to.
  `static final String[]`
  `ERC_NAMES`
  `static final int`
  `FLOAT_ERC`
  `int[]`
  `indices`
  For each PushInstruction, a pointer into instructions which gives the name of that instruction.
  `String[]`
  `instructions`
  Names of all the Push instructions I can be set to.
  `static final int`
  `INTEGER_ERC`
  `static double`
  `maxFloatERC`
  `static int`
  `maxIntegerERC`
  `static double`
  `minFloatERC`
  `static int`
  `minIntegerERC`
  `static final String`
  `P_FLOAT`
  `static final String`
  `P_FUNC`
  `static final String`
  `P_INSTRUCTION`
  `static final String`
  `P_INTEGER`
  `static final String`
  `P_MAX`
  `static final String`
  `P_MIN`
  `static final String`
  `P_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
  Description
  `Terminal()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `decode(DecodeReturn dret)`
  Decodes data into the ERC from dret.
  `Parameter`
  `defaultBase()`
  The default base for GPNodes -- defined even though GPNode is abstract so you don't have to in subclasses.
  `String`
  `encode()`
  Encodes data from the ERC, using ec.util.Code.
  `void`
  `eval(EvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, Problem problem)`
  Evaluates the node with the given thread, state, individual, problem, and stack.
  `int`
  `expectedChildren()`
  Usually ERCs don't have children, and this default implementation makes certain of it.
  `String`
  `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.
  `boolean`
  `nodeEquals(GPNode other)`
  Implement this to do ERC-to-ERC comparisons.
  `void`
  `resetNode(EvolutionState state, int thread)`
  Remember to override this to randomize your ERC after it has been cloned.
  `void`
  `setup(EvolutionState state, Parameter base)`
  Sets up a prototypical GPNode with those features all nodes of that prototype share, and nothing more.
  `String`
  `toStringForHumans()`
  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, 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`

  ### Methods inherited from class java.lang.Object

  `equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Field Details

  - ### P_INSTRUCTION

    public static final String P_INSTRUCTION
    See Also:
      - Constant Field Values

  - ### P_NUM_INSTRUCTIONS

    public static final String P_NUM_INSTRUCTIONS
    See Also:
      - Constant Field Values

  - ### P_FUNC

    public static final String P_FUNC
    See Also:
      - Constant Field Values

  - ### P_FLOAT

    public static final String P_FLOAT
    See Also:
      - Constant Field Values

  - ### P_INTEGER

    public static final String P_INTEGER
    See Also:
      - Constant Field Values

  - ### P_MIN

    public static final String P_MIN
    See Also:
      - Constant Field Values

  - ### P_MAX

    public static final 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 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 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.

- ## Constructor Details

  - ### Terminal

    public Terminal()

- ## Method Details

  - ### name

    public String name()
    Description copied from class: `ERC`
    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. 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.
    Overrides:
      `name` in class `ERC`

  - ### expectedChildren

    public int expectedChildren()
    Description copied from class: `ERC`
    Usually 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:
      `expectedChildren` in class `ERC`

  - ### toStringForHumans

    public String toStringForHumans()
    Description copied from class: `ERC`
    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. If a simple number or other object won't suffice, you might use something that begins with name() + [ + ... + ]
    Overrides:
      `toStringForHumans` in class `ERC`

  - ### defaultBase

    public Parameter defaultBase()
    Description copied from class: `GPNode`
    The default base for GPNodes -- defined even though GPNode is abstract so you don't have to in subclasses.
    Specified by:
      `defaultBase` in interface `Prototype`
    Overrides:
      `defaultBase` in class `GPNode`

  - ### setup

    public void setup(EvolutionState state, Parameter base)
    Description copied from class: `GPNode`
    Sets 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.
    Specified by:
      `setup` in interface `Prototype`
    Specified by:
      `setup` in interface `Setup`
    Overrides:
      `setup` in class `GPNode`

  - ### nodeEquals

    public boolean nodeEquals(GPNode other)
    Description copied from class: `ERC`
    Implement this to do ERC-to-ERC comparisons.
    Specified by:
      `nodeEquals` in class `ERC`

  - ### encode

    public String encode()
    Description copied from class: `ERC`
    Encodes data from the ERC, using ec.util.Code.
    Specified by:
      `encode` in class `ERC`

  - ### decode

    public boolean decode(DecodeReturn dret)
    Description copied from class: `ERC`
    Decodes 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.
    Overrides:
      `decode` in class `ERC`

  - ### resetNode

    public void resetNode(EvolutionState state, int thread)
    Description copied from class: `ERC`
    Remember to override this to randomize your ERC after it has been cloned. The prototype will not ever receive this method call.
    Specified by:
      `resetNode` in class `ERC`

  - ### eval

    public void eval(EvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, Problem problem)
    Description copied from class: `GPNode`
    Evaluates 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;
            }

    Specified by:
      `eval` in class `GPNode`