/* 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.*; /* * BitVectorIndividual.java * Created: Tue Mar 13 15:03:12 EST 2001 */ /** * BitVectorIndividual is a VectorIndividual whose genome is an array of booleans. * The default mutation method simply flips bits with mutationProbability. * *

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.bit-vect-ind * @author Sean Luke * @version 1.0 */ public class BitVectorIndividual extends VectorIndividual { public static final String P_BITVECTORINDIVIDUAL = "bit-vect-ind"; public boolean[] genome; public Parameter defaultBase() { return VectorDefaults.base().push(P_BITVECTORINDIVIDUAL); } public Object clone() { BitVectorIndividual myobj = (BitVectorIndividual) (super.clone()); // must clone the genome myobj.genome = (boolean[])(genome.clone()); return myobj; } public void setup(final EvolutionState state, final Parameter base) { super.setup(state,base); // actually unnecessary (Individual.setup() is empty) VectorSpecies s = (VectorSpecies)species; // where my default info is stored genome = new boolean[s.genomeSize]; } public void defaultCrossover(EvolutionState state, int thread, VectorIndividual ind) { VectorSpecies s = (VectorSpecies)species; // where my default info is stored BitVectorIndividual i = (BitVectorIndividual) ind; boolean tmp; int point; if (genome.length != i.genome.length) state.output.fatal("Genome lengths are not the same for fixed-length vector crossover"); switch(s.crossoverType) { case VectorSpecies.C_ONE_POINT: point = state.random[thread].nextInt((genome.length / s.chunksize)+1); 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.hashCode(); return hash; } public String genotypeToStringForHumans() { String s = ""; for( int i = 0 ; i < genome.length ; i++ ) { if( genome[i] ) s = s + " 1"; else s = s + " 0"; } return s; } public String genotypeToString() { StringBuffer s = new StringBuffer(); s.append( Code.encode( genome.length ) ); for( int i = 0 ; i < genome.length ; i++ ) s.append( Code.encode( genome[i] ) ); 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 boolean[ lll ]; // read in the genes for( int i = 0 ; i < genome.length ; i++ ) { Code.decode( d ); genome[i] = (boolean)(d.l!=0); } } public boolean equals(Object ind) { if (!(this.getClass().equals(ind.getClass()))) return false; // SimpleRuleIndividuals are special. BitVectorIndividual i = (BitVectorIndividual)ind; if( genome.length != i.genome.length ) return false; for( int j = 0 ; j < genome.length ; j++ ) if( genome[j] != i.genome[j] ) return false; return true; } public Object getGenome() { return genome; } public void setGenome(Object gen) { genome = (boolean[]) gen; } public int genomeLength() { return genome.length; } public void setGenomeLength(int len) { boolean[] newGenome = new boolean[len]; System.arraycopy(genome, 0, newGenome, 0, genome.length < newGenome.length ? genome.length : newGenome.length); genome = newGenome; } public void writeGenotype(final EvolutionState state, final DataOutput dataOutput) throws IOException { dataOutput.writeInt(genome.length); for(int x=0;x