KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > impl > WeightedRouletteSelectorTest


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.impl;
11
12 import java.util.*;
13 import org.jgap.*;
14 import junit.framework.*;
15
16 /**
17  * Tests the WeightedRouletteSelector class.
18  *
19  * @author Klaus Meffert
20  * @since 1.1
21  */

22 public class WeightedRouletteSelectorTest
23     extends JGAPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.27 $";
26
27   public static Test suite() {
28     TestSuite suite = new TestSuite(WeightedRouletteSelectorTest.class);
29     return suite;
30   }
31
32   public void setUp() {
33     super.setUp();
34     Configuration.reset();
35   }
36
37   /**
38    * Test if construction possible without failure.
39    */

40   public void testConstruct_0() {
41     new WeightedRouletteSelector();
42   }
43
44   /**
45    *
46    * @throws Exception
47    *
48    * @author Klaus Meffert
49    * @since 2.2
50    */

51   public void testAdd_0()
52       throws Exception JavaDoc {
53     WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
54     Configuration conf = new DefaultConfiguration();
55     Gene gene = new BooleanGene(conf);
56     Chromosome chrom = new Chromosome(conf, gene, 5);
57     conf.setFitnessFunction(new TestFitnessFunction());
58     conf.setSampleChromosome(chrom);
59     conf.setPopulationSize(5);
60     selector.add(chrom);
61     Map chromosomes = (Map) privateAccessor.getField(selector,
62         "m_wheel");
63     assertEquals(1, chromosomes.size());
64     Iterator it = chromosomes.keySet().iterator();
65     assertEquals(chrom, it.next());
66     selector.add(chrom);
67     assertEquals(1, chromosomes.size());
68     it = chromosomes.keySet().iterator();
69     assertEquals(chrom, it.next());
70   }
71
72   /**
73    * @throws Exception
74    *
75    * @author Klaus Meffert
76    * @since 2.2
77    */

78   public void testSelect_0()
79       throws Exception JavaDoc {
80     WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
81     Gene gene = new BooleanGene(conf);
82     gene.setAllele(Boolean.valueOf(true));
83     Chromosome bestChrom = new Chromosome(conf, gene, 7);
84     bestChrom.setFitnessValue(10);
85     selector.add(bestChrom);
86     Population p = new Population(conf);
87     selector.select(1, null, p);
88     assertSame(bestChrom, p.getChromosome(0));
89     assertEquals(1, p.size());
90   }
91
92   /**
93    *
94    * @throws Exception
95    *
96    * @author Klaus Meffert
97    * @since 2.2
98    */

99   public void testSelect_1()
100       throws Exception JavaDoc {
101     DefaultConfiguration conf = new DefaultConfiguration();
102     RandomGeneratorForTest randgen = new RandomGeneratorForTest();
103     randgen.setNextDouble(0.9999d);
104     conf.setRandomGenerator(randgen);
105     WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
106     selector.setDoubletteChromosomesAllowed(false);
107     // add first chromosome
108
// --------------------
109
Gene gene = new BooleanGene(conf);
110     gene.setAllele(Boolean.valueOf(true));
111     Chromosome thirdBestChrom = new Chromosome(conf, gene, 4);
112     thirdBestChrom.setFitnessValue(10);
113     selector.add(thirdBestChrom);
114     // add second chromosome
115
// ---------------------
116
gene = new DoubleGene(conf);
117     gene.setAllele(new Double JavaDoc(2.3d));
118     Chromosome bestChrom = new Chromosome(conf, gene, 3);
119     bestChrom.setFitnessValue(12);
120     selector.add(bestChrom);
121     // add third chromosome
122
// ---------------------
123
gene = new IntegerGene(conf);
124     gene.setAllele(new Integer JavaDoc(444));
125     Chromosome secondBestChrom = new Chromosome(conf, gene, 2);
126     secondBestChrom.setFitnessValue(11);
127     selector.add(secondBestChrom);
128     // receive top 1 (= best) chromosome
129
// ---------------------------------
130
Population popNew = new Population(conf);
131     selector.select(1, null, popNew);
132     IChromosome[] bestChroms = popNew.toChromosomes();
133     assertEquals(1, bestChroms.length);
134     assertEquals(thirdBestChrom, bestChroms[0]);
135     assertSame(thirdBestChrom, bestChroms[0]);
136     // now select top 4 chromosomes (should only select 3!)
137
// ----------------------------------------------------
138
popNew.getChromosomes().clear();
139     selector.select(4, null, popNew);
140     bestChroms = popNew.toChromosomes();
141     assertEquals(3, bestChroms.length);
142   }
143
144   /**
145    * @throws Exception
146    *
147    * @author Klaus Meffert
148    * @since 2.2
149    */

150   public void testSelect_2()
151       throws Exception JavaDoc {
152     DefaultConfiguration conf = new DefaultConfiguration();
153     RandomGeneratorForTest randgen = new RandomGeneratorForTest();
154     randgen.setNextDouble(0.9999d);
155     conf.setRandomGenerator(randgen);
156     WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
157     selector.setDoubletteChromosomesAllowed(false);
158     Population toAddFrom = new Population(conf);
159     // add first chromosome
160
// --------------------
161
Gene gene = new BooleanGene(conf);
162     gene.setAllele(Boolean.valueOf(true));
163     Chromosome thirdBestChrom = new Chromosome(conf, gene, 4);
164     thirdBestChrom.setFitnessValue(10);
165     toAddFrom.addChromosome(thirdBestChrom);
166     // add second chromosome
167
// ---------------------
168
gene = new DoubleGene(conf);
169     gene.setAllele(new Double JavaDoc(2.3d));
170     Chromosome bestChrom = new Chromosome(conf, gene, 3);
171     bestChrom.setFitnessValue(12);
172     toAddFrom.addChromosome(bestChrom);
173     // add third chromosome
174
// --------------------
175
gene = new IntegerGene(conf);
176     gene.setAllele(new Integer JavaDoc(444));
177     Chromosome secondBestChrom = new Chromosome(conf, gene, 2);
178     secondBestChrom.setFitnessValue(11);
179     toAddFrom.addChromosome(secondBestChrom);
180     // receive top 1 (= best) chromosome
181
// ---------------------------------
182
Population popNew = new Population(conf);
183     selector.select(1, toAddFrom, popNew);
184     IChromosome[] bestChroms = popNew.toChromosomes();
185     assertEquals(1, bestChroms.length);
186     assertEquals(thirdBestChrom, bestChroms[0]);
187     // now select top 4 chromosomes (should only select 3!)
188
// ----------------------------------------------------
189
popNew.getChromosomes().clear();
190     selector.select(4, toAddFrom, popNew);
191     bestChroms = popNew.toChromosomes();
192     assertEquals(3, bestChroms.length);
193   }
194
195   /**
196    * Ensure the scaling of fitness value works without error (like division
197    * by zero)
198    * @throws Exception
199    *
200    * @author Klaus Meffert
201    * @since 2.2
202    */

203   public void testSelect_3()
204       throws Exception JavaDoc {
205     DefaultConfiguration conf = new DefaultConfiguration();
206     conf.addNaturalSelector(new WeightedRouletteSelector(), false);
207     RandomGeneratorForTest randgen = new RandomGeneratorForTest();
208     randgen.setNextDouble(0.0d);
209     conf.setRandomGenerator(randgen);
210     WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
211     selector.setDoubletteChromosomesAllowed(false);
212     Population toAddFrom = new Population(conf);
213     // add first chromosome
214
// --------------------
215
Gene gene = new BooleanGene(conf);
216     gene.setAllele(Boolean.valueOf(true));
217     Chromosome thirdBestChrom = new Chromosome(conf, gene, 4);
218     thirdBestChrom.setFitnessValue(0);
219     toAddFrom.addChromosome(thirdBestChrom);
220     // add second chromosome
221
// ---------------------
222
gene = new DoubleGene(conf);
223     gene.setAllele(new Double JavaDoc(2.3d));
224     Chromosome bestChrom = new Chromosome(conf, gene, 3);
225     bestChrom.setFitnessValue(1.0d);
226     toAddFrom.addChromosome(bestChrom);
227     // add third chromosome
228
// ---------------------
229
gene = new IntegerGene(conf);
230     gene.setAllele(new Integer JavaDoc(444));
231     Chromosome secondBestChrom = new Chromosome(conf, gene, 2);
232     secondBestChrom.setFitnessValue( -1.0d);
233     toAddFrom.addChromosome(secondBestChrom);
234     // receive top 3 chromosomes, no error should occur
235
// ------------------------------------------------
236
Population popNew = new Population(conf);
237     selector.select(3, toAddFrom, popNew);
238   }
239
240   /**
241    * Use DeltaFitnessEvaluator
242    * @throws Exception
243    *
244    * @author Klaus Meffert
245    * @since 2.4
246    */

247   public void testSelect_4()
248       throws Exception JavaDoc {
249     conf.setFitnessEvaluator(new DeltaFitnessEvaluator());
250     RandomGeneratorForTest randgen = new RandomGeneratorForTest();
251     randgen.setNextDouble(0.9999d);
252     conf.setRandomGenerator(randgen);
253     WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
254     selector.setDoubletteChromosomesAllowed(false);
255     Population toAddFrom = new Population(conf);
256     // add first chromosome
257
// --------------------
258
Gene gene = new BooleanGene(conf);
259     gene.setAllele(Boolean.valueOf(true));
260     Chromosome thirdBestChrom = new Chromosome(conf, gene, 4);
261     thirdBestChrom.setFitnessValue(1);
262     toAddFrom.addChromosome(thirdBestChrom);
263     // add second chromosome
264
// ---------------------
265
gene = new DoubleGene(conf);
266     gene.setAllele(new Double JavaDoc(2.3d));
267     Chromosome secondBestChrom = new Chromosome(conf, gene, 3);
268     secondBestChrom.setFitnessValue(2);
269     toAddFrom.addChromosome(secondBestChrom);
270     // add third chromosome
271
// --------------------
272
gene = new IntegerGene(conf);
273     gene.setAllele(new Integer JavaDoc(444));
274     Chromosome bestChrom = new Chromosome(conf, gene, 2);
275     bestChrom.setFitnessValue(3);
276     toAddFrom.addChromosome(bestChrom);
277     // receive top 1 (= best) chromosome
278
// ---------------------------------
279
Population popNew = new Population(conf);
280     selector.select(1, toAddFrom, popNew);
281     IChromosome[] bestChroms = popNew.toChromosomes();
282     assertEquals(1, bestChroms.length);
283     assertEquals(bestChrom, bestChroms[0]);
284     // now select top 4 chromosomes (should only select 3!)
285
// ----------------------------------------------------
286
popNew.getChromosomes().clear();
287     selector.select(4, toAddFrom, popNew);
288     bestChroms = popNew.toChromosomes();
289     assertEquals(3, bestChroms.length);
290   }
291
292   /**
293    *
294    * @throws Exception
295    *
296    * @author Klaus Meffert
297    * @since 2.2
298    */

299   public void testEmpty_0()
300       throws Exception JavaDoc {
301     WeightedRouletteSelector selector = new WeightedRouletteSelector();
302     Configuration conf = new DefaultConfiguration();
303     conf.setPopulationSize(7);
304     conf.setFitnessFunction(new TestFitnessFunction());
305     Gene gene = new BooleanGene(conf);
306     Chromosome chrom = new Chromosome(conf, gene, 5);
307     conf.setSampleChromosome(chrom);
308     selector.add(chrom);
309     selector.empty();
310     Map chromosomes = (Map) privateAccessor.getField(selector, "m_wheel");
311     assertEquals(0, chromosomes.size());
312   }
313
314   /**
315    * Test if clear()-method does not affect original Population.
316    * @throws Exception
317    *
318    * @author Klaus Meffert
319    */

320   public void testEmpty_1()
321       throws Exception JavaDoc {
322     Configuration conf = new DefaultConfiguration();
323     WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
324     Gene gene = new BooleanGene(conf);
325     Chromosome chrom = new Chromosome(conf, gene, 5);
326     chrom.setFitnessValue(3);
327     Population pop = new Population(conf, 1);
328     pop.addChromosome(chrom);
329     selector.add(chrom);
330     Population popNew = new Population(conf);
331     selector.select(1, null, popNew);
332     selector.empty();
333     assertEquals(1, popNew.size());
334   }
335
336   /**
337    * Test if clear()-method does not affect return value.
338    * @throws Exception
339    *
340    * @author Klaus Meffert
341    */

342   public void testEmpty_2()
343       throws Exception JavaDoc {
344     Configuration conf = new DefaultConfiguration();
345     WeightedRouletteSelector selector = new WeightedRouletteSelector(conf);
346     Gene gene = new BooleanGene(conf);
347     Chromosome chrom = new Chromosome(conf, gene, 5);
348     chrom.setFitnessValue(7);
349     Population pop = new Population(conf, 1);
350     pop.addChromosome(chrom);
351     selector.add(chrom);
352     Population popNew = new Population(conf);
353     selector.select(1, null, popNew);
354     selector.empty();
355     assertEquals(1, popNew.size());
356   }
357
358   /**
359    * @author Klaus Meffert
360    * @since 2.2
361    */

362   public void testReturnsUniqueChromosomes_0() {
363     WeightedRouletteSelector selector = new WeightedRouletteSelector();
364     assertFalse(selector.returnsUniqueChromosomes());
365   }
366   /**@todo add test*/
367 // public void test_WeightedSelection_0() {
368
// WeightedRouletteSelector ws = new WeightedRouletteSelector();
369
// // set a population of size 2
370
// int numCrossovers = 100;
371
// int crossoversSoFar = 0;
372
// while (crossoversSoFar < numCrossovers) {
373
// ws.select(2, a_population, parents);
374
// Chromosome mother = parents.getChromosome(0);
375
// Chromosome father = parents.getChromosome(1);
376
// parents.removeChromosome(1);
377
// parents.removeChromosome(0);
378
// if (father.equals(mother)) {
379
// continue;
380
// }
381
// // I had some crossover function here yet you don't need it for the test case.
382
// crossoversSoFar++;
383
// }
384
// }
385
}
386
Popular Tags