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

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.

Impementing an ERC

A basic no-frills ERC needs to have the following things: A more advanced ERC will need some of the following gizmos:

See the ec.app.regression.func.RegERC class for an example of a simple but "fuly-implemented" ERC. A slightly more complicated example can be found in ec.app.lawnmower.func.LawnERC.

See Also:
Serialized Form

Field Summary
 
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, SITUATION_MUTATION, SITUATION_NEWIND
 
Constructor Summary
ERC()
           
 
Method Summary
 void checkConstraints(EvolutionState state, int tree, GPIndividual typicalIndividual, Parameter individualBase)
          Usually ERCs don't have children, and this default implementation makes certain of it.
 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".
 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.
 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 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, makeGraphvizSubtree, makeGraphvizTree, makeLatexTree, makeLispTree, nodeEquivalentTo, nodeInPosition, numNodes, numNodes, parentType, pathLength, printNode, printNode, printNode, printNodeForHumans, printNodeForHumans, printRootedTree, printRootedTree, printRootedTree, printRootedTreeForHumans, 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
 

Constructor Detail

ERC

public ERC()
Method Detail

name

public 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 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 GPNode

checkConstraints

public void checkConstraints(EvolutionState state,
                             int tree,
                             GPIndividual typicalIndividual,
                             Parameter individualBase)
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:
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 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. The default value, which may not be very good, is a combination of the class hash code and the hash code of the string returned by encode(). You might make a better hash 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 name() + [ + ... + ]

Overrides:
toStringForHumans in class GPNode

toString

public java.lang.String toString()
This defaults to simply name() + "[" + encode() + "]". You probably shouldn't deviate from this.

Specified by:
toString in class GPNode

encode

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


decode

public 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