From commits-return-3981-apmail-commons-commits-archive=commons.apache.org@commons.apache.org Sat Sep 27 18:06:13 2008 Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 97143 invoked from network); 27 Sep 2008 18:06:12 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 27 Sep 2008 18:06:12 -0000 Received: (qmail 93727 invoked by uid 500); 27 Sep 2008 18:06:09 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 93641 invoked by uid 500); 27 Sep 2008 18:06:09 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 93632 invoked by uid 99); 27 Sep 2008 18:06:09 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 27 Sep 2008 11:06:09 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 27 Sep 2008 18:05:16 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id DA72B23888A3; Sat, 27 Sep 2008 11:05:19 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@commons.apache.org From: psteitz@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080927180519.DA72B23888A3@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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()); } }