ec.gp
Class GPIndividual

java.lang.Object
  extended byec.Individual
      extended byec.gp.GPIndividual
All Implemented Interfaces:
java.lang.Cloneable, Prototype, java.io.Serializable, Setup

public class GPIndividual
extends Individual

GPIndividual is an Individual used for GP evolution runs. GPIndividuals contain, at the very least, a nonempty array of GPTrees. You can use GPIndividual directly, or subclass it to extend it as you see fit.

GPIndividuals have two clone methods. The standard protoClone method in GPIndividual is NOT a deep clone. If you require a deep clone, Individual provides the deepClone method, which all Individuals should properly override to ensure deep cloning.

In addition to serialization for checkpointing, Individuals may read and write themselves to streams in three ways.

In general, the various readers and writers do three things: they tell the Fitness to read/write itself, they read/write the evaluated flag, and they read/write the GPTree array (by having each GPTree read/write itself). If you add instance variables to GPIndividual, you'll need to read/write those variables as well.

Parameters

base.numtrees
int >= 1
(number of trees in the GPIndividual)
base.tree.n
classname, inherits or = ec.gp.GPTree
(class of tree n in the individual)

Default Base
gp.individual

Parameter bases

base.tree.n tree n in the individual

See Also:
Serialized Form

Field Summary
static java.lang.String EVALUATED_PREAMBLE
           
static java.lang.String P_INDIVIDUAL
           
static java.lang.String P_NUMTREES
           
static java.lang.String P_TREE
           
 GPTree[] trees
           
 
Fields inherited from class ec.Individual
evaluated, fitness, species
 
Constructor Summary
GPIndividual()
           
 
Method Summary
 Individual deepClone()
          Guaranteed DEEP-CLONES the individual.
 Parameter defaultBase()
          Returns the default base for this prototype.
 boolean equals(java.lang.Object ind)
          Returns true if I am genetically "equal" to ind.
 int hashCode()
          Returns a hashcode for the individual, such that individuals which are equals(...) each other always return the same hash code.
 void printIndividual(EvolutionState state, int log, int verbosity)
          Prints the individual in a way that it can be read in again by computer.
 void printIndividual(EvolutionState state, java.io.PrintWriter writer)
          Prints the individual in a way that it can be read in again by computer.
 void printIndividualForHumans(EvolutionState state, int log, int verbosity)
          A printer for the individual in a reasonable human-readable, fashion.
 java.lang.Object protoClone()
          Creates a new individual cloned from a prototype, and suitable to begin use in its own evolutionary context.
 void readGenotype(EvolutionState state, java.io.DataInput dataInput)
           
 void readIndividual(EvolutionState state, java.io.LineNumberReader reader)
          Reads in the individual from a form printed by printIndividual().
 void setup(EvolutionState state, Parameter base)
          Sets up a prototypical GPIndividual with those features which it shares with other GPIndividuals in its species, and nothing more.
 long size()
          Returns the "size" of the individual, namely, the number of nodes in all of its subtrees.
 void writeGenotype(EvolutionState state, java.io.DataOutput dataOutput)
           
 
Methods inherited from class ec.Individual
genotypeToString, genotypeToStringForHumans, parseGenotype, protoCloneSimple, readIndividual, toString, writeIndividual
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

P_INDIVIDUAL

public static final java.lang.String P_INDIVIDUAL
See Also:
Constant Field Values

P_NUMTREES

public static final java.lang.String P_NUMTREES
See Also:
Constant Field Values

P_TREE

public static final java.lang.String P_TREE
See Also:
Constant Field Values

EVALUATED_PREAMBLE

public static final java.lang.String EVALUATED_PREAMBLE
See Also:
Constant Field Values

trees

public GPTree[] trees
Constructor Detail

GPIndividual

public GPIndividual()
Method Detail

defaultBase

public Parameter defaultBase()
Description copied from interface: Prototype
Returns the default base for this prototype. This should generally be implemented by building off of the static base() method on the DefaultsForm object for the prototype's package. This should be callable during setup(...).


equals

public boolean equals(java.lang.Object ind)
Description copied from class: Individual
Returns true if I am genetically "equal" to ind. This should mostly be interpreted as saying that we are of the same class and that we hold the same data. It should NOT be a pointer comparison.

Specified by:
equals in class Individual

hashCode

public int hashCode()
Description copied from class: Individual
Returns a hashcode for the individual, such that individuals which are equals(...) each other always return the same hash code.

Specified by:
hashCode in class Individual

setup

public void setup(EvolutionState state,
                  Parameter base)
Sets up a prototypical GPIndividual with those features which it shares with other GPIndividuals in its species, and nothing more.

Specified by:
setup in interface Prototype
Overrides:
setup in class Individual

printIndividualForHumans

public void printIndividualForHumans(EvolutionState state,
                                     int log,
                                     int verbosity)
A printer for the individual in a reasonable human-readable, fashion. The default version prints out whether the individual has been evaluated, what its fitness is, and each of its trees in turn. Don't write the species. Modify this function as you see fit.

Overrides:
printIndividualForHumans in class Individual

printIndividual

public void printIndividual(EvolutionState state,
                            int log,
                            int verbosity)
Prints the individual in a way that it can be read in again by computer. The default version prints out whether the individual has been evaluated, what its fitness is, and each of its trees in turn. Don't write the species. Modify this function as you see fit.

Overrides:
printIndividual in class Individual

printIndividual

public void printIndividual(EvolutionState state,
                            java.io.PrintWriter writer)
Prints the individual in a way that it can be read in again by computer. The default version prints out whether the individual has been evaluated, what its fitness is, and each of its trees in turn. Don't write the species. Modify this function as you see fit.

Overrides:
printIndividual in class Individual

writeGenotype

public void writeGenotype(EvolutionState state,
                          java.io.DataOutput dataOutput)
                   throws java.io.IOException
Overrides:
writeGenotype in class Individual
Throws:
java.io.IOException

readGenotype

public void readGenotype(EvolutionState state,
                         java.io.DataInput dataInput)
                  throws java.io.IOException,
                         java.lang.CloneNotSupportedException
Overrides:
readGenotype in class Individual
Throws:
java.io.IOException
java.lang.CloneNotSupportedException

readIndividual

public void readIndividual(EvolutionState state,
                           java.io.LineNumberReader reader)
                    throws java.io.IOException,
                           java.lang.CloneNotSupportedException
Reads in the individual from a form printed by printIndividual(). The Fitness should have already been assigned to the individual at this point. Don't read in the species, it should also get set externally by whoever calls this function.

Overrides:
readIndividual in class Individual
Throws:
java.io.IOException
java.lang.CloneNotSupportedException

deepClone

public Individual deepClone()
Description copied from class: Individual
Guaranteed DEEP-CLONES the individual. You may need to override this method in Individual subclasses to guarantee this fact. Some individuals (notably GPIndividuals) do not have protoClone() deep-clone them because that is expensive. But if you need to guarantee that you have a unique individual, this is the way to do it.

Overrides:
deepClone in class Individual

protoClone

public java.lang.Object protoClone()
                            throws java.lang.CloneNotSupportedException
Description copied from interface: Prototype
Creates a new individual cloned from a prototype, and suitable to begin use in its own evolutionary context.

The question here is whether or not this means to perform a "deep" or "light" ("shallow") clone, or something in-between. You may need to deep-clone parts of your object rather than simply copying their references, depending on the situation:

  • If you hold objects which are shared with other instances, don't clone them.
  • If you hold objects which must be unique, clone them.
  • If you hold objects which were given to you as a gesture of kindness, and aren't owned by you, you probably shouldn't clone them.
  • DON'T attempt to clone: Singletons, Cliques, or Groups.
  • Arrays are not cloned automatically; you may need to clone an array if you're not sharing it with other instances. Arrays have the nice feature of being copyable by calling clone() on them.

Implementations.

  • If no ancestor of yours implements protoClone(), and you have no need to either (light cloning is fine with you), and you are abstract, then you should not declare protoClone().
  • If no ancestor of yours implements protoClone(), and you have no need to either (light cloning is fine with you), and you are not abstract, then you should implement it as follows:

            public Object protoClone() throws CloneNotSupportedException
            { 
            return super.clone();
            }
            
  • If no ancestor of yours implements protoClone(), but you need to deep-clone some things, then you should implement it as follows:

            public Object protoClone() throws CloneNotSupportedException
            {
            myobj = (MyObject) (super.clone());
    
            // put your deep-cloning code here...
            // ...you should use protoClone and not 
            // protoCloneSimple to clone subordinate objects...
            return myobj;
            } 
            
  • If you need to override an ancestors' implementation of protoClone, in order to do your own deep cloning as well, then you should implement it as follows:

            public Object protoClone() throws CloneNotSupportedException
            {
            MyObject myobj = (MyObject)(super.protoClone());
    
            // put your deep-cloning code here...
            // ...you should use protoClone and not 
            // protoCloneSimple to clone subordinate objects...
            return myobj;
            } 
            

If you know that your superclasses will never change their protoClone() implementations, you might try inlining them in your overridden protoClone() method. But this is dangerous (though it yields a small net increase).

In general, you want to keep your deep cloning to an absolute minimum, so that you don't have to call protoClone() but one time.

The approach taken here is the fastest that I am aware of while still permitting objects to be specified at runtime from a parameter file. It would be faster to use the "new" operator; but that would require hard-coding that we can't do. Although using java.lang.Object.clone() entails an extra layer that deals with stripping away the "protected" keyword and also wrapping the exception handling (which is a BIG hit, about three times as slow as using "new"), it's still MUCH faster than using java.lang.Class.newInstance(), and also much faster than rolling our own Clone() method.

Specified by:
protoClone in interface Prototype
Overrides:
protoClone in class Individual
Throws:
java.lang.CloneNotSupportedException

size

public long size()
Returns the "size" of the individual, namely, the number of nodes in all of its subtrees.

Overrides:
size in class Individual