Class GeneticAlgorithm
- java.lang.Object
-
- org.apache.commons.math3.genetics.GeneticAlgorithm
-
public class GeneticAlgorithm extends java.lang.ObjectImplementation of a genetic algorithm. All factors that govern the operation of the algorithm can be configured for a specific problem.- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description private CrossoverPolicycrossoverPolicythe crossover policy used by the algorithm.private doublecrossoverRatethe rate of crossover for the algorithm.private intgenerationsEvolvedthe number of generations evolved to reachStoppingConditionin the last run.private MutationPolicymutationPolicythe mutation policy used by the algorithm.private doublemutationRatethe rate of mutation for the algorithm.private static RandomGeneratorrandomGeneratorStatic random number generator shared by GA implementation classes.private SelectionPolicyselectionPolicythe selection policy used by the algorithm.
-
Constructor Summary
Constructors Constructor Description GeneticAlgorithm(CrossoverPolicy crossoverPolicy, double crossoverRate, MutationPolicy mutationPolicy, double mutationRate, SelectionPolicy selectionPolicy)Create a new genetic algorithm.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Populationevolve(Population initial, StoppingCondition condition)Evolve the given population.CrossoverPolicygetCrossoverPolicy()Returns the crossover policy.doublegetCrossoverRate()Returns the crossover rate.intgetGenerationsEvolved()Returns the number of generations evolved to reachStoppingConditionin the last run.MutationPolicygetMutationPolicy()Returns the mutation policy.doublegetMutationRate()Returns the mutation rate.static RandomGeneratorgetRandomGenerator()Returns the (static) random generator.SelectionPolicygetSelectionPolicy()Returns the selection policy.PopulationnextGeneration(Population current)Evolve the given population into the next generation.static voidsetRandomGenerator(RandomGenerator random)Set the (static) random generator.
-
-
-
Field Detail
-
randomGenerator
private static RandomGenerator randomGenerator
Static random number generator shared by GA implementation classes. Set the randomGenerator seed to get reproducible results. UsesetRandomGenerator(RandomGenerator)to supply an alternative to the default JDK-provided PRNG.
-
crossoverPolicy
private final CrossoverPolicy crossoverPolicy
the crossover policy used by the algorithm.
-
crossoverRate
private final double crossoverRate
the rate of crossover for the algorithm.
-
mutationPolicy
private final MutationPolicy mutationPolicy
the mutation policy used by the algorithm.
-
mutationRate
private final double mutationRate
the rate of mutation for the algorithm.
-
selectionPolicy
private final SelectionPolicy selectionPolicy
the selection policy used by the algorithm.
-
generationsEvolved
private int generationsEvolved
the number of generations evolved to reachStoppingConditionin the last run.
-
-
Constructor Detail
-
GeneticAlgorithm
public GeneticAlgorithm(CrossoverPolicy crossoverPolicy, double crossoverRate, MutationPolicy mutationPolicy, double mutationRate, SelectionPolicy selectionPolicy) throws OutOfRangeException
Create a new genetic algorithm.- Parameters:
crossoverPolicy- TheCrossoverPolicycrossoverRate- The crossover rate as a percentage (0-1 inclusive)mutationPolicy- TheMutationPolicymutationRate- The mutation rate as a percentage (0-1 inclusive)selectionPolicy- TheSelectionPolicy- Throws:
OutOfRangeException- if the crossover or mutation rate is outside the [0, 1] range
-
-
Method Detail
-
setRandomGenerator
public static void setRandomGenerator(RandomGenerator random)
Set the (static) random generator.- Parameters:
random- random generator
-
getRandomGenerator
public static RandomGenerator getRandomGenerator()
Returns the (static) random generator.- Returns:
- the static random generator shared by GA implementation classes
-
evolve
public Population evolve(Population initial, StoppingCondition condition)
Evolve the given population. Evolution stops when the stopping condition is satisfied. Updates thegenerationsEvolvedproperty with the number of generations evolved before the StoppingCondition is satisfied.- Parameters:
initial- the initial, seed population.condition- the stopping condition used to stop evolution.- Returns:
- the population that satisfies the stopping condition.
-
nextGeneration
public Population nextGeneration(Population current)
Evolve the given population into the next generation.- Get nextGeneration population to fill from
currentgeneration, using its nextGeneration method - Loop until new generation is filled:
- Apply configured SelectionPolicy to select a pair of parents
from
current - With probability =
getCrossoverRate(), apply configuredCrossoverPolicyto parents - With probability =
getMutationRate(), apply configuredMutationPolicyto each of the offspring - Add offspring individually to nextGeneration, space permitting
- Return nextGeneration
- Parameters:
current- the current population.- Returns:
- the population for the next generation.
- Get nextGeneration population to fill from
-
getCrossoverPolicy
public CrossoverPolicy getCrossoverPolicy()
Returns the crossover policy.- Returns:
- crossover policy
-
getCrossoverRate
public double getCrossoverRate()
Returns the crossover rate.- Returns:
- crossover rate
-
getMutationPolicy
public MutationPolicy getMutationPolicy()
Returns the mutation policy.- Returns:
- mutation policy
-
getMutationRate
public double getMutationRate()
Returns the mutation rate.- Returns:
- mutation rate
-
getSelectionPolicy
public SelectionPolicy getSelectionPolicy()
Returns the selection policy.- Returns:
- selection policy
-
getGenerationsEvolved
public int getGenerationsEvolved()
Returns the number of generations evolved to reachStoppingConditionin the last run.- Returns:
- number of generations evolved
- Since:
- 2.1
-
-