View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math4.legacy.genetics;
18  
19  
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.commons.math4.legacy.exception.OutOfRangeException;
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  public class ElitisticListPopulationTest {
28  
29      private static int counter = 0;
30  
31      @Test
32      public void testNextGeneration() {
33          ElitisticListPopulation pop = new ElitisticListPopulation(100, 0.203);
34  
35          for (int i=0; i<pop.getPopulationLimit(); i++) {
36              pop.addChromosome(new DummyChromosome());
37          }
38  
39          Population nextGeneration = pop.nextGeneration();
40  
41          Assert.assertEquals(20, nextGeneration.getPopulationSize());
42      }
43  
44      @Test
45      public void testSetElitismRate() {
46          final double rate = 0.25;
47          final ElitisticListPopulation pop = new ElitisticListPopulation(100, 0.203);
48          pop.setElitismRate(rate);
49          Assert.assertEquals(rate, pop.getElitismRate(), 1e-6);
50      }
51  
52      @Test(expected = OutOfRangeException.class)
53      public void testSetElitismRateTooLow() {
54          final double rate = -0.25;
55          final ElitisticListPopulation pop = new ElitisticListPopulation(100, 0.203);
56          pop.setElitismRate(rate);
57      }
58  
59      @Test(expected = OutOfRangeException.class)
60      public void testSetElitismRateTooHigh() {
61          final double rate = 1.25;
62          final ElitisticListPopulation pop = new ElitisticListPopulation(100, 0.203);
63          pop.setElitismRate(rate);
64      }
65  
66      @Test(expected = OutOfRangeException.class)
67      public void testConstructorTooLow() {
68          final double rate = -0.25;
69          new ElitisticListPopulation(100, rate);
70      }
71  
72      @Test(expected = OutOfRangeException.class)
73      public void testConstructorTooHigh() {
74          final double rate = 1.25;
75          new ElitisticListPopulation(100, rate);
76      }
77  
78      @Test(expected = OutOfRangeException.class)
79      public void testChromosomeListConstructorTooLow() {
80          final List<Chromosome> chromosomes = Collections.emptyList();
81          final double rate = -0.25;
82          new ElitisticListPopulation(chromosomes, 100, rate);
83      }
84  
85      @Test(expected = OutOfRangeException.class)
86      public void testChromosomeListConstructorTooHigh() {
87          final List<Chromosome> chromosomes = Collections.emptyList();
88          final double rate = 1.25;
89          new ElitisticListPopulation(chromosomes, 100, rate);
90      }
91  
92      private static final class DummyChromosome extends Chromosome {
93          private final int fitness;
94  
95          DummyChromosome() {
96              this.fitness = counter;
97              counter++;
98          }
99  
100         @Override
101         public double fitness() {
102             return this.fitness;
103         }
104     }
105 }