Download as a PDF
Transcript
children, that it has children, that it has a parent, etc., and issue an error if not. The primary function of this method is to allow Automatically Defined Functions (Section 3.2.10) a chance to make sure that everything is working. You don’t really need to override this method (if you do, be sure to call super(...)) but you’ll see it often implemented in the examples. Next come some methods which are usually overridden by Ephemeral Random Constants but rarely by any other kind of GPNode (the default implementation suffices): ec.gp.GPNode Methods public int nodeHashCode() Returns a hash code appropriate to your GPNode (hash by value, not by address). public boolean nodeEquals(GPNode node) Returns true if this method is identical to the given node. public boolean nodeEquivalentTo(GPNode node) Returns true if the two nodes are the same “kind” of node — usually meaning they could have been cloned from the same prototype node. The default form of this function returns true if the two nodes are the same class, have the same length child array, and have the same constraints. Often nodeEquals(...) and nodeEquivalentTo(...) may return the same thing, but in Ephemeral Random Constants, they often return different values. For example, two ERCs that are the same class, and have the same constraints, may hold different values (2.34 vs. 3.14 say). These ERCs would be equivalent to one another but not equal to one another. You’d rarely need to override this method. public String toStringForHumans() Writes the node to a String in a fashion readable by humans. The default version simply calls toString. public void resetNode(EvolutionState state, int thread) Randomizes the node. Ephemeral Random Constants randomize their internal values; other GPNodes typically do nothing. Next, some test functions: ec.gp.GPNode Methods public int atDepth() Returns the depth of the node (the root is at depth 0). public int depth() Returns the depth of the subtree rooted by the node (terminals have a subtree depth of 1). public GPNodeParent rootParent() Returns the parent of the root of the tree in which the GPNode resides. Though the method returns a GPNodeParent, this returned object should always be some kind of GPTree. public boolean contains(GPNode subnode) Returns true subnode exists somewhere within the subtree rooted by the GPNode. public pathLength(int nodesearch) Returns the sum of all paths from all nodes in the GPNode’s subtree to the GPNode itself. The nodesearch parameter allows us to restrict which nodes have paths included in the sum: only leaf nodes (terminals), non-leaf nodes (nonterminal), or all nodes: 132