1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.genetics;
18
19 import static 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 assertTrue(pair.getFirst().getFitness() > 0);
39 assertTrue(pair.getSecond().getFitness() > 0);
40 }
41 }
42
43 private static class DummyChromosome extends Chromosome {
44 private final int fitness;
45
46 public DummyChromosome() {
47 this.fitness = counter;
48 counter++;
49 }
50
51 public double fitness() {
52 return this.fitness;
53 }
54 }
55
56 }