Top Description Fields Constructors Methods
org.apache.commons.math3.genetics

public Class GeneticAlgorithm

extends Object
Class Inheritance
Imports
org.apache.commons.math3.exception.OutOfRangeException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.random.RandomGenerator, .JDKRandomGenerator

Implementation 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

Modifier and TypeField and Description
private final CrossoverPolicy
crossoverPolicy

the crossover policy used by the algorithm.

private final double
crossoverRate

the rate of crossover for the algorithm.

private int
generationsEvolved

the number of generations evolved to reach StoppingCondition in the last run.

private final MutationPolicy
mutationPolicy

the mutation policy used by the algorithm.

private final double
mutationRate

the rate of mutation for the algorithm.

private static RandomGenerator
randomGenerator

Static random number generator shared by GA implementation classes.

private final SelectionPolicy
selectionPolicy

the selection policy used by the algorithm.

Constructor Summary

AccessConstructor and Description
public
GeneticAlgorithm(final CrossoverPolicy crossoverPolicy, final double
The crossover rate as a percentage (0-1 inclusive)
crossoverRate
,
final MutationPolicy mutationPolicy, final double
The mutation rate as a percentage (0-1 inclusive)
mutationRate
,
final SelectionPolicy selectionPolicy)

Create a new genetic algorithm.

Method Summary

Modifier and TypeMethod and Description
public Population

Returns:

the population that satisfies the stopping condition.
evolve
(final Population
the initial, seed population.
initial
,
final StoppingCondition
the stopping condition used to stop evolution.
condition
)

Evolve the given population.

public CrossoverPolicy

Returns:

crossover policy
getCrossoverPolicy
()

Returns the crossover policy.

public double

Returns:

crossover rate
getCrossoverRate
()

Returns the crossover rate.

public int

Returns:

number of generations evolved
getGenerationsEvolved
()

Returns the number of generations evolved to reach StoppingCondition in the last run.

public MutationPolicy

Returns:

mutation policy
getMutationPolicy
()

Returns the mutation policy.

public double

Returns:

mutation rate
getMutationRate
()

Returns the mutation rate.

public static synchronized RandomGenerator

Returns:

the static random generator shared by GA implementation classes
getRandomGenerator
()

Returns the (static) random generator.

public SelectionPolicy

Returns:

selection policy
getSelectionPolicy
()

Returns the selection policy.

public Population

Returns:

the population for the next generation.
nextGeneration
(final Population
the current population.
current
)

Evolve the given population into the next generation.

public static synchronized void
setRandomGenerator(final RandomGenerator
random generator
random
)

Set the (static) random generator.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

crossoverPolicyback to summary
private final CrossoverPolicy crossoverPolicy

the crossover policy used by the algorithm.

crossoverRateback to summary
private final double crossoverRate

the rate of crossover for the algorithm.

generationsEvolvedback to summary
private int generationsEvolved

the number of generations evolved to reach StoppingCondition in the last run.

mutationPolicyback to summary
private final MutationPolicy mutationPolicy

the mutation policy used by the algorithm.

mutationRateback to summary
private final double mutationRate

the rate of mutation for the algorithm.

randomGeneratorback to summary
private static RandomGenerator randomGenerator

Static random number generator shared by GA implementation classes. Set the randomGenerator seed to get reproducible results. Use setRandomGenerator(RandomGenerator) to supply an alternative to the default JDK-provided PRNG.

selectionPolicyback to summary
private final SelectionPolicy selectionPolicy

the selection policy used by the algorithm.

Constructor Detail

GeneticAlgorithmback to summary
public GeneticAlgorithm(final CrossoverPolicy crossoverPolicy, final double crossoverRate, final MutationPolicy mutationPolicy, final double mutationRate, final SelectionPolicy selectionPolicy) throws OutOfRangeException

Create a new genetic algorithm.

Parameters
crossoverPolicy:CrossoverPolicy

The CrossoverPolicy

crossoverRate:double

The crossover rate as a percentage (0-1 inclusive)

mutationPolicy:MutationPolicy

The MutationPolicy

mutationRate:double

The mutation rate as a percentage (0-1 inclusive)

selectionPolicy:SelectionPolicy

The SelectionPolicy

Exceptions
OutOfRangeException:
if the crossover or mutation rate is outside the [0, 1] range

Method Detail

evolveback to summary
public Population evolve(final Population initial, final StoppingCondition condition)

Evolve the given population. Evolution stops when the stopping condition is satisfied. Updates the generationsEvolved property with the number of generations evolved before the StoppingCondition is satisfied.

Parameters
initial:Population

the initial, seed population.

condition:StoppingCondition

the stopping condition used to stop evolution.

Returns:Population

the population that satisfies the stopping condition.

getCrossoverPolicyback to summary
public CrossoverPolicy getCrossoverPolicy()

Returns the crossover policy.

Returns:CrossoverPolicy

crossover policy

getCrossoverRateback to summary
public double getCrossoverRate()

Returns the crossover rate.

Returns:double

crossover rate

getGenerationsEvolvedback to summary
public int getGenerationsEvolved()

Returns the number of generations evolved to reach StoppingCondition in the last run.

Returns:int

number of generations evolved

Since
2.1
getMutationPolicyback to summary
public MutationPolicy getMutationPolicy()

Returns the mutation policy.

Returns:MutationPolicy

mutation policy

getMutationRateback to summary
public double getMutationRate()

Returns the mutation rate.

Returns:double

mutation rate

getRandomGeneratorback to summary
public static synchronized RandomGenerator getRandomGenerator()

Returns the (static) random generator.

Returns:RandomGenerator

the static random generator shared by GA implementation classes

getSelectionPolicyback to summary
public SelectionPolicy getSelectionPolicy()

Returns the selection policy.

Returns:SelectionPolicy

selection policy

nextGenerationback to summary
public Population nextGeneration(final Population current)

Evolve the given population into the next generation.

  1. Get nextGeneration population to fill from current generation, using its nextGeneration method
  2. Loop until new generation is filled:
  3. Return nextGeneration
Parameters
current:Population

the current population.

Returns:Population

the population for the next generation.

setRandomGeneratorback to summary
public static synchronized void setRandomGenerator(final RandomGenerator random)

Set the (static) random generator.

Parameters
random:RandomGenerator

random generator