Java Library for Genetic Algorithms
int num_chromosomes public Geneticint num_genes_per_chromosome,
int num_chromosomes, float crossover_fraction,
float mutation_fraction
The method sort is used to sort the population of chromosomes in most fit first order. The methods getGene and setGene are used to fetch and change the value
of any gene bit in any chromosome. These methods are protected but you will probably not need to override them in derived classes.
protected void sort protected boolean getGeneint chromosome,
int gene protected void setGeneint chromosome,
int gene, int value protected void setGeneint chromosome,
int gene, boolean value
The methods evolve, doCrossovers, doM utations, and doRemoveDuplicates are utilities for running GA simulations. These methods are protected but you will
probably not need to override them in derived classes.
protected void evolve protected void doCrossovers
protected void doMutations protected void doRemoveDuplicates
When you subclass class Genetic you must implement the following abstract method calcF itness that will determine the evolution of chromosomes during the GA sim-
ulation.
Implement the following method in sub-classes: abstract public void calcFitness;
} The class Chromosome represents a bit set with a specified number of bits and a
floating point fitness value.
102
class Chromosome { private Chromosome
public Chromosomeint num_genes public boolean getBitint index
public void setBitint index, boolean value public float getFitness
public void setFitnessfloat value public boolean equalsChromosome c
}
The class ChromosomeComparator implements a Comparator interface and is application specific: it is used to sort a population in “best first” order:
class ChromosomeComparator implements ComparatorChromosome {
public int compareChromosome o1, Chromosome o2
} The last class ChromosomeComparator is used when using the Java Collection
class static sort method. The class Genetic is an abstract class: you must subclass it and implement the
method calcF itness that uses an application specific fitness function that you must supply to set a fitness value for each chromosome.
This GA library provides the following behavior: • Generates an initial random population with a specified number of bits or
genes per chromosome and a specified number of chromosomes in the popu- lation
• Ability to evaluate each chromosome based on a numeric fitness function • Ability to create new chromosomes from the most fit chromosomes in the
population using the genetic crossover and mutation operations There are two class constructors for Genetic set up a new GA experiment by setting
the number of genes or bits per chromosome, and the number of chromosomes in the population.
The Genetic class constructors build an array of integers rouletteW heel which is used to weight the most fit chromosomes in the population for choosing the parents
103
of crossover and mutation operations. When a chromosome is being chosen, a ran- dom integer is selected to be used as an index into the rouletteW heel array; the
values in the array are all integer indices into the chromosome array. More fit chro- mosomes are heavily weighted in favor of being chosen as parents for the crossover
operations. The algorithm for the crossover operation is fairly simple; here is the implementation:
public void doCrossovers { int num = intnumChromosomes crossoverFraction;
for int i = num - 1; i = 0; i-- { Don’t overwrite the best chromosome
from current generation: int c1 = 1 + int rouletteWheelSize - 1
Math.random 0.9999f; int c2 = 1 + int rouletteWheelSize - 1
Math.random 0.9999f; c1 = rouletteWheel[c1];
c2 = rouletteWheel[c2]; if c1 = c2 {
int locus = 1+intnumGenesPerChromosome-2 Math.random;
for int g = 0; gnumGenesPerChromosome; g++ { if g locus {
setGenei, g, getGenec1, g; } else {
setGenei, g, getGenec2, g; }
} }
} }
The method doM utations is similar to doCrossovers: we randomly choose chro- mosomes from the population and for these selected chromosomes we randomly
“flip” the value of one gene a gene is a bit in our implementation:
public void doMutations { int num = intnumChromosomes mutationFraction;
for int i = 0; i num; i++ { Don’t overwrite the best chromosome
from current generation: int c = 1 + int numChromosomes - 1
Math.random 0.99;
104
int g = int numGenesPerChromosome Math.random 0.99;
setGenec, g, getGenec, g; }
} We developed a general purpose library in this section for simulating populations of
chromosomes that can evolve to a more “fit” population given a fitness function that ranks individual chromosomes in order of fitness. In Section 6.3 we will develop an
example GA application by defining the size of a population and the fitness function defined by Equation 6.1.