/*
Copyright 2006 by Sean Luke
Licensed under the Academic Free License version 3.0
See the file "LICENSE" for more information
*/
package ec.vector;
import ec.*;
import ec.util.*;
import java.io.*;
/*
* GeneVectorIndividual.java
* Created: Thu Mar 22 13:13:20 EST 2001
*/
/**
* GeneVectorIndividual is a VectorIndividual whose genome is an array of VectorGenes.
* The default mutation method calls the mutate() method on each gene independently
* with species.mutationProbability. Initialization calls reset(), which
* should call reset() on each gene. Do not expect that the genes will actually
* exist during initialization -- see the default implementation of reset() as an example
* for how to handle this.
*
*
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.gene-vect-ind
* @author Sean Luke
* @version 1.0
*/
public class GeneVectorIndividual extends VectorIndividual
{
public static final String P_GENEVECTORINDIVIDUAL = "gene-vect-ind";
public VectorGene[] genome;
public Parameter defaultBase()
{
return VectorDefaults.base().push(P_GENEVECTORINDIVIDUAL);
}
public Object clone()
{
GeneVectorIndividual myobj = (GeneVectorIndividual) (super.clone());
// must clone the genome
myobj.genome = (VectorGene[])(genome.clone());
for(int x=0;x point) { int p = point0; point0 = point; point = p; }
for(int x=point0*s.chunksize;x=pieces.length-2)
point1 = genome.length;
else point1 = points[x+1];
}
}
/** Joins the n pieces and sets the genome to their concatenation.*/
public void join(Object[] pieces)
{
int sum=0;
for(int x=0;x0.0)
for(int x=0;x>> 31 ) ^ genome[x].hashCode();
return hash;
}
public String genotypeToStringForHumans()
{
StringBuffer s = new StringBuffer();
for( int i = 0 ; i < genome.length ; i++ )
{ s.append(" "); s.append(genome[i].printGeneToStringForHumans()); }
return s.toString();
}
public String genotypeToString()
{
StringBuffer s = new StringBuffer();
for( int i = 0 ; i < genome.length ; i++ )
{ s.append(" "); s.append(genome[i].printGeneToString()); }
return s.toString();
}
protected void parseGenotype(final EvolutionState state,
final LineNumberReader reader) throws IOException
{
// read in the next line. The first item is the number of genes
String s = reader.readLine();
DecodeReturn d = new DecodeReturn(s);
Code.decode( d );
int lll = (int)(d.l);
genome = new VectorGene[ lll ];
GeneVectorSpecies _species = (GeneVectorSpecies) species;
for( int i = 0 ; i < genome.length ; i++ )
{
genome[i] = (VectorGene)(_species.genePrototype.clone());
genome[i].readGene(state,reader);
}
}
public boolean equals(Object ind)
{
if (!(this.getClass().equals(ind.getClass()))) return false;
GeneVectorIndividual i = (GeneVectorIndividual)ind;
if( genome.length != i.genome.length )
return false;
for( int j = 0 ; j < genome.length ; j++ )
if( !(genome[j].equals(i.genome[j])))
return false;
return true;
}
public Object getGenome()
{ return genome; }
public void setGenome(Object gen)
{ genome = (VectorGene[]) gen; }
public int genomeLength()
{ return genome.length; }
// clone all the genes
public void cloneGenes(Object piece)
{
VectorGene[] genes = (VectorGene[]) piece;
for(int i = 0 ; i < genes.length; i++)
{
if (genes[i] != null) genes[i] = (VectorGene)(genes[i].clone());
}
}
public void writeGenotype(final EvolutionState state,
final DataOutput dataOutput) throws IOException
{
dataOutput.writeInt(genome.length);
for(int x=0;x