KickJava   Java API By Example, From Geeks To Geeks.

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


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 TournamentSelector class.
18  *
19  * @author Klaus Meffert
20  * @since 2.0
21  */

22 public class TournamentSelectorTest
23     extends JGAPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.12 $";
26
27   public static Test suite() {
28     TestSuite suite = new TestSuite(TournamentSelectorTest.class);
29     return suite;
30   }
31
32   /**
33    * Valid construction
34    * @throws Exception
35    *
36    * @author Klaus Meffert
37    * @since 2.1
38    */

39   public void testConstruct_0()
40       throws Exception JavaDoc {
41     TournamentSelector sel = new TournamentSelector(null, 1, 1.0d);
42     assertNotNull(privateAccessor.getField(sel, "m_chromosomes"));
43     assertNotNull(privateAccessor.getField(sel, "m_fitnessValueComparator"));
44     new TournamentSelector(null, 1, 0.5d);
45     new TournamentSelector(null, 10, 0.00001d);
46     new TournamentSelector(null, 50, 0.4d);
47   }
48
49   /**
50    * Invalid construction.
51    * @throws Exception
52    *
53    * @author Klaus Meffert
54    * @since 2.1
55    */

56   public void testConstruct_1()
57       throws Exception JavaDoc {
58     try {
59       new TournamentSelector(null, 0, 0.5d);
60       fail();
61     }
62     catch (IllegalArgumentException JavaDoc iex) {
63       ; //this is OK
64
}
65   }
66
67   /**
68    * Invalid construction,
69    * @throws Exception
70    *
71    * @author Klaus Meffert
72    * @since 2.1
73    */

74   public void testConstruct_2()
75       throws Exception JavaDoc {
76     try {
77       new TournamentSelector(null, -1, 0.5d);
78       fail();
79     }
80     catch (IllegalArgumentException JavaDoc iex) {
81       ; //this is OK
82
}
83   }
84
85   /**
86    * Invalid construction.
87    * @throws Exception
88    *
89    * @author Klaus Meffert
90    * @since 2.1
91    */

92   public void testConstruct_3()
93       throws Exception JavaDoc {
94     try {
95       new TournamentSelector(null, 4, 0.0d);
96       fail();
97     }
98     catch (IllegalArgumentException JavaDoc iex) {
99       ; //this is OK
100
}
101   }
102
103   /**
104    * Invalid construction.
105    * @throws Exception
106    *
107    * @author Klaus Meffert
108    * @since 2.1
109    */

110   public void testConstruct_4()
111       throws Exception JavaDoc {
112     try {
113       new TournamentSelector(null, 4, 1.0001d);
114       fail();
115     }
116     catch (IllegalArgumentException JavaDoc iex) {
117       ; //this is OK
118
}
119   }
120
121   public void testAdd_0()
122       throws Exception JavaDoc {
123     TournamentSelector selector = new TournamentSelector(conf, 5, 0.5d);
124     Gene gene = new BooleanGene(conf);
125     IChromosome chrom = new Chromosome(conf, gene, 5);
126     selector.add(chrom);
127     List chromosomes = ( (Vector) privateAccessor.getField(selector,
128         "m_chromosomes"));
129     assertEquals(1, chromosomes.size());
130     assertEquals(chrom, chromosomes.get(0));
131     selector.add(chrom);
132     assertEquals(chrom, chromosomes.get(0));
133     assertEquals(2, chromosomes.size());
134     selector.add(chrom);
135     assertEquals(3, chromosomes.size());
136   }
137
138   public void testEmpty_0()
139       throws Exception JavaDoc {
140     TournamentSelector selector = new TournamentSelector(conf, 4, 0.1d);
141     Gene gene = new BooleanGene(conf);
142     IChromosome chrom = new Chromosome(conf, gene, 5);
143     selector.add(chrom);
144     selector.empty();
145     List chromosomes = ( (Vector) privateAccessor.getField(selector,
146         "m_chromosomes"));
147     assertEquals(0, chromosomes.size());
148   }
149
150   /**
151    * Test if clear()-method does not affect original Population.
152    *
153    * @throws Exception
154    * @author Klaus Meffert
155    * @since 2.1
156    */

157   public void testEmpty_1()
158       throws Exception JavaDoc {
159     TournamentSelector selector = new TournamentSelector(conf, 4, 0.1d);
160     Gene gene = new BooleanGene(conf);
161     Chromosome chrom = new Chromosome(conf, gene, 5);
162     Population pop = new Population(conf, 1);
163     pop.addChromosome(chrom);
164     selector.add(chrom);
165     Population popNew = new Population(conf);
166     selector.select(1, null, popNew);
167     selector.empty();
168     assertEquals(1, popNew.size());
169     assertNotNull(popNew.getChromosome(0));
170   }
171
172   /**
173    * Test if clear()-method does not affect return value.
174    *
175    * @throws Exception
176    * @author Klaus Meffert
177    * @since 2.1
178    */

179   public void testEmpty_2()
180       throws Exception JavaDoc {
181     TournamentSelector selector = new TournamentSelector(conf, 10, 1.0d);
182     Gene gene = new BooleanGene(conf);
183     Chromosome chrom = new Chromosome(conf, gene, 5);
184     Population pop = new Population(conf, 1);
185     pop.addChromosome(chrom);
186     selector.add(chrom);
187     Population popNew = new Population(conf);
188     selector.select(1, null, popNew);
189     pop = popNew;
190     selector.empty();
191     assertEquals(1, pop.size());
192     assertNotNull(pop.getChromosome(0));
193   }
194
195   /**
196    * Test if below functionality working without error.
197    *
198    * @throws Exception
199    * @author Klaus Meffert
200    * @since 2.1
201    */

202   public void testSelect_0()
203       throws Exception JavaDoc {
204     TournamentSelector selector = new TournamentSelector(conf, 4, 0.3d);
205     Gene gene = new IntegerGene(conf);
206     gene.setAllele(new Integer JavaDoc(444));
207     Chromosome secondBestChrom = new Chromosome(conf, gene, 3);
208     secondBestChrom.setFitnessValue(11);
209     selector.add(secondBestChrom);
210     gene = new BooleanGene(conf);
211     gene.setAllele(Boolean.valueOf(false));
212     Chromosome bestChrom = new Chromosome(conf, gene, 3);
213     bestChrom.setFitnessValue(12);
214     selector.add(bestChrom);
215     selector.select(1, null, new Population(conf));
216   }
217
218   /**
219    * Always select best chromosome for granted if prob is 1.0 and index for
220    * selected chromosomes in tournament is equal to index of best chromosome.
221    *
222    * @throws Exception
223    * @author Klaus Meffert
224    * @since 2.1
225    */

226   public void testSelect_1()
227       throws Exception JavaDoc {
228     // random generator always returning 1 (index of best chromosome below)
229
RandomGeneratorForTest rn = new RandomGeneratorForTest(1);
230     conf.setRandomGenerator(rn);
231     TournamentSelector selector = new TournamentSelector(conf, 4, 1.0d);
232     // add first chromosome
233
// --------------------
234
Gene gene = new BooleanGene(conf);
235     gene.setAllele(Boolean.valueOf(true));
236     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
237     thirdBestChrom.setFitnessValue(10);
238     selector.add(thirdBestChrom);
239     // add second chromosome
240
// ---------------------
241
gene = new BooleanGene(conf);
242     gene.setAllele(Boolean.valueOf(false));
243     Chromosome bestChrom = new Chromosome(conf, gene, 3);
244     bestChrom.setFitnessValue(12);
245     selector.add(bestChrom);
246     // add third chromosome
247
// ---------------------
248
gene = new IntegerGene(conf);
249     gene.setAllele(new Integer JavaDoc(444));
250     Chromosome secondBestChrom = new Chromosome(conf, gene, 3);
251     secondBestChrom.setFitnessValue(11);
252     selector.add(secondBestChrom);
253     // receive top 1 (= best) chromosome
254
// ---------------------------------
255
Population pop = new Population(conf);
256     selector.select(1, null, pop);
257     IChromosome[] bestChroms = pop.toChromosomes();
258     assertEquals(1, bestChroms.length);
259     assertEquals(bestChrom, bestChroms[0]);
260     // receive top 3 chromosomes
261
// -------------------------
262
pop.getChromosomes().clear();
263     selector.select(3, null, pop);
264     bestChroms = pop.toChromosomes();
265     assertEquals(3, bestChroms.length);
266     assertEquals(bestChrom, bestChroms[0]);
267     assertEquals(bestChrom, bestChroms[1]);
268     assertEquals(bestChrom, bestChroms[2]);
269   }
270
271   /**
272    * Always select best chromosome for granted if prob is 1.0 and index for
273    * selected chromosomes in tournament is equal to index of best chromosome.
274    *
275    * @throws Exception
276    * @author Klaus Meffert
277    * @since 2.1
278    */

279   public void testSelect_2()
280       throws Exception JavaDoc {
281     // random generator always returning 1 (index of best chromosome below)
282
RandomGeneratorForTest rn = new RandomGeneratorForTest(1);
283     conf.setRandomGenerator(rn);
284     TournamentSelector selector = new TournamentSelector(conf, 4, 1.0d);
285     // add first chromosome
286
// --------------------
287
Gene gene = new BooleanGene(conf);
288     gene.setAllele(Boolean.valueOf(true));
289     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
290     thirdBestChrom.setFitnessValue(10);
291     selector.add(thirdBestChrom);
292     // add second chromosome
293
// ---------------------
294
gene = new BooleanGene(conf);
295     gene.setAllele(Boolean.valueOf(false));
296     Chromosome bestChrom = new Chromosome(conf, gene, 3);
297     bestChrom.setFitnessValue(12);
298     selector.add(bestChrom);
299     // receive top 1 (= best) chromosome
300
// ---------------------------------
301
Population pop = new Population(conf);
302     selector.select(1, null, pop);
303     IChromosome[] bestChroms = pop.toChromosomes();
304     assertEquals(1, bestChroms.length);
305     assertEquals(bestChrom, bestChroms[0]);
306     // receive top 30 chromosomes.
307
// ---------------------------
308
pop.getChromosomes().clear();
309     selector.select(30, null, pop);
310     bestChroms = pop.toChromosomes();
311     assertEquals(30, bestChroms.length);
312     assertEquals(bestChrom, bestChroms[0]);
313     assertEquals(bestChrom, bestChroms[1]);
314     assertTrue(bestChrom == bestChroms[0]);
315   }
316
317   /**
318    * @throws Exception
319    *
320    * @author Klaus Meffert
321    * @since 2.1
322    */

323   public void testSelect_3()
324       throws Exception JavaDoc {
325     //Set index of chromosome to be selected by ThresholdSelector to 1.
326
//1 because the best chromosome will be index 0 and the other one has
327
// index 1.
328
RandomGeneratorForTest rn = new RandomGeneratorForTest(0);
329     rn.setNextDouble(0.0d);
330     conf.setRandomGenerator(rn);
331     TournamentSelector selector = new TournamentSelector(conf, 2, 1.0d);
332     // add first chromosome
333
// --------------------
334
Gene gene = new BooleanGene(conf);
335     gene.setAllele(Boolean.valueOf(true));
336     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
337     thirdBestChrom.setFitnessValue(10);
338     selector.add(thirdBestChrom);
339     // add second chromosome
340
// ---------------------
341
gene = new BooleanGene(conf);
342     gene.setAllele(Boolean.valueOf(false));
343     Chromosome bestChrom = new Chromosome(conf, gene, 3);
344     bestChrom.setFitnessValue(12);
345     selector.add(bestChrom);
346     // receive top 1 (= best) chromosome
347
// ---------------------------------
348
Population pop = new Population(conf);
349     selector.select(1, null, pop);
350     IChromosome[] bestChroms = pop.toChromosomes();
351     assertFalse(bestChroms[0].equals(bestChrom));
352   }
353
354   /**
355    * Ensure that selected Chromosome's are not equal to added Chromosome's.
356    * @throws Exception
357    *
358    * @author Klaus Meffert
359    * @since 2.1
360    */

361   public void testSelect_4()
362       throws Exception JavaDoc {
363     TournamentSelector selector = new TournamentSelector(conf, 1, 0.2d);
364     // add first chromosome
365
// --------------------
366
Gene gene = new BooleanGene(conf);
367     gene.setAllele(Boolean.valueOf(true));
368     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
369     thirdBestChrom.setFitnessValue(10);
370     selector.add(thirdBestChrom);
371     // add second chromosome
372
// ---------------------
373
gene = new BooleanGene(conf);
374     gene.setAllele(Boolean.valueOf(false));
375     Chromosome bestChrom = new Chromosome(conf, gene, 3);
376     bestChrom.setFitnessValue(12);
377     selector.add(bestChrom);
378     // receive top 30 chromosomes.
379
// ---------------------------
380
Population pop = new Population(conf);
381     selector.select(30, null, pop);
382     Population bestChroms = pop;
383     List chromosomes = (Vector) privateAccessor.getField(selector,
384         "m_chromosomes");
385     assertFalse(bestChroms.equals(chromosomes));
386   }
387
388   /**
389    * Never select best chromosome if prob is 0.0d. it is not allowed to select
390    * probability to 0.0d therefor we set it via reflection.
391    * @throws Exception
392    *
393    * @author Klaus Meffert
394    * @since 2.1
395    */

396   public void testSelect_5()
397       throws Exception JavaDoc {
398     RandomGeneratorForTest rn = new RandomGeneratorForTest(0);
399     conf.setRandomGenerator(rn);
400     TournamentSelector selector = new TournamentSelector(conf, 4, 0.00001d);
401     setNestedField(selector, "m_config", "m_probability", new Double JavaDoc(0.0d));
402     // add first chromosome
403
// --------------------
404
Gene gene = new BooleanGene(conf);
405     gene.setAllele(Boolean.valueOf(true));
406     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
407     thirdBestChrom.setFitnessValue(10);
408     selector.add(thirdBestChrom);
409     // add second chromosome
410
// ---------------------
411
gene = new BooleanGene(conf);
412     gene.setAllele(Boolean.valueOf(false));
413     IChromosome bestChrom = new Chromosome(conf, gene, 3);
414     bestChrom.setFitnessValue(12);
415     selector.add(bestChrom);
416     // receive top 5 chromosomes.
417
// ---------------------------
418
Population pop = new Population(conf);
419     IChromosome[] bestChroms;
420     selector.select(5, null, pop);
421     bestChroms = pop.toChromosomes();
422     assertEquals(thirdBestChrom, bestChroms[0]);
423     assertEquals(thirdBestChrom, bestChroms[1]);
424   }
425
426   /**
427    * @throws Exception
428    *
429    * @author Klaus Meffert
430    * @since 2.2
431    */

432   public void testSelect_6()
433       throws Exception JavaDoc {
434     // random generator always returning 1 (index of best chromosome below)
435
RandomGeneratorForTest rn = new RandomGeneratorForTest(1);
436     conf.setRandomGenerator(rn);
437     TournamentSelector selector = new TournamentSelector(conf, 4, 1.0d);
438     Population toAddFrom = new Population(conf);
439     // add first chromosome
440
// --------------------
441
Gene gene = new BooleanGene(conf);
442     gene.setAllele(Boolean.valueOf(true));
443     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
444     thirdBestChrom.setFitnessValue(10);
445     toAddFrom.addChromosome(thirdBestChrom);
446     // add second chromosome
447
// ---------------------
448
gene = new BooleanGene(conf);
449     gene.setAllele(Boolean.valueOf(false));
450     Chromosome bestChrom = new Chromosome(conf, gene, 3);
451     bestChrom.setFitnessValue(12);
452     toAddFrom.addChromosome(bestChrom);
453     // add third chromosome
454
// ---------------------
455
gene = new IntegerGene(conf);
456     gene.setAllele(new Integer JavaDoc(444));
457     Chromosome secondBestChrom = new Chromosome(conf, gene, 3);
458     secondBestChrom.setFitnessValue(11);
459     toAddFrom.addChromosome(secondBestChrom);
460     // receive top 1 (= best) chromosome
461
// ---------------------------------
462
Population pop = new Population(conf);
463     selector.select(1, null, pop);
464     IChromosome[] bestChroms = pop.toChromosomes();
465     // nothing selected (from nothing!)
466
assertEquals(0, bestChroms.length);
467     selector.select(1, toAddFrom, pop);
468     bestChroms = pop.toChromosomes();
469     assertEquals(1, bestChroms.length);
470     assertEquals(bestChrom, bestChroms[0]);
471     // receive top 3 chromosomes
472
// -------------------------
473
pop.getChromosomes().clear();
474     selector.select(3, toAddFrom, pop);
475     bestChroms = pop.toChromosomes();
476     assertEquals(3, bestChroms.length);
477     assertEquals(bestChrom, bestChroms[0]);
478     assertEquals(bestChrom, bestChroms[1]);
479     assertEquals(bestChrom, bestChroms[2]);
480   }
481
482   /**
483    * @author Klaus Meffert
484    * @since 2.2
485    */

486   public void testReturnsUniqueChromosomes_0() {
487     TournamentSelector selector = new TournamentSelector(null, 2, 0.5d);
488     assertFalse(selector.returnsUniqueChromosomes());
489   }
490
491   /**
492    * @throws Exception
493    *
494    * @author Klaus Meffert
495    * @since 2.6
496    */

497   public void testSetTournametSize_0()
498       throws Exception JavaDoc {
499     TournamentSelector sel = new TournamentSelector(null, 1, 1.0d);
500     sel.setTournamentSize(5);
501     assertEquals(5, sel.getTournamentSize());
502     try {
503       sel.setTournamentSize(0);
504       fail();
505     }
506     catch (IllegalArgumentException JavaDoc iex) {
507       ; //this is OK
508
}
509   }
510
511   /**
512    * @throws Exception
513    *
514    * @author Klaus Meffert
515    * @since 2.6
516    */

517   public void testSetProbability_0()
518       throws Exception JavaDoc {
519     TournamentSelector sel = new TournamentSelector(null, 1, 1.0d);
520     sel.setProbability(0.6d);
521     assertEquals(0.6d, sel.getProbability(), DELTA);
522     try {
523       sel.setProbability(1.6d);
524       fail();
525     }
526     catch (IllegalArgumentException JavaDoc iex) {
527       ; // this is OK
528
}
529   }
530 }
531
Popular Tags