KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.impl;
11
12 import java.util.*;
13 import org.jgap.*;
14 import junit.framework.*;
15
16 /**
17  * Tests the CrossoverOperator class.
18  *
19  * @author Klaus Meffert
20  * @since 1.1
21  */

22 public class CrossoverOperatorTest
23     extends JGAPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private static final String JavaDoc CVS_REVISION = "$Revision: 1.26 $";
26
27   public static Test suite() {
28     TestSuite suite = new TestSuite(CrossoverOperatorTest.class);
29     return suite;
30   }
31
32   public void setUp() {
33     super.setUp();
34     Configuration.reset();
35   }
36
37   /**
38    * Following should be possible without error.
39    * @throws Exception
40    *
41    * @author Klaus Meffert
42    */

43   public void testConstruct_0()
44       throws Exception JavaDoc {
45     new CrossoverOperator(conf, null);
46     new CrossoverOperator(conf, new DefaultMutationRateCalculator(conf));
47     new CrossoverOperator(conf, 2);
48     new CrossoverOperator(new DefaultConfiguration(), 1);
49     new CrossoverOperator(conf, 50);
50   }
51
52   /**
53    * @throws Exception
54    *
55    * @author Klaus Meffert
56    */

57   public void testConstruct_1()
58       throws Exception JavaDoc {
59     try {
60       new CrossoverOperator(conf, 0);
61       fail();
62     }
63     catch (IllegalArgumentException JavaDoc iex) {
64       ; //this is OK
65
}
66   }
67
68   /**
69    * @throws Exception
70    *
71    * @author Klaus Meffert
72    */

73   public void testConstruct_2()
74       throws Exception JavaDoc {
75     try {
76       new CrossoverOperator(new DefaultConfiguration(), -3);
77       fail();
78     }
79     catch (IllegalArgumentException JavaDoc iex) {
80       ; //this is OK
81
}
82   }
83
84   /**
85    * Use flat crossover rate and just exchange two alleles via crossover.
86    * @throws Exception
87    *
88    * @author Klaus Meffert
89    * @since 1.1
90    */

91   public void testOperate_0()
92       throws Exception JavaDoc {
93     DefaultConfiguration conf = new DefaultConfiguration();
94 // conf.addGeneticOperator(op);
95
// preset "random" values: index first chromosome, index second chromosome,
96
// locus (index of gene on chromosome)
97
RandomGeneratorForTest rand = new RandomGeneratorForTest();
98     rand.setNextIntSequence(new int[] {
99                             0, 1, 0});
100     conf.setRandomGenerator(rand);
101     conf.setFitnessFunction(new TestFitnessFunction());
102     Gene sampleGene = new IntegerGene(conf, 1, 10);
103     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
104     conf.setSampleChromosome(chrom);
105     conf.setPopulationSize(6);
106     Gene cgene1 = new IntegerGene(conf, 1, 100);
107     cgene1.setAllele(new Integer JavaDoc(66));
108     Gene[] genes1 = new Gene[] {
109         cgene1};
110     Chromosome chrom1 = new Chromosome(conf, genes1);
111     Gene cgene2 = new IntegerGene(conf, 1, 100);
112     cgene2.setAllele(new Integer JavaDoc(88));
113     Gene[] genes2 = new Gene[] {
114         cgene2};
115     Chromosome chrom2 = new Chromosome(conf, genes2);
116     Chromosome[] population = new Chromosome[] {
117         chrom1, chrom2};
118     List chroms = new Vector();
119     Gene gene1 = new IntegerGene(conf, 1, 10);
120     gene1.setAllele(new Integer JavaDoc(5));
121     chroms.add(gene1);
122     Gene gene2 = new IntegerGene(conf, 1, 10);
123     gene2.setAllele(new Integer JavaDoc(7));
124     chroms.add(gene2);
125     Gene gene3 = new IntegerGene(conf, 1, 10);
126     gene3.setAllele(new Integer JavaDoc(4));
127     chroms.add(gene3);
128     CrossoverOperator op = new CrossoverOperator(conf);
129     // following crossover-operation should exchange alleles of cgene1 and
130
// cgene2.
131
op.operate(new Population(conf, population), chroms);
132     // chroms size = 3 + 2 genes
133
// 3 = number of already existent genes
134
// 2 = number of genes added from "population" (which contains
135
// 2 genes)
136
assertEquals(5, chroms.size());
137     // get Gene 3 = first new gene (0..2 = old genes, 3..4 = new genes)
138
Chromosome target = (Chromosome) chroms.get(3);
139     // 88 = allele of cgene2
140
assertEquals(88, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
141     // get Gene 4 = second new gene (0..2 = old genes, 3..4 = new genes)
142
target = (Chromosome) chroms.get(4);
143     // 66 = allele of cgene1
144
assertEquals(66, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
145   }
146
147   /**
148    * Consider crossover rate calculator.
149    *
150    * @throws Exception
151    *
152    * @author Klaus Meffert
153    * @since 2.6
154    */

155   public void testOperate_0_2()
156       throws Exception JavaDoc {
157     DefaultConfiguration conf = new DefaultConfiguration();
158 // conf.addGeneticOperator(op);
159
// preset "random" values: index first chromosome, index second chromosome,
160
// locus (index of gene on chromosome)
161
RandomGeneratorForTest rand = new RandomGeneratorForTest();
162     rand.setNextIntSequence(new int[] {
163                             0, 1, 0});
164     conf.setRandomGenerator(rand);
165     conf.setFitnessFunction(new TestFitnessFunction());
166     Gene sampleGene = new IntegerGene(conf, 1, 10);
167     Chromosome chrom = new Chromosome(conf, sampleGene, 2);
168     conf.setSampleChromosome(chrom);
169     conf.setPopulationSize(6);
170     Gene cgene1 = new IntegerGene(conf, 1, 10);
171     cgene1.setAllele(new Integer JavaDoc(6));
172     Gene[] genes1 = new Gene[] {
173         cgene1};
174     Chromosome chrom1 = new Chromosome(conf, genes1);
175     Gene cgene2 = new IntegerGene(conf, 1, 10);
176     cgene2.setAllele(new Integer JavaDoc(8));
177     Gene[] genes2 = new Gene[] {
178         cgene2};
179     Chromosome chrom2 = new Chromosome(conf, genes2);
180     Chromosome[] population = new Chromosome[] {
181         chrom1, chrom2};
182     List chroms = new Vector();
183     Gene gene1 = new IntegerGene(conf, 1, 10);
184     gene1.setAllele(new Integer JavaDoc(5));
185     chroms.add(gene1);
186     CrossoverOperator op = new CrossoverOperator(conf,
187                                                  new
188                                                  DefaultCrossoverRateCalculator(
189         conf));
190     op.operate(new Population(conf, population), chroms);
191     // chroms size = 1 + 2 genes
192
// 1 = number of already existent genes
193
// 2 = number of genes added from "population" (which contains
194
// 2 genes)
195
assertEquals(1 + 2, chroms.size());
196     // get Gene 1 = first new gene (0..0 = old genes, 1..2 = new genes)
197
Chromosome target = (Chromosome) chroms.get(1);
198     assertEquals(8, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
199     // get Gene 2 = second new gene (0..0 = old genes, 1..2 = new genes)
200
target = (Chromosome) chroms.get(2);
201     assertEquals(6, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
202     // chroms size = 1 + 2 + 2 genes
203
// 1 + 2 = number of already existent genes
204
// 2 = number of genes added from "population" (which
205
// contains 2 genes)
206
op.operate(new Population(conf, population), chroms);
207     assertEquals(1 + 2 + 2, chroms.size());
208   }
209
210   /**
211    * Tests if crossing over produces same results for two operate-runs.
212    *
213    * @throws Exception
214    *
215    * @author Klaus Meffert
216    * @since 2.0
217    */

218   public void testOperate_1()
219       throws Exception JavaDoc {
220     DefaultConfiguration conf = new DefaultConfiguration();
221 // conf.addGeneticOperator(op);
222
RandomGeneratorForTest rand = new RandomGeneratorForTest();
223     rand.setNextIntSequence(new int[] {
224                             0, 1, 0, 1, 2});
225     conf.setRandomGenerator(rand);
226     conf.setFitnessFunction(new TestFitnessFunction());
227     Gene sampleGene = new IntegerGene(conf, 1, 10);
228     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
229     conf.setSampleChromosome(chrom);
230     conf.setPopulationSize(6);
231     Gene cgene1 = new IntegerGene(conf, 1, 10);
232     cgene1.setAllele(new Integer JavaDoc(6));
233     Gene[] genes1 = new Gene[] {
234         cgene1};
235     Chromosome chrom1 = new Chromosome(conf, genes1);
236     Gene cgene2 = new IntegerGene(conf, 1, 10);
237     cgene2.setAllele(new Integer JavaDoc(8));
238     Gene[] genes2 = new Gene[] {
239         cgene2};
240     Chromosome chrom2 = new Chromosome(conf, genes2);
241     Chromosome[] population = new Chromosome[] {
242         chrom1, chrom2};
243     List chroms = new Vector();
244     // add some genes to chroms that should not be overridden
245
Gene gene1 = new IntegerGene(conf, 1, 10);
246     gene1.setAllele(new Integer JavaDoc(5));
247     chroms.add(gene1);
248     Gene gene2 = new IntegerGene(conf, 1, 10);
249     gene2.setAllele(new Integer JavaDoc(7));
250     chroms.add(gene2);
251     Chromosome[] population2 = (Chromosome[]) population.clone();
252     GeneticOperator op = new CrossoverOperator(conf);
253     op.operate(new Population(conf, population), chroms);
254     op.operate(new Population(conf, population2), chroms);
255     assertTrue(isChromosomesEqual(population, population2));
256     // check that original genes have not been modified
257
assertSame(gene1, chroms.get(0));
258     assertSame(gene2, chroms.get(1));
259   }
260
261   /**
262    * Test with CompositeGene.
263    *
264    * @throws Exception
265    *
266    * @author Klaus Meffert
267    * @since 2.1
268    */

269   public void testOperate_2()
270       throws Exception JavaDoc {
271     DefaultConfiguration conf = new DefaultConfiguration();
272     RandomGeneratorForTest rand = new RandomGeneratorForTest();
273     rand.setNextIntSequence(new int[] {
274                             0, 1, 0, 1, 2});
275     conf.setRandomGenerator(rand);
276     conf.setFitnessFunction(new TestFitnessFunction());
277     Gene sampleGene = new IntegerGene(conf, 1, 10);
278     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
279     conf.setSampleChromosome(chrom);
280     conf.setPopulationSize(6);
281     Gene cgene1 = new IntegerGene(conf, 1, 10);
282     cgene1.setAllele(new Integer JavaDoc(6));
283     CompositeGene compGene = new CompositeGene(conf);
284     compGene.addGene(cgene1);
285     Gene[] genes1 = new Gene[] {
286         compGene};
287     Chromosome chrom1 = new Chromosome(conf, genes1);
288     Gene cgene2 = new IntegerGene(conf, 1, 10);
289     cgene2.setAllele(new Integer JavaDoc(8));
290     Gene[] genes2 = new Gene[] {
291         cgene2};
292     Chromosome chrom2 = new Chromosome(conf, genes2);
293     Chromosome[] population = new Chromosome[] {
294         chrom1, chrom2};
295     List chroms = new Vector();
296     Gene gene1 = new IntegerGene(conf, 1, 10);
297     gene1.setAllele(new Integer JavaDoc(5));
298     chroms.add(gene1);
299     Gene gene2 = new IntegerGene(conf, 1, 10);
300     gene2.setAllele(new Integer JavaDoc(7));
301     chroms.add(gene2);
302     CrossoverOperator op = new CrossoverOperator(conf);
303     // Do the crossing over.
304
// ---------------------
305
op.operate(new Population(conf, population), chroms);
306     // new size of chroms = 2 (original chromosomes) + 2 (from "population")
307
assertEquals(2 + 2, chroms.size());
308     Chromosome target = (Chromosome) chroms.get(2);
309     CompositeGene result = (CompositeGene) target.getGene(0);
310     assertEquals(8, ( (Integer JavaDoc) ( (Vector) result.getAllele())
311                      .get(0)).intValue());
312     target = (Chromosome) chroms.get(3);
313     assertEquals(6, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
314   }
315
316   /**
317    * Considers IGeneticOperatorConstraint. Here, the crossing over of a
318    * BooleanGene is forbidden by that constraint.
319    *
320    * @throws Exception
321    *
322    * @author Klaus Meffert
323    * @since 2.6
324    */

325   public void testOperate_3()
326       throws Exception JavaDoc {
327     DefaultConfiguration conf = new DefaultConfiguration();
328     IGeneticOperatorConstraint constraint = new
329         GeneticOperatorConstraintForTest();
330     conf.getJGAPFactory().setGeneticOperatorConstraint(constraint);
331     RandomGeneratorForTest rand = new RandomGeneratorForTest();
332     rand.setNextIntSequence(new int[] {
333                             0, 2, 0, 1, 2});
334     conf.setRandomGenerator(rand);
335     conf.setFitnessFunction(new TestFitnessFunction());
336     Gene sampleGene = new IntegerGene(conf, 1, 10);
337     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
338     conf.setSampleChromosome(chrom);
339     conf.setPopulationSize(6);
340     Gene cgene0 = new IntegerGene(conf, 1, 10);
341     cgene0.setAllele(new Integer JavaDoc(8));
342     Gene[] genes0 = new Gene[] {
343         cgene0};
344     Chromosome chrom0 = new Chromosome(conf, genes0);
345     Gene cgene1 = new IntegerGene(conf, 1, 10);
346     cgene1.setAllele(new Integer JavaDoc(5));
347     Gene[] genes1 = new Gene[] {
348         cgene1};
349     ChromosomeForTest chrom1 = new ChromosomeForTest(conf, genes1);
350     Gene cgene2 = new IntegerGene(conf, 1, 10);
351     cgene2.setAllele(new Integer JavaDoc(6));
352     Gene[] genes2 = new Gene[] {
353         cgene2};
354     Chromosome chrom2 = new Chromosome(conf, genes2);
355     Chromosome[] population = new Chromosome[] {
356         chrom0, chrom1, chrom2};
357     // Add some nonsense objects to results list (to see if they are kept).
358
List chroms = new Vector();
359     Gene gene1 = new IntegerGene(conf, 1, 10);
360     gene1.setAllele(new Integer JavaDoc(5));
361     chroms.add(gene1);
362     Gene gene2 = new IntegerGene(conf, 1, 10);
363     gene2.setAllele(new Integer JavaDoc(7));
364     chroms.add(gene2);
365     CrossoverOperator op = new CrossoverOperator(conf);
366     // Do the crossing over.
367
// ---------------------
368
op.operate(new Population(conf, population), chroms);
369     assertEquals(2 + 2, chroms.size());
370     assertSame(gene1, chroms.get(0));
371     assertSame(gene2, chroms.get(1));
372     Chromosome target = (Chromosome) chroms.get(2);
373     assertEquals(6, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
374     target = (Chromosome) chroms.get(3);
375     assertEquals(8, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
376   }
377
378   /**
379    * Ensures the operator is implementing Serializable.
380    * @throws Exception
381    *
382    * @author Klaus Meffert
383    * @since 2.6
384    */

385   public void testIsSerializable_0()
386       throws Exception JavaDoc {
387     CrossoverOperator op = new CrossoverOperator(conf);
388     assertTrue(isSerializable(op));
389   }
390
391   /**
392    * Ensures that the operator and all objects contained implement Serializable.
393    * Here, we use a null configuration.
394    * @throws Exception
395    *
396    * @author Klaus Meffert
397    * @since 2.6
398    */

399   public void testDoSerialize_0()
400       throws Exception JavaDoc {
401     // construct object to be serialized
402
CrossoverOperator op = new CrossoverOperator(conf,
403                                                  new
404                                                  DefaultCrossoverRateCalculator(
405         conf));
406     CrossoverOperator o = (CrossoverOperator) doSerialize(op);
407     assertEquals(o, op);
408   }
409
410   /**
411    * Ensures that the operator and all objects contained implement Serializable.
412    * Here, we set a configuration.
413    * @throws Exception
414    *
415    * @author Klaus Meffert
416    * @since 2.6
417    */

418   public void testDoSerialize_1()
419       throws Exception JavaDoc {
420     Configuration conf = new DefaultConfiguration();
421     // construct object to be serialized
422
CrossoverOperator op = new CrossoverOperator(conf,
423                                                  new
424                                                  DefaultCrossoverRateCalculator(
425         conf));
426     CrossoverOperator o = (CrossoverOperator) doSerialize(op);
427     assertEquals(o, op);
428   }
429
430   public class GeneticOperatorConstraintForTest
431       implements IGeneticOperatorConstraint {
432     public boolean isValid(Population a_pop, List a_chromosomes,
433                            GeneticOperator a_caller) {
434       Iterator it = a_chromosomes.iterator();
435       while (it.hasNext()) {
436         Chromosome chrom = (Chromosome) it.next();
437         if (ChromosomeForTest.class == chrom.getClass()) {
438           return false;
439         }
440       }
441       return true;
442     }
443   }
444   /**
445    * Test equals with classcast object.
446    *
447    * @throws Exception
448    * @author Klaus Meffert
449    * @since 2.6
450    */

451   public void testEquals_0()
452       throws Exception JavaDoc {
453     GeneticOperator op = new CrossoverOperator(conf);
454     assertFalse(op.equals(new Chromosome(conf)));
455   }
456
457   /**
458    * @throws Exception
459    *
460    * @author Klaus Meffert
461    * @since 2.6
462    */

463   public void testCompareTo_0()
464       throws Exception JavaDoc {
465     CrossoverOperator op = new CrossoverOperator(conf);
466     assertEquals(1, op.compareTo(null));
467     CrossoverOperator op2 = new CrossoverOperator(conf);
468     assertEquals(0, op.compareTo(op2));
469     op = new CrossoverOperator(conf, 3);
470     assertEquals(1, op.compareTo(op2));
471     assertEquals( -1, op2.compareTo(op));
472     op = new CrossoverOperator(conf, new DefaultCrossoverRateCalculator(conf));
473     assertEquals(1, op.compareTo(op2));
474     assertEquals( -1, op2.compareTo(op));
475     op2 = new CrossoverOperator(conf, new DefaultCrossoverRateCalculator(conf));
476     assertEquals(0, op.compareTo(op2));
477   }
478 }
479
Popular Tags