KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > ChromosomeTest


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 org.jgap.util.*;
15 import junit.framework.*;
16
17 /**
18  * Tests the Chromosome class.
19  *
20  * @author Klaus Meffert
21  * @since 1.1
22  */

23 public class ChromosomeTest
24     extends JGAPTestCase {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.61 $";
27
28   public static Test suite() {
29     return new TestSuite(ChromosomeTest.class);
30   }
31
32   public void setUp() {
33     super.setUp();
34     Configuration.reset();
35   }
36
37   /**
38    * Illegal constructions regarding second parameter.
39    *
40    * @throws Exception
41    *
42    * @author Klaus Meffert
43    * @since 2.5
44    */

45   public void testConstruct_0()
46       throws Exception JavaDoc {
47     try {
48       new Chromosome(conf, null, 1);
49       fail();
50     } catch (IllegalArgumentException JavaDoc iex) {
51       ; //this is OK
52
}
53   }
54
55   /**
56    * Illegal constructions regarding second parameter.
57    *
58    * @throws Exception
59    *
60    * @author Klaus Meffert
61    * @since 2.5
62    */

63   public void testConstruct_0_2()
64       throws Exception JavaDoc {
65     try {
66       new Chromosome(conf, null, 1, new MyConstraintChecker());
67       fail();
68     } catch (IllegalArgumentException JavaDoc iex) {
69       ; //this is OK
70
}
71   }
72
73   /**
74    * Illegal constructions regarding third parameter.
75    *
76    * @throws Exception
77    *
78    * @author Klaus Meffert
79    */

80   public void testConstruct_1()
81       throws Exception JavaDoc {
82     try {
83       new Chromosome(conf, new IntegerGene(conf), 0);
84       fail();
85     } catch (IllegalArgumentException JavaDoc iex) {
86       ; //this is OK
87
}
88   }
89
90   /**
91    * Illegal constructions regarding third parameter.
92    *
93    * @throws Exception
94    *
95    * @author Klaus Meffert
96    * @since 2.5
97    */

98   public void testConstruct_1_2()
99       throws Exception JavaDoc {
100     try {
101       new Chromosome(conf, new IntegerGene(conf), -1);
102       fail();
103     } catch (IllegalArgumentException JavaDoc iex) {
104       ; //this is OK
105
}
106   }
107
108   /**
109    * Illegal constructions regarding second parameter.
110    *
111    * @throws Exception
112    *
113    * @author Klaus Meffert
114    * @since 2.5
115    */

116   public void testConstruct_1_3()
117       throws Exception JavaDoc {
118     try {
119       new Chromosome(new ConfigurationForTest(), new IntegerGene(conf), -500);
120       fail();
121     } catch (IllegalArgumentException JavaDoc iex) {
122       ; //this is OK
123
}
124   }
125
126   /**
127    * Illegal constructions regarding second and third parameter.
128    *
129    * @throws Exception
130    *
131    * @author Klaus Meffert
132    */

133   public void testConstruct_2()
134       throws Exception JavaDoc {
135     try {
136       new Chromosome(conf, null, 0);
137       fail();
138     } catch (IllegalArgumentException JavaDoc iex) {
139       ; //this is OK
140
}
141   }
142
143   /**
144    * Illegal constructions regarding second and third parameter.
145    *
146    * @throws Exception
147    *
148    * @author Klaus Meffert
149    * @since 2.5
150    */

151   public void testConstruct_2_2()
152       throws Exception JavaDoc {
153     try {
154       new Chromosome(conf, null, 0, null);
155       fail();
156     } catch (IllegalArgumentException JavaDoc iex) {
157       ; //this is OK
158
}
159   }
160
161   /**
162    * Legal construction.
163    *
164    * @throws Exception
165    *
166    * @author Klaus Meffert
167    */

168   public void testConstruct_3()
169       throws Exception JavaDoc {
170     Chromosome chrom = new Chromosome(conf, new IntegerGene(conf), 1);
171     assertEquals(1, chrom.size());
172     chrom = new Chromosome(conf, new IntegerGene(conf), 1, null);
173     assertEquals(1, chrom.size());
174     assertNull(chrom.getConstraintChecker());
175     IGeneConstraintChecker cc = new MyConstraintChecker();
176     chrom = new Chromosome(conf, new IntegerGene(conf), 1, cc);
177     assertEquals(1, chrom.size());
178     assertEquals(cc, chrom.getConstraintChecker());
179   }
180
181   /**
182    * Illegal constructions regarding an element of the second parameter.
183    *
184    * @throws Exception
185    *
186    * @author Klaus Meffert
187    */

188   public void testConstruct_5_2()
189       throws Exception JavaDoc {
190     try {
191       Gene[] genes = new IntegerGene[1];
192       genes[0] = null;
193       new Chromosome(conf, genes);
194       fail();
195     } catch (IllegalArgumentException JavaDoc iex) {
196       ; //this is OK
197
}
198   }
199
200   /**
201    * Illegal constructions regarding a gene type forbidden by the constraint
202    * checker used.
203    *
204    * @throws Exception
205    *
206    * @author Klaus Meffert
207    * @since 2.5
208    */

209   public void testConstruct_5_3()
210       throws Exception JavaDoc {
211     try {
212       Gene[] genes = new Gene[2];
213       genes[0] = new IntegerGene();
214       genes[1] = new DoubleGene();
215       IGeneConstraintChecker cc = new MyConstraintChecker(DoubleGene.class);
216       new Chromosome(conf, genes, cc);
217       fail();
218     } catch (IllegalArgumentException JavaDoc iex) {
219       fail();
220     } catch (InvalidConfigurationException cex) {
221       ; //this is OK
222
}
223   }
224
225   /**
226    * Illegal constructions regarding second parameter.
227    *
228    * @throws Exception
229    *
230    * @author Klaus Meffert
231    */

232   public void testConstruct_6()
233       throws Exception JavaDoc {
234     try {
235       new Chromosome(conf, (Gene[])null);
236       fail();
237     } catch (IllegalArgumentException JavaDoc illex) {
238       ; //this is OK
239
}
240   }
241
242   /**
243    * Illegal constructions regarding second parameter.
244    *
245    * @throws Exception
246    *
247    * @author Klaus Meffert
248    * @since 2.4
249    */

250   public void testConstruct_7_1()
251       throws Exception JavaDoc {
252     try {
253       new Chromosome(conf, 0);
254       fail();
255     } catch (IllegalArgumentException JavaDoc illex) {
256       ; //this is OK
257
}
258   }
259
260   /**
261    * Illegal constructions regarding second parameter.
262    *
263    * @throws Exception
264    *
265    * @author Klaus Meffert
266    * @since 2.4
267    */

268   public void testConstruct_7_2()
269       throws Exception JavaDoc {
270     try {
271       new Chromosome(conf, -5);
272       fail();
273     } catch (IllegalArgumentException JavaDoc illex) {
274       ; //this is OK
275
}
276   }
277
278   /**
279    * @throws Exception
280    *
281    * @author Klaus Meffert
282    * @since 2.4
283    */

284   public void testConstruct_7_3()
285       throws Exception JavaDoc {
286     Chromosome chrom = new Chromosome(conf, 5);
287     assertEquals(5, chrom.getGenes().length);
288     for (int i = 0; i < 5; i++) {
289       assertEquals(null, chrom.getGene(i));
290     }
291   }
292
293   /**
294    * Illegal constructions regarding first parameter.
295    *
296    * @throws Exception
297    *
298    * @author Klaus Meffert
299    */

300   public void testConstruct_8()
301       throws Exception JavaDoc {
302     try {
303       Gene[] genes = new IntegerGene[2];
304       genes[0] = new IntegerGene(conf);
305       genes[1] = null;
306       new Chromosome(conf, genes);
307       fail();
308     } catch (IllegalArgumentException JavaDoc illex) {
309       ; //this is OK
310
}
311   }
312
313   /**
314    *
315    * @throws Exception
316    *
317    * @author Klaus Meffert
318    * @since 3.2
319    */

320   public void testConstruct_9()
321       throws Exception JavaDoc {
322     Configuration conf = new DefaultConfiguration();
323     Gene[] genes1 = new Gene[2];
324     genes1[0] = new IntegerGene(conf);
325     genes1[1] = new BooleanGene(conf);
326     Chromosome chrom = new Chromosome(conf, genes1);
327     String JavaDoc repr = chrom.getPersistentRepresentation();
328     Chromosome chrom2 = new Chromosome(conf, repr);
329     assertEquals(chrom, chrom2);
330     assertEquals(chrom.getPersistentRepresentation(),
331                  chrom2.getPersistentRepresentation());
332   }
333
334   /**
335    * @throws Exception
336    *
337    * @author Klaus Meffert
338    */

339   public void testConstruct_10()
340       throws Exception JavaDoc {
341     Configuration conf = new DefaultConfiguration();
342     Gene[] genes = new IntegerGene[2];
343     genes[0] = new IntegerGene(conf);
344     genes[1] = new IntegerGene(conf);
345     Chromosome chrom = new Chromosome(conf, genes);
346     assertEquals(2, chrom.size());
347     assertEquals(genes[0], chrom.getGene(0));
348     assertEquals(genes[1], chrom.getGene(1));
349   }
350
351   /**
352    * @throws Exception
353    *
354    * @author Klaus Meffert
355    */

356   public void testConstruct_11()
357       throws Exception JavaDoc {
358     Configuration conf = new DefaultConfiguration();
359     Gene[] genes = new BooleanGene[3];
360     genes[0] = new BooleanGene(conf);
361     genes[1] = new BooleanGene(conf);
362     genes[2] = new BooleanGene(conf);
363     genes[2].setAllele(Boolean.valueOf(true));
364     conf.setFitnessFunction(new RandomFitnessFunction());
365     Chromosome chrom = new Chromosome(conf, genes);
366     assertEquals(3, chrom.size());
367     assertEquals(genes[0], chrom.getGene(0));
368     assertEquals(genes[1], chrom.getGene(1));
369     assertEquals(genes[2], chrom.getGene(2));
370   }
371
372   /**
373    * @throws Exception
374    *
375    * @author Klaus Meffert
376    */

377   public void testConstruct_12()
378       throws Exception JavaDoc {
379     Configuration conf = new DefaultConfiguration();
380     Gene[] genes = new IntegerGene[2];
381     genes[0] = new IntegerGene(conf);
382     genes[1] = new IntegerGene(conf);
383     conf.setFitnessFunction(new RandomFitnessFunction());
384     Chromosome chrom2 = new Chromosome(conf, genes);
385     conf.setSampleChromosome(chrom2);
386     new Chromosome(conf, genes);
387   }
388
389   /**
390    * @throws Exception
391    *
392    * @author Klaus Meffert
393    */

394   public void testConstruct_14()
395       throws Exception JavaDoc {
396     Gene gene = new BooleanGene(conf);
397     Chromosome chrom = new Chromosome(conf, gene, 7);
398     Gene[] genes = chrom.getGenes();
399     assertEquals(7, genes.length);
400     Gene sample;
401     for (int i = 0; i < genes.length; i++) {
402       sample = (Gene) genes[i];
403       assertEquals(gene, sample);
404     }
405   }
406
407   /**
408    * Tests cloning and cleanup of a chromosome.
409    *
410    * @throws Exception
411    *
412    * @author Klaus Meffert
413    * @since 2.4
414    */

415   public void testClone_5()
416       throws Exception JavaDoc {
417     Configuration conf = new DefaultConfiguration();
418     conf.setFitnessFunction(new RandomFitnessFunction());
419     Chromosome chrom = new Chromosome(conf);
420     assertEquals(0, chrom.size());
421     Chromosome chrom2 = (Chromosome) chrom.clone();
422     assertEquals(chrom, chrom2);
423     conf.getChromosomePool().releaseChromosome(chrom);
424     assertEquals(Chromosome.class, chrom.clone().getClass());
425     chrom.cleanup();
426     conf.setChromosomePool(null);
427   }
428
429   /**
430    * Tests cloning of a chromosome with genes using the chromosome pool.
431    *
432    * @throws Exception
433    *
434    * @author Klaus Meffert
435    * @since 2.6
436    */

437   public void testClone_6()
438       throws Exception JavaDoc {
439     Configuration conf = new DefaultConfiguration();
440     conf.setFitnessFunction(new RandomFitnessFunction());
441     Gene[] genes = new IntegerGene[2];
442     genes[0] = new IntegerGene(conf);
443     genes[1] = new IntegerGene(conf);
444     Chromosome chrom = new Chromosome(conf, genes);
445     chrom.cleanup();
446     assertEquals(2, chrom.size());
447     Chromosome chrom2 = (Chromosome) chrom.clone();
448     assertEquals(chrom, chrom2);
449     Chromosome chrom3 = (Chromosome) chrom.clone();
450     assertEquals(chrom3, chrom2);
451   }
452
453   /**
454    * Test clone with configuration set.
455    *
456    * @throws InvalidConfigurationException
457    *
458    * @author Klaus Meffert
459    */

460   public void testClone_1()
461       throws InvalidConfigurationException {
462     Gene[] genes = new Gene[2];
463     genes[0] = new IntegerGene(conf);
464     genes[1] = new IntegerGene(conf);
465     Configuration conf = new DefaultConfiguration();
466     conf.setFitnessFunction(new StaticFitnessFunction(20));
467     Chromosome chrom3 = new Chromosome(conf, genes);
468     conf.setSampleChromosome(chrom3);
469     conf.setPopulationSize(5);
470     Chromosome chrom = new Chromosome(conf, genes);
471     Chromosome chrom2 = (Chromosome) chrom.clone();
472     assertEquals(chrom.hashCode(), chrom2.hashCode());
473     assertEquals(chrom.getFitnessValue(), chrom2.getFitnessValue(), DELTA);
474     assertEquals(chrom.isSelectedForNextGeneration(),
475                  chrom2.isSelectedForNextGeneration());
476     assertEquals(chrom.size(), chrom2.size());
477     assertEquals(chrom.getGene(0), chrom2.getGene(0));
478     assertEquals(chrom.getGene(1), chrom2.getGene(1));
479     assertEquals(chrom.getGenes().getClass(), chrom2.getGenes().getClass());
480     assertEquals(chrom.toString(), chrom2.toString());
481     assertTrue(chrom.equals(chrom2));
482     assertNotSame(chrom.getGenes(), chrom2.getGenes());
483     assertNotSame(chrom.getGene(0), chrom2.getGene(0));
484     assertNotSame(chrom.getGene(1), chrom2.getGene(1));
485   }
486
487   /**
488    * Test clone with set application data implementing interface Cloneable,
489    * but restricting access because MyAppData is a package protected class (and
490    * the Chromosome class resides in different package) and also not
491    * implementing IApplicationData. But by using setAccessible in framework
492    * code, now it works. Some months ago it didn't *uuumm*.
493    *
494    * @throws InvalidConfigurationException
495    *
496    * @author Klaus Meffert
497    */

498   public void testClone_2()
499       throws InvalidConfigurationException {
500     Configuration conf = new DefaultConfiguration();
501     Gene[] genes = new IntegerGene[2];
502     genes[0] = new IntegerGene(conf);
503     genes[1] = new IntegerGene(conf);
504     conf.setFitnessFunction(new StaticFitnessFunction(20));
505     Chromosome chrom2 = new Chromosome(conf, genes);
506     conf.setSampleChromosome(chrom2);
507     conf.setPopulationSize(5);
508     Chromosome chrom = new Chromosome(conf, genes);
509     Object JavaDoc appObj = new MyAppObject();
510     chrom.setApplicationData(appObj);
511     Chromosome cloned = (Chromosome) chrom.clone();
512     assertSame(appObj, cloned.getApplicationData());
513   }
514
515   /**
516    * Test clone with set application data, where cloning supported. Access is
517    * not granted via Cloneable (because of inner class) but via explicit and
518    * specially considered interface IApplicationData (see MyAppObject2)!
519    *
520    * @throws InvalidConfigurationException
521    *
522    * @author Klaus Meffert
523    */

524   public void testClone_3()
525       throws InvalidConfigurationException {
526     Configuration conf = new DefaultConfiguration();
527     Gene[] genes = new IntegerGene[2];
528     genes[0] = new IntegerGene(conf);
529     genes[1] = new IntegerGene(conf);
530     conf.setFitnessFunction(new StaticFitnessFunction(20));
531     Chromosome chrom2 = new Chromosome(conf, genes);
532     conf.setSampleChromosome(chrom2);
533     conf.setPopulationSize(5);
534     Chromosome chrom = new Chromosome(conf, genes);
535     Object JavaDoc appObj = new MyAppObject2();
536     chrom.setApplicationData(appObj);
537     chrom2 = (Chromosome) chrom.clone();
538     assertTrue(chrom.equals(chrom2));
539     assertEquals(appObj, chrom2.getApplicationData());
540     assertFalse(appObj == chrom2.getApplicationData());
541   }
542
543   /**
544    * Test clone with Gene's energy set (value should be considered, too).
545    *
546    * @throws InvalidConfigurationException
547    *
548    * @author Klaus Meffert
549    * @since 2.6
550    */

551   public void testClone_4()
552       throws InvalidConfigurationException {
553     Configuration conf = new DefaultConfiguration();
554     Gene[] genes = new Gene[2];
555     genes[0] = new IntegerGene(conf);
556     genes[0].setEnergy(47.11d);
557     genes[1] = new IntegerGene(conf);
558     genes[1].setEnergy(8.15d);
559     conf.setFitnessFunction(new StaticFitnessFunction(20));
560     Chromosome chrom3 = new Chromosome(conf, genes);
561     conf.setSampleChromosome(chrom3);
562     conf.setPopulationSize(5);
563     Chromosome chrom = new Chromosome(conf, genes);
564     Chromosome chrom2 = (Chromosome) chrom.clone();
565     Gene[] clonedGenes = chrom2.getGenes();
566     assertEquals(genes[0].getEnergy(), clonedGenes[0].getEnergy(), DELTA);
567     assertEquals(genes[1].getEnergy(), clonedGenes[1].getEnergy(), DELTA);
568   }
569
570   private final static int MAX_CHROMOSOME_TO_TEST = 1000;
571
572   private final static int MAX_GENES_TO_TEST = 25;
573
574   private final static int MAX_GENES_TYPES = 6;
575
576   /**
577    * Test hashcode for intensity of diversity.<p>
578    * <b>Warning:</b> when a new Gene type is added the constant MAX_GENES_TYPES
579    * must be adjusted as well as adding the new type in the switch case below.
580    *
581    * @throws InvalidConfigurationException
582    *
583    * @author John Serri
584    * @since 2.1
585    */

586   public void testHashCode_0()
587       throws InvalidConfigurationException {
588     int count;
589     int numGenes;
590     int geneCount;
591     int geneType;
592     Gene[] genes;
593     Chromosome chrom;
594     TestHashcode thc = new TestHashcode();
595     thc.setVerbose(!true);
596     List uniqueChromosome = new ArrayList();
597     List equalChromosome = new ArrayList();
598     // Build Random Chromosomes
599
for (count = 0; count < MAX_CHROMOSOME_TO_TEST; count++) {
600       numGenes = (int) (Math.random() * (MAX_GENES_TO_TEST - 1)) + 1;
601       genes = new Gene[numGenes];
602       for (geneCount = 0; geneCount < numGenes; geneCount++) {
603         geneType = (int) (Math.random() * MAX_GENES_TYPES);
604         switch (geneType) {
605           case 0:
606             genes[geneCount] = new IntegerGene(conf);
607             break;
608           case 1:
609             genes[geneCount] = new BooleanGene(conf);
610             break;
611           case 2:
612             genes[geneCount] = new CompositeGene(conf);
613             break;
614           case 3:
615             genes[geneCount] = new DoubleGene(conf);
616             break;
617           case 4:
618             genes[geneCount] = new FixedBinaryGene(conf, 5);
619             break;
620           case 5:
621             genes[geneCount] = new StringGene(conf);
622             break;
623         }
624       }
625       chrom = new Chromosome(new ConfigurationForTest(), genes);
626       // We only want to add unique object, since equal object will
627
// return the same hashcode
628
if (!uniqueChromosome.contains(chrom)) {
629         uniqueChromosome.add(chrom);
630       }
631     }
632     //Test to see if enough hashcodes are unique
633
thc.setFractionUnique(.95);
634     if (!thc.testHashCodeUniqueness(uniqueChromosome)) {
635       System.out.println(
636           "testHashCodeUniqueness failed\n Actual Percent unique = " +
637           thc.getActualFractionUnique());
638       fail();
639     }
640     //Test mathematical average and dispersion of hashcode
641
//I am not sure of the value of this test since boundry values are
642
// pretty much arbitrary
643
thc.setAverageMax(2100000000);
644     thc.setAverageMin( -140000000);
645     thc.setStdDevMax(2100000000);
646     thc.setStdDevMin(9000000);
647     if (!thc.testDispersion(uniqueChromosome)) {
648       fail();
649     }
650     // Build identical Chromosomes
651
for (count = 0; count < 3; count++) {
652       genes = new Gene[1];
653       genes[0] = new IntegerGene(conf);
654       chrom = new Chromosome(new ConfigurationForTest(), genes);
655       equalChromosome.add(chrom);
656     }
657     //If an object is equal it must have the same hashcode
658
if (!thc.testHashCodeEquality(equalChromosome)) {
659       fail();
660     }
661   }
662
663   /**
664    *
665    * @throws Exception
666    *
667    * @author Klaus Meffert
668    */

669   public void testEquals_0()
670       throws Exception JavaDoc {
671     Gene[] genes = new IntegerGene[2];
672     genes[0] = new IntegerGene(conf);
673     genes[1] = new IntegerGene(conf);
674     Chromosome chrom = new Chromosome(conf, genes);
675     assertTrue(chrom.equals(chrom));
676     Chromosome chrom2 = new Chromosome(conf, genes);
677     assertTrue(chrom.equals(chrom2));
678   }
679
680   /**
681    *
682    * @throws Exception
683    *
684    * @author Klaus Meffert
685    */

686   public void testEquals_1()
687       throws Exception JavaDoc {
688     Gene[] genes = new IntegerGene[2];
689     genes[0] = new IntegerGene(conf);
690     genes[1] = new IntegerGene(conf);
691     Chromosome chrom = new Chromosome(conf, genes);
692     assertTrue(chrom.equals(chrom));
693     genes = new BooleanGene[2];
694     genes[0] = new BooleanGene(conf);
695     genes[1] = new BooleanGene(conf);
696     Chromosome chrom2 = new Chromosome(conf, genes);
697     assertFalse(chrom.equals(chrom2));
698   }
699
700   /**
701    *
702    * @throws Exception
703    *
704    * @author Klaus Meffert
705    */

706   public void testEquals_2()
707       throws Exception JavaDoc {
708     Gene[] genes = new IntegerGene[2];
709     Gene gen0 = new IntegerGene(conf);
710     gen0.setAllele(new Integer JavaDoc(1));
711     Gene gen1 = new IntegerGene(conf);
712     gen1.setAllele(new Integer JavaDoc(2));
713     genes[0] = gen0;
714     genes[1] = gen1;
715     Chromosome chrom = new Chromosome(conf, genes);
716     assertTrue(chrom.equals(chrom));
717     genes = new IntegerGene[2];
718     gen0 = new IntegerGene(conf);
719     gen0.setAllele(new Integer JavaDoc(1));
720     gen1 = new IntegerGene(conf);
721     gen1.setAllele(new Integer JavaDoc(3));
722     genes[0] = gen0;
723     genes[1] = gen1;
724     Chromosome chrom2 = new Chromosome(conf, genes);
725     assertFalse(chrom.equals(chrom2));
726     gen1.setAllele(new Integer JavaDoc(2));
727     assertTrue(chrom.equals(chrom2));
728     gen0.setAllele(new Integer JavaDoc(2));
729     assertFalse(chrom.equals(chrom2));
730     gen1.setAllele(new Integer JavaDoc(1));
731     assertFalse(chrom.equals(chrom2));
732   }
733
734   /**
735    *
736    * @throws Exception
737    *
738    * @author Klaus Meffert
739    */

740   public void testEquals_3()
741       throws Exception JavaDoc {
742     Gene[] genes = new IntegerGene[2];
743     genes[0] = new IntegerGene(conf);
744     genes[1] = new IntegerGene(conf);
745     Chromosome chrom = new Chromosome(conf, genes);
746     assertFalse(chrom.equals(null));
747     // no ClassCastException is expected next!
748
// ---------------------------------------
749
assertFalse(chrom.equals(genes));
750   }
751
752   /**
753    *
754    * @throws Exception
755    *
756    * @author Klaus Meffert
757    */

758   public void testGetFitnessValue_0()
759       throws Exception JavaDoc {
760     Configuration conf = new DefaultConfiguration();
761     Gene[] genes = new IntegerGene[2];
762     genes[0] = new IntegerGene(conf);
763     genes[1] = new IntegerGene(conf);
764     StaticFitnessFunction ff = new StaticFitnessFunction(20);
765     conf.setFitnessFunction(ff);
766     Chromosome chrom = new Chromosome(conf, genes);
767     conf.setSampleChromosome(chrom);
768     conf.setPopulationSize(5);
769     chrom = new Chromosome(conf, genes);
770     assertEquals(ff.getStaticFitnessValue(), chrom.getFitnessValue(), DELTA);
771     //intentionally assert it a second time
772
assertEquals(ff.getStaticFitnessValue(), chrom.getFitnessValue(), DELTA);
773   }
774
775   /**
776    *
777    * @throws Exception
778    *
779    * @author Klaus Meffert
780    */

781   public void testGetFitnessValue_1()
782       throws Exception JavaDoc {
783     Configuration conf = new DefaultConfiguration();
784     Gene[] genes = new IntegerGene[2];
785     genes[0] = new IntegerGene(conf);
786     genes[1] = new IntegerGene(conf);
787     StaticFitnessFunction ff = new StaticFitnessFunction(20);
788     conf.setFitnessFunction(ff);
789     Chromosome chrom = new Chromosome(conf, genes);
790     conf.setSampleChromosome(chrom);
791     conf.setPopulationSize(5);
792     chrom = new Chromosome(conf, genes);
793     assertEquals(ff.getStaticFitnessValue(), chrom.getFitnessValue(), DELTA);
794     // set fitness value to a different one (should not affect first set value
795
// as no computation is performed once the fitness is known in the
796
// chromosome.
797
ff.setStaticFitnessValue(44.235d);
798     assertEquals(20, chrom.getFitnessValue(), DELTA);
799   }
800
801   /**
802    *
803    * @throws Exception
804    *
805    * @author Klaus Meffert
806    */

807   public void testSize_0()
808       throws Exception JavaDoc {
809     Gene[] genes = new IntegerGene[2];
810     genes[0] = new IntegerGene(conf);
811     genes[1] = new IntegerGene(conf);
812     Chromosome chrom = new Chromosome(conf, genes);
813     assertEquals(2, chrom.size());
814   }
815
816   /**
817    *
818    * @throws Exception
819    *
820    * @author Klaus Meffert
821    */

822   public void testSize_1()
823       throws Exception JavaDoc {
824     Gene[] genes = new IntegerGene[1];
825     genes[0] = new IntegerGene(conf);
826     Chromosome chrom = new Chromosome(conf, genes);
827     assertEquals(1, chrom.size());
828   }
829
830   /**
831    *
832    * @throws Exception
833    *
834    * @author Klaus Meffert
835    */

836   public void testSize_2()
837       throws Exception JavaDoc {
838     Gene[] genes = new IntegerGene[0];
839     try {
840       new Chromosome(conf, genes);
841       fail();
842     } catch (IllegalArgumentException JavaDoc iex) {
843       ; //this is OK
844
}
845   }
846
847   /**
848    *
849    * @throws Exception
850    *
851    * @author Klaus Meffert
852    */

853   public void testCompareTo_0()
854       throws Exception JavaDoc {
855     Configuration conf = new DefaultConfiguration();
856     Gene[] genes = new IntegerGene[2];
857     genes[0] = new IntegerGene(conf);
858     genes[1] = new IntegerGene(conf);
859     conf.setFitnessFunction(new StaticFitnessFunction(20));
860     Chromosome chrom2 = new Chromosome(conf, genes);
861     conf.setSampleChromosome(chrom2);
862     conf.setPopulationSize(5);
863     Chromosome chrom = new Chromosome(conf, genes);
864     assertTrue(chrom.compareTo(chrom2) == 0);
865     assertTrue(chrom2.compareTo(chrom) == 0);
866   }
867
868   /**
869    *
870    * @throws Exception
871    *
872    * @author Klaus Meffert
873    */

874   public void testCompareTo_0_2()
875       throws Exception JavaDoc {
876     Configuration conf = new DefaultConfiguration();
877     Gene[] genes = new IntegerGene[2];
878     genes[0] = new IntegerGene(conf);
879     genes[1] = new IntegerGene(conf);
880     conf.setFitnessFunction(new StaticFitnessFunction(20));
881     Chromosome chrom2 = new Chromosome(conf, genes);
882     chrom2.setCompareApplicationData(true);
883     conf.setSampleChromosome(chrom2);
884     conf.setPopulationSize(5);
885     Chromosome chrom = new Chromosome(conf, genes);
886     chrom.setCompareApplicationData(true);
887     assertTrue(chrom.compareTo(chrom2) == 0);
888     assertTrue(chrom2.compareTo(chrom) == 0);
889   }
890
891   /**
892    *
893    * @throws Exception
894    *
895    * @author Klaus Meffert
896    */

897   public void testCompareTo_0_3()
898       throws Exception JavaDoc {
899     Configuration conf = new DefaultConfiguration();
900     Gene[] genes = new IntegerGene[2];
901     genes[0] = new IntegerGene(conf);
902     genes[1] = new IntegerGene(conf);
903     conf.setFitnessFunction(new StaticFitnessFunction(20));
904     Chromosome chrom2 = new Chromosome(conf, genes);
905     chrom2.setCompareApplicationData(true);
906     chrom2.setApplicationData(new Object JavaDoc());
907     conf.setSampleChromosome(chrom2);
908     conf.setPopulationSize(5);
909     Chromosome chrom = new Chromosome(conf, genes);
910     chrom.setCompareApplicationData(true);
911     chrom.setApplicationData(null);
912     assertFalse(chrom.compareTo(chrom2) == 0);
913     assertFalse(chrom2.compareTo(chrom) == 0);
914   }
915
916   /**
917    *
918    * @throws Exception
919    *
920    * @author Klaus Meffert
921    */

922   public void testCompareTo_0_4()
923       throws Exception JavaDoc {
924     Configuration conf = new DefaultConfiguration();
925     Gene[] genes = new IntegerGene[2];
926     genes[0] = new IntegerGene(conf);
927     genes[1] = new IntegerGene(conf);
928     conf.setFitnessFunction(new StaticFitnessFunction(20));
929     Chromosome chrom2 = new Chromosome(conf, genes);
930     chrom2.setCompareApplicationData(true);
931     chrom2.setApplicationData(new Object JavaDoc());
932     conf.setSampleChromosome(chrom2);
933     conf.setPopulationSize(5);
934     Chromosome chrom = new Chromosome(conf, genes);
935     chrom.setCompareApplicationData(true);
936     chrom.setApplicationData(new Date());
937     assertFalse(chrom.compareTo(chrom2) == 0);
938     assertFalse(chrom2.compareTo(chrom) == 0);
939   }
940
941   /**
942    *
943    * @throws Exception
944    *
945    * @author Klaus Meffert
946    */

947   public void testCompareTo_1()
948       throws Exception JavaDoc {
949     Configuration conf = new DefaultConfiguration();
950     Gene[] genes = new IntegerGene[2];
951     Gene gen0 = new IntegerGene(conf);
952     gen0.setAllele(new Integer JavaDoc(1147));
953     genes[0] = gen0;
954     genes[1] = new IntegerGene(conf);
955     Chromosome chrom0 = new Chromosome(conf, genes);
956     conf.setFitnessFunction(new StaticFitnessFunction(20));
957     conf.setSampleChromosome(chrom0);
958     conf.setPopulationSize(5);
959     Chromosome chrom = new Chromosome(conf, genes);
960     Gene[] genes2 = new IntegerGene[2];
961     Gene gene01 = new IntegerGene(conf);
962     gene01.setAllele(new Integer JavaDoc(4711));
963     genes2[0] = gene01;
964     genes2[1] = new IntegerGene(conf);
965     Chromosome chrom2 = new Chromosome(conf, genes2);
966     assertTrue(chrom.compareTo(chrom2) < 0);
967     assertTrue(chrom2.compareTo(chrom) > 0);
968   }
969
970   /**
971    *
972    * @throws Exception
973    *
974    * @author Klaus Meffert
975    */

976   public void testCompareTo_2()
977       throws Exception JavaDoc {
978     Configuration conf = new DefaultConfiguration();
979     Gene[] genes = new IntegerGene[2];
980     genes[0] = new IntegerGene(conf);
981     genes[1] = new IntegerGene(conf);
982     conf.setFitnessFunction(new StaticFitnessFunction(20));
983     Chromosome chrom = new Chromosome(conf, genes);
984     Gene[] genes2 = new IntegerGene[3];
985     genes2[0] = new IntegerGene(conf);
986     genes2[1] = new IntegerGene(conf);
987     genes2[2] = new IntegerGene(conf);
988     Chromosome chrom2 = new Chromosome(conf, genes2);
989     conf.setSampleChromosome(chrom2);
990     conf.setPopulationSize(5);
991     assertTrue(chrom.compareTo(chrom2) < 0);
992     assertTrue(chrom2.compareTo(chrom) > 0);
993   }
994
995   /**
996    *
997    * @throws Exception
998    *
999    * @author Klaus Meffert
1000   */

1001  public void testCompareTo_3()
1002      throws Exception JavaDoc {
1003    Gene[] genes = new IntegerGene[1];
1004    genes[0] = new IntegerGene(conf);
1005    Chromosome chrom = new Chromosome(conf, genes);
1006    assertTrue(chrom.compareTo(null) > 0);
1007  }
1008
1009  /**
1010   *
1011   * @throws Exception
1012   *
1013   * @author Klaus Meffert
1014   */

1015  public void testCompareTo_4()
1016      throws Exception JavaDoc {
1017    Configuration conf = new DefaultConfiguration();
1018    Gene[] genes = new IntegerGene[2];
1019    Gene gen0 = new IntegerGene(conf);
1020    gen0.setAllele(new Integer JavaDoc(4711));
1021    genes[0] = gen0;
1022    genes[1] = new IntegerGene(conf);
1023    conf.setFitnessFunction(new StaticFitnessFunction(20));
1024    conf.setPopulationSize(5);
1025    Chromosome chrom0 = new Chromosome(conf, genes);
1026    conf.setSampleChromosome(chrom0);
1027    Chromosome chrom = new Chromosome(conf, genes);
1028    Gene[] genes2 = new IntegerGene[2];
1029    Gene gene01 = new IntegerGene(conf);
1030    gene01.setAllele(new Integer JavaDoc(4711));
1031    genes2[0] = gene01;
1032    genes2[1] = new IntegerGene(conf);
1033    Chromosome chrom2 = new Chromosome(conf, genes2);
1034    assertTrue(chrom.compareTo(chrom2) == 0);
1035    assertTrue(chrom2.compareTo(chrom) == 0);
1036  }
1037
1038  /**
1039   *
1040   * @throws Exception
1041   *
1042   * @author Klaus Meffert
1043   */

1044  public void testCompareTo_5()
1045      throws Exception JavaDoc {
1046    Configuration conf = new DefaultConfiguration();
1047    Gene[] genes = new Gene[2];
1048    genes[0] = new IntegerGene(conf);
1049    genes[1] = new BooleanGene(conf);
1050    conf.setFitnessFunction(new StaticFitnessFunction(20));
1051    Chromosome chrom2 = new Chromosome(conf, genes);
1052    conf.setSampleChromosome(chrom2);
1053    conf.setPopulationSize(5);
1054    Chromosome chrom = new Chromosome(conf, genes);
1055    assertTrue(chrom.compareTo(chrom2) == 0);
1056    assertTrue(chrom2.compareTo(chrom) == 0);
1057  }
1058
1059  /**
1060   *
1061   * @throws Exception
1062   *
1063   * @author Klaus Meffert
1064   */

1065  public void testCompareTo_6()
1066      throws Exception JavaDoc {
1067    Configuration conf = new DefaultConfiguration();
1068    Gene[] genes1 = new Gene[2];
1069    genes1[0] = new IntegerGene(conf);
1070    genes1[1] = new BooleanGene(conf);
1071    Gene[] genes2 = new Gene[2];
1072    genes2[0] = new IntegerGene(conf);
1073    genes2[1] = new BooleanGene(conf);
1074    conf.setFitnessFunction(new StaticFitnessFunction(20));
1075    Chromosome chrom2 = new Chromosome(conf, genes1);
1076    conf.setSampleChromosome(chrom2);
1077    conf.setPopulationSize(5);
1078    Chromosome chrom = new Chromosome(conf, genes2);
1079    assertTrue(chrom.compareTo(chrom2) == 0);
1080    assertTrue(chrom2.compareTo(chrom) == 0);
1081    genes2[1].setAllele(Boolean.valueOf(false));
1082    genes1[1].setAllele(Boolean.valueOf(true));
1083    assertFalse(chrom2.compareTo(chrom) == 0);
1084  }
1085
1086  /**
1087   *
1088   * @throws Exception
1089   *
1090   * @author Klaus Meffert
1091   */

1092  public void testRandomInitialChromosome_0()
1093      throws Exception JavaDoc {
1094    try {
1095      Chromosome.randomInitialChromosome(null);
1096      fail();
1097    } catch (IllegalArgumentException JavaDoc iex) {
1098      ; //this is OK
1099
}
1100  }
1101
1102  /**
1103   * @throws Exception
1104   *
1105   * @author Klaus Meffert
1106   * @since 2.2
1107   */

1108  public void testRandomInitialChromosome_1()
1109      throws Exception JavaDoc {
1110    Configuration conf = new ConfigurationForTest();
1111    conf.setChromosomePool(new ChromosomePool());
1112    conf.setRandomGenerator(new RandomGeneratorForTest(true));
1113    IChromosome chrom = Chromosome.randomInitialChromosome(conf);
1114    //The BooleanGene comes from the sample chrom set in ConfigurationForTest
1115
assertTrue( ( (BooleanGene) chrom.getGene(0)).booleanValue());
1116  }
1117
1118  /**
1119   * @throws Exception
1120   *
1121   * @author Klaus Meffert
1122   * @since 2.2
1123   */

1124  public void testRandomInitialChromosome_2()
1125      throws Exception JavaDoc {
1126    Configuration conf = new ConfigurationForTest();
1127    conf.setChromosomePool(new ChromosomePool());
1128    conf.setRandomGenerator(new RandomGeneratorForTest(false));
1129    IChromosome chrom = Chromosome.randomInitialChromosome(conf);
1130    //The BooleanGene comes from the sample chrom set in ConfigurationForTest
1131
assertFalse( ( (BooleanGene) chrom.getGene(0)).booleanValue());
1132  }
1133
1134  /**
1135   * Use Chromosome Pool.
1136   *
1137   * @throws Exception
1138   *
1139   * @author Klaus Meffert
1140   * @since 2.6
1141   */

1142  public void testRandomInitialChromosome_3()
1143      throws Exception JavaDoc {
1144    Configuration conf = new ConfigurationForTest();
1145    conf.setChromosomePool(new ChromosomePool());
1146    conf.setRandomGenerator(new RandomGeneratorForTest(true));
1147    IChromosome chrom = Chromosome.randomInitialChromosome(conf);
1148    chrom.cleanup(); //fill the pool
1149
chrom = Chromosome.randomInitialChromosome(conf);
1150    assertEquals(FitnessFunction.NO_FITNESS_VALUE,
1151                 chrom.getFitnessValueDirectly(), DELTA);
1152    assertTrue( ( (BooleanGene) chrom.getGene(0)).booleanValue());
1153  }
1154
1155  /**
1156   * Setting random generator not allowed after configuration has been locked.
1157   *
1158   * @throws Exception
1159   *
1160   * @author Klaus Meffert
1161   * @since 3.0
1162   */

1163  public void testRandomInitialChromosome_4()
1164      throws Exception JavaDoc {
1165    Configuration conf = new DefaultConfiguration();
1166    conf.setFitnessFunction(new StaticFitnessFunction(2.5d));
1167    Gene[] genes = new Gene[1];
1168    Gene gene = new BooleanGene(conf);
1169    genes[0] = gene;
1170    Chromosome chrom = new Chromosome(conf, genes);
1171    conf.setSampleChromosome(chrom);
1172    conf.setPopulationSize(5);
1173    // following command locks configuration
1174
Chromosome.randomInitialChromosome(conf);
1175    try {
1176      conf.setRandomGenerator(new RandomGeneratorForTest(true));
1177      fail();
1178    } catch (InvalidConfigurationException iex) {
1179      ; //this is OK
1180
}
1181  }
1182
1183  /**
1184   *
1185   * @throws Exception
1186   *
1187   * @author Klaus Meffert
1188   */

1189  public void testCleanup_0()
1190      throws Exception JavaDoc {
1191    Configuration conf = new ConfigurationForTest();
1192    IChromosome chrom = Chromosome.randomInitialChromosome(conf);
1193    chrom.cleanup();
1194  }
1195
1196  /**
1197   * @throws Exception
1198   *
1199   * @author Klaus Meffert
1200   * @since 2.2
1201   */

1202  public void testCleanup_2()
1203      throws Exception JavaDoc {
1204    Configuration conf = new ConfigurationForTest();
1205    ChromosomePool chromosomePool = new ChromosomePool();
1206    assertNull(chromosomePool.acquireChromosome());
1207    conf.setChromosomePool(chromosomePool);
1208    IChromosome chrom = Chromosome.randomInitialChromosome(conf);
1209    chrom.cleanup();
1210    assertSame(chrom, chromosomePool.acquireChromosome());
1211  }
1212
1213  /**
1214   * @throws Exception
1215   *
1216   * @author Klaus Meffert
1217   */

1218  public void testSetCompareApplicationData_0()
1219      throws Exception JavaDoc {
1220    Configuration conf = new ConfigurationForTest();
1221    Chromosome chrom = (Chromosome) Chromosome.randomInitialChromosome(conf);
1222    assertFalse(chrom.isCompareApplicationData());
1223    chrom.setCompareApplicationData(true);
1224    assertTrue(chrom.isCompareApplicationData());
1225  }
1226
1227  /**
1228   * No genes.
1229   *
1230   * @throws Exception
1231   *
1232   * @author Klaus Meffert
1233   * @since 2.4
1234   */

1235  public void testToString_0()
1236      throws Exception JavaDoc {
1237    Configuration conf = new ConfigurationForTest();
1238    Chromosome chrom = new Chromosome(conf, 3);
1239    assertEquals(IChromosome.S_SIZE + ":" + chrom.size()
1240                 + ", " + IChromosome.S_FITNESS_VALUE + ":" +
1241                 FitnessFunction.NO_FITNESS_VALUE
1242                 + ", " + IChromosome.S_ALLELES + ":[null, null, null]"
1243                 + ", " + IChromosome.S_APPLICATION_DATA + ":null",
1244                 chrom.toString());
1245  }
1246
1247  /**
1248   * Two genes.
1249   *
1250   * @throws Exception
1251   *
1252   * @author Klaus Meffert
1253   * @since 2.4
1254   */

1255  public void testToString_1()
1256      throws Exception JavaDoc {
1257    Configuration conf = new ConfigurationForTest();
1258    Gene[] genes = new IntegerGene[2];
1259    genes[0] = new IntegerGene(conf, 0, 77);
1260    genes[0].setAllele(new Integer JavaDoc(47));
1261    genes[1] = new IntegerGene(conf, 2, 333);
1262    genes[1].setAllele(new Integer JavaDoc(55));
1263    Chromosome chrom = new Chromosome(conf, 2);
1264    chrom.setFitnessValue(47.11d);
1265    chrom.setGenes(genes);
1266    assertEquals(IChromosome.S_SIZE + ":" + chrom.size()
1267                 + ", " + IChromosome.S_FITNESS_VALUE + ":" +
1268                 47.11d
1269                 + ", " + IChromosome.S_ALLELES + ":[IntegerGene(0,77)=47,"
1270                 + " IntegerGene(2,333)=55]"
1271                 + ", " + IChromosome.S_APPLICATION_DATA + ":null",
1272                 chrom.toString());
1273  }
1274
1275  /**
1276   * Considering application data.
1277   *
1278   * @throws Exception
1279   *
1280   * @author Klaus Meffert
1281   * @since 2.6
1282   */

1283  public void testToString_2()
1284      throws Exception JavaDoc {
1285    Configuration conf = new ConfigurationForTest();
1286    Chromosome chrom = new Chromosome(conf, 3);
1287    chrom.setApplicationData("uIoP");
1288    assertEquals(IChromosome.S_SIZE + ":" + chrom.size()
1289                 + ", " + IChromosome.S_FITNESS_VALUE + ":" +
1290                 FitnessFunction.NO_FITNESS_VALUE
1291                 + ", " + IChromosome.S_ALLELES + ":[null, null, null]"
1292                 + ", " + IChromosome.S_APPLICATION_DATA + ":uIoP",
1293                 chrom.toString());
1294  }
1295
1296  /**
1297   * Test setter/getter of constraint checker.
1298   *
1299   * @throws Exception
1300   *
1301   * @author Klaus Meffert
1302   * @since 2.5
1303   */

1304  public void testSetConstraintChecker_0()
1305      throws Exception JavaDoc {
1306    Chromosome c = new Chromosome(new ConfigurationForTest(), 2);
1307    assertNull(c.getConstraintChecker());
1308    IGeneConstraintChecker cc = new MyConstraintChecker();
1309    c.setConstraintChecker(cc);
1310    assertEquals(cc, c.getConstraintChecker());
1311    c.setConstraintChecker(null);
1312    assertNull(c.getConstraintChecker());
1313  }
1314
1315  /**
1316   * Test setter/getter of constraint checker.
1317   *
1318   * @throws Exception
1319   *
1320   * @author Klaus Meffert
1321   * @since 2.5
1322   */

1323  public void testSetConstraintChecker_1()
1324      throws Exception JavaDoc {
1325    Gene gene = new IntegerGene(conf);
1326    Chromosome c = new Chromosome(conf, gene, 2);
1327    assertNull(c.getConstraintChecker());
1328    IGeneConstraintChecker cc = new MyConstraintChecker(IntegerGene.class);
1329    try {
1330      c.setConstraintChecker(cc);
1331      fail();
1332    } catch (InvalidConfigurationException cex) {
1333      ; //this is OK
1334
}
1335    assertNull(c.getConstraintChecker());
1336  }
1337
1338  /**
1339   * Test setter/getter of constraint checker.
1340   *
1341   * @throws Exception
1342   *
1343   * @author Klaus Meffert
1344   * @since 2.6
1345   */

1346  public void testSetConstraintChecker_2()
1347      throws Exception JavaDoc {
1348    Gene gene = new IntegerGene(conf);
1349    Chromosome c = new Chromosome(conf, gene, 2);
1350    Gene[] genes = new Gene[] {
1351        gene};
1352    c.setConstraintChecker(null);
1353    assertNull(c.getConstraintChecker());
1354    // the following should be possible without exception
1355
c.setGenes(genes);
1356  }
1357
1358  /**
1359   * Test setter/getter of constraint checker which forbids the used gene type.
1360   *
1361   * @throws Exception
1362   *
1363   * @author Klaus Meffert
1364   * @since 2.6
1365   */

1366  public void testSetConstraintChecker_3()
1367      throws Exception JavaDoc {
1368    Gene gene = new IntegerGene(conf);
1369    Chromosome c = new Chromosome(conf, gene, 2);
1370    IGeneConstraintChecker cc = new MyConstraintChecker(IntegerGene.class);
1371    try {
1372      c.setConstraintChecker(cc);
1373      fail();
1374    } catch (InvalidConfigurationException iex) {
1375      ; //this is OK
1376
}
1377  }
1378
1379  /**
1380   * Test setter/getter of constraint checker which allows ther used gene type.
1381   *
1382   * @throws Exception
1383   *
1384   * @author Klaus Meffert
1385   * @since 2.6
1386   */

1387  public void testSetConstraintChecker_4()
1388      throws Exception JavaDoc {
1389    Gene gene = new IntegerGene(conf);
1390    Chromosome c = new Chromosome(conf, gene, 2);
1391    IGeneConstraintChecker cc = new MyConstraintChecker(DoubleGene.class);
1392    c.setConstraintChecker(cc);
1393    Gene[] genes = new Gene[] {
1394        gene};
1395    // the following should be possible without exception
1396
c.setGenes(genes);
1397  }
1398
1399  class MyAppObject
1400      extends TestFitnessFunction implements Cloneable JavaDoc {
1401    public int compareTo(Object JavaDoc o) {
1402      return 0;
1403    }
1404
1405    public Object JavaDoc clone() {
1406      return this;
1407    }
1408  }
1409  class MyAppObject2
1410      extends TestFitnessFunction implements IApplicationData {
1411    public boolean equals(Object JavaDoc o2) {
1412      return compareTo(o2) == 0;
1413    }
1414
1415    public int compareTo(Object JavaDoc o) {
1416      return 0;
1417    }
1418
1419    public Object JavaDoc clone() {
1420      return new MyAppObject2();
1421    }
1422  }
1423  /**
1424   * Class needs to be static, otherwise the serialization of the Chromosome
1425   * does not work properly (in JBuilder it does but running the test with ant
1426   * fails).
1427   *
1428   * @author Klaus Meffert
1429   */

1430  static class MyConstraintChecker
1431      implements IGeneConstraintChecker {
1432    private Class JavaDoc m_forbidden;
1433
1434    public MyConstraintChecker() {
1435      this(null);
1436    }
1437
1438    public MyConstraintChecker(Class JavaDoc a_forbiddenClass) {
1439      m_forbidden = a_forbiddenClass;
1440    }
1441
1442    public boolean verify(final Gene a_gene, final Object JavaDoc a_value,
1443                          final IChromosome a_chrom, final int a_geneIndex) {
1444      if (m_forbidden == null) {
1445        return true;
1446      }
1447      return! (a_gene.getClass().equals(m_forbidden));
1448    }
1449  }
1450  /**
1451   * Ensures Chromosome is implementing Serializable.
1452   *
1453   * @throws Exception
1454   *
1455   * @author Klaus Meffert
1456   * @since 2.6
1457   */

1458  public void testIsSerializable_0()
1459      throws Exception JavaDoc {
1460    Chromosome chrom = new Chromosome(new ConfigurationForTest(), new Gene[] {
1461      new IntegerGene(new ConfigurationForTest(), 1, 5)
1462    });
1463    assertTrue(isSerializable(chrom));
1464  }
1465
1466  /**
1467   * Ensures that Chromosome and all objects contained implement Serializable
1468   * correctly.
1469   *
1470   * @throws Exception
1471   *
1472   * @author Klaus Meffert
1473   * @since 2.6
1474   */

1475  public void testDoSerialize_0()
1476      throws Exception JavaDoc {
1477    // construct chromosome to be serialized
1478
Chromosome chrom = new Chromosome(new ConfigurationForTest(), new Gene[] {
1479      new IntegerGene(new ConfigurationForTest(), 1, 5)
1480    });
1481    IGeneConstraintChecker checker = new MyConstraintChecker();
1482    chrom.setConstraintChecker(checker);
1483    Object JavaDoc o = doSerialize(chrom);
1484    assertEquals(o, chrom);
1485  }
1486
1487  /**
1488   * @throws Exception
1489   *
1490   * @author Klaus Meffert
1491   * @since 2.6
1492   */

1493  public void testIsHandlerFor_0()
1494      throws Exception JavaDoc {
1495    Chromosome chrom = new Chromosome(new ConfigurationForTest(), 3);
1496    assertTrue(chrom.isHandlerFor(chrom, Chromosome.class));
1497    assertFalse(chrom.isHandlerFor(chrom, ChromosomeForTest.class));
1498    assertFalse(chrom.isHandlerFor(chrom, Object JavaDoc.class));
1499    assertTrue(chrom.perform(chrom, Chromosome.class, null) instanceof
1500               Chromosome);
1501  }
1502
1503  /**
1504   * @throws Exception
1505   *
1506   * @author Klaus Meffert
1507   * @since 3.0
1508   */

1509  public void testSetMultiObjectives_0()
1510      throws Exception JavaDoc {
1511    Chromosome chrom = new Chromosome(new ConfigurationForTest(), 3);
1512    List l = new Vector();
1513    l.add("Entry_1");
1514    l.add("Entry_2");
1515    chrom.setMultiObjectives(l);
1516    assertEquals(l, chrom.getMultiObjectives());
1517    List l2 = new Vector();
1518    l2.add("Entry_3");
1519    l2.add("Entry_4");
1520    chrom.setMultiObjectives(l2);
1521    assertEquals(l2, chrom.getMultiObjectives());
1522    assertEquals(2, chrom.getMultiObjectives().size());
1523  }
1524
1525  /**
1526   *
1527   * @throws Exception
1528   *
1529   * @author Klaus Meffert
1530   * @since 3.2
1531   */

1532  public void testPersistentRepresentation_0()
1533      throws Exception JavaDoc {
1534    Configuration conf = new DefaultConfiguration();
1535    Gene[] genes1 = new Gene[2];
1536    genes1[0] = new IntegerGene(conf);
1537    genes1[1] = new BooleanGene(conf);
1538    Chromosome chrom = new Chromosome(conf, genes1);
1539    String JavaDoc repr = chrom.getPersistentRepresentation();
1540    Chromosome chrom2 = new Chromosome(conf);
1541    chrom2.setValueFromPersistentRepresentation(repr);
1542    assertEquals(chrom, chrom2);
1543    assertEquals(chrom.getPersistentRepresentation(),
1544                 chrom2.getPersistentRepresentation());
1545  }
1546
1547  /**
1548   *
1549   * @throws Exception
1550   *
1551   * @author Klaus Meffert
1552   * @since 3.2
1553   */

1554  public void testPersistentRepresentation_1()
1555      throws Exception JavaDoc {
1556    Configuration conf = new DefaultConfiguration();
1557    Gene[] genes1 = new Gene[2];
1558    genes1[0] = new StringGene(conf);
1559    genes1[1] = new DoubleGene(conf);
1560    Chromosome chrom = new Chromosome(conf, genes1);
1561    String JavaDoc repr = chrom.getPersistentRepresentation();
1562    Chromosome chrom2 = new Chromosome(conf);
1563    chrom2.setValueFromPersistentRepresentation(repr);
1564    assertEquals(chrom, chrom2);
1565    assertEquals(chrom.getPersistentRepresentation(),
1566                 chrom2.getPersistentRepresentation());
1567  }
1568
1569  /**
1570   * @throws Exception
1571   *
1572   * @author Klaus Meffert
1573   * @since 3.2
1574   */

1575  public void testPersistentRepresentation_2()
1576      throws Exception JavaDoc {
1577    Chromosome chrom = new Chromosome(conf);
1578    chrom.setValueFromPersistentRepresentation(null);
1579    assertEquals(0, chrom.size());
1580  }
1581
1582  /**
1583   * @throws Exception
1584   *
1585   * @author Klaus Meffert
1586   * @since 3.2
1587   */

1588  public void testPersistentRepresentation_3()
1589      throws Exception JavaDoc {
1590    Chromosome chrom = new Chromosome(conf);
1591    try {
1592      chrom.setValueFromPersistentRepresentation("1"
1593          + Chromosome.CHROM_DELIMITER
1594          + "2");
1595      fail();
1596    } catch (UnsupportedRepresentationException uex) {
1597      ; //this is OK
1598
}
1599  }
1600
1601  /**
1602   * @throws Exception
1603   *
1604   * @author Klaus Meffert
1605   * @since 3.2
1606   */

1607  public void testPersistentRepresentation_4()
1608      throws Exception JavaDoc {
1609    Chromosome chrom = new Chromosome(conf);
1610    try {
1611      chrom.setValueFromPersistentRepresentation("1"
1612          + Chromosome.CHROM_DELIMITER
1613          + "0"
1614          + Chromosome.CHROM_DELIMITER);
1615      fail();
1616    } catch (UnsupportedRepresentationException uex) {
1617      ; //this is OK
1618
}
1619  }
1620
1621  /**
1622   * Invalid closing tag.
1623   *
1624   * @throws Exception
1625   *
1626   * @author Klaus Meffert
1627   * @since 3.2
1628   */

1629  public void testPersistentRepresentation_6()
1630      throws Exception JavaDoc {
1631    Chromosome chrom = new Chromosome(conf);
1632    try {
1633      chrom.setValueFromPersistentRepresentation("47.11"
1634          + Chromosome.CHROM_DELIMITER
1635          + "1"
1636          + Chromosome.CHROM_DELIMITER
1637          + "<" + IntegerGene.class.getName()
1638          + Chromosome.GENE_DELIMITER
1639          + "2<");
1640      fail();
1641    } catch (UnsupportedRepresentationException uex) {
1642      ; //this is OK
1643
}
1644  }
1645
1646  /**
1647   *
1648   * @throws Exception
1649   *
1650   * @author Klaus Meffert
1651   * @since 3.2
1652   */

1653  public void testPersistentRepresentation_7()
1654      throws Exception JavaDoc {
1655    Configuration conf = new DefaultConfiguration();
1656    Gene[] genes1 = new Gene[2];
1657    genes1[0] = new BooleanGene(conf);
1658    genes1[1] = new DoubleGene(conf);
1659    Chromosome chrom = new Chromosome(conf, genes1);
1660    String JavaDoc repr = chrom.getPersistentRepresentation();
1661    Chromosome chrom2 = new Chromosome(conf);
1662    chrom2.setValueFromPersistentRepresentation(repr);
1663    assertEquals(chrom, chrom2);
1664    assertEquals(chrom.getPersistentRepresentation(),
1665                 chrom2.getPersistentRepresentation());
1666  }
1667
1668  /**
1669   * Empty representation.
1670   *
1671   * @throws Exception
1672   *
1673   * @author Klaus Meffert
1674   * @since 3.2
1675   */

1676  public void testPersistentRepresentation_5()
1677      throws Exception JavaDoc {
1678    Chromosome chrom = new Chromosome(conf);
1679    try {
1680      chrom.setValueFromPersistentRepresentation("47.11"
1681          + Chromosome.CHROM_DELIMITER
1682          + "1"
1683          + Chromosome.CHROM_DELIMITER
1684          + "<" + IntegerGene.class.getName()
1685          + Chromosome.GENE_DELIMITER
1686          + "2:4:4"
1687          + Chromosome.GENE_DELIMITER
1688          + "><>");
1689      fail();
1690    } catch (UnsupportedRepresentationException uex) {
1691      ; //this is OK
1692
assertEquals(1, chrom.size());
1693    }
1694  }
1695}
1696
Popular Tags