1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
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 }