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.ArrayList;
21 import java.util.Iterator;
22
23 import org.apache.commons.math4.legacy.exception.NotPositiveException;
24 import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
25 import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
26 import org.junit.Assert;
27 import org.junit.Test;
28
29 public class ListPopulationTest {
30
31 @Test
32 public void testGetFittestChromosome() {
33 Chromosome c1 = new Chromosome() {
34 @Override
35 public double fitness() {
36 return 0;
37 }
38 };
39 Chromosome c2 = new Chromosome() {
40 @Override
41 public double fitness() {
42 return 10;
43 }
44 };
45 Chromosome c3 = new Chromosome() {
46 @Override
47 public double fitness() {
48 return 15;
49 }
50 };
51
52 ArrayList<Chromosome> chromosomes = new ArrayList<> ();
53 chromosomes.add(c1);
54 chromosomes.add(c2);
55 chromosomes.add(c3);
56
57 ListPopulation population = new ListPopulation(chromosomes, 10) {
58 @Override
59 public Population nextGeneration() {
60
61 return null;
62 }
63 };
64
65 Assert.assertEquals(c3, population.getFittestChromosome());
66 }
67
68 @Test
69 public void testChromosomes() {
70 final ArrayList<Chromosome> chromosomes = new ArrayList<> ();
71 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
72 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
73 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
74
75 final ListPopulation population = new ListPopulation(10) {
76 @Override
77 public Population nextGeneration() {
78
79 return null;
80 }
81 };
82
83 population.addChromosomes(chromosomes);
84
85 Assert.assertEquals(chromosomes, population.getChromosomes());
86 Assert.assertEquals(chromosomes.toString(), population.toString());
87
88 population.setPopulationLimit(50);
89 Assert.assertEquals(50, population.getPopulationLimit());
90 }
91
92 @Test(expected = NotPositiveException.class)
93 public void testSetPopulationLimit() {
94 final ListPopulation population = new ListPopulation(10) {
95 @Override
96 public Population nextGeneration() {
97
98 return null;
99 }
100 };
101
102 population.setPopulationLimit(-50);
103 }
104
105 @Test(expected = NotPositiveException.class)
106 public void testConstructorPopulationLimitNotPositive() {
107 new ListPopulation(-10) {
108 @Override
109 public Population nextGeneration() {
110
111 return null;
112 }
113 };
114 }
115
116 @Test(expected = NotPositiveException.class)
117 public void testChromosomeListConstructorPopulationLimitNotPositive() {
118 final ArrayList<Chromosome> chromosomes = new ArrayList<> ();
119 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
120 new ListPopulation(chromosomes, -10) {
121 @Override
122 public Population nextGeneration() {
123
124 return null;
125 }
126 };
127 }
128
129 @Test(expected = NumberIsTooLargeException.class)
130 public void testConstructorListOfChromosomesBiggerThanPopulationSize() {
131 final ArrayList<Chromosome> chromosomes = new ArrayList<> ();
132 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
133 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
134 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
135 new ListPopulation(chromosomes, 1) {
136 @Override
137 public Population nextGeneration() {
138
139 return null;
140 }
141 };
142 }
143
144 @Test(expected=NumberIsTooLargeException.class)
145 public void testAddTooManyChromosomes() {
146 final ArrayList<Chromosome> chromosomes = new ArrayList<> ();
147 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
148 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
149 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
150
151 final ListPopulation population = new ListPopulation(2) {
152 @Override
153 public Population nextGeneration() {
154
155 return null;
156 }
157 };
158
159 population.addChromosomes(chromosomes);
160 }
161
162 @Test(expected=NumberIsTooLargeException.class)
163 public void testAddTooManyChromosomesSingleCall() {
164
165 final ListPopulation population = new ListPopulation(2) {
166 @Override
167 public Population nextGeneration() {
168
169 return null;
170 }
171 };
172
173 for (int i = 0; i <= population.getPopulationLimit(); i++) {
174 population.addChromosome(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
175 }
176 }
177
178 @Test(expected = UnsupportedOperationException.class)
179 public void testIterator() {
180 final ArrayList<Chromosome> chromosomes = new ArrayList<>();
181 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
182 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
183 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
184
185 final ListPopulation population = new ListPopulation(10) {
186 @Override
187 public Population nextGeneration() {
188
189 return null;
190 }
191 };
192
193 population.addChromosomes(chromosomes);
194
195 final Iterator<Chromosome> iter = population.iterator();
196 while (iter.hasNext()) {
197 iter.next();
198 iter.remove();
199 }
200 }
201
202 @Test(expected=NumberIsTooSmallException.class)
203 public void testSetPopulationLimitTooSmall() {
204 final ArrayList<Chromosome> chromosomes = new ArrayList<> ();
205 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
206 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
207 chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3)));
208
209 final ListPopulation population = new ListPopulation(chromosomes, 3) {
210 @Override
211 public Population nextGeneration() {
212
213 return null;
214 }
215 };
216
217 population.setPopulationLimit(2);
218 }
219 }