Package org.jppf.ga
Class GeneticAlgorithm
java.lang.Object
org.jppf.ga.GeneticAlgorithm
- All Implemented Interfaces:
AutoCloseable
- Author:
- Laurent Cohen
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected doubleProbability of corssover for each chromosome.protected intThe count of generations (epochs).protected intThe number of chromosomes to select.protected intprotected doubleintHow often to display the latest state.protected Chromosome[]The genetic algorithm's population.protected Random -
Constructor Summary
ConstructorsConstructorDescriptionGeneticAlgorithm(Chromosome[] population, int nbToKeep, int nbSelect, int tournamentSize, double crossoverProbability) -
Method Summary
Modifier and TypeMethodDescriptionaddToPoulation(Chromosome[] toAdd) Add new chromosomes to the population.voidclose()protected voidcomputeFitness(Chromosome[] pop) Compute the fitness scores for the specified population.voiddisplayStats(int gen, Chromosome[] pop, long elapsed) Displays some statistics on the current state of the algorithm.intintGet the current number of generations.performCrossover(Chromosome[] pop) Perform the crossover operation for the specified population.performMutation(Chromosome[] pop, int nbSelect) Mutate the specified population.protected voidpostEpoch(Chromosome best) Callback invoked at the end of each epoch.run(int maxGenerations) protected booleanshouldStop(Chromosome best) boolean
-
Field Details
-
population
The genetic algorithm's population. -
generationCount
protected int generationCountThe count of generations (epochs). -
nbSelect
protected int nbSelectThe number of chromosomes to select. -
crossoverProbability
protected double crossoverProbabilityProbability of corssover for each chromosome. -
random
-
outputFrequency
public int outputFrequencyHow often to display the latest state. -
nbToKeep
protected int nbToKeep -
nbToKeepPct
protected double nbToKeepPct
-
-
Constructor Details
-
GeneticAlgorithm
public GeneticAlgorithm(Chromosome[] population, int nbToKeep, int nbSelect, int tournamentSize, double crossoverProbability) - Parameters:
population- the inital population.nbToKeep- the number of chromosomes to keep at each generation.nbSelect- the number of chromosomes to select at each generation.tournamentSize- the tournament selector size.crossoverProbability- probability of crossover for each chromosome.
-
-
Method Details
-
run
- Parameters:
maxGenerations- max number of generations (epochs) to run for.- Returns:
- the chromosome with the best fitness score.
-
getCurrentPopulationSize
public int getCurrentPopulationSize()- Returns:
- the current population size.
-
postEpoch
Callback invoked at the end of each epoch. Intended to be overriden in subclasses.- Parameters:
best- the chromosome with the best fitness score.
-
shouldStop
- Parameters:
best- the chromosome with the best fitness score.- Returns:
- whether to stop the algorithm.
-
computeFitness
Compute the fitness scores for the specified population.- Parameters:
pop- the chromosomes for which to compute the fitness.
-
performCrossover
Perform the crossover operation for the specified population.- Parameters:
pop- the chromosomes to crossover.- Returns:
- the new population obtained by corssover.
-
performMutation
Mutate the specified population.- Parameters:
pop- the population to mutate.nbSelect- the number of chromosmes to mutate.- Returns:
- the mutated population.
-
displayStats
Displays some statistics on the current state of the algorithm.- Parameters:
gen- current generation number.pop- current population.elapsed- elpased time since previous generation.
-
testValid
public boolean testValid()- Returns:
- whether the population is valid.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
addToPoulation
Add new chromosomes to the population.- Parameters:
toAdd- the chromosomes to add.- Returns:
- the new population.
-
getGenerationCount
public int getGenerationCount()Get the current number of generations.- Returns:
- the generation count as an int.
-