/*
  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 Genes.
 * 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. * *

*

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 Gene[] genome; public Parameter defaultBase() { return VectorDefaults.base().push(P_GENEVECTORINDIVIDUAL); } public Object clone() { GeneVectorIndividual myobj = (GeneVectorIndividual) (super.clone()); // must clone the genome myobj.genome = (Gene[])(genome.clone()); for(int x=0;x point) { int p = point0; point0 = point; point = p; } for(int x=point0*s.chunksize;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;x>> 31 ) ^ genome[x].hashCode(); return hash; } public String genotypeToStringForHumans() { StringBuilder s = new StringBuilder(); for( int i = 0 ; i < genome.length ; i++ ) { if (i > 0) s.append(" "); s.append(genome[i].printGeneToStringForHumans()); } return s.toString(); } public String genotypeToString() { StringBuilder s = new StringBuilder(); 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 ); if (d.type != DecodeReturn.T_INTEGER) // uh oh state.output.fatal("Individual with genome:\n" + s + "\n... does not have an integer at the beginning indicating the genome count."); int lll = (int)(d.l); genome = new Gene[ lll ]; GeneVectorSpecies _species = (GeneVectorSpecies) species; for( int i = 0 ; i < genome.length ; i++ ) { genome[i] = (Gene)(_species.genePrototype.clone()); genome[i].readGene(state,reader); } } public boolean equals(Object ind) { if (ind == null) return false; 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 = (Gene[]) gen; } public int genomeLength() { return genome.length; } // clone all the genes public void cloneGenes(Object piece) { Gene[] genes = (Gene[]) piece; for(int i = 0 ; i < genes.length; i++) { if (genes[i] != null) genes[i] = (Gene)(genes[i].clone()); } } public void writeGenotype(final EvolutionState state, final DataOutput dataOutput) throws IOException { dataOutput.writeInt(genome.length); for(int x=0;x

Ads help maintain this website.