KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > audit > EvaluatorTest


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.audit;
11
12 import org.jgap.*;
13 import org.jgap.impl.*;
14 import junit.framework.*;
15
16 /**
17  * Tests the Evaluator class.
18  *
19  * @author Klaus Meffert
20  * @since 2.2
21  */

22 public class EvaluatorTest
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 void setUp() {
28     super.setUp();
29   }
30
31   public static Test suite() {
32     TestSuite suite = new TestSuite(EvaluatorTest.class);
33     return suite;
34   }
35
36   public void testStoreGenotype_0()
37       throws Exception JavaDoc {
38     Configuration conf = new ConfigurationForTest();
39     PermutingConfiguration pconf = new PermutingConfiguration(conf);
40     Evaluator eval = new Evaluator(pconf);
41     Population pop = new Population(conf);
42     Gene[] genes = new Gene[1];
43     Gene gene = new BooleanGene(conf);
44     genes[0] = gene;
45     Chromosome chrom = new Chromosome(conf, genes);
46     chrom.setFitnessValue(7.3d);
47     pop.addChromosome(chrom);
48     chrom = new Chromosome(conf, genes);
49     chrom.setFitnessValue(4.8d);
50     pop.addChromosome(chrom);
51     chrom = new Chromosome(conf, genes);
52     chrom.setFitnessValue(11.4d);
53     pop.addChromosome(chrom);
54     Genotype genotype = new Genotype(conf, pop);
55     eval.storeGenotype(0, 0, genotype);
56     Evaluator.GenotypeData genotypeData = eval.retrieveGenotype(0, 0);
57     assertEquals(genotype.getPopulation().size(), genotypeData.size);
58     assertEquals(genotype.getConfiguration().getGenerationNr(),
59                  genotypeData.generation);
60     Evaluator.ChromosomeData[] chromData = genotypeData.chromosomeData;
61     for (int i = 0; i < chromData.length; i++) {
62       assertEquals(genotype.getPopulation().getChromosome(i).getFitnessValue(),
63                    chromData[i].fitnessValue, DELTA);
64       assertEquals(genotype.getPopulation().getChromosome(i).size(),
65                    chromData[i].size);
66     }
67     assertEquals(7.3d,
68                  genotype.getPopulation().getChromosome(0).getFitnessValue(),
69                  DELTA);
70   }
71
72   public void testCalcPerformance_0()
73       throws Exception JavaDoc {
74     Configuration conf = new ConfigurationForTest();
75     PermutingConfiguration pconf = new PermutingConfiguration(conf);
76     Evaluator eval = new Evaluator(pconf);
77     Population pop = new Population(conf);
78     Gene[] genes = new Gene[1];
79     Gene gene = new BooleanGene(conf);
80     genes[0] = gene;
81     Chromosome chrom = new Chromosome(conf, genes);
82     chrom.setFitnessValue(7.3d);
83     pop.addChromosome(chrom);
84     chrom = new Chromosome(conf, genes);
85     chrom.setFitnessValue(4.8d);
86     pop.addChromosome(chrom);
87     chrom = new Chromosome(conf, genes);
88     chrom.setFitnessValue(11.4d);
89     pop.addChromosome(chrom);
90     Genotype genotype = new Genotype(conf, pop);
91     eval.storeGenotype(0, 0, genotype);
92     Evaluator.GenotypeDataAvg avg = eval.calcPerformance(0);
93     assertEquals(pop.determineFittestChromosome().getFitnessValue(),
94                  avg.bestFitnessValue, DELTA);
95     assertEquals(0, avg.bestFitnessValueGeneration);
96     assertEquals( (7.3 + 4.8 + 11.4) / 3, avg.avgFitnessValue, DELTA);
97     assertEquals( (Math.abs(4.8 - 7.3) / 2 + Math.abs(11.4 - 4.8) / 2) / 1,
98                  avg.avgDiversityFitnessValue, DELTA);
99     assertEquals(0.0d, avg.avgBestDeltaFitnessValue, DELTA); //because only 1 run
100
}
101
102   public void testCalcPerformance_1()
103       throws Exception JavaDoc {
104     Configuration conf = new ConfigurationForTest();
105     PermutingConfiguration pconf = new PermutingConfiguration(conf);
106     Evaluator eval = new Evaluator(pconf);
107     // run 0
108
Population pop = new Population(conf);
109     Gene[] genes = new Gene[1];
110     Gene gene = new BooleanGene(conf);
111     genes[0] = gene;
112     Chromosome chrom = new Chromosome(conf, genes);
113     chrom.setFitnessValue(7.3d);
114     pop.addChromosome(chrom);
115     chrom = new Chromosome(conf, genes);
116     chrom.setFitnessValue(4.8d);
117     pop.addChromosome(chrom);
118     chrom = new Chromosome(conf, genes);
119     chrom.setFitnessValue(11.4d);
120     pop.addChromosome(chrom);
121     Genotype genotype = new Genotype(conf, pop);
122     eval.storeGenotype(0, 0, genotype);
123     // run 1
124
pop = new Population(conf);
125     genes = new Gene[1];
126     gene = new BooleanGene(conf);
127     genes[0] = gene;
128     chrom = new Chromosome(conf, genes);
129     chrom.setFitnessValue(7);
130     pop.addChromosome(chrom);
131     chrom = new Chromosome(conf, genes);
132     chrom.setFitnessValue(17);
133     pop.addChromosome(chrom);
134     chrom = new Chromosome(conf, genes);
135     chrom.setFitnessValue(19);
136     pop.addChromosome(chrom);
137     genotype = new Genotype(conf, pop);
138     eval.storeGenotype(0, 1, genotype);
139     Evaluator.GenotypeDataAvg avg = eval.calcPerformance(0);
140     assertEquals(pop.determineFittestChromosome().getFitnessValue(),
141                  avg.bestFitnessValue, DELTA);
142     assertEquals(0, avg.bestFitnessValueGeneration);
143     assertEquals( ( (7.3 + 4.8 + 11.4) / 3) / 2
144                  + ( (7.0d + 17.0d + 19) / 3) / 2, avg.avgFitnessValue, DELTA);
145     assertEquals( (Math.abs(4.8 - 7.3) / 2 + Math.abs(11.4 - 4.8) / 2) / 2
146                  + (Math.abs(17.0 - 7) / 2 + Math.abs(19.0 - 17) / 2) / 2,
147                  avg.avgDiversityFitnessValue, DELTA);
148     assertEquals( (Math.abs(19 - 11.4d)) / 1, avg.avgBestDeltaFitnessValue,
149                  DELTA);
150     assertEquals( (double) 3 / 2 + (double) 3 / 2, avg.sizeAvg, DELTA);
151   }
152
153   public void testGetNumberOfRuns_0()
154       throws Exception JavaDoc {
155     Configuration conf = new ConfigurationForTest();
156     PermutingConfiguration pconf = new PermutingConfiguration(conf);
157     Evaluator eval = new Evaluator(pconf);
158     Population pop = new Population(conf);
159     Gene[] genes = new Gene[1];
160     Gene gene = new BooleanGene(conf);
161     genes[0] = gene;
162     Chromosome chrom = new Chromosome(conf, genes);
163     chrom.setFitnessValue(7.3d);
164     pop.addChromosome(chrom);
165     chrom = new Chromosome(conf, genes);
166     chrom.setFitnessValue(4.8d);
167     pop.addChromosome(chrom);
168     chrom = new Chromosome(conf, genes);
169     chrom.setFitnessValue(11.4d);
170     pop.addChromosome(chrom);
171     Genotype genotype = new Genotype(conf, pop);
172     eval.storeGenotype(0, 0, genotype);
173     assertEquals(1, eval.getNumberOfRuns(0));
174     assertEquals(0, eval.getNumberOfRuns(1));
175   }
176
177   /**
178    * @throws Exception
179    *
180    * @author Klaus Meffert
181    * @since 2.4
182    */

183   public void testCalcPerformance_2()
184       throws Exception JavaDoc {
185     Configuration conf = new ConfigurationForTest();
186     PermutingConfiguration pconf = new PermutingConfiguration(conf);
187     Evaluator eval = new Evaluator(pconf);
188     // run 0, permutation 0
189
Population pop00 = new Population(conf);
190     Gene[] genes = new Gene[1];
191     Gene gene = new BooleanGene(conf);
192     genes[0] = gene;
193     Chromosome chrom = new Chromosome(conf, genes);
194     chrom.setFitnessValue(7.3d);
195     pop00.addChromosome(chrom);
196     chrom = new Chromosome(conf, genes);
197     chrom.setFitnessValue(4.8d);
198     pop00.addChromosome(chrom);
199     chrom = new Chromosome(conf, genes);
200     chrom.setFitnessValue(11.4d);
201     pop00.addChromosome(chrom);
202     Genotype genotype = new Genotype(conf, pop00);
203     eval.storeGenotype(0, 0, genotype);
204     // run 1, permutation 0
205
Population pop10 = new Population(conf);
206     genes = new Gene[1];
207     gene = new BooleanGene(conf);
208     genes[0] = gene;
209     chrom = new Chromosome(conf, genes);
210     chrom.setFitnessValue(7);
211     pop10.addChromosome(chrom);
212     chrom = new Chromosome(conf, genes);
213     chrom.setFitnessValue(17);
214     pop10.addChromosome(chrom);
215     chrom = new Chromosome(conf, genes);
216     chrom.setFitnessValue(19);
217     pop10.addChromosome(chrom);
218     genotype = new Genotype(conf, pop10);
219     eval.storeGenotype(0, 1, genotype);
220     // run 0, permutation 1
221
Population pop01 = new Population(conf);
222     genes = new Gene[1];
223     gene = new BooleanGene(conf);
224     genes[0] = gene;
225     chrom = new Chromosome(conf, genes);
226     chrom.setFitnessValue(4);
227     pop01.addChromosome(chrom);
228     chrom = new Chromosome(conf, genes);
229     chrom.setFitnessValue(9);
230     pop01.addChromosome(chrom);
231     chrom = new Chromosome(conf, genes);
232     chrom.setFitnessValue(8);
233     pop01.addChromosome(chrom);
234     genotype = new Genotype(conf, pop01);
235     eval.storeGenotype(1, 0, genotype);
236     // run 1, permutation 1
237
Population pop11 = new Population(conf);
238     genes = new Gene[1];
239     gene = new BooleanGene(conf);
240     genes[0] = gene;
241     chrom = new Chromosome(conf, genes);
242     chrom.setFitnessValue(14);
243     pop11.addChromosome(chrom);
244     chrom = new Chromosome(conf, genes);
245     chrom.setFitnessValue(11);
246     pop11.addChromosome(chrom);
247     chrom = new Chromosome(conf, genes);
248     chrom.setFitnessValue(28);
249     pop11.addChromosome(chrom);
250     genotype = new Genotype(conf, pop11);
251     eval.storeGenotype(1, 1, genotype);
252     Evaluator.GenotypeDataAvg avg = eval.calcPerformance(0);
253     assertEquals(Math.max(pop00.determineFittestChromosome().getFitnessValue(),
254                           pop10.determineFittestChromosome().getFitnessValue()),
255                  avg.bestFitnessValue, DELTA);
256     assertEquals(0, avg.bestFitnessValueGeneration);
257     assertEquals( ( (7.3 + 4.8 + 11.4) / 3) / 2
258                  + ( (7.0d + 17.0d + 19) / 3) / 2, avg.avgFitnessValue, DELTA);
259     assertEquals( (Math.abs(4.8 - 7.3) / 2 + Math.abs(11.4 - 4.8) / 2) / 2
260                  + (Math.abs(17.0 - 7) / 2 + Math.abs(19.0 - 17) / 2) / 2,
261                  avg.avgDiversityFitnessValue, DELTA);
262     assertEquals( (Math.abs(19 - 11.4d)) / 1, avg.avgBestDeltaFitnessValue,
263                  DELTA);
264     assertEquals( (double) 3 / 2 + (double) 3 / 2, avg.sizeAvg, DELTA);
265     avg = eval.calcPerformance(1);
266     assertEquals(Math.max(pop01.determineFittestChromosome().getFitnessValue(),
267                           pop11.determineFittestChromosome().getFitnessValue()),
268                  avg.bestFitnessValue, DELTA);
269     assertEquals(0, avg.bestFitnessValueGeneration);
270     assertEquals( ( (4 + 9 + 8.0d) / 3) / 2
271                  + ( (14 + 11.0d + 28) / 3) / 2, avg.avgFitnessValue, DELTA);
272     assertEquals( (Math.abs(9.0d - 4) / 2 + Math.abs(8.0d - 9) / 2) / 2
273                  + (Math.abs(11.0d - 14) / 2 + Math.abs(28.0d - 11) / 2) / 2,
274                  avg.avgDiversityFitnessValue, DELTA);
275     assertEquals( (Math.abs(28 - 9)) / (double) 1, avg.avgBestDeltaFitnessValue,
276                  DELTA);
277     assertEquals( (double) 3 / 2 + (double) 3 / 2, avg.sizeAvg, DELTA);
278   }
279
280   /**
281    * @throws Exception
282    *
283    * @author Klaus Meffert
284    * @since 2.6
285    */

286   public void testConstruct_0()
287       throws Exception JavaDoc {
288     try {
289       Evaluator eval = new Evaluator(null);
290       fail();
291     } catch (IllegalArgumentException JavaDoc iex) {
292       ; //this is OK
293
}
294   }
295
296   /**
297    * @throws Exception
298    *
299    * @author Klaus Meffert
300    * @since 3.0
301    */

302   public void testConstruct_1()
303       throws Exception JavaDoc {
304     Configuration conf = new ConfigurationForTest();
305     PermutingConfiguration pconf = new PermutingConfiguration(conf);
306     Evaluator eval = new Evaluator(pconf);
307     assertEquals(0, eval.getData().getRowCount());
308     assertEquals(0, eval.getData().getColumnCount());
309   }
310
311   /**
312    * @throws Exception
313    *
314    * @author Klaus Meffert
315    * @since 2.6
316    */

317   public void testHasNext_0()
318       throws Exception JavaDoc {
319     Configuration conf = new ConfigurationForTest();
320     PermutingConfiguration pconf = new PermutingConfiguration(conf);
321     Evaluator eval = new Evaluator(pconf);
322     assertEquals(pconf.hasNext(), eval.hasNext());
323   }
324
325   /**
326    * @throws Exception
327    *
328    * @author Klaus Meffert
329    * @since 3.0
330    */

331   public void testNext_0()
332       throws Exception JavaDoc {
333     Configuration conf = new ConfigurationForTest();
334     PermutingConfiguration pconf = new PermutingConfiguration(conf);
335     pconf.addGeneticOperatorSlot(new MutationOperator(conf));
336     pconf.addRandomGeneratorSlot(new StockRandomGenerator());
337     pconf.addFitnessFunctionSlot(new TestFitnessFunction());
338     pconf.addNaturalSelectorSlot(new BestChromosomesSelector(conf));
339     Evaluator eval = new Evaluator(pconf);
340     assertTrue(eval.hasNext());
341     Configuration config = eval.next();
342     assertNotNull(config);
343   }
344
345   /**
346    * @throws Exception
347    *
348    * @author Klaus Meffert
349    * @since 3.0
350    */

351   public void testSetValue_0()
352       throws Exception JavaDoc {
353     Configuration conf = new ConfigurationForTest();
354     PermutingConfiguration pconf = new PermutingConfiguration(conf);
355     Evaluator eval = new Evaluator(pconf);
356     Comparable JavaDoc rowKey = new Integer JavaDoc(4);
357     Comparable JavaDoc colKey = new Integer JavaDoc(6);
358     double value = 2.3d;
359     eval.setValue(value, rowKey, colKey);
360     assertEquals(value, eval.getValue(rowKey, colKey).doubleValue(), DELTA);
361     assertNull(eval.getValue(rowKey, rowKey));
362   }
363
364   /**
365    * @throws Exception
366    *
367    * @author Klaus Meffert
368    * @since 3.0
369    */

370   public void testSetValue_1()
371       throws Exception JavaDoc {
372     Configuration conf = new ConfigurationForTest();
373     PermutingConfiguration pconf = new PermutingConfiguration(conf);
374     Evaluator eval = new Evaluator(pconf);
375     Comparable JavaDoc rowKey = new Integer JavaDoc(4);
376     Comparable JavaDoc colKey = new Integer JavaDoc(6);
377     double value = 2.3d;
378     eval.setValue(1, 2, value, rowKey, colKey);
379     assertEquals(value, eval.getValue(1, 2, rowKey, colKey).doubleValue(),
380                  DELTA);
381     assertNull(eval.getValue(1, 1, rowKey, colKey));
382   }
383
384   /**
385    * @throws Exception
386    *
387    * @author Klaus Meffert
388    * @since 3.0
389    */

390   public void testSetValue_2()
391       throws Exception JavaDoc {
392     Configuration conf = new ConfigurationForTest();
393     PermutingConfiguration pconf = new PermutingConfiguration(conf);
394     Evaluator eval = new Evaluator(pconf);
395     Comparable JavaDoc rowKey = new Integer JavaDoc(4);
396     Comparable JavaDoc colKey = new Integer JavaDoc(6);
397     double value = 2.3d;
398     eval.setValue(1, 2, value, rowKey, colKey);
399     double value2 = 4.8d;
400     eval.setValue(2, 2, value2, rowKey, colKey);
401     assertEquals(value, eval.getValue(1, 2, rowKey, colKey).doubleValue(),
402                  DELTA);
403     assertEquals(value2, eval.getValue(2, 2, rowKey, colKey).doubleValue(),
404                  DELTA);
405   }
406
407   /**
408    * Test for overwriting an already set value.
409    * @throws Exception
410    *
411    * @author Klaus Meffert
412    * @since 3.0
413    */

414   public void testSetValue_3()
415       throws Exception JavaDoc {
416     Configuration conf = new ConfigurationForTest();
417     PermutingConfiguration pconf = new PermutingConfiguration(conf);
418     Evaluator eval = new Evaluator(pconf);
419     Comparable JavaDoc rowKey = new Integer JavaDoc(4);
420     Comparable JavaDoc colKey = new Integer JavaDoc(6);
421     double value = 2.3d;
422     eval.setValue(1, 2, value, rowKey, colKey);
423     double value2 = 4.8d;
424     eval.setValue(1, 2, value2, rowKey, colKey);
425     assertEquals(value2, eval.getValue(1, 2, rowKey, colKey).doubleValue(),
426                  DELTA);
427   }
428
429   /**
430    * Consider one permutation.
431    * @throws Exception
432    *
433    * @author Klaus Meffert
434    * @since 3.1
435    */

436   public void testCalcAvgFitness_0()
437       throws Exception JavaDoc {
438     Configuration conf = new ConfigurationForTest();
439     PermutingConfiguration pconf = new PermutingConfiguration(conf);
440     Evaluator eval = new Evaluator(pconf);
441     Comparable JavaDoc rowKey1 = new Integer JavaDoc(3);
442     Comparable JavaDoc rowKey2 = new Integer JavaDoc(4);
443     Comparable JavaDoc colKey = new Integer JavaDoc(6);
444     // Run 1
445
double value1 = 2.3d;
446     eval.setValue(1, 1, value1, rowKey1, colKey);
447     // Run 2
448
double value2 = 4.8d;
449     eval.setValue(1, 2, value2, rowKey2, colKey);
450     KeyedValues2D fitnessvals = eval.calcAvgFitness(1);
451     assertEquals(2, fitnessvals.getRowCount());
452     assertEquals(1, fitnessvals.getColumnCount());
453     Number JavaDoc val = fitnessvals.getValue(rowKey1, colKey);
454     assertEquals(value1 / 2, val.doubleValue(), DELTA);
455     val = fitnessvals.getValue(rowKey2, colKey);
456     assertEquals(value2 / 2, val.doubleValue(), DELTA);
457   }
458
459   /**
460    * Consider all permutations.
461    * @throws Exception
462    *
463    * @author Klaus Meffert
464    * @since 3.1
465    */

466   public void testCalcAvgFitness_1()
467       throws Exception JavaDoc {
468     Configuration conf = new ConfigurationForTest();
469     PermutingConfiguration pconf = new PermutingConfiguration(conf);
470     Evaluator eval = new Evaluator(pconf);
471     Comparable JavaDoc rowKey1 = new Integer JavaDoc(3);
472     Comparable JavaDoc rowKey2 = new Integer JavaDoc(4);
473     Comparable JavaDoc colKey = new Integer JavaDoc(6);
474     // Perm. 1, Run 1
475
double value1 = 2.3d;
476     eval.setValue(1, 1, value1, rowKey1, colKey);
477     // Perm. 1, Run 2
478
double value2 = 4.8d;
479     eval.setValue(1, 2, value2, rowKey2, colKey);
480     // Perm. 2, Run 1
481
double value3 = 11.5d;
482     eval.setValue(2, 1, value3, rowKey1, colKey);
483     // Perm. 2, Run 2
484
double value4 = 8.0d;
485     eval.setValue(2, 2, value4, rowKey2, colKey);
486     KeyedValues2D fitnessvals = eval.calcAvgFitness( -1);
487     assertEquals(2, fitnessvals.getRowCount());
488     assertEquals(1, fitnessvals.getColumnCount());
489     Number JavaDoc val = fitnessvals.getValue(rowKey1, colKey);
490     assertEquals(value3 / 2 + value1 / 2, val.doubleValue(), DELTA);
491     val = fitnessvals.getValue(rowKey2, colKey);
492     assertEquals(value2 / 2 + value4 / 2, val.doubleValue(), DELTA);
493   }
494 }
495
Popular Tags