[MATH-776] Use same range check in ctor as in setter for ElitisticListPopulation.

Thanks to Reid Hochstedler

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1308454 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2012-04-02 18:46:42 +00:00
parent c016eb832b
commit b9ca51f09f
3 changed files with 63 additions and 3 deletions

View File

@ -52,6 +52,12 @@ If the output is not quite correct, check for invisible trailing spaces!
<body> <body>
<release version="3.1" date="TBD" description=" <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"> <action dev="tn" type="add" issue="MATH-773" due-to="Reid Hochstedler">
Added class FixedElapsedTime (new StoppingCondition for evolution of generations) to genetics package. Added class FixedElapsedTime (new StoppingCondition for evolution of generations) to genetics package.
</action> </action>

View File

@ -24,7 +24,7 @@ import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.util.FastMath; 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). * chromosomes is directly copied to the next generation).
* *
* @version $Id$ * @version $Id$
@ -42,12 +42,13 @@ public class ElitisticListPopulation extends ListPopulation {
* @param populationLimit maximal size of the population * @param populationLimit maximal size of the population
* @param elitismRate how many best chromosomes will be directly transferred to the * @param elitismRate how many best chromosomes will be directly transferred to the
* next generation [in %] * next generation [in %]
* @throws OutOfRangeException if the elitism rate is outside the [0, 1] range
*/ */
public ElitisticListPopulation(final List<Chromosome> chromosomes, public ElitisticListPopulation(final List<Chromosome> chromosomes,
final int populationLimit, final int populationLimit,
final double elitismRate) { final double elitismRate) {
super(chromosomes, populationLimit); super(chromosomes, populationLimit);
this.elitismRate = elitismRate; setElitismRate(elitismRate);
} }
/** /**
@ -57,10 +58,11 @@ public class ElitisticListPopulation extends ListPopulation {
* @param populationLimit maximal size of the population * @param populationLimit maximal size of the population
* @param elitismRate how many best chromosomes will be directly transferred to the * @param elitismRate how many best chromosomes will be directly transferred to the
* next generation [in %] * next generation [in %]
* @throws OutOfRangeException if the elitism rate is outside the [0, 1] range
*/ */
public ElitisticListPopulation(final int populationLimit, final double elitismRate) { public ElitisticListPopulation(final int populationLimit, final double elitismRate) {
super(populationLimit); super(populationLimit);
this.elitismRate = elitismRate; setElitismRate(elitismRate);
} }
/** /**

View File

@ -17,6 +17,10 @@
package org.apache.commons.math3.genetics; 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.Assert;
import org.junit.Test; import org.junit.Test;
@ -37,6 +41,54 @@ public class ElitisticListPopulationTest {
Assert.assertEquals(20, nextGeneration.getPopulationSize()); 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 static class DummyChromosome extends Chromosome {
private final int fitness; private final int fitness;