ec.vector
Class DoubleVectorIndividual
java.lang.Object
ec.Individual
ec.vector.VectorIndividual
ec.vector.DoubleVectorIndividual
- All Implemented Interfaces:
- Prototype, Setup, java.io.Serializable, java.lang.Cloneable
public class DoubleVectorIndividual
- extends VectorIndividual
DoubleVectorIndividual is a VectorIndividual whose genome is an array of
doubles. Gene values may range from species.mingene(x) to species.maxgene(x),
inclusive. The default mutation method randomizes genes to new values in this
range, with species.mutationProbability. It can also add gaussian
noise to the genes, if so directed in the FloatVectorSpecies. If the gaussian
noise pushes the gene out of range, a new noise value is generated.
From ec.Individual:
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. readIndividual reads
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.
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 gene array. If you add instance variables to
a VectorIndividual or subclass, you'll need to read/write those variables as well.
Default Base
vector.double-vect-ind
- See Also:
- Serialized Form
Method Summary |
void |
clamp()
Clips each gene value to be within its specified [min,max] range. |
java.lang.Object |
clone()
Creates a new individual cloned from a prototype,
and suitable to begin use in its own evolutionary
context. |
Parameter |
defaultBase()
Returns the default base for this prototype. |
void |
defaultCrossover(EvolutionState state,
int thread,
VectorIndividual ind)
Destructively crosses over the individual with another in some default manner. |
void |
defaultMutate(EvolutionState state,
int thread)
Destructively mutates the individual in some default manner. |
double |
distanceTo(Individual otherInd)
Returns the metric distance to another individual, if such a thing can be measured. |
boolean |
equals(java.lang.Object ind)
Returns true if I am genetically "equal" to ind. |
long |
genomeLength()
Returns the length of the gene array. |
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. |
java.lang.Object |
getGenome()
Returns the gene array. |
int |
hashCode()
Returns a hashcode for the individual, such that individuals which
are equals(...) each other always return the same
hash code. |
boolean |
isInRange()
Returns true if each gene value is within is specified [min,max] range. |
void |
join(java.lang.Object[] pieces)
Joins the n pieces and sets the genome to their concatenation. |
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 |
readGenotype(EvolutionState state,
java.io.DataInput dataInput)
Reads in the genotypic information from a DataInput, erasing the previous genotype
of this Individual. |
void |
reset(EvolutionState state,
int thread)
Initializes the individual by randomly choosing doubles uniformly from
mingene to maxgene. |
void |
setGenome(java.lang.Object gen)
Sets the gene array. |
void |
setGenomeLength(int len)
Sets the genome length. |
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. |
void |
split(int[] points,
java.lang.Object[] pieces)
Splits the genome into n pieces, according to points, which *must* be
sorted. |
void |
writeGenotype(EvolutionState state,
java.io.DataOutput dataOutput)
Writes the genotypic information to a DataOutput. |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
P_DOUBLEVECTORINDIVIDUAL
public static final java.lang.String P_DOUBLEVECTORINDIVIDUAL
- See Also:
- Constant Field Values
genome
public double[] genome
DoubleVectorIndividual
public DoubleVectorIndividual()
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(...).
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 Individual
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
- Overrides:
setup
in class Individual
defaultCrossover
public void defaultCrossover(EvolutionState state,
int thread,
VectorIndividual ind)
- Description copied from class:
VectorIndividual
- Destructively crosses over the individual with another in some default manner. In most
implementations provided in ECJ, one-, two-, and any-point crossover is done with a
for loop, rather than a possibly more efficient approach like arrayCopy(). The disadvantage
is that arrayCopy() takes advantage of a CPU's bulk copying. The advantage is that arrayCopy()
would require a scratch array, so you'd be allocing and GCing an array for every crossover.
Dunno which is more efficient.
- Overrides:
defaultCrossover
in class VectorIndividual
split
public void split(int[] points,
java.lang.Object[] pieces)
- Splits the genome into n pieces, according to points, which *must* be
sorted. pieces.length must be 1 + points.length
- Overrides:
split
in class VectorIndividual
join
public void join(java.lang.Object[] pieces)
- Joins the n pieces and sets the genome to their concatenation.
- Overrides:
join
in class VectorIndividual
defaultMutate
public void defaultMutate(EvolutionState state,
int thread)
- Destructively mutates the individual in some default manner. The default
form simply randomizes genes to a uniform distribution from the min and
max of the gene values. It can also add gaussian noise to the genes, if
so directed in the FloatVectorSpecies. If the gaussian noise pushes the
gene out of range, a new noise value is generated.
- Overrides:
defaultMutate
in class VectorIndividual
reset
public void reset(EvolutionState state,
int thread)
- Initializes the individual by randomly choosing doubles uniformly from
mingene to maxgene.
- Specified by:
reset
in class VectorIndividual
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
genotypeToStringForHumans
public java.lang.String genotypeToStringForHumans()
- Description copied from class:
Individual
- 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.
- Overrides:
genotypeToStringForHumans
in class Individual
genotypeToString
public java.lang.String genotypeToString()
- Description copied from class:
Individual
- 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.
- Overrides:
genotypeToString
in class Individual
parseGenotype
protected void parseGenotype(EvolutionState state,
java.io.LineNumberReader reader)
throws java.io.IOException
- Description copied from class:
Individual
- 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.
- Overrides:
parseGenotype
in class Individual
- Throws:
java.io.IOException
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
getGenome
public java.lang.Object getGenome()
- Description copied from class:
VectorIndividual
- Returns the gene array. If you know the type of the array, you can cast it and work on
it directly. Otherwise, you can still manipulate it in general, because arrays (like
all objects) respond to clone() and can be manipulated with arrayCopy without bothering
with their type. This might be useful in creating special generalized crossover operators
-- we apologize in advance for the fact that Java doesn't have a template system. :-(
The default version returns null.
- Overrides:
getGenome
in class VectorIndividual
setGenome
public void setGenome(java.lang.Object gen)
- Description copied from class:
VectorIndividual
- Sets the gene array. See getGenome(). The default version does nothing.
- Overrides:
setGenome
in class VectorIndividual
genomeLength
public long genomeLength()
- Description copied from class:
VectorIndividual
- Returns the length of the gene array. By default, this method returns 0.
- Overrides:
genomeLength
in class VectorIndividual
writeGenotype
public void writeGenotype(EvolutionState state,
java.io.DataOutput dataOutput)
throws java.io.IOException
- Description copied from class:
Individual
- 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
- Overrides:
writeGenotype
in class Individual
- Throws:
java.io.IOException
readGenotype
public void readGenotype(EvolutionState state,
java.io.DataInput dataInput)
throws java.io.IOException
- Description copied from class:
Individual
- 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
- Overrides:
readGenotype
in class Individual
- Throws:
java.io.IOException
clamp
public void clamp()
- Clips each gene value to be within its specified [min,max] range.
NaN is presently considered in range but the behavior of this method
should be assumed to be unspecified on encountering NaN.
setGenomeLength
public void setGenomeLength(int len)
- Description copied from class:
VectorIndividual
- Sets the genome length. If the length is longer, then it is filled with a default value (likely 0 or false).
This may or may not be a valid value -- you will need to set appropriate values here.
The default implementation does nothing; but all subclasses in ECJ implement a subset of this.
- Overrides:
setGenomeLength
in class VectorIndividual
isInRange
public boolean isInRange()
- Returns true if each gene value is within is specified [min,max] range.
NaN is presently considered in range but the behavior of this method
should be assumed to be unspecified on encountering NaN.
distanceTo
public double distanceTo(Individual otherInd)
- Description copied from class:
Individual
- Returns the metric distance to another individual, if such a thing can be measured.
Subclassess of Individual should implement this if it exists for their representation.
The default implementation here, which isn't very helpful, returns 0 if the individuals are equal
and infinity if they are not.
- Overrides:
distanceTo
in class Individual