public abstract class GPNode extends Object implements GPNodeParent, Prototype
GPNodes contain a number of important items:
In addition to serialization for checkpointing, GPNodes may read and write themselves to streams in three ways.
Parameters
base.nc String |
(name of the node constraints for the GPNode) |
Default Base
gp.node
Modifier and Type | Field and Description |
---|---|
byte |
argposition
The argument position of the child in its parent.
|
GPNode[] |
children |
static int |
CHILDREN_UNKNOWN |
byte |
constraints
The GPNode's constraints.
|
static String |
GPNODEPRINTTAB |
static int |
MAXPRINTBYTES |
static int |
NODESEARCH_ALL |
(package private) static int |
NODESEARCH_CUSTOM |
static int |
NODESEARCH_NONTERMINALS |
static int |
NODESEARCH_TERMINALS |
static String |
P_NODE |
static String |
P_NODECONSTRAINTS |
GPNodeParent |
parent
The GPNode's parent.
|
Constructor and Description |
---|
GPNode() |
Modifier and Type | Method and Description |
---|---|
int |
atDepth()
Returns the depth at which I appear in the tree, which is a value >= 0.
|
void |
checkConstraints(EvolutionState state,
int tree,
GPIndividual typicalIndividual,
Parameter individualBase)
You ought to override this method to check to make sure that the
constraints are valid as best you can tell.
|
Object |
clone()
Deep-clones the tree rooted at this node, and returns the entire
copied tree.
|
GPNode |
cloneReplacing()
Deprecated.
use clone() instead.
|
GPNode |
cloneReplacing(GPNode[] newSubtrees,
GPNode[] oldSubtrees)
Deep-clones the tree rooted at this node, and returns the entire
copied tree.
|
GPNode |
cloneReplacing(GPNode newSubtree,
GPNode oldSubtree)
Deep-clones the tree rooted at this node, and returns the entire
copied tree.
|
GPNode |
cloneReplacingAtomic(GPNode[] newNodes,
GPNode[] oldNodes)
Clones a new subtree, but with each node in oldNodes[] respectively
(which may or may not be in the subtree) replaced with
the equivalent
nodes in newNodes[] (and not clones).
|
GPNode |
cloneReplacingAtomic(GPNode newNode,
GPNode oldNode)
Clones a new subtree, but with the single node oldNode
(which may or may not be in the subtree)
replaced with a newNode (not a clone of newNode).
|
GPNode |
cloneReplacingNoSubclone(GPNode newSubtree,
GPNode oldSubtree)
Deep-clones the tree rooted at this node, and returns the entire
copied tree.
|
GPNodeConstraints |
constraints(GPInitializer initializer) |
boolean |
contains(GPNode subnode)
Returns true if the subtree rooted at this node contains subnode.
|
Parameter |
defaultBase()
The default base for GPNodes -- defined even though
GPNode is abstract so you don't have to in subclasses.
|
int |
depth()
Returns the depth of the tree, which is a value >= 1.
|
String |
errorInfo()
A convenience function for identifying a GPNode in an error message
|
abstract 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()
Returns the number of children this node expects to have.
|
Iterator |
iterator()
Returns an iterator over all the GPNodes in the subtree rooted by this GPNode.
|
Iterator |
iterator(GPNodeGatherer g)
Returns an iterator over all the GPNodes in the subtree rooted by this GPNode,
filtered by the provided GPNodeGatherer.
|
Iterator |
iterator(int nodesearch)
Returns an iterator over all the GPNodes in the subtree rooted by this GPNode,
filtered by the provided nodesearch option (either NODSEARCH_TERMINALS, NODESEARCH_NONTERMINALS,
or NODESEARCH_ALL)
|
GPNode |
lightClone() |
String |
makeCTree(boolean parentMadeParens,
boolean printTerminalsAsVariables,
boolean useOperatorForm)
Producess a String consisting of the tree in pseudo-C form, given that the parent already will wrap the
expression in parentheses (or not).
|
protected String |
makeGraphvizSubtree(String prefix)
Produces the inner code for a graphviz subtree.
|
String |
makeGraphvizTree()
Produces the Graphviz code for a Graphviz tree of the subtree rooted at this node.
|
String |
makeLatexTree()
Produces the LaTeX code for a LaTeX tree of the subtree rooted at this node, using the epic
and fancybox packages, as described in sections 10.5.2 (page 307)
and 10.1.3 (page 278) of The LaTeX Companion, respectively.
|
String |
makeLispTree() |
StringBuilder |
makeLispTree(StringBuilder buf)
Produces a tree for human consumption in Lisp form similar to that generated by printTreeForHumans().
|
(package private) int |
meanDepth(int nodesearch)
Returns the mean depth of the tree, which is path length (sum of all paths from all nodes to the root) divided by the number of nodes.
|
String |
name()
Returns a Lisp-like atom for the node and any nodes of the same class.
|
boolean |
nodeEquals(GPNode node)
Returns true if I am the "genetically" identical to this node, and our
children arrays are the same length, though
we may have different parents and children.
|
boolean |
nodeEquivalentTo(GPNode node)
Returns true if I and the provided node are the same kind of
node -- that is, we could have both been cloned() and reset() from
the same prototype node.
|
int |
nodeHashCode()
Returns a hashcode usually associated with all nodes that are
equal to you (using nodeEquals(...)).
|
GPNode |
nodeInPosition(int p,
GPNodeGatherer g)
Returns the p'th node, constrained by nodesearch,
in the subtree for which this GPNode is root.
|
(package private) int |
nodeInPosition(int p,
GPNodeGatherer g,
int nodesearch) |
GPNode |
nodeInPosition(int p,
int nodesearch)
Returns the p'th node, constrained by nodesearch,
in the subtree for which this GPNode is root.
|
int |
numNodes(GPNodeGatherer g)
Returns the number of nodes, constrained by g.test(...)
in the subtree for which this GPNode is root.
|
int |
numNodes(int nodesearch)
Returns the number of nodes, constrained by nodesearch,
in the subtree for which this GPNode is root.
|
GPType |
parentType(GPInitializer initializer)
Returns the argument type of the slot that I fit into in my parent.
|
int |
pathLength(int nodesearch)
Returns the path length of the tree, which is the sum of all paths from all nodes to the root.
|
(package private) int |
pathLength(int nodesearch,
int currentDepth) |
int |
printNode(EvolutionState state,
int log)
Prints out a COMPUTER-readable and Lisp-like atom for the node, which
is also suitable for readNode to read, and returns
the number of bytes in the string that you sent to the log (use print(),
not println()).
|
int |
printNode(EvolutionState state,
int log,
int verbosity)
Deprecated.
Verbosity no longer has an effect.
|
int |
printNode(EvolutionState state,
PrintWriter writer)
Prints out a COMPUTER-readable and Lisp-like atom for the node, which
is also suitable for readNode to read, and returns
the number of bytes in the string that you sent to the log (use print(),
not println()).
|
int |
printNodeForHumans(EvolutionState state,
int log)
Prints out a human-readable and Lisp-like atom for the node,
and returns the number of bytes in the string that you sent
to the log (use print(),
not println()).
|
int |
printNodeForHumans(EvolutionState state,
int log,
int verbosity)
Deprecated.
Verbosity no longer has an effect.
|
int |
printRootedTree(EvolutionState state,
int log,
int printbytes)
Prints out the tree on a single line, with no ending \n, in a fashion that can
be read in later by computer.
|
int |
printRootedTree(EvolutionState state,
int log,
int verbosity,
int printbytes)
Deprecated.
Verbosity no longer has an effect.
|
int |
printRootedTree(EvolutionState state,
PrintWriter writer,
int printbytes)
Prints out the tree on a single line, with no ending \n, in a fashion that can
be read in later by computer.
|
int |
printRootedTreeForHumans(EvolutionState state,
int log,
int tablevel,
int printbytes)
Prints out the tree in a readable Lisp-like multi-line fashion.
|
int |
printRootedTreeForHumans(EvolutionState state,
int log,
int verbosity,
int tablevel,
int printbytes)
Deprecated.
Verbosity no longer has an effect.
|
GPNode |
readNode(DecodeReturn dret)
Reads the node symbol,
advancing the DecodeReturn to the first character in the string
beyond the node symbol, and returns a new, empty GPNode of the
appropriate class representing that symbol, else null if the
node symbol is not of the correct type for your GPNode class.
|
void |
readNode(EvolutionState state,
DataInput dataInput)
Override this to read any additional node-specific information from dataInput besides: the number of arguments,
the specific node class, the children, and the parent.
|
static GPNode |
readRootedTree(EvolutionState state,
DataInput dataInput,
GPType expectedType,
GPFunctionSet set,
GPNodeParent parent,
int argposition) |
static GPNode |
readRootedTree(int linenumber,
DecodeReturn dret,
GPType expectedType,
GPFunctionSet set,
GPNodeParent parent,
int argposition,
EvolutionState state)
Reads the node and its children from the form printed out by printRootedTree.
|
void |
replaceWith(GPNode newNode)
Replaces the node with another node in its position in the tree.
|
void |
resetNode(EvolutionState state,
int thread)
Starts a node in a new life immediately after it has been cloned.
|
boolean |
rootedTreeEquals(GPNode node)
Returns true if the two rooted trees are "genetically" equal, though
they may have different parents.
|
int |
rootedTreeHashCode()
Returns a hashcode associated with all the nodes in the tree.
|
GPNodeParent |
rootParent()
Returns the root ancestor of this node.
|
void |
setup(EvolutionState state,
Parameter base)
Sets up a prototypical GPNode with those features all nodes of that
prototype share, and nothing more.
|
boolean |
swapCompatibleWith(GPInitializer initializer,
GPNode node)
Returns true if I can swap into node's position.
|
abstract String |
toString()
Returns a Lisp-like atom for the node which can be read in again by computer.
|
String |
toStringForError()
Returns a description of the node that can make it easy to identify
in error messages (by default, at least its name and the tree it's found in).
|
String |
toStringForHumans()
Returns a Lisp-like atom for the node which is intended for human
consumption, and not to be read in again.
|
(package private) int |
verify(EvolutionState state,
GPFunctionSet set,
int index)
Verification of validity of the node in the tree -- strictly for debugging purposes only
|
void |
writeNode(EvolutionState state,
DataOutput dataOutput)
Override this to write any additional node-specific information to dataOutput besides: the number of arguments,
the specific node class, the children, and the parent.
|
void |
writeRootedTree(EvolutionState state,
GPType expectedType,
GPFunctionSet set,
DataOutput dataOutput) |
public static final String P_NODE
public static final String P_NODECONSTRAINTS
public static final String GPNODEPRINTTAB
public static final int MAXPRINTBYTES
public static final int NODESEARCH_ALL
public static final int NODESEARCH_TERMINALS
public static final int NODESEARCH_NONTERMINALS
static final int NODESEARCH_CUSTOM
public static final int CHILDREN_UNKNOWN
public GPNodeParent parent
public GPNode[] children
public byte argposition
public byte constraints
public final GPNodeConstraints constraints(GPInitializer initializer)
public Parameter defaultBase()
defaultBase
in interface Prototype
public void checkConstraints(EvolutionState state, int tree, GPIndividual typicalIndividual, Parameter individualBase)
public int expectedChildren()
public void setup(EvolutionState state, Parameter base)
public final GPType parentType(GPInitializer initializer)
final int verify(EvolutionState state, GPFunctionSet set, int index)
public final boolean swapCompatibleWith(GPInitializer initializer, GPNode node)
public int numNodes(GPNodeGatherer g)
public int numNodes(int nodesearch)
public int depth()
public int pathLength(int nodesearch)
int pathLength(int nodesearch, int currentDepth)
int meanDepth(int nodesearch)
public int atDepth()
public Iterator iterator(GPNodeGatherer g)
public Iterator iterator(int nodesearch)
public Iterator iterator()
public GPNode nodeInPosition(int p, GPNodeGatherer g)
public GPNode nodeInPosition(int p, int nodesearch)
int nodeInPosition(int p, GPNodeGatherer g, int nodesearch)
public GPNodeParent rootParent()
public boolean contains(GPNode subnode)
public void resetNode(EvolutionState state, int thread)
public String errorInfo()
public GPNode lightClone()
public Object clone()
public final GPNode cloneReplacing()
public final GPNode cloneReplacing(GPNode newSubtree, GPNode oldSubtree)
public final GPNode cloneReplacingNoSubclone(GPNode newSubtree, GPNode oldSubtree)
public final GPNode cloneReplacing(GPNode[] newSubtrees, GPNode[] oldSubtrees)
public final GPNode cloneReplacingAtomic(GPNode newNode, GPNode oldNode)
public final GPNode cloneReplacingAtomic(GPNode[] newNodes, GPNode[] oldNodes)
public final void replaceWith(GPNode newNode)
public boolean nodeEquivalentTo(GPNode node)
public int nodeHashCode()
public int rootedTreeHashCode()
public boolean nodeEquals(GPNode node)
public boolean rootedTreeEquals(GPNode node)
public int printNodeForHumans(EvolutionState state, int log)
public int printNodeForHumans(EvolutionState state, int log, int verbosity)
public int printNode(EvolutionState state, int log)
public int printNode(EvolutionState state, int log, int verbosity)
public int printNode(EvolutionState state, PrintWriter writer)
public String name()
public abstract String toString()
public String toStringForHumans()
public String toStringForError()
public String makeGraphvizTree()
protected String makeGraphvizSubtree(String prefix)
public String makeLatexTree()
public String makeCTree(boolean parentMadeParens, boolean printTerminalsAsVariables, boolean useOperatorForm)
public StringBuilder makeLispTree(StringBuilder buf)
public String makeLispTree()
public int printRootedTree(EvolutionState state, int log, int printbytes)
public int printRootedTree(EvolutionState state, int log, int verbosity, int printbytes)
public int printRootedTree(EvolutionState state, PrintWriter writer, int printbytes)
public int printRootedTreeForHumans(EvolutionState state, int log, int tablevel, int printbytes)
public int printRootedTreeForHumans(EvolutionState state, int log, int verbosity, int tablevel, int printbytes)
public GPNode readNode(DecodeReturn dret)
public void writeRootedTree(EvolutionState state, GPType expectedType, GPFunctionSet set, DataOutput dataOutput) throws IOException
IOException
public static GPNode readRootedTree(EvolutionState state, DataInput dataInput, GPType expectedType, GPFunctionSet set, GPNodeParent parent, int argposition) throws IOException
IOException
public void writeNode(EvolutionState state, DataOutput dataOutput) throws IOException
IOException
public void readNode(EvolutionState state, DataInput dataInput) throws IOException
IOException
public static GPNode readRootedTree(int linenumber, DecodeReturn dret, GPType expectedType, GPFunctionSet set, GPNodeParent parent, int argposition, EvolutionState state)
public abstract void eval(EvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, Problem problem)
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;
}
Copyright © 2019. All rights reserved.