KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

38   public void testConstruct_0()
39       throws Exception JavaDoc {
40     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
41     Boolean JavaDoc needsSorting = (Boolean JavaDoc) privateAccessor.getField(selector,
42         "m_needsSorting");
43     assertEquals(Boolean.FALSE, needsSorting);
44     assertTrue(selector.returnsUniqueChromosomes());
45     assertFalse(selector.getDoubletteChromosomesAllowed());
46     Object JavaDoc fitnessValueComparator = privateAccessor.getField(selector,
47         "m_fitnessValueComparator");
48     assertTrue(fitnessValueComparator != null);
49   }
50
51   /**
52    * @throws Exception
53    *
54    * @author Klaus Meffert
55    * @since 3.1
56    */

57   public void testConstruct_3()
58       throws Exception JavaDoc {
59     Genotype.setStaticConfiguration(conf);
60     BestChromosomesSelector op = new BestChromosomesSelector();
61     assertSame(conf, op.getConfiguration());
62   }
63
64   /**
65    * @throws Exception
66    *
67    * @author Klaus Meffert
68    * @since 1.1
69    */

70   public void testDoubletteChromosomesAllowed_0()
71       throws Exception JavaDoc {
72     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
73     selector.setDoubletteChromosomesAllowed(true);
74     assertTrue(selector.getDoubletteChromosomesAllowed());
75     selector.setDoubletteChromosomesAllowed(false);
76     assertFalse(selector.getDoubletteChromosomesAllowed());
77     selector.setDoubletteChromosomesAllowed(true);
78     assertTrue(selector.getDoubletteChromosomesAllowed());
79     selector.setDoubletteChromosomesAllowed(true);
80     assertTrue(selector.getDoubletteChromosomesAllowed());
81   }
82
83   /**
84    * @throws Exception
85    *
86    * @author Klaus Meffert
87    * @since 1.1
88    */

89   public void testAdd_0()
90       throws Exception JavaDoc {
91     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
92     Gene gene = new BooleanGene(conf);
93     Chromosome chrom = new Chromosome(conf, gene, 5);
94     selector.add(chrom);
95     Boolean JavaDoc needsSorting = (Boolean JavaDoc) privateAccessor.getField(selector,
96         "m_needsSorting");
97     assertEquals(Boolean.TRUE, needsSorting);
98     List chromosomes = ( (Population) privateAccessor.getField(selector,
99         "m_chromosomes")).getChromosomes();
100     assertEquals(1, chromosomes.size());
101     assertEquals(chrom, chromosomes.get(0));
102     selector.add(chrom);
103     assertEquals(chrom, chromosomes.get(0));
104     // if BestChromosomesSelector adds non-unique chroms, then we have a count
105
// of two then after the add(..), else a count of 1
106
selector.setDoubletteChromosomesAllowed(false);
107     assertEquals(1, chromosomes.size());
108     selector.setDoubletteChromosomesAllowed(true);
109     selector.add(chrom);
110     assertEquals(2, chromosomes.size());
111   }
112
113   /**
114    * Test if below functionality available without error.
115    *
116    * @throws Exception
117    * @author Klaus Meffert
118    * @since 1.1
119    */

120   public void testSelect_0()
121       throws Exception JavaDoc {
122     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
123     Gene gene = new IntegerGene(conf);
124     gene.setAllele(new Integer JavaDoc(444));
125     Chromosome secondBestChrom = new Chromosome(conf, gene, 3);
126     secondBestChrom.setFitnessValue(11);
127     selector.add(secondBestChrom);
128     gene = new BooleanGene(conf);
129     gene.setAllele(Boolean.valueOf(false));
130     Chromosome bestChrom = new Chromosome(conf, gene, 3);
131     bestChrom.setFitnessValue(12);
132     selector.add(bestChrom);
133     selector.select(1, null, new Population(conf));
134   }
135
136   /**
137    * Test selection algorithm.
138    *
139    * @throws Exception
140    *
141    * @author Klaus Meffert
142    * @since 1.1
143    */

144   public void testSelect_1()
145       throws Exception JavaDoc {
146     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
147     // add first chromosome
148
// --------------------
149
Gene gene = new BooleanGene(conf);
150     gene.setAllele(Boolean.valueOf(true));
151     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
152     thirdBestChrom.setFitnessValue(10);
153     selector.add(thirdBestChrom);
154     // add second chromosome
155
// ---------------------
156
gene = new BooleanGene(conf);
157     gene.setAllele(Boolean.valueOf(false));
158     Chromosome bestChrom = new Chromosome(conf, gene, 3);
159     bestChrom.setFitnessValue(12);
160     selector.add(bestChrom);
161     // add third chromosome
162
// ---------------------
163
gene = new IntegerGene(conf);
164     gene.setAllele(new Integer JavaDoc(444));
165     Chromosome secondBestChrom = new Chromosome(conf, gene, 3);
166     secondBestChrom.setFitnessValue(11);
167     selector.add(secondBestChrom);
168     // receive top 1 (= best) chromosome
169
// ---------------------------------
170
Population pop = new Population(conf);
171     selector.select(1, null, pop);
172     IChromosome[] bestChroms = pop.toChromosomes();
173     assertEquals(1, bestChroms.length);
174     assertEquals(bestChrom, bestChroms[0]);
175     selector.setOriginalRate(1.0d);
176     // receive top 3 chromosomes
177
// -------------------------
178
pop.getChromosomes().clear();
179     selector.select(3, null, pop);
180     bestChroms = pop.toChromosomes();
181     assertEquals(3, bestChroms.length);
182     assertEquals(bestChrom, bestChroms[0]);
183     assertEquals(secondBestChrom, bestChroms[1]);
184     assertEquals(thirdBestChrom, bestChroms[2]);
185   }
186
187   /**
188    * Test selection algorithm.
189    *
190    * @throws Exception
191    *
192    * @author Klaus Meffert
193    * @since 1.1
194    */

195   public void testSelect_2()
196       throws Exception JavaDoc {
197     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
198     // add first chromosome
199
// --------------------
200
Gene gene = new BooleanGene(conf);
201     gene.setAllele(Boolean.valueOf(true));
202     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
203     thirdBestChrom.setFitnessValue(10);
204     selector.add(thirdBestChrom);
205     // add second chromosome
206
// ---------------------
207
gene = new BooleanGene(conf);
208     gene.setAllele(Boolean.valueOf(false));
209     Chromosome bestChrom = new Chromosome(conf, gene, 3);
210     bestChrom.setFitnessValue(12);
211     selector.add(bestChrom);
212     // receive top 1 (= best) chromosome
213
// ---------------------------------
214
Population pop = new Population(conf);
215     selector.select(1, null, pop);
216     IChromosome[] bestChroms = pop.toChromosomes();
217     assertEquals(1, bestChroms.length);
218     assertEquals(bestChrom, bestChroms[0]);
219     selector.setOriginalRate(1.0d);
220     // receive top 30 chromosomes (select-method should take into account only
221
// 2 chroms!)
222
// -----------------------------------------------------------------------
223
pop.getChromosomes().clear();
224     selector.select(30, null, pop);
225     bestChroms = pop.toChromosomes();
226     assertEquals(2, bestChroms.length);
227     assertEquals(bestChrom, bestChroms[0]);
228     assertEquals(thirdBestChrom, bestChroms[1]);
229     assertSame(bestChrom, bestChroms[0]);
230   }
231
232   /**
233    * Ensure that selected Chromosome's are not equal to added Chromosome's.
234    *
235    * @throws Exception
236    * @author Klaus Meffert
237    * @since 1.1
238    */

239   public void testSelect_3()
240       throws Exception JavaDoc {
241     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
242     // add first chromosome
243
// --------------------
244
Gene gene = new BooleanGene(conf);
245     gene.setAllele(Boolean.valueOf(true));
246     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
247     thirdBestChrom.setFitnessValue(10);
248     selector.add(thirdBestChrom);
249     // add second chromosome
250
// ---------------------
251
gene = new BooleanGene(conf);
252     gene.setAllele(Boolean.valueOf(false));
253     Chromosome bestChrom = new Chromosome(conf, gene, 3);
254     bestChrom.setFitnessValue(12);
255     selector.add(bestChrom);
256     selector.setOriginalRate(1.0d);
257     // receive top 30 chromosomes (select-method should take into account only
258
// 2 chroms!)
259
// -----------------------------------------------------------------------
260
Population pop = new Population(conf);
261     selector.select(30, null, pop);
262     Population bestChroms = pop;
263     Population chromosomes = (Population) privateAccessor.getField(selector,
264         "m_chromosomes");
265     assertTrue(bestChroms.equals(chromosomes));
266   }
267
268   /**
269    * Test selection algorithm with allowed doublettes.
270    *
271    * @throws Exception
272    *
273    * @author Klaus Meffert
274    * @since 1.1
275    */

276   public void testSelect_4()
277       throws Exception JavaDoc {
278     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
279     selector.setDoubletteChromosomesAllowed(true);
280     // the following original rate controls that only 30% of the chromosomes
281
// will be considered for selection as given with BestChromosomesSelector.
282
// The last 70% will be added as doublettes in this case.
283
selector.setOriginalRate(0.3d);
284     // add first chromosome
285
// --------------------
286
Gene gene = new BooleanGene(conf);
287     gene.setAllele(Boolean.valueOf(true));
288     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
289     thirdBestChrom.setFitnessValue(10);
290     selector.add(thirdBestChrom);
291     // add second chromosome
292
// ---------------------
293
gene = new BooleanGene(conf);
294     gene.setAllele(Boolean.valueOf(false));
295     Chromosome bestChrom = new Chromosome(conf, gene, 3);
296     bestChrom.setFitnessValue(12);
297     selector.add(bestChrom);
298     // add third chromosome
299
// ---------------------
300
gene = new IntegerGene(conf);
301     gene.setAllele(new Integer JavaDoc(444));
302     Chromosome secondBestChrom = new Chromosome(conf, gene, 3);
303     secondBestChrom.setFitnessValue(11);
304     selector.add(secondBestChrom);
305     // receive top 1 (= best) chromosome
306
// ---------------------------------
307
Population pop = new Population(conf);
308     selector.select(1, null, pop);
309     IChromosome[] bestChroms = pop.toChromosomes();
310     assertEquals(1, bestChroms.length);
311     assertEquals(bestChrom, bestChroms[0]);
312     // receive top 4 chromosomes with original rate = 0.3
313
// --------------------------------------------------
314
pop.getChromosomes().clear();
315     selector.select(4, null, pop);
316     bestChroms = pop.toChromosomes();
317     assertEquals(4, bestChroms.length);
318     assertEquals(bestChrom, bestChroms[0]);
319     assertEquals(bestChrom, bestChroms[1]); //because of originalRate = 0.3
320
assertEquals(secondBestChrom, bestChroms[2]);
321     assertEquals(thirdBestChrom, bestChroms[3]);
322     // non-unique chromosomes should have been returned
323
assertSame(bestChroms[0], bestChroms[1]);
324     // receive top 4 chromosomes with original rate = 1
325
// ------------------------------------------------
326
pop.getChromosomes().clear();
327     selector.setOriginalRate(1.0d);
328     selector.select(4, null, pop);
329     bestChroms = pop.toChromosomes();
330     assertEquals(4, bestChroms.length);
331     assertEquals(bestChrom, bestChroms[0]);
332     assertEquals(secondBestChrom, bestChroms[1]);
333     assertEquals(thirdBestChrom, bestChroms[2]);
334     assertEquals(bestChrom, bestChroms[3]);
335   }
336
337   /**
338    * @throws Exception
339    *
340    * @author Klaus Meffert
341    * @since 1.1
342    */

343   public void testEmpty_0()
344       throws Exception JavaDoc {
345     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
346     Gene gene = new BooleanGene(conf);
347     Chromosome chrom = new Chromosome(conf, gene, 5);
348     selector.add(chrom);
349     selector.empty();
350     Boolean JavaDoc needsSorting = (Boolean JavaDoc) privateAccessor.getField(selector,
351         "m_needsSorting");
352     assertEquals(Boolean.FALSE, needsSorting);
353     List chromosomes = ( (Population) privateAccessor.getField(selector,
354         "m_chromosomes")).getChromosomes();
355     assertEquals(0, chromosomes.size());
356   }
357
358   /**
359    * Test if clear()-method does not affect original Population.
360    *
361    * @throws Exception
362    * @author Klaus Meffert
363    * @since 1.1
364    */

365   public void testEmpty_1()
366       throws Exception JavaDoc {
367     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
368     Gene gene = new BooleanGene(conf);
369     Chromosome chrom = new Chromosome(conf, gene, 5);
370     selector.add(chrom);
371     Population popNew = new Population(conf);
372     selector.select(1, null, popNew);
373     selector.empty();
374     assertEquals(1, popNew.size());
375     assertNotNull(popNew.getChromosome(0));
376   }
377
378   /**
379    * Test if clear()-method does not affect return value.
380    *
381    * @throws Exception
382    *
383    * @author Klaus Meffert
384    * @since 1.1
385    */

386   public void testEmpty_2()
387       throws Exception JavaDoc {
388     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
389     Gene gene = new BooleanGene(conf);
390     Chromosome chrom = new Chromosome(conf, gene, 5);
391     Population pop = new Population(conf, 1);
392     pop.addChromosome(chrom);
393     Population popNew = new Population(conf);
394     selector.select(1, pop, popNew);
395     selector.empty();
396     assertEquals(1, popNew.size());
397     assertNotNull(popNew.getChromosome(0));
398   }
399
400   /**
401    * @throws Exception
402    *
403    * @author Klaus Meffert
404    * @since 2.2
405    */

406   public void testSetOriginalRate_0()
407       throws Exception JavaDoc {
408     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
409     try {
410       selector.setOriginalRate(1.01d);
411       fail();
412     }
413     catch (IllegalArgumentException JavaDoc iex) {
414       ; //this is OK
415
}
416   }
417
418   /**
419    * @throws Exception
420    *
421    * @author Klaus Meffert
422    * @since 2.2
423    */

424   public void testSetOriginalRate_0_1()
425       throws Exception JavaDoc {
426     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
427     try {
428       selector.setOriginalRate( -0.1d);
429       fail();
430     }
431     catch (IllegalArgumentException JavaDoc iex) {
432       ; //this is OK
433
}
434   }
435
436   /**
437    * @throws Exception
438    *
439    * @author Klaus Meffert
440    * @since 2.2
441    */

442   public void testSetOriginalRate_1()
443       throws Exception JavaDoc {
444     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
445     selector.setOriginalRate(0.3d);
446     assertEquals(0.3d, selector.getOriginalRate(), DELTA);
447   }
448
449   /**
450    * @throws Exception
451    *
452    * @author Klaus Meffert
453    * @since 2.6
454    */

455   public void testReturnsUnique_0()
456       throws Exception JavaDoc {
457     BestChromosomesSelector selector = new BestChromosomesSelector(conf);
458     assertTrue(selector.returnsUniqueChromosomes());
459   }
460 }
461
Popular Tags