org.jscience.computing.ai.evolutionary.geneticalgorithms
Class BinaryCodedGA

java.lang.Object
  extended by org.jscience.computing.ai.evolutionary.geneticalgorithms.BinaryCodedGA

public abstract class BinaryCodedGA
extends java.lang.Object

Implementation of binary-coded genetic algorithm.

If you don't want to use default implementation of any method. You should extend this class and override that method.


Field Summary
protected  double avgFitness
          Average fitness in the current population
protected  int bestChromosomeNo
          Index of the best chromosome in the current generation
protected  int chromosomeLength
          Size of the chromosomes for individuals
protected  double crossoverProbability
          Probability of crossover operation
protected  boolean[][] currentPopulation
          Individuals in the current generation
protected  double[] currentPopulationFitness
          Fitnesses of the individuals in the current generation
protected  int generationNo
          Current generation number
protected  double[] historyAvgFitness
          Average fitnesses over all generations
protected  int historyLength
          Maxiumum hist�ry length for historyMaxFitness, historyMinFitness and historyAvgFitness variables
protected  double[] historyMaxFitness
          Maximum fitnesses over all generations
protected  double[] historyMinFitness
          Minimum fitnesses over all generations
protected  double maxFitness
          Maximum fitness in the current population
protected  double minFitness
          Minimum fitness in the current population
protected  double mutationProbability
          Probability of mutation operation
protected  boolean[][] nextPopulation
          Individuals in the next generation
protected  int populationSize
          Size of the population
protected  double tmpSumOfFitnesses
          TODO: Remove this vriable in the future relases.
protected  double tmpSumx
          TODO: Remove this vriable in the future relases.
 
Constructor Summary
BinaryCodedGA()
          Creates a binary-coded genetic algorithm with default parameters.
BinaryCodedGA(int populationSize, int chromosomeLength, double crossoverProbability, double mutationProbability)
          Creates a binary-coded genetic algorithm with given parameters.
BinaryCodedGA(int populationSize, int chromosomeLength, double crossoverProbability, double mutationProbability, int maxHistory)
          Creates a binary-coded genetic algorithm with given parameters.
 
Method Summary
protected  void crossoverChromosomes(boolean[] chromosome1, boolean[] chromosome2)
          Applies crossover operator to given chromosomes.
static boolean[] deSerializeIndividual(java.lang.String fileName, boolean isBinary)
          Deserializes given individual from disk with the given file name.
 double evaluateIndividual(int chromosomeNo, boolean[] chromosome)
          Evaluates an individual in the population.
 void evolve()
          Evolves the population.
 double getAvgFitness()
          Returns average fitness in the current generation
 double[] getAvgHistory()
          Returns the history of the average fitnesses
 int getChromosomeLength()
          Returns the length of chromosomes.
 double getCrossoverProbability()
          Returns probability of the crossover operation
 double[] getCurrentPopulationFitness()
          Returns fitnesses of the individuals in the current generation
 boolean[] getFittestChromosome()
          Returns the best individual in the current generation
 double getMaxFitness()
          Returns maximum fitness in the current generation
 double[] getMaxHistory()
          Returns the fitness history of best individuals over all generations.
 double getMinFitness()
          Returns minimum fitness in the current generation
 double[] getMinHistory()
          Returns the fitness history of worst individuals over all generations.
 double getMutationProbability()
          Returns probability of the mutation operation
 boolean[][] getPopulation()
          Returns individuals of the current generation
 int getPopulationSize()
          Returns population size
 void initializePopulation()
          Intitializes the population.
protected  boolean mutateChromosome(boolean bit)
          Mutates a given bit in a chromosome randomly.
 void printIndividual(boolean[] chromosome)
          Prints given individual
protected  int selectChromosome()
          Selects a chromosome using roulette-selection.
static void serializeIndividual(boolean[] chromosome, java.lang.String fileName, boolean isBinary)
          Serializes given individual to disk with the given file name.
 void setChromosomeFitness(int chromosomeNo, double newFitness)
          Changes fitness of the given chromosome
 void setCrossoverProbability(double newProbability)
          Changes probability of the crossover operation
 void setMutationProbability(double newProbability)
          Changes probability of the mutation operation
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

populationSize

protected int populationSize
Size of the population


chromosomeLength

protected int chromosomeLength
Size of the chromosomes for individuals


crossoverProbability

protected double crossoverProbability
Probability of crossover operation


mutationProbability

protected double mutationProbability
Probability of mutation operation


currentPopulation

protected boolean[][] currentPopulation
Individuals in the current generation


nextPopulation

protected boolean[][] nextPopulation
Individuals in the next generation


currentPopulationFitness

protected double[] currentPopulationFitness
Fitnesses of the individuals in the current generation


generationNo

protected int generationNo
Current generation number


bestChromosomeNo

protected int bestChromosomeNo
Index of the best chromosome in the current generation


maxFitness

protected double maxFitness
Maximum fitness in the current population


minFitness

protected double minFitness
Minimum fitness in the current population


avgFitness

protected double avgFitness
Average fitness in the current population


historyMaxFitness

protected double[] historyMaxFitness
Maximum fitnesses over all generations


historyMinFitness

protected double[] historyMinFitness
Minimum fitnesses over all generations


historyAvgFitness

protected double[] historyAvgFitness
Average fitnesses over all generations


historyLength

protected int historyLength
Maxiumum hist�ry length for historyMaxFitness, historyMinFitness and historyAvgFitness variables


tmpSumx

protected double tmpSumx
TODO: Remove this vriable in the future relases.


tmpSumOfFitnesses

protected double tmpSumOfFitnesses
TODO: Remove this vriable in the future relases.

Constructor Detail

BinaryCodedGA

public BinaryCodedGA()
Creates a binary-coded genetic algorithm with default parameters.


BinaryCodedGA

public BinaryCodedGA(int populationSize,
                     int chromosomeLength,
                     double crossoverProbability,
                     double mutationProbability)
Creates a binary-coded genetic algorithm with given parameters.

Parameters:
populationSize - size of the population
chromosomeLength - size of the chromosomes for individuals
crossoverProbability - probability of crossover operation
mutationProbability - probability of mutation operation

BinaryCodedGA

public BinaryCodedGA(int populationSize,
                     int chromosomeLength,
                     double crossoverProbability,
                     double mutationProbability,
                     int maxHistory)
Creates a binary-coded genetic algorithm with given parameters.

Parameters:
populationSize - size of the population
chromosomeLength - size of the chromosomes for individuals
crossoverProbability - probability of crossover operation
mutationProbability - probability of mutation operation
maxHistory - maxiumum hist�ry length for historyMaxFitness, historyMinFitness and historyAvgFitness variables
Method Detail

initializePopulation

public void initializePopulation()
                          throws InvalidFitnessValueException
Intitializes the population.

Individuals in the population are created randomly and their fitnesses are calculated.

If histories (historyAvgFitness ...) are used they are also updated.

Throws:
InvalidFitnessValueException - if the fitness value of any individual is lower than zero.

evaluateIndividual

public double evaluateIndividual(int chromosomeNo,
                                 boolean[] chromosome)
Evaluates an individual in the population.

Choromosome index parameter is used to update fitness related data of the individual. Ex. currentPopulationFitness

Parameters:
chromosomeNo - index of the chromosome in the current population to be evaluated.
chromosome - chromosome of the individual to be evaluated.
Returns:
fitness of the individual.

evolve

public void evolve()
            throws InvalidFitnessValueException
Evolves the population.

Throws:
InvalidFitnessValueException - if the fitness value of any individual is lower than zero.

selectChromosome

protected int selectChromosome()
Selects a chromosome using roulette-selection.

Returns:
index of the selected chromosome in the current population.

mutateChromosome

protected boolean mutateChromosome(boolean bit)
Mutates a given bit in a chromosome randomly.

If a randomly chosen number between zero and one is lower than mutationProbability then the bit is mutated.

Parameters:
bit - bit to mutate
Returns:
mutated bit
See Also:
mutationProbability

crossoverChromosomes

protected void crossoverChromosomes(boolean[] chromosome1,
                                    boolean[] chromosome2)
Applies crossover operator to given chromosomes.

Parameters:
chromosome1 - first parent chromosome
chromosome2 - second parent chromosome

setChromosomeFitness

public void setChromosomeFitness(int chromosomeNo,
                                 double newFitness)
                          throws InvalidFitnessValueException
Changes fitness of the given chromosome

Parameters:
chromosomeNo - index of the chromosome in the current population
newFitness - new fitness of the individual
Throws:
InvalidFitnessValueException - if a fitness value lower than zero is tried to be used.

getMaxFitness

public double getMaxFitness()
Returns maximum fitness in the current generation

Returns:
maximum fitness in the current generation

getMinFitness

public double getMinFitness()
Returns minimum fitness in the current generation

Returns:
minimum fitness in the current generation.

getAvgFitness

public double getAvgFitness()
Returns average fitness in the current generation

Returns:
average fitness in the current generation.

getCurrentPopulationFitness

public double[] getCurrentPopulationFitness()
Returns fitnesses of the individuals in the current generation

Returns:
fitnesses of the current population

getPopulation

public boolean[][] getPopulation()
Returns individuals of the current generation

Returns:
current population

getFittestChromosome

public boolean[] getFittestChromosome()
Returns the best individual in the current generation

Returns:
best individual

getMaxHistory

public double[] getMaxHistory()
Returns the fitness history of best individuals over all generations.

Returns:
fitnesses of the best individuals for all previous generations

getMinHistory

public double[] getMinHistory()
Returns the fitness history of worst individuals over all generations.

Returns:
fitnesses of the worst individuals for all previous generations

getAvgHistory

public double[] getAvgHistory()
Returns the history of the average fitnesses

Returns:
fitnesses of the average fitnesses for all previous generations

getCrossoverProbability

public double getCrossoverProbability()
Returns probability of the crossover operation

Returns:
probability of crossover

getMutationProbability

public double getMutationProbability()
Returns probability of the mutation operation

Returns:
probability of mutation

setCrossoverProbability

public void setCrossoverProbability(double newProbability)
Changes probability of the crossover operation

Parameters:
newProbability - new crossover probability

setMutationProbability

public void setMutationProbability(double newProbability)
Changes probability of the mutation operation

Parameters:
newProbability - new mutation probability

getPopulationSize

public int getPopulationSize()
Returns population size

Returns:
population size

getChromosomeLength

public int getChromosomeLength()
Returns the length of chromosomes.

Returns:
number of bits in the chromosome

printIndividual

public void printIndividual(boolean[] chromosome)
Prints given individual

Parameters:
chromosome - individual to print

serializeIndividual

public static void serializeIndividual(boolean[] chromosome,
                                       java.lang.String fileName,
                                       boolean isBinary)
                                throws java.io.IOException
Serializes given individual to disk with the given file name.

Parameters:
chromosome - individual to serialize
fileName - name of the file where the serialized individual will be written
isBinary - specifies whether the serialization done with Java's own serialization method or with a simple text representation. If set to true, then Java serialization will be used.
Throws:
java.io.IOException - if serialization fails

deSerializeIndividual

public static boolean[] deSerializeIndividual(java.lang.String fileName,
                                              boolean isBinary)
                                       throws java.io.IOException,
                                              java.lang.ClassNotFoundException
Deserializes given individual from disk with the given file name.

Parameters:
fileName - name of the file where the serialized individual will be written
isBinary - specifies whether the serialization done with Java's own serialization method or with a simple text representation. If set to true, then Java serialization will be used.
Returns:
deserialized individual
Throws:
java.lang.ClassNotFoundException - if deserialization fails
java.io.IOException - if deserialization fails