org.generation5.bio
Class GeneticAlgorithm

java.lang.Object
  extended byorg.generation5.bio.GeneticAlgorithm
All Implemented Interfaces:
Steppable

public class GeneticAlgorithm
extends java.lang.Object
implements Steppable

GeneticAlgorithm will evolve anything that implements the Evolvable interface. The genetic algorithm implements Steppable, with each step of the GA being implemented within doStep.

Currently, only elitism is supported.

See Also:
Steppable, Steppable.doStep(), Evolvable

Field Summary
protected  double averageFitness
          The average fitness for this generation.
protected  java.util.LinkedList averageFitnesses
          The average fitness linked list.
protected  java.util.LinkedList bestFitnesses
          The best fitness linked list.
protected  int currentIteration
          The current iteration of the genetic algorithm, incremented upon each call to doStep.
protected  double elitePercentage
          The percentage of elites to copy to the new population (defaults to 0.05, or 5%).
protected  boolean logData
          Determines whether average and best fitness data should be logged.
protected  int maximumDataPoints
          The maximum number of data points to store in the linked lists.
protected  int maximumIterations
          The maximum number of iterations (currently unused).
protected  double mutationRate
          The mutation rate (default to 0.05, or 5%).
protected  Evolvable[][] population
          The population to evolve.
 
Constructor Summary
GeneticAlgorithm()
          Creates a new instance of GeneticAlgorithm
GeneticAlgorithm(Evolvable[] population)
          Create a instance of the genetic algorithm with an initial population.
 
Method Summary
 void doStep()
          Steps through one iteration of the genetic algorithm.
protected  void flipBuffer()
          Flips the genetic algorithm buffer.
 double getAverageFitness()
          Return the average fitness of this generation.
 java.util.LinkedList getAverageFitnesses()
          Return the average fitness linked list.
 Evolvable getBest()
          Retrieves the best Evolvable in the population.
 double getBestFitness()
          Retrieves the best fitness level.
 java.util.LinkedList getBestFitnesses()
          Return the best fitness linked list.
 int getCurrentIteration()
          Return the current iteration of the genetic algorithm.
 Evolvable[] getCurrentPopulation()
          Return the current population (double-buffering taken into account).
 int getMaximumDataPoints()
          Returns the maximum number of data points the GA is logging.
 double getMutationRate()
          Retrieve the current mutation rate.
 void init()
          Initialize the genetic algorithm.
protected  Evolvable[] inPopulation()
          The current population
protected  Evolvable[] outPopulation()
          The new population.
 void reset()
          Resets the genetic algorithm.
 void setMaximumDataPoints(int mdp)
          Set the maximum number of data points that the GA should log.
 void setMutationRate(double mr)
          Set the mutation rate (should generally be between 0.0 and 0.2).
 void setPopulation(Evolvable[] thisPopulation)
          Set the population to an initial array of Evolvables.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

population

protected Evolvable[][] population
The population to evolve. The population is double-buffered, which increases speed but does require some caution when retrieving values from the class. See doStep and getBest for more information.

See Also:
doStep(), getBest()

currentIteration

protected int currentIteration
The current iteration of the genetic algorithm, incremented upon each call to doStep.


maximumIterations

protected int maximumIterations
The maximum number of iterations (currently unused).


elitePercentage

protected double elitePercentage
The percentage of elites to copy to the new population (defaults to 0.05, or 5%).


mutationRate

protected double mutationRate
The mutation rate (default to 0.05, or 5%).


averageFitness

protected double averageFitness
The average fitness for this generation.


logData

protected boolean logData
Determines whether average and best fitness data should be logged.


maximumDataPoints

protected int maximumDataPoints
The maximum number of data points to store in the linked lists.


averageFitnesses

protected java.util.LinkedList averageFitnesses
The average fitness linked list. Fitnesses are stored as PlotPoints for easy plotting.

See Also:
PlotPoint, Plot2D

bestFitnesses

protected java.util.LinkedList bestFitnesses
The best fitness linked list. Fitnesses are stored as PlotPoints for easy plotting.

See Also:
PlotPoint, Plot2D
Constructor Detail

GeneticAlgorithm

public GeneticAlgorithm()
Creates a new instance of GeneticAlgorithm


GeneticAlgorithm

public GeneticAlgorithm(Evolvable[] population)
Create a instance of the genetic algorithm with an initial population.

Parameters:
population - the initial population.
Method Detail

getAverageFitnesses

public java.util.LinkedList getAverageFitnesses()
Return the average fitness linked list.

Returns:
the average fitness linked list.

getBestFitnesses

public java.util.LinkedList getBestFitnesses()
Return the best fitness linked list.

Returns:
the best fitness linked list.

setPopulation

public void setPopulation(Evolvable[] thisPopulation)
Set the population to an initial array of Evolvables.

Parameters:
thisPopulation - the population to assign to the genetic algorithm.
See Also:
Evolvable

getCurrentPopulation

public Evolvable[] getCurrentPopulation()
Return the current population (double-buffering taken into account).

Returns:
the current population.

flipBuffer

protected void flipBuffer()
Flips the genetic algorithm buffer.


inPopulation

protected Evolvable[] inPopulation()
The current population

Returns:
the current population.

outPopulation

protected Evolvable[] outPopulation()
The new population.

Returns:
the new population.

doStep

public void doStep()
Steps through one iteration of the genetic algorithm. Fitnesses are calculated for each member of the population, sorted by fitness and mated together. doStep also implements elitism and mutation.

Note that at the end of the iteration, flipBuffer is called. This means that the inPopulation returns the new, unsorted population and outPopulation returns the "current", sorted population.

Specified by:
doStep in interface Steppable

setMutationRate

public void setMutationRate(double mr)
Set the mutation rate (should generally be between 0.0 and 0.2).

Parameters:
mr - the mutation rate.

getMutationRate

public double getMutationRate()
Retrieve the current mutation rate.

Returns:
the mutation rate.

init

public void init()
Initialize the genetic algorithm.

Specified by:
init in interface Steppable

getBest

public Evolvable getBest()
Retrieves the best Evolvable in the population. It is assumed that getBest is called after doStep, therefore the best population member will be equal to outPopulation()[0].

Returns:
the best Evolvable in the population.
See Also:
outPopulation()

getBestFitness

public double getBestFitness()
Retrieves the best fitness level. This works the same as getBest, so the assumptions remain the same.

Returns:
the best fitness in the population.

getAverageFitness

public double getAverageFitness()
Return the average fitness of this generation.

Returns:
the average fitness.

reset

public void reset()
Resets the genetic algorithm. This just calls init.

Specified by:
reset in interface Steppable
See Also:
init()

getCurrentIteration

public int getCurrentIteration()
Return the current iteration of the genetic algorithm.

Returns:
the current iteration of the genetic algorithm.

setMaximumDataPoints

public void setMaximumDataPoints(int mdp)
Set the maximum number of data points that the GA should log.

Parameters:
mdp - maximum number of data points to log.

getMaximumDataPoints

public int getMaximumDataPoints()
Returns the maximum number of data points the GA is logging.

Returns:
the maximum data points.

This documentation is part of the Generation5 JDK.