KickJava   Java API By Example, From Geeks To Geeks.

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


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 import junitx.util.*;
16
17 /**
18  * Tests the ThresholdSelector class.
19  *
20  * @author Klaus Meffert
21  * @since 2.0
22  */

23 public class ThresholdSelectorTest
24     extends JGAPTestCase {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.16 $";
27
28   public static Test suite() {
29     TestSuite suite = new TestSuite(ThresholdSelectorTest.class);
30     return suite;
31   }
32
33   public void testConstruct_0()
34       throws Exception JavaDoc {
35     try {
36       new ThresholdSelector(null, 1.1d);
37       fail();
38     }
39     catch (IllegalArgumentException JavaDoc ex) {
40       ; //this is OK
41
}
42   }
43
44   public void testConstruct_1()
45       throws Exception JavaDoc {
46     try {
47       new ThresholdSelector(null, -0.5d);
48       fail();
49     }
50     catch (IllegalArgumentException JavaDoc ex) {
51       ; //this is OK
52
}
53   }
54
55   public void testConstruct_2()
56       throws Exception JavaDoc {
57     ThresholdSelector selector = new ThresholdSelector(null, 0.5d);
58     Double JavaDoc m_bestChroms_Percentage = (Double JavaDoc) getNestedField(selector,
59         "m_config", "m_bestChroms_Percentage");
60     assertEquals(0.5d, m_bestChroms_Percentage.doubleValue(), DELTA);
61     assertFalse(selector.returnsUniqueChromosomes());
62     Object JavaDoc m_fitnessValueComparator = privateAccessor.getField(selector,
63         "m_fitnessValueComparator");
64     assertTrue(m_fitnessValueComparator != null);
65   }
66
67   /**
68    * @throws Exception
69    *
70    * @author Klaus Meffert
71    * @since 3.1
72    */

73   public void testConstruct_3()
74       throws Exception JavaDoc {
75     Genotype.setStaticConfiguration(conf);
76     ThresholdSelector op = new ThresholdSelector();
77     assertSame(conf, op.getConfiguration());
78   }
79
80   /**
81    * @throws Exception
82    *
83    * @author Klaus Meffert
84    * @since 2.1
85    */

86   public void testAdd_0()
87       throws Exception JavaDoc {
88     ThresholdSelector selector = new ThresholdSelector(conf, 0.5d);
89     Gene gene = new BooleanGene(conf);
90     Chromosome chrom = new Chromosome(conf, gene, 5);
91     selector.add(chrom);
92     List chromosomes = ( (Vector) PrivateAccessor.getField(selector,
93         "m_chromosomes"));
94     assertEquals(1, chromosomes.size());
95     assertEquals(chrom, chromosomes.get(0));
96     selector.add(chrom);
97     assertEquals(chrom, chromosomes.get(0));
98     assertEquals(2, chromosomes.size());
99     selector.add(chrom);
100     assertEquals(3, chromosomes.size());
101   }
102
103   /**
104    * Test if below functionality available without error.
105    * @throws Exception
106    *
107    * @author Klaus Meffert
108    * @since 2.1
109    */

110   public void testSelect_0()
111       throws Exception JavaDoc {
112     ThresholdSelector selector = new ThresholdSelector(conf, 0.3d);
113     Gene gene = new IntegerGene(conf);
114     gene.setAllele(new Integer JavaDoc(444));
115     Chromosome secondBestChrom = new Chromosome(conf, gene, 3);
116     secondBestChrom.setFitnessValue(11);
117     selector.add(secondBestChrom);
118     gene = new BooleanGene(conf);
119     gene.setAllele(Boolean.valueOf(false));
120     Chromosome bestChrom = new Chromosome(conf, gene, 3);
121     bestChrom.setFitnessValue(12);
122     selector.add(bestChrom);
123     selector.select(1, null, new Population(conf));
124   }
125
126   /**
127    * @throws Exception
128    *
129    * @author Klaus Meffert
130    * @since 2.1
131    */

132   public void testSelect_1()
133       throws Exception JavaDoc {
134     ThresholdSelector selector = new ThresholdSelector(conf, 1.0d);
135     // add first chromosome
136
// --------------------
137
Gene gene = new BooleanGene(conf);
138     gene.setAllele(Boolean.valueOf(true));
139     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
140     thirdBestChrom.setFitnessValue(10);
141     selector.add(thirdBestChrom);
142     // add second chromosome
143
// ---------------------
144
gene = new BooleanGene(conf);
145     gene.setAllele(Boolean.valueOf(false));
146     Chromosome bestChrom = new Chromosome(conf, gene, 3);
147     bestChrom.setFitnessValue(12);
148     selector.add(bestChrom);
149     // add third chromosome
150
// ---------------------
151
gene = new IntegerGene(conf);
152     gene.setAllele(new Integer JavaDoc(444));
153     Chromosome secondBestChrom = new Chromosome(conf, gene, 3);
154     secondBestChrom.setFitnessValue(11);
155     selector.add(secondBestChrom);
156     // receive top 1 (= best) chromosome
157
// ---------------------------------
158
Population pop = new Population(conf);
159     selector.select(1, null, pop);
160     IChromosome[] bestChroms = pop.toChromosomes();
161     assertEquals(1, bestChroms.length);
162     assertEquals(bestChrom, bestChroms[0]);
163     // receive top 3 chromosomes
164
// -------------------------
165
pop.getChromosomes().clear();
166     selector.select(3, null, pop);
167     bestChroms = pop.toChromosomes();
168     assertEquals(3, bestChroms.length);
169     assertEquals(bestChrom, bestChroms[0]);
170     assertEquals(secondBestChrom, bestChroms[1]);
171     assertEquals(thirdBestChrom, bestChroms[2]);
172   }
173
174   /**
175    * Always select best chromosome if threshold is 1.0d.
176    * @throws Exception
177    *
178    * @author Klaus Meffert
179    * @since 2.1
180    */

181   public void testSelect_2()
182       throws Exception JavaDoc {
183     ThresholdSelector selector = new ThresholdSelector(conf, 1.0d);
184     // add first chromosome
185
// --------------------
186
Gene gene = new BooleanGene(conf);
187     gene.setAllele(Boolean.valueOf(true));
188     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
189     thirdBestChrom.setFitnessValue(10);
190     selector.add(thirdBestChrom);
191     // add second chromosome
192
// ---------------------
193
gene = new BooleanGene(conf);
194     gene.setAllele(Boolean.valueOf(false));
195     Chromosome bestChrom = new Chromosome(conf, gene, 3);
196     bestChrom.setFitnessValue(12);
197     selector.add(bestChrom);
198     // receive top 1 (= best) chromosome
199
// ---------------------------------
200
Population pop = new Population(conf);
201     selector.select(1, null, pop);
202     IChromosome[] bestChroms = pop.toChromosomes();
203     assertEquals(1, bestChroms.length);
204     assertEquals(bestChrom, bestChroms[0]);
205     // receive top 30 chromosomes.
206
// ---------------------------
207
pop.getChromosomes().clear();
208     selector.select(30, null, pop);
209     bestChroms = pop.toChromosomes();
210     assertEquals(30, bestChroms.length);
211     assertEquals(bestChrom, bestChroms[0]);
212     assertEquals(thirdBestChrom, bestChroms[1]);
213     assertTrue(bestChrom == bestChroms[0]);
214   }
215
216   /**
217    * Never select best chromosome for granted if threshold is 0.0d.
218    * @throws Exception
219    *
220    * @author Klaus Meffert
221    * @since 2.1
222    */

223   public void testSelect_3()
224       throws Exception JavaDoc {
225     //Set index of chromosome to be selected by ThresholdSelector to 1.
226
//1 because the best chromosome will be index 0 and the other one has
227
// index 1.
228
conf.setRandomGenerator(new RandomGeneratorForTest(1));
229     ThresholdSelector selector = new ThresholdSelector(conf, 0.0d);
230     // add first chromosome
231
// --------------------
232
Gene gene = new BooleanGene(conf);
233     gene.setAllele(Boolean.valueOf(true));
234     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
235     thirdBestChrom.setFitnessValue(10);
236     selector.add(thirdBestChrom);
237     // add second chromosome
238
// ---------------------
239
gene = new BooleanGene(conf);
240     gene.setAllele(Boolean.valueOf(false));
241     Chromosome bestChrom = new Chromosome(conf, gene, 3);
242     bestChrom.setFitnessValue(12);
243     selector.add(bestChrom);
244     // receive top 1 (= best) chromosome
245
// ---------------------------------
246
Population pop = new Population(conf);
247     selector.select(1, null, pop);
248     IChromosome[] bestChroms = pop.toChromosomes();
249     assertFalse(bestChroms[0].equals(bestChrom));
250   }
251
252   /**
253    * Ensure that selected Chromosome's are not equal to added Chromosome's.
254    * @throws Exception
255    *
256    * @author Klaus Meffert
257    * @since 2.1
258    */

259   public void testSelect_4()
260       throws Exception JavaDoc {
261     ThresholdSelector selector = new ThresholdSelector(conf, 1.0d);
262     // add first chromosome
263
// --------------------
264
Gene gene = new BooleanGene(conf);
265     gene.setAllele(Boolean.valueOf(true));
266     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
267     thirdBestChrom.setFitnessValue(10);
268     selector.add(thirdBestChrom);
269     // add second chromosome
270
// ---------------------
271
gene = new BooleanGene(conf);
272     gene.setAllele(Boolean.valueOf(false));
273     Chromosome bestChrom = new Chromosome(conf, gene, 3);
274     bestChrom.setFitnessValue(12);
275     selector.add(bestChrom);
276     // receive top 30 chromosomes.
277
// ---------------------------
278
Population pop = new Population(conf);
279     selector.select(30, null, pop);
280     Population bestChroms = pop;
281     List chromosomes = (Vector) PrivateAccessor.getField(selector,
282         "m_chromosomes");
283     assertFalse(bestChroms.equals(chromosomes));
284   }
285
286   /**
287    * Always select best chromosome if threshold is 1.0d. Target population not
288    * empty.
289    * @throws Exception
290    *
291    * @author Klaus Meffert
292    * @since 2.6
293    */

294   public void testSelect_5()
295       throws Exception JavaDoc {
296     ThresholdSelector selector = new ThresholdSelector(conf, 1.0d);
297     // add first chromosome
298
// --------------------
299
Gene gene = new BooleanGene(conf);
300     gene.setAllele(Boolean.valueOf(true));
301     Chromosome thirdBestChrom = new Chromosome(conf, gene, 7);
302     thirdBestChrom.setFitnessValue(10);
303     selector.add(thirdBestChrom);
304     // add second chromosome
305
// ---------------------
306
gene = new BooleanGene(conf);
307     gene.setAllele(Boolean.valueOf(false));
308     Chromosome bestChrom = new Chromosome(conf, gene, 3);
309     bestChrom.setFitnessValue(12);
310     selector.add(bestChrom);
311     // receive top 1 (= best) chromosome
312
// ---------------------------------
313
Population pop = new Population(conf);
314     selector.select(1, null, pop);
315     IChromosome[] bestChroms = pop.toChromosomes();
316     assertEquals(1, bestChroms.length);
317     assertEquals(bestChrom, bestChroms[0]);
318     // receive top 30 chromosomes.
319
// ---------------------------
320
selector.select(30, pop, pop);
321     bestChroms = pop.toChromosomes();
322     assertEquals(31, bestChroms.length);
323     assertEquals(bestChrom, bestChroms[0]);
324     assertEquals(thirdBestChrom, bestChroms[3]);
325     assertTrue(bestChrom == bestChroms[0]);
326   }
327
328   /**
329    * @throws Exception
330    *
331    * @author Klaus Meffert
332    * @since 2.1
333    */

334   public void testEmpty_0()
335       throws Exception JavaDoc {
336     ThresholdSelector selector = new ThresholdSelector(conf, 1.0d);
337     Gene gene = new BooleanGene(conf);
338     Chromosome chrom = new Chromosome(conf, gene, 5);
339     selector.add(chrom);
340     selector.empty();
341     Boolean JavaDoc needsSorting = (Boolean JavaDoc) PrivateAccessor.getField(selector,
342         "m_needsSorting");
343     assertEquals(Boolean.FALSE, needsSorting);
344     List chromosomes = ( (Vector) PrivateAccessor.getField(selector,
345         "m_chromosomes"));
346     assertEquals(0, chromosomes.size());
347   }
348
349   /**
350    * Test if method clear() does not affect original Population.
351    * @throws Exception
352    *
353    * @author Klaus Meffert
354    * @since 2.1
355    */

356   public void testEmpty_1()
357       throws Exception JavaDoc {
358     ThresholdSelector selector = new ThresholdSelector(conf, 1.0d);
359     Gene gene = new BooleanGene(conf);
360     Chromosome chrom = new Chromosome(conf, gene, 5);
361     Population pop = new Population(conf, 1);
362     pop.addChromosome(chrom);
363     selector.add(chrom);
364     Population popNew = new Population(conf);
365     selector.select(1, null, popNew);
366     selector.empty();
367     assertEquals(1, popNew.size());
368     assertNotNull(popNew.getChromosome(0));
369   }
370
371   /**
372    * Test if method clear() does not affect return value.
373    * @throws Exception
374    *
375    * @author Klaus Meffert
376    * @since 2.1
377    */

378   public void testEmpty_2()
379       throws Exception JavaDoc {
380     ThresholdSelector selector = new ThresholdSelector(conf, 1.0d);
381     Gene gene = new BooleanGene(conf);
382     Chromosome chrom = new Chromosome(conf, gene, 5);
383     Population pop = new Population(conf, 1);
384     pop.addChromosome(chrom);
385     selector.add(chrom);
386     Population popNew = new Population(conf);
387     selector.select(1, null, popNew);
388     pop = popNew;
389     selector.empty();
390     assertEquals(1, pop.size());
391     assertNotNull(pop.getChromosome(0));
392   }
393 }
394
Popular Tags