ec
Class Individual
java.lang.Object
ec.Individual
- All Implemented Interfaces:
- Prototype, Setup, java.io.Serializable, java.lang.Cloneable
- Direct Known Subclasses:
- GPIndividual, RuleIndividual, VectorIndividual
public abstract class Individual
- extends java.lang.Object
- implements Prototype
An Individual is an item in the EC population stew which is evaluated
and assigned a fitness which determines its likelihood of selection.
Individuals are created most commonly by the newIndividual(...) method
of the ec.Species class.
In general Individuals are immutable. That is, once they are created
their genetic material should not be modified. This protocol helps insure that they are
safe to read under multithreaded conditions. You can violate this protocol,
but try to do so when you know you have only have a single thread.
In addition to serialization for checkpointing, Individuals may read and write themselves to streams in three ways.
- writeIndividual(...,DataOutput)/readIndividual(...,DataInput) This method
transmits or receives an individual in binary. It is the most efficient approach to sending
individuals over networks, etc. These methods write the evaluated flag and the fitness, then
call readGenotype/writeGenotype, which you must implement to write those parts of your
Individual special to your functions-- the default versions of readGenotype/writeGenotype throw errors.
You don't need to implement them if you don't plan on using read/writeIndividual.
- printIndividual(...,PrintWriter)/readIndividual(...,LineNumberReader) This
approach transmits or receives an indivdual in text encoded such that the individual is largely readable
by humans but can be read back in 100% by ECJ as well. To do this, these methods will encode numbers
using the ec.util.Code class. These methods are mostly used to write out populations to
files for inspection, slight modification, then reading back in later on. readIndividualreads
in the fitness and the evaluation flag, then calls parseGenotype to read in the remaining individual.
You are responsible for implementing parseGenotype: the Code class is there to help you.
printIndividual writes out the fitness and evaluation flag, then calls genotypeToString
and printlns the resultant string. You are responsible for implementing the genotypeToString method in such
a way that parseGenotype can read back in the individual println'd with genotypeToString. The default form
of genotypeToString simply calls toString, which you may override instead if you like. The default
form of parseGenotype throws an error. You are not required to implement these methods, but without
them you will not be able to write individuals to files in a simultaneously computer- and human-readable fashion.
- printIndividualForHumans(...,PrintWriter) This
approach prints an individual in a fashion intended for human consumption only.
printIndividualForHumans writes out the fitness and evaluation flag, then calls genotypeToStringForHumans
and printlns the resultant string. You are responsible for implementing the genotypeToStringForHumans method.
The default form of genotypeToStringForHumans simply calls toString, which you may override instead if you like
(though note that genotypeToString's default also calls toString). You should handle one of these methods properly
to ensure individuals can be printed by ECJ.
Since individuals should be largely immutable, why is there a readIndividual method?
after all this method doesn't create a new individual -- it just erases the existing one. This is
largely historical; but the method is used underneath by the various newIndividual methods in Species,
which do create new individuals read from files. If you're trying to create a brand new individual
read from a file, look in Species.
- See Also:
- Serialized Form
Field Summary |
boolean |
evaluated
Has the individual been evaluated and its fitness determined yet? |
static java.lang.String |
EVALUATED_PREAMBLE
A string appropriate to put in front of whether or not the individual has been printed. |
Fitness |
fitness
The fitness of the Individual. |
static java.lang.String |
P_INDIVIDUAL
A reasonable parameter base element for individuals |
Species |
species
The species of the Individual. |
Method Summary |
java.lang.Object |
clone()
Creates a new individual cloned from a prototype,
and suitable to begin use in its own evolutionary
context. |
abstract boolean |
equals(java.lang.Object ind)
Returns true if I am genetically "equal" to ind. |
java.lang.String |
genotypeToString()
Print to a string the genotype of the Individual in a fashion intended
to be parsed in again via parseGenotype(...). |
java.lang.String |
genotypeToStringForHumans()
Print to a string the genotype of the Individual in a fashion readable by humans, and not intended
to be parsed in again. |
abstract int |
hashCode()
Returns a hashcode for the individual, such that individuals which
are equals(...) each other always return the same
hash code. |
protected void |
parseGenotype(EvolutionState state,
java.io.LineNumberReader reader)
This method is used only by the default version of readIndividual(state,reader),
and it is intended to be overridden to parse in that part of the individual that
was outputted in the genotypeToString() method. |
void |
printIndividual(EvolutionState state,
int log,
int verbosity)
Should print the individual in a way that can be read by computer,
including its fitness, using state.output.println(...,verbosity,log)
You can get fitness to print itself at the appropriate time by calling
fitness.printFitness(state,log,verbosity); |
void |
printIndividual(EvolutionState state,
java.io.PrintWriter writer)
Should print the individual in a way that can be read by computer,
including its fitness. |
void |
printIndividualForHumans(EvolutionState state,
int log,
int verbosity)
Should print the individual out in a pleasing way for humans,
including its
fitness, using state.output.println(...,verbosity,log)
You can get fitness to print itself at the appropriate time by calling
fitness.printFitnessForHumans(state,log,verbosity); |
void |
readGenotype(EvolutionState state,
java.io.DataInput dataInput)
Reads in the genotypic information from a DataInput, erasing the previous genotype
of this Individual. |
void |
readIndividual(EvolutionState state,
java.io.DataInput dataInput)
Reads the binary form of an individual from a DataInput, erasing the previous
information stored in this Individual. |
void |
readIndividual(EvolutionState state,
java.io.LineNumberReader reader)
Reads in the individual from a form printed by printIndividual(), erasing the previous
information stored in this Individual. |
void |
setup(EvolutionState state,
Parameter base)
Sets up the object by reading it from the parameters stored
in state, built off of the parameter base base. |
long |
size()
Returns the "size" of the individual. |
java.lang.String |
toString()
Overridden here because hashCode() is not expected to return the pointer
to the object. |
void |
writeGenotype(EvolutionState state,
java.io.DataOutput dataOutput)
Writes the genotypic information to a DataOutput. |
void |
writeIndividual(EvolutionState state,
java.io.DataOutput dataOutput)
Writes the binary form of an individual out to a DataOutput. |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
P_INDIVIDUAL
public static final java.lang.String P_INDIVIDUAL
- A reasonable parameter base element for individuals
- See Also:
- Constant Field Values
EVALUATED_PREAMBLE
public static final java.lang.String EVALUATED_PREAMBLE
- A string appropriate to put in front of whether or not the individual has been printed.
- See Also:
- Constant Field Values
fitness
public Fitness fitness
- The fitness of the Individual.
species
public Species species
- The species of the Individual.
evaluated
public boolean evaluated
- Has the individual been evaluated and its fitness determined yet?
Individual
public Individual()
clone
public java.lang.Object clone()
- Description copied from interface:
Prototype
- Creates a new individual cloned from a prototype,
and suitable to begin use in its own evolutionary
context.
Typically this should be a full "deep" clone.
However, you may share certain elements with other objects
rather than clone hem, 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 clone(),
and you have no need to do clone deeply,
and you are abstract, then you should not declare clone().
- If no ancestor of yours implements clone(),
and you have no need to do clone deeply,
and you are not abstract, then you should implement
it as follows:
public Object clone()
{
try
{
return super.clone();
}
catch ((CloneNotSupportedException e)
{ throw new InternalError(); } // never happens
}
- If no ancestor of yours implements clone(), but you
need to deep-clone some things, then you should implement it
as follows:
public Object clone()
{
try
{
MyObject myobj = (MyObject) (super.clone());
// put your deep-cloning code here...
}
catch ((CloneNotSupportedException e)
{ throw new InternalError(); } // never happens
return myobj;
}
- If an ancestor has implemented clone(), and you also need
to deep clone some things, then you should implement it as follows:
public Object clone()
{
MyObject myobj = (MyObject) (super.clone());
// put your deep-cloning code here...
return myobj;
}
- Specified by:
clone
in interface Prototype
- Overrides:
clone
in class java.lang.Object
size
public long size()
- Returns the "size" of the individual. This is used for things like
parsimony pressure. The default form of this method returns 0 --
if you care about parsimony pressure, you'll need to override the
default to provide a more descriptive measure of size.
equals
public abstract boolean equals(java.lang.Object ind)
- 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.
- Overrides:
equals
in class java.lang.Object
hashCode
public abstract int hashCode()
- Returns a hashcode for the individual, such that individuals which
are equals(...) each other always return the same
hash code.
- Overrides:
hashCode
in class java.lang.Object
toString
public java.lang.String toString()
- Overridden here because hashCode() is not expected to return the pointer
to the object. toString() normally uses hashCode() to print a unique identifier,
and that's no longer the case. You're welcome to override this anyway you
like to make the individual print out in a more lucid fashion.
- Overrides:
toString
in class java.lang.Object
genotypeToStringForHumans
public java.lang.String genotypeToStringForHumans()
- Print to a string the genotype of the Individual in a fashion readable by humans, and not intended
to be parsed in again. The fitness and evaluated flag should not be included. The default form
simply calls toString(), but you'll probably want to override this to something else.
genotypeToString
public java.lang.String genotypeToString()
- Print to a string the genotype of the Individual in a fashion intended
to be parsed in again via parseGenotype(...).
The fitness and evaluated flag should not be included. The default form
simply calls toString(), which is almost certainly wrong, and you'll probably want to override
this to something else.
setup
public void setup(EvolutionState state,
Parameter base)
- Description copied from interface:
Prototype
- Sets up the object by reading it from the parameters stored
in state, built off of the parameter base base.
If an ancestor implements this method, be sure to call
super.setup(state,base); before you do anything else.
For prototypes, setup(...) is typically called once for
the prototype instance; cloned instances do not receive
the setup(...) call. setup(...) may be called
more than once; the only guarantee is that it will get
called at least once on an instance or some "parent"
object from which it was ultimately cloned.
- Specified by:
setup
in interface Prototype
- Specified by:
setup
in interface Setup
printIndividualForHumans
public void printIndividualForHumans(EvolutionState state,
int log,
int verbosity)
- Should print the individual out in a pleasing way for humans,
including its
fitness, using state.output.println(...,verbosity,log)
You can get fitness to print itself at the appropriate time by calling
fitness.printFitnessForHumans(state,log,verbosity);
The default form of this method simply prints out whether or not the
individual has been evaluated, its fitness, and then calls Individual.genotypeToStringForHumans().
Feel free to override this to produce more sophisticated behavior,
though it is rare to need to -- instead you could just override genotypeToStringForHumans().
printIndividual
public void printIndividual(EvolutionState state,
int log,
int verbosity)
- Should print the individual in a way that can be read by computer,
including its fitness, using state.output.println(...,verbosity,log)
You can get fitness to print itself at the appropriate time by calling
fitness.printFitness(state,log,verbosity);
The default form of this method simply prints out whether or not the
individual has been evaluated, its fitness, and then calls Individual.genotypeToString().
Feel free to override this to produce more sophisticated behavior,
though it is rare to need to -- instead you could just override genotypeToString().
printIndividual
public void printIndividual(EvolutionState state,
java.io.PrintWriter writer)
- Should print the individual in a way that can be read by computer,
including its fitness. You can get fitness to print itself at the
appropriate time by calling fitness.printFitness(state,log,writer);
Usually you should try to use printIndividual(state,log,verbosity)
instead -- use this method only if you can't print through the
Output facility for some reason.
The default form of this method simply prints out whether or not the
individual has been evaluated, its fitness, and then calls Individual.genotypeToString().
Feel free to override this to produce more sophisticated behavior,
though it is rare to need to -- instead you could just override genotypeToString().
readIndividual
public void readIndividual(EvolutionState state,
java.io.LineNumberReader reader)
throws java.io.IOException
- Reads in the individual from a form printed by printIndividual(), erasing the previous
information stored in this Individual. If you are trying to create an Individual
from information read in from a stream or DataInput,
see the various newIndividual() methods in Species. The default form of this method
simply reads in evaluation information, then fitness information, and then
calls parseGenotype() (which you should implement). The Species is not changed or
attached, so you may need to do that elsewhere. Feel free to override
this method to produce more sophisticated behavior,
though it is rare to need to -- instead you could just override parseGenotype().
- Throws:
java.io.IOException
parseGenotype
protected void parseGenotype(EvolutionState state,
java.io.LineNumberReader reader)
throws java.io.IOException
- This method is used only by the default version of readIndividual(state,reader),
and it is intended to be overridden to parse in that part of the individual that
was outputted in the genotypeToString() method. The default version of this method
exits the program with an "unimplemented" error. You'll want to override this method,
or to override readIndividual(...) to not use this method.
- Throws:
java.io.IOException
writeIndividual
public void writeIndividual(EvolutionState state,
java.io.DataOutput dataOutput)
throws java.io.IOException
- Writes the binary form of an individual out to a DataOutput. This is not for serialization:
the object should only write out the data relevant to the object sufficient to rebuild it from a DataInput.
The Species will be reattached later, and you should not write it. The default version of this
method writes the evaluated and fitness information, then calls writeGenotype() to write the genotype
information. Feel free to override this method to produce more sophisticated behavior,
though it is rare to need to -- instead you could just override writeGenotype().
- Throws:
java.io.IOException
writeGenotype
public void writeGenotype(EvolutionState state,
java.io.DataOutput dataOutput)
throws java.io.IOException
- Writes the genotypic information to a DataOutput. Largely called by writeIndividual(), and
nothing else. The default simply throws an error. Various subclasses of Individual override this as
appropriate. For example, if your custom individual's genotype consists of an array of
integers, you might do this:
dataOutput.writeInt(integers.length);
for(int x=0;x
- Throws:
java.io.IOException
readGenotype
public void readGenotype(EvolutionState state,
java.io.DataInput dataInput)
throws java.io.IOException
- Reads in the genotypic information from a DataInput, erasing the previous genotype
of this Individual. Largely called by readIndividual(), and nothing else.
If you are trying to create an Individual
from information read in from a stream or DataInput,
see the various newIndividual() methods in Species.
The default simply throws an error. Various subclasses of Individual override this as
appropriate. For example, if your custom individual's genotype consists of an array of
integers, you might do this:
integers = new int[dataInput.readInt()];
for(int x=0;x
- Throws:
java.io.IOException
readIndividual
public void readIndividual(EvolutionState state,
java.io.DataInput dataInput)
throws java.io.IOException
- Reads the binary form of an individual from a DataInput, erasing the previous
information stored in this Individual. This is not for serialization:
the object should only read in the data written out via printIndividual(state,dataInput).
If you are trying to create an Individual
from information read in from a stream or DataInput,
see the various newIndividual() methods in Species. The default form of this method
simply reads in evaluation information, then fitness information, and then
calls readGenotype() (which you will need to override -- its default form simply throws an error).
The Species is not changed or attached, so you may need to do that elsewhere. Feel free to override
this method to produce more sophisticated behavior, though it is rare to need to -- instead you could
just override readGenotype().
- Throws:
java.io.IOException