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 import org.junit.Assert;
20 import org.junit.Test;
21
22 public class TournamentSelectionTest {
23
24 private static int counter = 0;
25
26 @Test
27 public void testSelect() {
28 TournamentSelection ts = new TournamentSelection(2);
29 ElitisticListPopulation pop = new ElitisticListPopulation(100, 0.203);
30
31 for (int i=0; i<pop.getPopulationLimit(); i++) {
32 pop.addChromosome(new DummyChromosome());
33 }
34
35 for (int i=0; i<20; i++) {
36 ChromosomePair pair = ts.select(pop);
37
38 Assert.assertTrue(pair.getFirst().getFitness() > 0);
39 Assert.assertTrue(pair.getSecond().getFitness() > 0);
40 }
41 }
42
43 private static final class DummyChromosome extends Chromosome {
44 private final int fitness;
45
46 DummyChromosome() {
47 this.fitness = counter;
48 counter++;
49 }
50
51 @Override
52 public double fitness() {
53 return this.fitness;
54 }
55 }
56 }