14 Genetic Algorithms14.2 GA FrameworkGeneticAlgorithm provides an execution framework for Genetic Algorithms (GA). Populations, consisting of Chromosomes are evolved by the GeneticAlgorithm until a StoppingCondition is reached. Evolution is determined by SelectionPolicy, MutationPolicy and Fitness. The GA itself is implemented by the evolve method of the GeneticAlgorithm class, which looks like this: public Population evolve(Population initial, StoppingCondition condition) {
Population current = initial;
while (!condition.isSatisfied(current)) {
current = nextGeneration(current);
}
return current;
}
14.3 ImplementationHere is an example GA execution:
// initialize a new genetic algorithm
GeneticAlgorithm ga = new GeneticAlgorithm(
new OnePointCrossover<Integer>(),
1,
new RandomKeyMutation(),
0.10,
new TournamentSelection(TOURNAMENT_ARITY)
);
// initial population
Population initial = getInitialPopulation();
// stopping condition
StoppingCondition stopCond = new FixedGenerationCount(NUM_GENERATIONS);
// run the algorithm
Population finalPopulation = ga.evolve(initial, stopCond);
// best chromosome from the final population
Chromosome bestFinal = finalPopulation.getFittestChromosome();
The algorithm starts with an initial population of Chromosomes. and executes until the specified StoppingCondition is reached. In the example above, a FixedGenerationCount stopping condition is used, which means the algorithm proceeds through a fixed number of generations. |