commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1308454 - in /commons/proper/math/trunk/src: changes/changes.xml main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java test/java/org/apache/commons/math3/genetics/ElitisticListPopulationTest.java
Date Mon, 02 Apr 2012 18:46:43 GMT
Author: tn
Date: Mon Apr  2 18:46:42 2012
New Revision: 1308454

URL: http://svn.apache.org/viewvc?rev=1308454&view=rev
Log:
[MATH-776] Use same range check in ctor as in setter for ElitisticListPopulation.
Thanks to Reid Hochstedler

Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/genetics/ElitisticListPopulationTest.java

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1308454&r1=1308453&r2=1308454&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Mon Apr  2 18:46:42 2012
@@ -52,6 +52,12 @@ If the output is not quite correct, chec
   <body>
     <release version="3.1" date="TBD" description="
 ">
+      <action dev="tn" type="fix" issue="MATH-776" due-to="Reid Hochstedler">
+        Use same range check in constructor for ElitisticListPopulation as in corresponding
setter.
+      </action>
+      <action dev="tn" type="fix" issue="MATH-767" due-to="Dennis Hendriks">
+        Fixed unbalanced use of code tags in javadoc of several classes.
+      </action>
       <action dev="tn" type="add" issue="MATH-773" due-to="Reid Hochstedler">
         Added class FixedElapsedTime (new StoppingCondition for evolution of generations)
to genetics package.
       </action>

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java?rev=1308454&r1=1308453&r2=1308454&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java
Mon Apr  2 18:46:42 2012
@@ -24,7 +24,7 @@ import org.apache.commons.math3.exceptio
 import org.apache.commons.math3.util.FastMath;
 
 /**
- * Population of chromosomes which uses elitism (certain percentace of the best
+ * Population of chromosomes which uses elitism (certain percentage of the best
  * chromosomes is directly copied to the next generation).
  *
  * @version $Id$
@@ -42,12 +42,13 @@ public class ElitisticListPopulation ext
      * @param populationLimit maximal size of the population
      * @param elitismRate how many best chromosomes will be directly transferred to the
      *                    next generation [in %]
+     * @throws OutOfRangeException if the elitism rate is outside the [0, 1] range
      */
     public ElitisticListPopulation(final List<Chromosome> chromosomes,
                                    final int populationLimit,
                                    final double elitismRate) {
         super(chromosomes, populationLimit);
-        this.elitismRate = elitismRate;
+        setElitismRate(elitismRate);
     }
 
     /**
@@ -57,10 +58,11 @@ public class ElitisticListPopulation ext
      * @param populationLimit maximal size of the population
      * @param elitismRate how many best chromosomes will be directly transferred to the
      *                    next generation [in %]
+     * @throws OutOfRangeException if the elitism rate is outside the [0, 1] range
      */
     public ElitisticListPopulation(final int populationLimit, final double elitismRate) {
         super(populationLimit);
-        this.elitismRate = elitismRate;
+        setElitismRate(elitismRate);
     }
 
     /**

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/genetics/ElitisticListPopulationTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/genetics/ElitisticListPopulationTest.java?rev=1308454&r1=1308453&r2=1308454&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/genetics/ElitisticListPopulationTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/genetics/ElitisticListPopulationTest.java
Mon Apr  2 18:46:42 2012
@@ -17,6 +17,10 @@
 package org.apache.commons.math3.genetics;
 
 
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.commons.math3.exception.OutOfRangeException;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -36,6 +40,54 @@ public class ElitisticListPopulationTest
 
         Assert.assertEquals(20, nextGeneration.getPopulationSize());
     }
+    
+    @Test
+    public void testSetElitismRate() {
+        final double rate = 0.25;
+        final ElitisticListPopulation pop = new ElitisticListPopulation(100, 0.203);
+        pop.setElitismRate(rate);
+        Assert.assertEquals(rate, pop.getElitismRate(), 1e-6);
+    }
+    
+    @Test(expected = OutOfRangeException.class)
+    public void testSetElitismRateTooLow() {
+        final double rate = -0.25;
+        final ElitisticListPopulation pop = new ElitisticListPopulation(100, 0.203);
+        pop.setElitismRate(rate);
+    }
+    
+    @Test(expected = OutOfRangeException.class)
+    public void testSetElitismRateTooHigh() {
+        final double rate = 1.25;
+        final ElitisticListPopulation pop = new ElitisticListPopulation(100, 0.203);
+        pop.setElitismRate(rate);
+    }
+    
+    @Test(expected = OutOfRangeException.class)
+    public void testConstructorTooLow() {
+        final double rate = -0.25;
+        new ElitisticListPopulation(100, rate);
+    }
+    
+    @Test(expected = OutOfRangeException.class)
+    public void testConstructorTooHigh() {
+        final double rate = 1.25;
+        new ElitisticListPopulation(100, rate);
+    }
+
+    @Test(expected = OutOfRangeException.class)
+    public void testChromosomeListConstructorTooLow() {
+        final List<Chromosome> chromosomes = Collections.emptyList();
+        final double rate = -0.25;
+        new ElitisticListPopulation(chromosomes, 100, rate);
+    }
+
+    @Test(expected = OutOfRangeException.class)
+    public void testChromosomeListConstructorTooHigh() {
+        final List<Chromosome> chromosomes = Collections.emptyList();
+        final double rate = 1.25;
+        new ElitisticListPopulation(chromosomes, 100, rate);
+    }
 
     private static class DummyChromosome extends Chromosome {
         private final int fitness;



Mime
View raw message