commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r699704 - /commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java
Date Sat, 27 Sep 2008 18:05:19 GMT
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.
+     * <p>Evolve the given population into the next generation.</p>
+     * <p><ol>
+     *    <li>Get nextGeneration polulation to fill from <code>current</code>
+     *        generation, using its nextGeneration method</li>
+     *    <li>Loop until new generation is filled:</li>
+     *    <ul><li>Apply configured SelectionPolicy to select a pair of parents
+     *            from <code>current</code></li>
+     *        <li>With probability = {@link #getCrossoverRate()}, apply
+     *            configured {@link CrossoverPolicy} to parents</li>
+     *        <li>With probability = {@link #getMutationRate()}, apply
+     *            configured {@link MutationPolicy} to each parent</li>
+     *        <li>Add resulting chromosomes individually to nextGeneration,
+     *            space permitting</li>
+     *    </ul>
+     *    <li>Return nextGeneration</li>
+     *    </ol>
+     * </p>
+     * 
      * 
      * @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());
             }
         }
 



Mime
View raw message