4
Licensed to the Apache Software Foundation (ASF) under one or more
5
contributor license agreements. See the NOTICE file distributed with
6
this work for additional information regarding copyright ownership.
7
The ASF licenses this file to You under the Apache License, Version 2.0
8
(the "License"); you may not use this file except in compliance with
9
the License. You may obtain a copy of the License at
11
http://www.apache.org/licenses/LICENSE-2.0
13
Unless required by applicable law or agreed to in writing, software
14
distributed under the License is distributed on an "AS IS" BASIS,
15
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
See the License for the specific language governing permissions and
17
limitations under the License.
20
<?xml-stylesheet type="text/xsl" href="./xdoc.xsl"?>
21
<!-- $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $ -->
22
<document url="genetics.html">
24
<title>The Commons Math User Guide - Genetic Algorithms</title>
27
<section name="14 Genetic Algorithms">
28
<subsection name="14.1 Overview" href="overview">
30
The genetics package provides a framework and implementations for
34
<subsection name="14.2 GA Framework">
36
<a href="../apidocs/org/apache/commons/math/genetics/GeneticAlgorithm.html">
37
org.apache.commons.math.genetic.GeneticAlgorithm</a> provides an
38
execution framework for Genetic Algorithms (GA).
39
<a href="../apidocs/org/apache/commons/math/genetics/Population.html">Populations,</a> consisting
40
of <a href="../apidocs/org/apache/commons/math/genetics/Chromosome.html">
41
Chromosomes</a> are evolved by the <code>GeneticAlgorithm</code> until a
42
<a href="../apidocs/org/apache/commons/math/genetics/StoppingCondition.html">StoppingCondition</a>
43
is reached. Evolution is determined by
44
<a href="../apidocs/org/apache/commons/math/genetics/SelectionPolicy.html">SelectionPolicies</a>,
45
<a href="../apidocs/org/apache/commons/math/genetics/MutationPolicy.html"> MutationPolicies</a>
46
and <a href="../apidocs/org/apache/commons/math/genetics/Fitness.html">Fitness</a>.
49
The GA itself is implemented by the <code>evolve</code> method of the <code>GeneticAlgorithm</code> class,
50
which looks like this:
52
public Population evolve(Population initial, StoppingCondition condition) {
53
Population current = initial;
54
while (!condition.isSatisfied(current)) {
55
current = nextGeneration(current);
60
The <code>nextGeneration</code> method implements the following algorithm:
62
<li>Get nextGeneration population to fill from <code>current</code>
63
generation, using its nextGeneration method</li>
64
<li>Loop until new generation is filled:</li>
65
<ul><li>Apply configured <code>SelectionPolicy</code> to select a pair of parents
66
from <code>current</code></li>
67
<li>With probability =
68
<a href="../apidocs/org/apache/commons/math/genetics/GeneticAlgorithm.html#getCrossoverRate()">
69
getCrossoverRate()</a>, apply configured <code>CrossoverPolicy</code> to parents</li>
70
<li>With probability =
71
<a href="../apidocs/org/apache/commons/math/genetics/GeneticAlgorithm.html#getMutationRate()">
72
getMutationRate()</a>,
73
apply configured <code>MutationPolicy</code> to each of the offspring</li>
74
<li>Add offspring individually to nextGeneration,
77
<li>Return nextGeneration</li>
81
<subsection name="14.3 Implementation">
83
Here is an example GA execution:
85
// initialize a new genetic algorithm
86
GeneticAlgorithm ga = new GeneticAlgorithm(
87
new OnePointCrossover<Integer>(),
89
new RandomKeyMutation(),
91
new TournamentSelection(TOURNAMENT_ARITY)
95
Population initial = getInitialPopulation();
98
StoppingCondition stopCond = new FixedGenerationCount(NUM_GENERATIONS);
101
Population finalPopulation = ga.evolve(initial, stopCond);
103
// best chromosome from the final population
104
Chromosome bestFinal = finalPopulation.getFittestChromosome();
106
The arguments to the <code>GeneticAlgorithm</code> constructor above are: <br/>
108
<tr><th>Parameter</th><th>value in example</th><th>meaning</th></tr>
109
<tr><td>crossoverPolicy</td>
110
<td><a href="../apidocs/org/apache/commons/math/genetics/OnePointCrossover.html">OnePointCrossover</a></td>
111
<td>A random crossover point is selected and the first part from each parent is copied to the corresponding
112
child, and the second parts are copied crosswise.</td></tr>
113
<tr><td>crossoverRate</td>
115
<td>Always apply crossover</td></tr>
116
<tr><td>mutationPolicy</td>
117
<td><a href="../apidocs/org/apache/commons/math/genetics/RandomKeyMutation.html">RandomKeyMutation</a></td>
118
<td>Changes a randomly chosen element of the array representation to a random value uniformly distributed in [0,1].</td></tr>
119
<tr><td>mutationRate</td>
121
<td>Apply mutation with probability 0.1 - that is, 10% of the time.</td></tr>
122
<tr><td>selectionPolicy</td>
123
<td><a href="../apidocs/org/apache/commons/math/genetics/TournamentSelection.html">TournamentSelection</a></td>
124
<td>Each of the two selected chromosomes is selected based on an n-ary tournament -- this is done by drawing
125
n random chromosomes without replacement from the population, and then selecting the fittest chromosome among them.</td></tr>
127
The algorithm starts with an <code>initial</code> population of <code>Chromosomes.</code> and executes until
128
the specified <a href="../apidocs/org/apache/commons/math/genetics/StoppingCondition.html">StoppingCondition</a>
129
is reached. In the example above, a
130
<a href="../apidocs/org/apache/commons/math/genetics/FixedGenerationCount.html">FixedGenerationCount</a>
131
stopping condition is used, which means the algorithm proceeds through a fixed number of generations.