From b9ca51f09f38e5a4d73bcdafd09080c91670379c Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Mon, 2 Apr 2012 18:46:42 +0000 Subject: [PATCH] [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 --- src/changes/changes.xml | 6 +++ .../genetics/ElitisticListPopulation.java | 8 +-- .../genetics/ElitisticListPopulationTest.java | 52 +++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 65a32d0c4..5aa9b2d42 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -52,6 +52,12 @@ If the output is not quite correct, check for invisible trailing spaces! + + Use same range check in constructor for ElitisticListPopulation as in corresponding setter. + + + Fixed unbalanced use of code tags in javadoc of several classes. + Added class FixedElapsedTime (new StoppingCondition for evolution of generations) to genetics package. diff --git a/src/main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java b/src/main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java index a3090806c..829c97d3b 100644 --- a/src/main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java +++ b/src/main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java @@ -24,7 +24,7 @@ import org.apache.commons.math3.exception.util.LocalizedFormats; 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 extends ListPopulation { * @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 chromosomes, final int populationLimit, final double elitismRate) { 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 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); } /** diff --git a/src/test/java/org/apache/commons/math3/genetics/ElitisticListPopulationTest.java b/src/test/java/org/apache/commons/math3/genetics/ElitisticListPopulationTest.java index f2d18719b..dbfc9f12e 100644 --- a/src/test/java/org/apache/commons/math3/genetics/ElitisticListPopulationTest.java +++ b/src/test/java/org/apache/commons/math3/genetics/ElitisticListPopulationTest.java @@ -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 chromosomes = Collections.emptyList(); + final double rate = -0.25; + new ElitisticListPopulation(chromosomes, 100, rate); + } + + @Test(expected = OutOfRangeException.class) + public void testChromosomeListConstructorTooHigh() { + final List chromosomes = Collections.emptyList(); + final double rate = 1.25; + new ElitisticListPopulation(chromosomes, 100, rate); + } private static class DummyChromosome extends Chromosome { private final int fitness;