KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > PopulationTest


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;
11
12 import java.util.*;
13 import org.jgap.impl.*;
14 import junit.framework.*;
15
16 /**
17  * Tests the Population class.
18  *
19  * @author Klaus Meffert
20  * @author Chris Knowles
21  * @since 2.0
22  */

23 public class PopulationTest
24     extends JGAPTestCase {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.37 $";
27
28   public static Test suite() {
29     TestSuite suite = new TestSuite(PopulationTest.class);
30     return suite;
31   }
32
33   public void testConstruct_0()
34       throws Exception JavaDoc {
35     try {
36       new Population(new ConfigurationForTest(), (IChromosome[])null);
37       fail();
38     } catch (NullPointerException JavaDoc e) {
39       ; //this is OK
40
}
41   }
42
43   public void testConstruct_1()
44       throws Exception JavaDoc {
45     try {
46       new Population(conf, -1);
47     } catch (IllegalArgumentException JavaDoc iae) {
48       ; //this is ok
49
}
50   }
51
52   public void testConstruct_2()
53       throws Exception JavaDoc {
54     try {
55       new Population(new ConfigurationForTest(), (IChromosome)null);
56       fail();
57     } catch (IllegalArgumentException JavaDoc e) {
58       ; //this is OK
59
}
60   }
61
62   public void testConstruct_3()
63       throws Exception JavaDoc {
64     int nTot = 100;
65     Chromosome[] chromosomes = new Chromosome[nTot];
66     Population pop = new Population(conf, chromosomes);
67     assertNotNull(pop);
68     //check size is correct
69
assertEquals(nTot, pop.size());
70   }
71
72   public void testAddChromosome_0()
73       throws Exception JavaDoc {
74     Gene g = new IntegerGene(conf);
75     Chromosome c = new Chromosome(conf, g, 29);
76     c.setFitnessValue(45);
77     Population p = new Population(conf);
78     p.addChromosome(c);
79     assertEquals(1, p.size());
80   }
81
82   public void testAddChromosome_1()
83       throws Exception JavaDoc {
84     Population p = new Population(conf);
85     p.addChromosome(null);
86     assertEquals(0, p.size());
87   }
88
89   public void testAddChromosomes_0()
90       throws Exception JavaDoc {
91     Gene g = new DoubleGene(conf);
92     Chromosome c = new Chromosome(conf, g, 10);
93     c.setFitnessValue(45);
94     Population p1 = new Population(conf);
95     p1.addChromosome(c);
96     Population p2 = new Population(conf);
97     p2.addChromosomes(p1);
98     assertEquals(p1.size(), p2.size());
99   }
100
101   public void testAddChromosomes_1()
102       throws Exception JavaDoc {
103     Population p = new Population(conf);
104     p.addChromosomes(null);
105     assertEquals(0, p.size());
106   }
107
108   public void testSetChromosome_0()
109       throws Exception JavaDoc {
110     Gene g = new DoubleGene(conf);
111     Chromosome c = new Chromosome(conf, g, 10);
112     Population p = new Population(conf);
113     p.setChromosome(0, c);
114     p.setChromosome(0, c);
115     assertEquals(1, p.size());
116     try {
117       p.setChromosome(2, c);
118       fail();
119     } catch (IndexOutOfBoundsException JavaDoc oex) {
120       ; //this is OK
121
}
122   }
123
124   public void testSetChromosome_1()
125       throws Exception JavaDoc {
126     Gene g = new DoubleGene(conf);
127     Chromosome c = new Chromosome(conf, g, 10);
128     Population p = new Population(conf);
129     p.addChromosome(c);
130     Chromosome c2 = new Chromosome(conf, g, 20);
131     p.setChromosome(0, c2);
132     assertEquals(1, p.size());
133     assertEquals(p.getChromosome(0), c2);
134     assertFalse(c.equals(c2));
135   }
136
137   public void testSetChromosomes_0()
138       throws Exception JavaDoc {
139     List chromosomes = new ArrayList();
140     Gene g = null;
141     Chromosome c = null;
142     int nTot = 200;
143     for (int i = 0; i < nTot; i++) {
144       g = new DoubleGene(conf);
145       c = new Chromosome(conf, g, 10);
146       chromosomes.add(c);
147     }
148     Population p = new Population(conf);
149     p.setChromosomes(chromosomes);
150     assertEquals(nTot, p.size());
151   }
152
153   public void testGetChromosomes_0()
154       throws Exception JavaDoc {
155     List chromosomes = new ArrayList();
156     Gene g = null;
157     Chromosome c = null;
158     int nTot = 200;
159     for (int i = 0; i < nTot; i++) {
160       g = new DoubleGene(conf);
161       c = new Chromosome(conf, g, 10);
162       chromosomes.add(c);
163     }
164     Population p = new Population(conf);
165     p.setChromosomes(chromosomes);
166     assertEquals(chromosomes, p.getChromosomes());
167   }
168
169   public void testGetChromosome_0()
170       throws Exception JavaDoc {
171     List chromosomes = new ArrayList();
172     Gene g = null;
173     Chromosome c = null;
174     Chromosome thechosenone = null;
175     int nTot = 200;
176     for (int i = 0; i < nTot; i++) {
177       g = new DoubleGene(conf);
178       c = new Chromosome(conf, g, 10);
179       chromosomes.add(c);
180       if (i == 100) {
181         thechosenone = c;
182       }
183     }
184     Population p = new Population(conf);
185     p.setChromosomes(chromosomes);
186     assertEquals(thechosenone, p.getChromosome(100));
187   }
188
189   public void testToChromosomes_0()
190       throws Exception JavaDoc {
191     List chromosomes = new ArrayList();
192     Gene g = null;
193     Chromosome c = null;
194     int nTot = 200;
195     for (int i = 0; i < nTot; i++) {
196       g = new DoubleGene(conf);
197       c = new Chromosome(conf, g, 10);
198       c.getGene(0).setAllele(new Double JavaDoc(i));
199       chromosomes.add(c);
200     }
201     Population p = new Population(conf);
202     p.setChromosomes(chromosomes);
203     IChromosome[] aChrom = p.toChromosomes();
204     assertEquals(aChrom.length, chromosomes.size());
205     // compare populations with unsorted list of chromosomes
206
Population newPop = new Population(conf, aChrom);
207     assertEquals(p, newPop);
208     assertEquals(newPop, p);
209     // compare list of chromosomes
210
Collections.sort(chromosomes);
211     List toChromosomes = Arrays.asList(aChrom);
212     Collections.sort(toChromosomes);
213     assertEquals(chromosomes, toChromosomes);
214     // compare populations with sorted list of chromosomes
215
Population newPop2 = new Population(conf);
216     newPop2.setChromosomes(toChromosomes);
217     assertEquals(p, newPop2);
218     assertEquals(newPop, newPop2);
219     assertEquals(newPop2, p);
220     assertEquals(newPop2, newPop);
221   }
222
223   /**
224    * Empty population
225    * @throws Exception
226    *
227    * @author Klaus Meffert
228    * @since 2.4
229    */

230   public void testDetermineFittestChromosome_0()
231       throws Exception JavaDoc {
232     Population p = new Population(conf);
233     assertEquals(null, p.determineFittestChromosome());
234   }
235
236   /**
237    * Unordered list of fitness values of chroms in population.
238    * @throws Exception
239    *
240    * @author Klaus Meffert
241    * @since 2.4
242    */

243   public void testDetermineFittestChromosome_1()
244       throws Exception JavaDoc {
245     List chromosomes = new ArrayList();
246     Gene g = null;
247     Chromosome c = null;
248     Population p = new Population(conf);
249     g = new DoubleGene(conf);
250     c = new Chromosome(conf, g, 10);
251     c.setFitnessValue(5);
252     p.addChromosome(c);
253     chromosomes.add(c);
254     c = new Chromosome(conf, g, 3);
255     c.setFitnessValue(19);
256     p.addChromosome(c);
257     chromosomes.add(c);
258     c = new Chromosome(conf, g, 1);
259     c.setFitnessValue(11);
260     p.addChromosome(c);
261     chromosomes.add(c);
262     c = new Chromosome(conf, g, 8);
263     c.setFitnessValue(18);
264     p.addChromosome(c);
265     chromosomes.add(c);
266     Chromosome fittest = (Chromosome) chromosomes.get(1);
267     p.setChromosomes(chromosomes);
268     assertEquals(fittest, p.determineFittestChromosome());
269   }
270
271   /**
272    * Ordered list of fitness values of chroms in population.
273    * @throws Exception
274    */

275   public void testDetermineFittestChromosome_2()
276       throws Exception JavaDoc {
277     List chromosomes = new ArrayList();
278     Gene g = null;
279     Chromosome c = null;
280     Population p = new Population(conf);
281     int nTot = 100;
282     for (int i = 0; i < nTot; i++) {
283       g = new DoubleGene(conf);
284       c = new Chromosome(conf, g, 10);
285       c.setFitnessValue(i);
286       p.addChromosome(c);
287       chromosomes.add(c);
288     }
289     Chromosome fittest = (Chromosome) chromosomes.get(99);
290     p.setChromosomes(chromosomes);
291     assertEquals(fittest, p.determineFittestChromosome());
292   }
293
294   /**
295    * Ordered list of fitness values of chroms in population. Use fitness
296    * evaluator different from standard one.
297    * @throws Exception
298    *
299    * @author Klaus Meffert
300    * @since 2.4
301    */

302   public void testDetermineFittestChromosome_3()
303       throws Exception JavaDoc {
304     conf.resetProperty(Configuration.PROPERTY_FITEVAL_INST);
305     conf.setFitnessEvaluator(new DeltaFitnessEvaluator());
306     List chromosomes = new ArrayList();
307     Gene g = null;
308     Chromosome c = null;
309     Population p = new Population(conf);
310     int nTot = 100;
311     for (int i = 0; i < nTot; i++) {
312       g = new DoubleGene(conf);
313       c = new Chromosome(conf, g, 10);
314       c.setFitnessValue(i);
315       p.addChromosome(c);
316       chromosomes.add(c);
317     }
318     Chromosome fittest = (Chromosome) chromosomes.get(0);
319     p.setChromosomes(chromosomes);
320     assertEquals(fittest, p.determineFittestChromosome());
321   }
322
323   /**
324    * @throws Exception
325    *
326    * @author Klaus Meffert
327    * @since 2.6
328    */

329   public void testDetermineFittestChromosome_4()
330       throws Exception JavaDoc {
331     Population p = new Population(conf);
332     Gene g = new DoubleGene(conf);
333     Chromosome c = new Chromosome(conf, g, 10);
334     c.setFitnessValue(22);
335     p.addChromosome(c);
336     assertEquals(null, p.determineFittestChromosomes(0));
337     assertEquals(c, p.determineFittestChromosomes(1).get(0));
338   }
339
340   /**
341    * Special case exposing a previous bug in method under test.
342    * @throws Exception
343    *
344    * @author Klaus Meffert
345    * @since 3.1
346    */

347   public void testDetermineFittestChromosome_5()
348       throws Exception JavaDoc {
349     List chromosomes = new ArrayList();
350     Gene g = null;
351     Chromosome c = null;
352     Population p = new Population(conf);
353     conf.reset();
354     conf.setFitnessEvaluator(new MyFitnessEvaluator());
355     g = new DoubleGene(conf);
356     c = new Chromosome(conf, g, 10);
357     c.setFitnessValue(5);
358     chromosomes.add(c);
359     g = new DoubleGene(conf);
360     c = new Chromosome(conf, g, 10);
361     c.setFitnessValue(3);
362     chromosomes.add(c);
363     p.setChromosomes(chromosomes);
364     assertEquals(c, p.determineFittestChromosome());
365     // next is important to come into a dangerous situation.
366
c = new Chromosome(conf, g, 10);
367     c.setFitnessValue(1); //the fittest!
368
p.addChromosome(c);
369     assertEquals(c, p.determineFittestChromosome());
370   }
371
372   /**
373    * @throws Exception
374    *
375    * @author Dan Clark,Klaus Meffert
376    * @since 2.6
377    */

378   public void testDetermineFittestChromosomes_1()
379       throws Exception JavaDoc {
380     Population p = getNewPopulation(conf);
381     assertEquals(1, p.determineFittestChromosomes(1).size());
382     assertEquals(1, p.determineFittestChromosomes(1).size());
383     assertEquals(3, p.determineFittestChromosomes(3).size());
384     assertEquals(3, p.determineFittestChromosomes(3).size());
385     // Expect result list with 5 entries (not 6) as population consists
386
// of 5 entries. Even if more are requested, the maximum returned is 5
387
// in this case.
388
assertEquals(5, p.determineFittestChromosomes(6).size());
389   }
390
391   /**
392    * Exposes bug 1422962.
393    * @throws Exception
394    *
395    * @author Dan Clark, Klaus Meffert
396    * @since 2.6
397    */

398   public void testDetermineFittestChromosomes_2()
399       throws Exception JavaDoc {
400     Population population = getNewPopulation(conf);
401     IChromosome topC = population.determineFittestChromosome();
402     List top = population.determineFittestChromosomes(1);
403     assertEquals(topC, population.determineFittestChromosome());
404     assertEquals(top.get(0), population.determineFittestChromosome());
405     top = population.determineFittestChromosomes(2);
406     assertEquals(top.get(0), population.determineFittestChromosome());
407     top = population.determineFittestChromosomes(3);
408     assertEquals(top.get(0), population.determineFittestChromosome());
409   }
410
411   /**
412    * Exposes bug 1422962.
413    * @throws Exception
414    *
415    * @author Dan Clark, Klaus Meffert
416    * @since 2.6
417    */

418   public void testDetermineFittestChromosomes_3()
419       throws Exception JavaDoc {
420     Population population = getNewPopulation(conf);
421     assertTrue(population.isChanged());
422     List top = population.determineFittestChromosomes(1);
423     assertEquals(top.get(0), population.determineFittestChromosome());
424     top = population.determineFittestChromosomes(2);
425     assertEquals(top.get(0), population.determineFittestChromosome());
426     top = population.determineFittestChromosomes(3);
427     assertEquals(top.get(0), population.determineFittestChromosome());
428     population = getNewPopulation(conf);
429     assertEquals(24, population.determineFittestChromosome().getFitnessValue(),
430                  DELTA);
431     top = population.determineFittestChromosomes(5);
432     assertFalse(population.isChanged());
433     assertEquals(24, ( (IChromosome) top.get(0)).getFitnessValue(), DELTA);
434     assertEquals(23, ( (IChromosome) top.get(1)).getFitnessValue(), DELTA);
435     assertEquals(23, ( (IChromosome) top.get(2)).getFitnessValue(), DELTA);
436     assertEquals(23, ( (IChromosome) top.get(3)).getFitnessValue(), DELTA);
437     assertEquals(22, ( (IChromosome) top.get(4)).getFitnessValue(), DELTA);
438     double oldFitness = population.getChromosome(0).getFitnessValue();
439     for (int i = 1; i < population.size(); i++) {
440       double currFitness = population.getChromosome(i).getFitnessValue();
441       assertTrue(currFitness <= oldFitness);
442       oldFitness = currFitness;
443     }
444   }
445
446   private static Population getNewPopulation(Configuration a_conf)
447       throws InvalidConfigurationException {
448     Population population = new Population(a_conf);
449     Gene g = new DoubleGene(a_conf);
450     Chromosome c = new Chromosome(a_conf, g, 10);
451     c.setFitnessValue(22);
452     population.addChromosome(c);
453     c = new Chromosome(a_conf, g, 10);
454     c.setFitnessValue(24);
455     population.addChromosome(c);
456     c = new Chromosome(a_conf, g, 10);
457     c.setFitnessValue(23);
458     population.addChromosome(c);
459     population.addChromosome(c);
460     population.addChromosome(c);
461     return population;
462   }
463
464   public void testSize_0()
465       throws Exception JavaDoc {
466     Population p = new Population(conf, 10);
467     // size only counts number of "real" chromosomes not placeholders
468
assertEquals(0, p.size());
469     Gene g = new DoubleGene(conf);
470     Chromosome c = new Chromosome(conf, g, 5);
471     p.addChromosome(c);
472     assertEquals(1, p.size());
473     c = new Chromosome(conf, g, 3);
474     p.addChromosome(c);
475     assertEquals(2, p.size());
476   }
477
478   public void testIterator_0()
479       throws Exception JavaDoc {
480     Population p = new Population(conf, 10);
481     Iterator it = p.iterator();
482     assertFalse(it.hasNext());
483     // size only counts number of "real" chromosomes not placeholders
484
assertEquals(0, p.size());
485     Gene g = new DoubleGene(conf);
486     Chromosome c = new Chromosome(conf, g, 5);
487     p.addChromosome(c);
488     assertTrue(it.hasNext());
489   }
490
491   public void testContains_0()
492       throws Exception JavaDoc {
493     Gene g = new DoubleGene(conf);
494     Chromosome c = new Chromosome(conf, g, 10);
495     c.setFitnessValue(45);
496     Population p1 = new Population(conf);
497     assertFalse(p1.contains(c));
498     assertFalse(p1.contains(null));
499     p1.addChromosome(c);
500     assertTrue(p1.contains(c));
501     assertFalse(p1.contains(null));
502     assertFalse(p1.contains(new Chromosome(conf, g, 5)));
503   }
504
505   /**
506    * Single chromosome.
507    * @throws Exception
508    *
509    * @author Klaus Meffert
510    * @since 2.3
511    */

512   public void testGetGenome_0()
513       throws Exception JavaDoc {
514     Population pop = new Population(conf);
515     Gene g1 = new DoubleGene(conf);
516     Gene g2 = new StringGene(conf);
517     Chromosome c1 = new Chromosome(conf, new Gene[] {
518                                    g1, g2
519     });
520     pop.addChromosome(c1);
521     List genes = pop.getGenome(true);
522     assertEquals(2, genes.size());
523     assertEquals(g1, genes.get(0));
524     assertEquals(g2, genes.get(1));
525     genes = pop.getGenome(false);
526     assertEquals(2, genes.size());
527     assertEquals(g1, genes.get(0));
528     assertEquals(g2, genes.get(1));
529   }
530
531   /**
532    * Two chromosomes.
533    * @throws Exception
534    *
535    * @author Klaus Meffert
536    * @since 2.3
537    */

538   public void testGetGenome_1()
539       throws Exception JavaDoc {
540     Population pop = new Population(conf);
541     assertTrue(pop.isChanged());
542     Gene g1 = new DoubleGene(conf);
543     Gene g2 = new StringGene(conf);
544     Chromosome c1 = new Chromosome(conf, new Gene[] {g1, g2});
545     pop.addChromosome(c1);
546     Gene g3 = new BooleanGene(conf);
547     Gene g4 = new IntegerGene(conf, 0, 10);
548     Gene g5 = new FixedBinaryGene(conf, 4);
549     Chromosome c2 = new Chromosome(conf, new Gene[] {g3, g4, g5});
550     pop.addChromosome(c2);
551     List genes = pop.getGenome(true);
552     assertEquals(5, genes.size());
553     assertEquals(g1, genes.get(0));
554     assertEquals(g2, genes.get(1));
555     assertEquals(g3, genes.get(2));
556     assertEquals(g4, genes.get(3));
557     assertEquals(g5, genes.get(4));
558     genes = pop.getGenome(false);
559     assertEquals(5, genes.size());
560     assertEquals(g1, genes.get(0));
561     assertEquals(g2, genes.get(1));
562     assertEquals(g3, genes.get(2));
563     assertEquals(g4, genes.get(3));
564     assertEquals(g5, genes.get(4));
565   }
566
567   /**
568    * Using CompositeGene.
569    * @throws Exception
570    *
571    * @author Klaus Meffert
572    * @since 2.3
573    */

574   public void testGetGenome_2()
575       throws Exception JavaDoc {
576     Population pop = new Population(conf);
577     Gene g1 = new DoubleGene(conf);
578     Gene g2 = new StringGene(conf);
579     Chromosome c1 = new Chromosome(conf, new Gene[] {g1, g2});
580     pop.addChromosome(c1);
581     Gene g3 = new BooleanGene(conf);
582     CompositeGene g4 = new CompositeGene(conf);
583     Gene g5 = new FixedBinaryGene(conf, 4);
584     g4.addGene(g5);
585     Gene g6 = new DoubleGene(conf, 1.0d, 4.0d);
586     g4.addGene(g6);
587     Chromosome c2 = new Chromosome(conf, new Gene[] {g3, g4});
588     pop.addChromosome(c2);
589     assertTrue(pop.isChanged());
590     // resolve CompositeGene with the following call
591
List genes = pop.getGenome(true);
592     assertEquals(5, genes.size());
593     assertEquals(g1, genes.get(0));
594     assertEquals(g2, genes.get(1));
595     assertEquals(g3, genes.get(2));
596     assertEquals(g5, genes.get(3));
597     assertEquals(g6, genes.get(4));
598     // don't resolve CompositeGene with the following call
599
genes = pop.getGenome(false);
600     assertEquals(4, genes.size());
601     assertEquals(g1, genes.get(0));
602     assertEquals(g2, genes.get(1));
603     assertEquals(g3, genes.get(2));
604     assertEquals(g4, genes.get(3));
605   }
606
607   /**
608    * Ensures that the Population class is implementing the Serializable
609    * interface.
610    * @throws Exception
611    *
612    * @author Klaus Meffert
613    * @since 2.3
614    */

615   public void testIsSerializable_0()
616       throws Exception JavaDoc {
617     assertTrue(isSerializable(new Population(conf)));
618   }
619
620   /**
621    * Ensures that Population and all objects contained implement Serializable.
622    * @throws Exception
623    *
624    * @author Klaus Meffert
625    * @since 2.3
626    */

627   public void testDoSerialize_0()
628       throws Exception JavaDoc {
629     // construct genotype to be serialized
630
Chromosome[] chroms = new Chromosome[1];
631     chroms[0] = new Chromosome(conf, new Gene[] {
632                                new IntegerGene(conf, 1, 5)
633     });
634     Population pop = new Population(conf, chroms);
635     assertEquals(pop, doSerialize(pop));
636     assertTrue(pop.isChanged());
637   }
638
639   /**
640    * @throws Exception
641    *
642    * @author Klaus Meffert
643    * @since 2.4
644    */

645   public void testRemoveChromosome_0()
646       throws Exception JavaDoc {
647     Chromosome[] chroms = new Chromosome[1];
648     chroms[0] = new Chromosome(conf, new Gene[] {
649                                new IntegerGene(conf, 1, 5)});
650     Population pop = new Population(conf, chroms);
651     assertEquals(chroms[0], pop.removeChromosome(0));
652     assertEquals(0, pop.size());
653     try {
654       pop.removeChromosome(0);
655       fail();
656     } catch (IllegalArgumentException JavaDoc iex) {
657       ; //this is OK
658
}
659   }
660
661   /**
662    * With illegal index.
663    * @throws Exception
664    *
665    * @author Klaus Meffert
666    * @since 2.4
667    */

668   public void testRemoveChromosome_1()
669       throws Exception JavaDoc {
670     Chromosome[] chroms = new Chromosome[1];
671     chroms[0] = new Chromosome(conf, new Gene[] {
672                                new IntegerGene(conf, 1, 5)});
673     Population pop = new Population(conf, chroms);
674     try {
675       pop.removeChromosome(1);
676       fail();
677     } catch (IllegalArgumentException JavaDoc iex) {
678       ; //this is OK
679
}
680   }
681
682   /**
683    * With illegal index.
684    * @throws Exception
685    *
686    * @author Klaus Meffert
687    * @since 2.4
688    */

689   public void testRemoveChromosome_2()
690       throws Exception JavaDoc {
691     Chromosome[] chroms = new Chromosome[1];
692     chroms[0] = new Chromosome(conf, new Gene[] {
693                                new IntegerGene(conf, 1, 5)});
694     Population pop = new Population(conf, chroms);
695     try {
696       pop.removeChromosome( -1);
697       fail();
698     } catch (IllegalArgumentException JavaDoc iex) {
699       ; //this is OK
700
}
701   }
702
703   /**
704    * @throws Exception
705    *
706    * @author Klaus Meffert
707    * @since 2.4
708    */

709   public void testRemoveChromosome_3()
710       throws Exception JavaDoc {
711     Chromosome[] chroms = new Chromosome[1];
712     chroms[0] = new Chromosome(conf, new Gene[] {
713                                new IntegerGene(conf, 1, 5)});
714     Population pop = new Population(conf, chroms);
715     IChromosome c = pop.removeChromosome(0);
716     assertEquals(chroms[0], c);
717     assertTrue(pop.isChanged());
718   }
719
720   /**
721    * @throws Exception
722    *
723    * @author Klaus Meffert
724    * @since 2.6
725    */

726   public void testSortByFitness_0()
727       throws Exception JavaDoc {
728     IChromosome[] chroms = new Chromosome[3];
729     chroms[0] = new Chromosome(conf, new Gene[] {
730                                new DoubleGene(conf, 1, 5)});
731     chroms[0].setFitnessValueDirectly(45d);
732     chroms[1] = new Chromosome(conf, new Gene[] {
733                                new DoubleGene(conf, 1, 5)});
734     chroms[1].setFitnessValueDirectly(41d);
735     chroms[2] = new Chromosome(conf, new Gene[] {
736                                new DoubleGene(conf, 1, 5)});
737     chroms[2].setFitnessValueDirectly(47d);
738     Population pop = new Population(conf, chroms);
739     assertTrue(pop.isChanged());
740     assertFalse(pop.isSorted());
741     pop.sortByFitness();
742     assertFalse(pop.isChanged());
743     assertTrue(pop.isSorted());
744     double oldFitness = pop.getChromosome(0).getFitnessValue();
745     for (int i = 1; i < pop.size(); i++) {
746       double currFitness = pop.getChromosome(i).getFitnessValue();
747       assertTrue(currFitness <= oldFitness);
748       oldFitness = currFitness;
749     }
750   }
751
752   /**
753    * @throws Exception
754    *
755    * @author Klaus Meffert
756    * @since 2.6
757    */

758   public void testSortByFitness_1()
759       throws Exception JavaDoc {
760     Population pop = new Population(conf);
761     Gene g = new DoubleGene(conf);
762     Chromosome c = new Chromosome(conf, g, 10);
763     c.setFitnessValue(4.5d);
764     pop.addChromosome(c);
765     g = new DoubleGene(conf);
766     c = new Chromosome(conf, g, 10);
767     c.setFitnessValue(4.1d);
768     pop.addChromosome(c);
769     g = new DoubleGene(conf);
770     c = new Chromosome(conf, g, 10);
771     c.setFitnessValue(4.7d);
772     pop.addChromosome(c);
773     assertTrue(pop.isChanged());
774     assertFalse(pop.isSorted());
775     pop.sortByFitness();
776     assertFalse(pop.isChanged());
777     assertTrue(pop.isSorted());
778     double oldFitness = pop.getChromosome(0).getFitnessValue();
779     for (int i = 1; i < pop.size(); i++) {
780       double currFitness = pop.getChromosome(i).getFitnessValue();
781       assertTrue(currFitness <= oldFitness);
782       oldFitness = currFitness;
783     }
784   }
785
786   /**
787    * @throws Exception
788    *
789    * @author Klaus Meffert
790    * @since 2.6
791    */

792   public void testCompareTo_0()
793       throws Exception JavaDoc {
794     Population pop = new Population(conf);
795     assertEquals(1, pop.compareTo(null));
796     Population pop2 = new Population(conf);
797     assertEquals(0, pop.compareTo(pop2));
798     pop2.addChromosome(new Chromosome(conf));
799     assertEquals( -1, pop.compareTo(pop2));
800     assertEquals(1, pop2.compareTo(pop));
801   }
802
803   /**
804    * @throws Exception
805    *
806    * @author Klaus Meffert
807    * @since 3.2
808    */

809   public void testPersistentRepresentation_0()
810       throws Exception JavaDoc {
811     Gene g = new IntegerGene(conf);
812     Chromosome c = new Chromosome(conf, g, 29);
813     c.setFitnessValue(45);
814     Population p = new Population(conf);
815     p.addChromosome(c);
816     String JavaDoc repr = p.getPersistentRepresentation();
817     Population q = new Population(conf);
818     q.setValueFromPersistentRepresentation(repr);
819     assertEquals(p, q);
820     assertEquals(p.getPersistentRepresentation(), q.getPersistentRepresentation());
821   }
822
823   /**
824    *
825    * @throws Exception
826    *
827    * @author Klaus Meffert
828    * @since 3.2
829    */

830   public void testPersistentRepresentation_1()
831       throws Exception JavaDoc {
832     Gene[] genes1 = new Gene[2];
833     genes1[0] = new StringGene(conf);
834     genes1[1] = new DoubleGene(conf);
835     Chromosome chrom = new Chromosome(conf, genes1);
836     Population pop = new Population(conf, chrom);
837     String JavaDoc repr = pop.getPersistentRepresentation();
838     Population q = new Population(conf);
839     q.setValueFromPersistentRepresentation(repr);
840     assertEquals(pop, q);
841     assertEquals(pop.getPersistentRepresentation(),
842                  q.getPersistentRepresentation());
843   }
844
845   /**
846    * @throws Exception
847    *
848    * @author Klaus Meffert
849    * @since 3.2
850    */

851   public void testPersistentRepresentation_2()
852       throws Exception JavaDoc {
853     Population pop = new Population(conf);
854     pop.setValueFromPersistentRepresentation(null);
855     assertEquals(0, pop.size());
856   }
857
858   /**
859    * @throws Exception
860    *
861    * @author Klaus Meffert
862    * @since 3.2
863    */

864   public void testPersistentRepresentation_3()
865       throws Exception JavaDoc {
866     Population pop = new Population(conf);
867     try {
868       pop.setValueFromPersistentRepresentation("1"
869           + Population.CHROM_DELIMITER
870           + "2");
871       fail();
872     } catch (UnsupportedRepresentationException uex) {
873       ; //this is OK
874
}
875   }
876
877   /**
878    * @throws Exception
879    *
880    * @author Klaus Meffert
881    * @since 3.2
882    */

883   public void testPersistentRepresentation_4()
884       throws Exception JavaDoc {
885     Population pop = new Population(conf);
886     try {
887       pop.setValueFromPersistentRepresentation("1"
888           + Population.CHROM_DELIMITER
889           + "0"
890           + Population.CHROM_DELIMITER);
891       fail();
892     } catch (UnsupportedRepresentationException uex) {
893       ; //this is OK
894
}
895   }
896
897   /**
898    * Invalid closing tag.
899    *
900    * @throws Exception
901    *
902    * @author Klaus Meffert
903    * @since 3.2
904    */

905   public void testPersistentRepresentation_5()
906       throws Exception JavaDoc {
907     Population pop = new Population(conf);
908     pop.setValueFromPersistentRepresentation(Population.CHROM_DELIMITER_HEADING
909         + Chromosome.class.getName()
910         + Population.CHROM_DELIMITER
911         + "47.11"
912         + Chromosome.CHROM_DELIMITER
913         + "1"
914         + Chromosome.CHROM_DELIMITER
915         + "<" + IntegerGene.class.getName()
916         + Chromosome.GENE_DELIMITER
917         + "2:4:4>"
918         + Population.CHROM_DELIMITER_CLOSING);
919     assertEquals(1, pop.size());
920     IChromosome chrom = pop.getChromosome(0);
921     assertEquals(1, chrom.size());
922   }
923
924   /**
925    * Invalid closing tag.
926    *
927    * @throws Exception
928    *
929    * @author Klaus Meffert
930    * @since 3.2
931    */

932   public void testPersistentRepresentation_6()
933       throws Exception JavaDoc {
934     Population pop = new Population(conf);
935     try {
936       pop.setValueFromPersistentRepresentation(Population.
937           CHROM_DELIMITER_HEADING
938           + Chromosome.class.getName()
939           + Population.CHROM_DELIMITER
940           + "47.11"
941           + Chromosome.CHROM_DELIMITER
942           + "1"
943           + Chromosome.CHROM_DELIMITER
944           + "<" + IntegerGene.class.getName()
945           + Chromosome.GENE_DELIMITER
946           + "2:4:4>/");
947       fail();
948     } catch (UnsupportedRepresentationException uex) {
949       ; //this is OK
950
}
951   }
952
953   class MyFitnessEvaluator
954       implements FitnessEvaluator {
955     public boolean isFitter(final double a_fitness_value1,
956                             final double a_fitness_value2) {
957       return a_fitness_value1 < a_fitness_value2;
958     }
959
960     public boolean isFitter(IChromosome a_chrom1, IChromosome a_chrom2) {
961       return isFitter(a_chrom1.getFitnessValue(), a_chrom2.getFitnessValue());
962     }
963   }
964 }
965
Popular Tags