KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Teste the GreedyCrossover class.
18  *
19  * @author Klaus Meffert
20  * @since 2.1
21  */

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

43   public void testOperate_0()
44       throws Exception JavaDoc {
45     DefaultConfiguration conf = new DefaultConfiguration();
46     RandomGeneratorForTest rand = new RandomGeneratorForTest();
47     rand.setNextIntSequence(new int[] {
48                             0, 1, 0, 1, 2});
49     conf.setRandomGenerator(rand);
50     conf.setFitnessFunction(new TestFitnessFunction());
51     Gene sampleGene = new IntegerGene(conf, 1, 10);
52     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
53     conf.setSampleChromosome(chrom);
54     conf.setPopulationSize(6);
55     GreedyCrossover op = new GreedyCrossover(conf);
56     op.ASSERTIONS = true;
57     op.setStartOffset(0);
58     Gene cgene1 = new IntegerGene(conf, 1, 10);
59     cgene1.setAllele(new Integer JavaDoc(6));
60     Gene cgene2 = new IntegerGene(conf, 1, 10);
61     cgene2.setAllele(new Integer JavaDoc(8));
62     Gene[] genes1 = new Gene[] {
63         cgene1, cgene2};
64     Chromosome chrom1 = new Chromosome(conf, genes1);
65     Gene[] genes2 = new Gene[] {
66         cgene2, cgene1};
67     Chromosome chrom2 = new Chromosome(conf, genes2);
68     Chromosome[] population = new Chromosome[] {
69         chrom1, chrom2};
70     List chroms = new Vector();
71     Gene gene1 = new IntegerGene(conf, 1, 10);
72     gene1.setAllele(new Integer JavaDoc(5));
73     chroms.add(gene1);
74     Gene gene2 = new IntegerGene(conf, 1, 10);
75     gene2.setAllele(new Integer JavaDoc(7));
76     chroms.add(gene2);
77     Gene gene3 = new IntegerGene(conf, 1, 10);
78     gene3.setAllele(new Integer JavaDoc(4));
79     chroms.add(gene3);
80     op.operate(new Population(conf, population), chroms);
81     assertEquals(5, chroms.size());
82     Chromosome target = (Chromosome) chroms.get(4);
83     assertEquals(8, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
84     target = (Chromosome) chroms.get(3);
85     assertEquals(6, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
86   }
87
88   /**
89    * Same as testOperate_0 except op.setStartOffset(1) instead of 0.
90    * @throws Exception
91    *
92    * @author Klaus Meffert
93    * @since 2.1
94    */

95   public void testOperate_1()
96       throws Exception JavaDoc {
97     DefaultConfiguration conf = new DefaultConfiguration();
98     RandomGeneratorForTest rand = new RandomGeneratorForTest();
99     rand.setNextIntSequence(new int[] {
100                             0, 1, 0, 1, 2});
101     conf.setRandomGenerator(rand);
102     conf.setFitnessFunction(new TestFitnessFunction());
103     Gene sampleGene = new IntegerGene(conf, 1, 10);
104     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
105     conf.setSampleChromosome(chrom);
106     conf.setPopulationSize(6);
107     GreedyCrossover op = new GreedyCrossover(conf);
108     op.ASSERTIONS = true;
109     op.setStartOffset(1);
110     Gene cgene1 = new IntegerGene(conf, 1, 10);
111     cgene1.setAllele(new Integer JavaDoc(6));
112     Gene cgene2 = new IntegerGene(conf, 1, 10);
113     cgene2.setAllele(new Integer JavaDoc(8));
114     Gene[] genes1 = new Gene[] {
115         cgene1, cgene2};
116     Chromosome chrom1 = new Chromosome(conf, genes1);
117     Gene[] genes2 = new Gene[] {
118         cgene2, cgene1};
119     Chromosome chrom2 = new Chromosome(conf, genes2);
120     Chromosome[] population = new Chromosome[] {
121         chrom1, chrom2};
122     List chroms = new Vector();
123     Gene gene1 = new IntegerGene(conf, 1, 10);
124     gene1.setAllele(new Integer JavaDoc(5));
125     chroms.add(gene1);
126     Gene gene2 = new IntegerGene(conf, 1, 10);
127     gene2.setAllele(new Integer JavaDoc(7));
128     chroms.add(gene2);
129     try {
130       op.operate(new Population(conf, population), chroms);
131       fail();
132     }
133     catch (Error JavaDoc e) {
134       ; //this is OK
135
}
136   }
137
138   /**
139    * Test with CompositeGene.
140    * @throws Exception
141    *
142    * @author Klaus Meffert
143    * @since 2.1
144    */

145   public void testOperate_2()
146       throws Exception JavaDoc {
147     DefaultConfiguration conf = new DefaultConfiguration();
148     RandomGeneratorForTest rand = new RandomGeneratorForTest();
149     rand.setNextIntSequence(new int[] {
150                             0, 1, 0, 1, 2});
151     conf.setRandomGenerator(rand);
152     conf.setFitnessFunction(new TestFitnessFunction());
153     GreedyCrossover op = new GreedyCrossover(conf);
154     op.ASSERTIONS = true;
155     Gene sampleGene = new IntegerGene(conf, 1, 10);
156     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
157     conf.setSampleChromosome(chrom);
158     conf.setPopulationSize(6);
159     Gene cgene1 = new IntegerGene(conf, 1, 10);
160     cgene1.setAllele(new Integer JavaDoc(6));
161     CompositeGene compGene = new CompositeGene(conf);
162     compGene.addGene(cgene1);
163     Gene cgene2 = new IntegerGene(conf, 1, 10);
164     cgene2.setAllele(new Integer JavaDoc(8));
165     Gene[] genes1 = new Gene[] {
166         cgene1, cgene2};
167     Chromosome chrom1 = new Chromosome(conf, genes1);
168     Gene[] genes2 = new Gene[] {
169         cgene1, cgene2, cgene1};
170     Chromosome chrom2 = new Chromosome(conf, genes2);
171     Chromosome[] population = new Chromosome[] {
172         chrom1, chrom2};
173     List chroms = new Vector();
174     Gene gene1 = new IntegerGene(conf, 1, 10);
175     gene1.setAllele(new Integer JavaDoc(5));
176     chroms.add(gene1);
177     try {
178       op.operate(new Population(conf, population), chroms);
179       fail();
180     }
181     catch (Error JavaDoc e) {
182       ; //this is OK
183
}
184   }
185
186   /**
187    * Test with CompositeGene and two identical Genes in a Chromosome.
188    * @throws Exception
189    *
190    * @author Klaus Meffert
191    * @since 2.1
192    */

193   public void testOperate_3()
194       throws Exception JavaDoc {
195     DefaultConfiguration conf = new DefaultConfiguration();
196     RandomGeneratorForTest rand = new RandomGeneratorForTest();
197     rand.setNextIntSequence(new int[] {
198                             0, 1, 0, 1, 2});
199     conf.setRandomGenerator(rand);
200     conf.setFitnessFunction(new TestFitnessFunction());
201     GreedyCrossover op = new GreedyCrossover(conf);
202     op.ASSERTIONS = true;
203     op.setStartOffset(0);
204     Gene sampleGene = new IntegerGene(conf, 1, 10);
205     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
206     conf.setSampleChromosome(chrom);
207     conf.setPopulationSize(6);
208     Gene cgene1 = new IntegerGene(conf, 1, 10);
209     cgene1.setAllele(new Integer JavaDoc(6));
210     CompositeGene compGene = new CompositeGene(conf);
211     compGene.addGene(cgene1);
212     Gene cgene2 = new IntegerGene(conf, 1, 10);
213     cgene2.setAllele(new Integer JavaDoc(8));
214     Gene[] genes1 = new Gene[] {
215         compGene, cgene1, cgene1};
216     Chromosome chrom1 = new Chromosome(conf, genes1);
217     Gene[] genes2 = new Gene[] {
218         compGene, cgene1, cgene1};
219     Chromosome chrom2 = new Chromosome(conf, genes2);
220     Chromosome[] population = new Chromosome[] {
221         chrom1, chrom2};
222     List chroms = new Vector();
223     Gene gene1 = new IntegerGene(conf, 1, 10);
224     gene1.setAllele(new Integer JavaDoc(5));
225     chroms.add(gene1);
226     Gene gene2 = new IntegerGene(conf, 1, 10);
227     gene2.setAllele(new Integer JavaDoc(7));
228     chroms.add(gene2);
229     Gene gene3 = new IntegerGene(conf, 1, 10);
230     gene3.setAllele(new Integer JavaDoc(4));
231     chroms.add(gene3);
232     try {
233       op.operate(new Population(conf, population), chroms);
234       fail();
235     }
236     catch (Error JavaDoc e) {
237       ; //this is OK
238
}
239   }
240
241   /**
242    * Test the example from the literature.
243    * This test tests the crossover main algorithm, not the whole operator.
244    * @throws Exception
245    *
246    * @author Audrius Meskauskas
247    * @since 2.1
248    */

249   public void testOperate_4()
250       throws Exception JavaDoc {
251     Configuration conf = new DefaultConfiguration();
252     conf.setFitnessFunction(new TestFitnessFunction());
253     GreedyCrossover cross = new GreedyCrossover(conf) {
254       /* Computes the distances how it was described in the
255          literature example */

256       public double distance(Object JavaDoc a_from, Object JavaDoc a_to) {
257         IntegerGene from = (IntegerGene) a_from;
258         IntegerGene to = (IntegerGene) a_to;
259         int a = from.intValue();
260         int b = to.intValue();
261         if (a > b) {
262           int t = a;
263           a = b;
264           b = t;
265         }
266         ;
267         // 4,1 is shorter than 4,5
268
if (a == 1 && b == 4) {
269           return 1;
270         }
271         if (a == 4 && b == 5) {
272           return 2;
273         }
274         // 1,2 is shorter that 1,3
275
if (a == 1 && b == 2) {
276           return 10;
277         }
278         if (a == 1 && b == 3) {
279           return 20;
280         }
281         // 2,0 is shorter than 2,3
282
if (a == 0 && b == 2) {
283           return 100;
284         }
285         if (a == 2 && b == 3) {
286           return 200;
287         }
288         throw new Error JavaDoc("These two should not be compared: " + a + " and " + b);
289       }
290     };
291     cross.ASSERTIONS = true;
292     cross.setStartOffset(0);
293     Chromosome a = chromosome(new int[] {1, 2, 3, 4, 5, 0});
294     Chromosome b = chromosome(new int[] {4, 1, 3, 2, 0, 5});
295     // in the literature example it was 1, 2, 0, 5, 4, 3, but the random
296
// choice is involved in the last step. In this implementation
297
// the choice is not random and the last two genes are always
298
// returned as 3, 4.
299
// -----------------------------------------------------------------
300
Chromosome must_a = chromosome(new int[] {1, 2, 0, 5, 3, 4});
301     // this is same as in the literature, the random choice is not involved.
302
// ---------------------------------------------------------------------
303
Chromosome must_b = chromosome(new int[] {4, 1, 2, 0, 5, 3});
304     cross.operate(b, a);
305     assertEquals(a, must_a);
306     assertEquals(b, must_b);
307   }
308
309   /**
310    * Tests if population size grows expectedly after two consecutive calls.
311    * @throws Exception
312    *
313    * @author Klaus Meffert
314    * @since 2.1
315    */

316   public void testOperate_5()
317       throws Exception JavaDoc {
318     DefaultConfiguration conf = new DefaultConfiguration();
319     GreedyCrossover op = new GreedyCrossover(conf);
320     op.ASSERTIONS = true;
321     conf.addGeneticOperator(op);
322     RandomGeneratorForTest rand = new RandomGeneratorForTest();
323     rand.setNextDouble(0.45d);
324     rand.setNextInt(0);
325     op.setStartOffset(0);
326     conf.setRandomGenerator(rand);
327     conf.setFitnessFunction(new TestFitnessFunction());
328     Gene sampleGene = new IntegerGene(conf, 1, 10);
329     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
330     conf.setSampleChromosome(chrom);
331     conf.setPopulationSize(6);
332     Gene cgene1 = new IntegerGene(conf, 1, 10);
333     cgene1.setAllele(new Integer JavaDoc(6));
334     Gene cgene1_2 = new IntegerGene(conf, 1, 150);
335     cgene1.setAllele(new Integer JavaDoc(99));
336     Gene[] genes1 = new Gene[] {
337         cgene1, cgene1_2};
338     Chromosome chrom1 = new Chromosome(conf, genes1);
339     Gene cgene2 = new IntegerGene(conf, 1, 10);
340     cgene2.setAllele(new Integer JavaDoc(9));
341     Gene cgene2_2 = new IntegerGene(conf, 1, 10);
342     cgene2.setAllele(new Integer JavaDoc(1));
343     Gene[] genes2 = new Gene[] {
344         cgene2, cgene2_2};
345     Chromosome chrom2 = new Chromosome(conf, genes2);
346     Chromosome[] population = new Chromosome[] {
347         chrom1, chrom2};
348     List chroms = new Vector();
349     Gene gene1 = new IntegerGene(conf, 1, 10);
350     gene1.setAllele(new Integer JavaDoc(5));
351     chroms.add(gene1);
352     Gene gene2 = new IntegerGene(conf, 1, 10);
353     gene2.setAllele(new Integer JavaDoc(7));
354     chroms.add(gene2);
355     Gene gene3 = new IntegerGene(conf, 1, 10);
356     gene3.setAllele(new Integer JavaDoc(4));
357     chroms.add(gene3);
358     assertEquals(3, chroms.size());
359     Population pop = new Population(conf, population);
360     op.operate(pop, chroms);
361     assertEquals(2, pop.size());
362     assertEquals(3 + 2, chroms.size());
363     op.operate(pop, chroms);
364     assertEquals(2, pop.size());
365     assertEquals(3 + 2 + 2, chroms.size());
366   }
367
368   /**
369    * Tests if error thrown because of wrong length of gene.
370    * @throws Exception
371    *
372    * @author Klaus Meffert
373    * @since 2.4
374    */

375   public void testOperate_6()
376       throws Exception JavaDoc {
377     DefaultConfiguration conf = new DefaultConfiguration();
378     GreedyCrossover op = new GreedyCrossover(conf);
379     op.ASSERTIONS = true;
380     conf.addGeneticOperator(op);
381     RandomGeneratorForTest rand = new RandomGeneratorForTest();
382     rand.setNextDouble(0.45d);
383     rand.setNextInt(0);
384     op.setStartOffset(0);
385     conf.setRandomGenerator(rand);
386     conf.setFitnessFunction(new TestFitnessFunction());
387     Gene sampleGene = new IntegerGene(conf, 1, 10);
388     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
389     conf.setSampleChromosome(chrom);
390     conf.setPopulationSize(6);
391     Gene cgene1 = new IntegerGene(conf, 1, 10);
392     cgene1.setAllele(new Integer JavaDoc(6));
393     Gene[] genes1 = new Gene[] {
394         cgene1};
395     Chromosome chrom1 = new Chromosome(conf, genes1);
396     Gene cgene2 = new IntegerGene(conf, 1, 10);
397     cgene2.setAllele(new Integer JavaDoc(9));
398     Gene[] genes2 = new Gene[] {
399         cgene2};
400     Chromosome chrom2 = new Chromosome(conf, genes2);
401     Chromosome[] population = new Chromosome[] {
402         chrom1, chrom2};
403     List chroms = new Vector();
404     Gene gene1 = new IntegerGene(conf, 1, 10);
405     gene1.setAllele(new Integer JavaDoc(5));
406     chroms.add(gene1);
407     Gene gene2 = new IntegerGene(conf, 1, 10);
408     gene2.setAllele(new Integer JavaDoc(7));
409     chroms.add(gene2);
410     Gene gene3 = new IntegerGene(conf, 1, 10);
411     gene3.setAllele(new Integer JavaDoc(4));
412     chroms.add(gene3);
413     assertEquals(3, chroms.size());
414     Population pop = new Population(conf, population);
415     try {
416       op.operate(pop, chroms);
417       fail();
418     }
419     catch (Error JavaDoc e) {
420       ; //this is OK
421
}
422   }
423
424   /**
425    * Make a chromosome from the array of integer genes.
426    * @param a_genes input genes
427    * @return chromosome containing input genes
428    * @throws Exception
429    *
430    * @author Audrius Meskauskas
431    * @since 2.1
432    */

433   private Chromosome chromosome(int[] a_genes)
434       throws Exception JavaDoc {
435     IntegerGene[] ig = new IntegerGene[a_genes.length];
436     for (int i = 0; i < ig.length; i++) {
437       ig[i] = new IntegerGene(conf, 0, 5);
438       ig[i].setAllele(new Integer JavaDoc(a_genes[i]));
439     }
440     return new Chromosome(conf, ig);
441   }
442
443   /**
444    * @throws Exception
445    *
446    * @author Klaus Meffert
447    * @since 2.2
448    */

449   public void testStartoffset_0()
450       throws Exception JavaDoc {
451     GreedyCrossover op = new GreedyCrossover(conf);
452     assertEquals(1, op.getStartOffset());
453     op.setStartOffset(2);
454     assertEquals(2, op.getStartOffset());
455     op.setStartOffset(1);
456     assertEquals(1, op.getStartOffset());
457     op.setStartOffset(0);
458     assertEquals(0, op.getStartOffset());
459   }
460
461   /**
462    * Ensures the operator is implementing Serializable
463    * @throws Exception
464    *
465    * @author Klaus Meffert
466    * @since 2.6
467    */

468   public void testIsSerializable_0()
469       throws Exception JavaDoc {
470     GreedyCrossover op = new GreedyCrossover(conf);
471     assertTrue(isSerializable(op));
472   }
473
474   /**
475    * Ensures that the operator and all objects contained implement Serializable
476    * @throws Exception
477    *
478    * @author Klaus Meffert
479    * @since 2.6
480    */

481   public void testDoSerialize_0()
482       throws Exception JavaDoc {
483     // construct object to be serialized
484
GreedyCrossover op = new GreedyCrossover(conf);
485     GreedyCrossover o = (GreedyCrossover) doSerialize(op);
486     assertEquals(o, op);
487   }
488
489   /**
490    * Test equals with classcast object.
491    * @throws Exception
492    *
493    * @author Klaus Meffert
494    * @since 2.6
495    */

496   public void testEquals_0()
497       throws Exception JavaDoc {
498     GeneticOperator op = new GreedyCrossover(conf);
499     assertFalse(op.equals(new Chromosome(conf)));
500   }
501
502   /**
503    * @throws Exception
504    *
505    * @author Klaus Meffert
506    * @since 2.6
507    */

508   public void testCompareTo_0()
509       throws Exception JavaDoc {
510     GreedyCrossover op = new GreedyCrossover(conf);
511     assertEquals(1, op.compareTo(null));
512     GreedyCrossover op2 = new GreedyCrossover(conf);
513     assertEquals(0, op.compareTo(op2));
514     op.setStartOffset(2);
515     assertEquals( -1, op.compareTo(op2));
516     assertEquals(1, op2.compareTo(op));
517   }
518 }
519
Popular Tags