ec.gp
Class ERC

java.lang.Object
  extended by ec.gp.GPNode
      extended by ec.gp.ERC
All Implemented Interfaces:
GPNodeParent, Prototype, Setup, java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
LawnERC, RegERC

public abstract class ERC
extends GPNode

ERC is an abstract GPNode which implements Ephemeral Random Constants, as described in Koza I. An ERC is a node which, when first instantiated, gets set to some random constant value which it always returns from then on, even after being crossed over into other individuals. In order to implement an ERC, you need to override several methods below.

Remember that if your ERC value isn't a simple or mutable type (like an int or a string), you'll have to deep-clone it in an overridden clone() method.

Version:
1.0
Author:
Sean Luke
See Also:
Serialized Form

Field Summary
static java.lang.String ERC_PREFIX
           
 
Fields inherited from class ec.gp.GPNode
argposition, children, constraints, GPNODEPRINTTAB, MAXPRINTBYTES, NODESEARCH_ALL, NODESEARCH_CUSTOM, NODESEARCH_NONTERMINALS, NODESEARCH_TERMINALS, P_NODE, P_NODECONSTRAINTS, parent, REPLACEMENT_CHAR, SITUATION_MUTATION, SITUATION_NEWIND
 
Constructor Summary
ERC()
           
 
Method Summary
 void checkConstraints(EvolutionState state, int tree, GPIndividual typicalIndividual, Parameter individualBase)
          Checks to make certain that the ERC has no children.
abstract  boolean decode(DecodeReturn dret)
          Decodes data into the ERC from dret.
abstract  java.lang.String encode()
          Encodes data from the ERC, using ec.util.Code.
 void mutateERC(EvolutionState state, int thread)
          Mutates the node's "value".
abstract  java.lang.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.
abstract  boolean nodeEquals(GPNode node)
          Implement this to do ERC-to-ERC comparisons.
abstract  int nodeHashCode()
          Implement this to hash ERCs, along with other nodes, in such a way that two "equal" ERCs will usually hash to the same value.
 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, java.io.DataInput dataInput)
          To successfully read from a DataOutput, you must override this to read your specific ERC data in.
abstract  void resetNode(EvolutionState state, int thread)
          Remember to override this to randomize your ERC after it has been cloned.
 java.lang.String toString()
          This defaults to simply ERC_PREFIX + name() + "[" + encode() + "]"
 java.lang.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.
 void writeNode(EvolutionState state, java.io.DataOutput dataOutput)
          To successfully write to a DataOutput, you must override this to write your specific ERC data out.
 
Methods inherited from class ec.gp.GPNode
atDepth, clone, cloneReplacing, cloneReplacing, cloneReplacing, cloneReplacingAtomic, cloneReplacingAtomic, cloneReplacingNoSubclone, constraints, contains, defaultBase, depth, errorInfo, eval, lightClone, makeCTree, makeLatexTree, nodeEquivalentTo, nodeInPosition, numNodes, numNodes, parentType, printNode, printNode, printNodeForHumans, printRootedTree, printRootedTree, printRootedTreeForHumans, readRootedTree, readRootedTree, replaceWith, rootedTreeEquals, rootedTreeHashCode, rootParent, setup, swapCompatibleWith, toStringForError, writeRootedTree
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ERC_PREFIX

public static final java.lang.String ERC_PREFIX
See Also:
Constant Field Values
Constructor Detail

ERC

public ERC()
Method Detail

name

public abstract java.lang.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. If you have only one ERC function class, you can just return "". Whatever the name is, it should generally only have letters, numbers, or hyphens or underscores in it. No whitespace or other characters.


checkConstraints

public void checkConstraints(EvolutionState state,
                             int tree,
                             GPIndividual typicalIndividual,
                             Parameter individualBase)
Checks to make certain that the ERC has no children.

Overrides:
checkConstraints in class GPNode

resetNode

public abstract void resetNode(EvolutionState state,
                               int thread)
Remember to override this to randomize your ERC after it has been cloned. The prototype will not ever receive this method call.

Overrides:
resetNode in class GPNode

nodeEquals

public abstract boolean nodeEquals(GPNode node)
Implement this to do ERC-to-ERC comparisons.

Overrides:
nodeEquals in class GPNode

nodeHashCode

public abstract int nodeHashCode()
Implement this to hash ERCs, along with other nodes, in such a way that two "equal" ERCs will usually hash to the same value.

Overrides:
nodeHashCode in class GPNode

toStringForHumans

public java.lang.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. If a simple number or other object won't suffice, you might use something that begins with ERC_PREFIX + name() + [ + ... + ]

Overrides:
toStringForHumans in class GPNode

toString

public java.lang.String toString()
This defaults to simply ERC_PREFIX + name() + "[" + encode() + "]"

Specified by:
toString in class GPNode

encode

public abstract java.lang.String encode()
Encodes data from the ERC, using ec.util.Code.


decode

public abstract boolean decode(DecodeReturn dret)
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.


mutateERC

public void mutateERC(EvolutionState state,
                      int thread)
Mutates the node's "value". This is called by mutating operators which specifically mutate the "value" of ERCs, as opposed to replacing them with whole new ERCs. The default form of this function simply calls resetNode(state,thread), but you might want to modify this to do a specialized form of mutation, applying gaussian noise for example.


writeNode

public void writeNode(EvolutionState state,
                      java.io.DataOutput dataOutput)
               throws java.io.IOException
To successfully write to a DataOutput, you must override this to write your specific ERC data out. The default implementation issues a fatal error.

Overrides:
writeNode in class GPNode
Throws:
java.io.IOException

readNode

public void readNode(EvolutionState state,
                     java.io.DataInput dataInput)
              throws java.io.IOException
To successfully read from a DataOutput, you must override this to read your specific ERC data in. The default implementation issues a fatal error.

Overrides:
readNode in class GPNode
Throws:
java.io.IOException

readNode

public GPNode readNode(DecodeReturn dret)
Description copied from class: GPNode
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. You may assume that initial whitespace has been eliminated. Generally should be case-SENSITIVE, unlike in Lisp. The default version usually works for "simple" function names, that is, not ERCs or other stuff where you have to encode the symbol.

Overrides:
readNode in class GPNode