From 6460cd272a4c0605c1e9167f641930bba16715ba Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Sat, 27 Sep 2008 18:05:19 +0000 Subject: [PATCH] Eliminated the "mutation requirement" in nextGeneration method. Added algorithm description in javadoc. JIRA: MATH-207 Reported and patched by David Stefka git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/branches/MATH_2_0@699704 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/genetics/GeneticAlgorithm.java | 46 ++++++++++++++----- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java b/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java index 888310a49..482d4bc6a 100644 --- a/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java +++ b/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java @@ -19,6 +19,7 @@ package org.apache.commons.math.genetics; /** * 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 @@ public class GeneticAlgorithm { } /** - * 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. + * + *
  5. Return nextGeneration
  6. + *
+ *

+ * * * @param current the current population. * @return the population for the next generation. @@ -112,23 +130,29 @@ public class GeneticAlgorithm { // 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()); } }