Author: psteitz Date: Sat Sep 27 11:05:19 2008 New Revision: 699704 URL: http://svn.apache.org/viewvc?rev=699704&view=rev Log: Eliminated the "mutation requirement" in nextGeneration method. Added algorithm description in javadoc. JIRA: MATH-207 Reported and patched by David Stefka Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java?rev=699704&r1=699703&r2=699704&view=diff ============================================================================== --- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java (original) +++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java Sat Sep 27 11:05:19 2008 @@ -19,6 +19,7 @@ /** * Implementation of a genetic algorithm. All factors that govern the operation * of the algorithm can be configured for a specific problem. + * * @version \$Revision\$ \$Date\$ */ public class GeneticAlgorithm { @@ -99,7 +100,24 @@ } /** - * Evolve the given population into the next generation. + *

Evolve the given population into the next generation.

+ *

+ *
1. Get nextGeneration polulation to fill from `current` + * generation, using its nextGeneration method
2. + *
3. Loop until new generation is filled:
4. + *
• Apply configured SelectionPolicy to select a pair of parents + * from `current`
• + *
• With probability = {@link #getCrossoverRate()}, apply + * configured {@link CrossoverPolicy} to parents
• + *
• With probability = {@link #getMutationRate()}, apply + * configured {@link MutationPolicy} to each parent
• + *
• Add resulting chromosomes individually to nextGeneration, + * space permitting
• + *
+ *
5. Return nextGeneration
6. + *
+ *

+ * * * @param current the current population. * @return the population for the next generation. @@ -112,23 +130,29 @@ // select parent chromosomes ChromosomePair pair = getSelectionPolicy().select(current); - // apply crossover policy to create two offspring + // crossover? if (Math.random() < getCrossoverRate()) { + // apply crossover policy to create two offspring pair = getCrossoverPolicy().crossover(pair.getFirst(), pair.getSecond()); } - // apply mutation policy to first offspring + // mutation? if (Math.random() < getMutationRate()) { - nextGeneration.addChromosome(getMutationPolicy().mutate( - pair.getFirst())); + // apply mutation policy to the chromosomes + pair = new ChromosomePair( + getMutationPolicy().mutate(pair.getFirst()), + getMutationPolicy().mutate(pair.getSecond()) + ); + } - if (nextGeneration.getPopulationSize() < nextGeneration - .getPopulationLimit()) { - // apply mutation policy to second offspring - nextGeneration.addChromosome(getMutationPolicy().mutate( - pair.getSecond())); - } + // add the first chromosome to the population + nextGeneration.addChromosome(pair.getFirst()); + // is there still a place for the second chromosome? + if (nextGeneration.getPopulationSize() < nextGeneration + .getPopulationLimit()) { + // add the second chromosome to the population + nextGeneration.addChromosome(pair.getSecond()); } }