org.vizzini.ai.geneticalgorithm
Class AbstractGeneticAlgorithm

java.lang.Object
  extended by org.vizzini.ai.geneticalgorithm.AbstractGeneticAlgorithm
All Implemented Interfaces:
IGeneticAlgorithm
Direct Known Subclasses:
AbstractGameGeneticAlgorithm, AbstractGeneticAlgorithmGP, CharacterRecognitionGA, PrimeGA

public abstract class AbstractGeneticAlgorithm
extends Object
implements IGeneticAlgorithm

Provides base functionality for genetic algorithms.

Since:
v0.1
Version:
v0.3
Author:
Jeffrey M. Thompson

Field Summary
protected  double _crossoverFraction
          Number of chromosomes to form from crossover of the retained ones as a fraction of the population size.
protected  CrossoverType _crossoverType
          Crossover type.
protected  long _elapsedTime
          Elapsed time (milliseconds).
protected  int _generation
          Current generation.
 
Constructor Summary
AbstractGeneticAlgorithm()
           
 
Method Summary
protected  void averageCrossover(IChromosome c0, IChromosome c1, IChromosome c2, IChromosome c3)
          Perform uniform crossover using the first two given chromosomes into the third given chromosome.
protected abstract  IChromosome createChromosome()
          Create a new chromosome.
protected abstract  void evaluateFitness()
          Evaluate the fitness of all chromosomes in the population.
protected  void fillPopulation()
          Fill the population with chromosomes.
protected  void generateNewPopulation()
          Create the new population of chromosomes from the current population.
 double getAverageCrossoverFraction()
           
 double getCrossoverFraction()
          Return the crossover fraction.
 CrossoverType getCrossoverType()
          Return the crossover type.
 long getElapsedTime()
          Return the elapsed time.
 int getGeneration()
          Return the number of generations run.
 IChromosome getMostFit()
          Return the most fit chromosome.
 double getMutationFraction()
           
 double getMutationMagnitude()
          Return the mutation magnitude.
 double getMutationRate()
          Return the mutation rate.
protected  int getPerfectFitness()
          Return the perfect fitness.
protected  IPopulation getPopulation()
          Return the population.
 int getPopulationSize()
          Return the population size.
 double getProcessingRate()
          Return the processing rate.
 boolean isDuplicateAllowed()
          Return true if duplicate chromosomes are allowed.
protected  void mutation(IPopulation oldPopulation, IPopulation newPopulation)
          Perform the mutation operation to fill the given new population from the given old population.
protected  void onePointCrossover(IChromosome c0, IChromosome c1, IChromosome c2, IChromosome c3)
          Perform one point crossover using the first two given chromosomes into the third given chromosome.
protected  void reproductionAndCrossover(IPopulation oldPopulation, IPopulation newPopulation)
          Perform the reproduction and crossover operations to fill the given new population from the given old population.
 void reset()
          Reset.
 int runGenerations(int numGenerations, int printFrequency)
          Run for the given number of generations.
 void setAverageCrossoverFraction(double averageCrossoverFraction)
           
 void setCrossoverFraction(double crossoverFraction)
          Set the crossover fraction.
 void setCrossoverType(CrossoverType crossoverType)
          Set the crossover type.
 void setDuplicateAllowed(boolean isDuplicateAllowed)
          Set the flag indicating whether duplicate chromosomes are allowed.
 void setMutationFraction(double mutationFraction)
           
 void setMutationMagnitude(double mutationMagnitude)
          Set the mutation magnitude.
 void setMutationRate(double mutationRate)
          Set the mutation rate.
protected  void setPopulation(IPopulation population)
          Set the population.
 void setPopulationSize(int size)
          Set the population size.
protected  void sortPopulation()
          Sort the chromosomes by fitness.
 String toString()
          Return a string representation of this object.
protected  void uniformCrossover(IChromosome c0, IChromosome c1, IChromosome c2, IChromosome c3)
          Perform uniform crossover using the first two given chromosomes into the third given chromosome.
 void writeReport()
          Write a report.
protected  void writeStats(Writer writer, int localGens)
          Write statistics using the given parameters.
protected  void writeStatsHeader(Writer writer)
          Write a header for the statistics.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_crossoverFraction

protected double _crossoverFraction
Number of chromosomes to form from crossover of the retained ones as a fraction of the population size.


_crossoverType

protected CrossoverType _crossoverType
Crossover type.


_elapsedTime

protected long _elapsedTime
Elapsed time (milliseconds).


_generation

protected int _generation
Current generation.

Constructor Detail

AbstractGeneticAlgorithm

public AbstractGeneticAlgorithm()
Method Detail

getAverageCrossoverFraction

public double getAverageCrossoverFraction()
Returns:
Return averageCrossoverFraction.

getCrossoverFraction

public double getCrossoverFraction()
Return the crossover fraction.

Specified by:
getCrossoverFraction in interface IGeneticAlgorithm
Since:
v0.1

getCrossoverType

public CrossoverType getCrossoverType()
Return the crossover type.

Specified by:
getCrossoverType in interface IGeneticAlgorithm
Since:
v0.1

getElapsedTime

public long getElapsedTime()
Return the elapsed time.

Specified by:
getElapsedTime in interface IGeneticAlgorithm
Since:
v0.1

getGeneration

public int getGeneration()
Return the number of generations run.

Specified by:
getGeneration in interface IGeneticAlgorithm
Since:
v0.1

getMostFit

public IChromosome getMostFit()
Return the most fit chromosome.

Specified by:
getMostFit in interface IGeneticAlgorithm
Since:
v0.1

getMutationFraction

public double getMutationFraction()
Specified by:
getMutationFraction in interface IGeneticAlgorithm
Returns:
Return mutationFraction.
Since:
v0.3

getMutationMagnitude

public double getMutationMagnitude()
Return the mutation magnitude.

Specified by:
getMutationMagnitude in interface IGeneticAlgorithm
Since:
v0.1

getMutationRate

public double getMutationRate()
Return the mutation rate.

Specified by:
getMutationRate in interface IGeneticAlgorithm
Since:
v0.1

getPopulationSize

public int getPopulationSize()
Return the population size.

Specified by:
getPopulationSize in interface IGeneticAlgorithm
Since:
v0.1

getProcessingRate

public double getProcessingRate()
Return the processing rate.

Specified by:
getProcessingRate in interface IGeneticAlgorithm
Since:
v0.1

isDuplicateAllowed

public boolean isDuplicateAllowed()
Return true if duplicate chromosomes are allowed.

Since:
v0.2

reset

public void reset()
Reset.

Specified by:
reset in interface IGeneticAlgorithm
Since:
v0.1

runGenerations

public int runGenerations(int numGenerations,
                          int printFrequency)
Run for the given number of generations.

Specified by:
runGenerations in interface IGeneticAlgorithm
Parameters:
numGenerations - Number of generations to run.
printFrequency - Print frequency.
Since:
v0.1

setAverageCrossoverFraction

public void setAverageCrossoverFraction(double averageCrossoverFraction)
Specified by:
setAverageCrossoverFraction in interface IGeneticAlgorithm
Parameters:
averageCrossoverFraction - The averageCrossoverFraction to set.

setCrossoverFraction

public void setCrossoverFraction(double crossoverFraction)
Set the crossover fraction.

Specified by:
setCrossoverFraction in interface IGeneticAlgorithm
Parameters:
crossoverFraction - Number of chromosomes to form from crossover of the retained ones as a fraction of the population size.
Since:
v0.1

setCrossoverType

public void setCrossoverType(CrossoverType crossoverType)
Set the crossover type.

Specified by:
setCrossoverType in interface IGeneticAlgorithm
Since:
v0.1

setDuplicateAllowed

public void setDuplicateAllowed(boolean isDuplicateAllowed)
Set the flag indicating whether duplicate chromosomes are allowed.

Specified by:
setDuplicateAllowed in interface IGeneticAlgorithm
Since:
v0.2

setMutationFraction

public void setMutationFraction(double mutationFraction)
Specified by:
setMutationFraction in interface IGeneticAlgorithm
Parameters:
mutationFraction - the mutationFraction to set
Since:
v0.3

setMutationMagnitude

public void setMutationMagnitude(double mutationMagnitude)
Set the mutation magnitude.

Specified by:
setMutationMagnitude in interface IGeneticAlgorithm
Parameters:
mutationMagnitude - The maximum allowed magnitude of the mutation.
Since:
v0.1

setMutationRate

public void setMutationRate(double mutationRate)
Set the mutation rate.

Specified by:
setMutationRate in interface IGeneticAlgorithm
Parameters:
mutationRate - Probability of a gene being mutated.
Since:
v0.1

setPopulationSize

public void setPopulationSize(int size)
Set the population size.

Specified by:
setPopulationSize in interface IGeneticAlgorithm
Since:
v0.2

toString

public String toString()
Return a string representation of this object.

Overrides:
toString in class Object
Since:
v0.1

writeReport

public void writeReport()
Write a report.

Specified by:
writeReport in interface IGeneticAlgorithm
Since:
v0.1

createChromosome

protected abstract IChromosome createChromosome()
Create a new chromosome.

Since:
v0.2

evaluateFitness

protected abstract void evaluateFitness()
Evaluate the fitness of all chromosomes in the population.

Since:
v0.1

averageCrossover

protected void averageCrossover(IChromosome c0,
                                IChromosome c1,
                                IChromosome c2,
                                IChromosome c3)
Perform uniform crossover using the first two given chromosomes into the third given chromosome.

Since:
v0.3

fillPopulation

protected void fillPopulation()
Fill the population with chromosomes.

Since:
v0.2

generateNewPopulation

protected void generateNewPopulation()
Create the new population of chromosomes from the current population.

Since:
v0.1

getPerfectFitness

protected int getPerfectFitness()
Return the perfect fitness.

Since:
v0.1

getPopulation

protected IPopulation getPopulation()
Return the population.

Since:
v0.1

mutation

protected void mutation(IPopulation oldPopulation,
                        IPopulation newPopulation)
Perform the mutation operation to fill the given new population from the given old population.

Parameters:
oldPopulation -
newPopulation -
Since:
v0.3

onePointCrossover

protected void onePointCrossover(IChromosome c0,
                                 IChromosome c1,
                                 IChromosome c2,
                                 IChromosome c3)
Perform one point crossover using the first two given chromosomes into the third given chromosome.

Since:
v0.3

reproductionAndCrossover

protected void reproductionAndCrossover(IPopulation oldPopulation,
                                        IPopulation newPopulation)
Perform the reproduction and crossover operations to fill the given new population from the given old population.

Parameters:
oldPopulation -
newPopulation -
Since:
v0.3

setPopulation

protected void setPopulation(IPopulation population)
Set the population.

Since:
v0.1

sortPopulation

protected void sortPopulation()
Sort the chromosomes by fitness.

Since:
v0.1

uniformCrossover

protected void uniformCrossover(IChromosome c0,
                                IChromosome c1,
                                IChromosome c2,
                                IChromosome c3)
Perform uniform crossover using the first two given chromosomes into the third given chromosome.

Since:
v0.3

writeStats

protected void writeStats(Writer writer,
                          int localGens)
Write statistics using the given parameters.

Parameters:
writer - Writer.
localGens - Current generation.
Since:
v0.3

writeStatsHeader

protected void writeStatsHeader(Writer writer)
Write a header for the statistics.

Parameters:
writer - Writer.
Since:
v0.3


Copyright © 2007 Vizzini.org. All Rights Reserved. 2007.12.25.03.00.02