KickJava   Java API By Example, From Geeks To Geeks.

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


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 AveragingCrossoverOperator class.
18  *
19  * @author Klaus Meffert
20  * @since 2.0
21  */

22 public class AveragingCrossoverOperatorTest
23     extends JGAPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private static final String JavaDoc CVS_REVISION = "$Revision: 1.21 $";
26
27   public static Test suite() {
28     TestSuite suite = new TestSuite(AveragingCrossoverOperatorTest.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.0
42    */

43   public void testOperate_0()
44       throws Exception JavaDoc {
45 // conf.addGeneticOperator(op);
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     Gene cgene1 = new IntegerGene(conf, 1, 10);
56     cgene1.setAllele(new Integer JavaDoc(6));
57     Gene[] genes1 = new Gene[] {
58         cgene1};
59     Chromosome chrom1 = new Chromosome(conf, genes1);
60     Gene cgene2 = new IntegerGene(conf, 1, 10);
61     cgene2.setAllele(new Integer JavaDoc(8));
62     Gene[] genes2 = new Gene[] {
63         cgene2};
64     Chromosome chrom2 = new Chromosome(conf, genes2);
65     Chromosome[] population = new Chromosome[] {
66         chrom1, chrom2};
67     List chroms = new Vector();
68     Gene gene1 = new IntegerGene(conf, 1, 10);
69     gene1.setAllele(new Integer JavaDoc(5));
70     chroms.add(gene1);
71     Gene gene2 = new IntegerGene(conf, 1, 10);
72     gene2.setAllele(new Integer JavaDoc(7));
73     chroms.add(gene2);
74     Gene gene3 = new IntegerGene(conf, 1, 10);
75     gene3.setAllele(new Integer JavaDoc(4));
76     chroms.add(gene3);
77     GeneticOperator op = new AveragingCrossoverOperator(conf);
78     op.operate(new Population(conf, population), chroms);
79     assertEquals(5, chroms.size());
80     Chromosome target = (Chromosome) chroms.get(4);
81     assertEquals(6, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
82     target = (Chromosome) chroms.get(3);
83     assertEquals(8, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
84   }
85
86   /**
87    * Tests if crossing over produces same results for two operate-runs.
88    * @throws Exception
89    *
90    * @author Klaus Meffert
91    * @since 2.0
92    */

93   public void testOperate_1()
94       throws Exception JavaDoc {
95     DefaultConfiguration conf = new DefaultConfiguration();
96 // conf.addGeneticOperator(op);
97
RandomGeneratorForTest rand = new RandomGeneratorForTest();
98     rand.setNextIntSequence(new int[] {
99                             0, 1, 0, 1, 2});
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, 10);
107     cgene1.setAllele(new Integer JavaDoc(6));
108     Gene[] genes1 = new Gene[] {
109         cgene1};
110     Chromosome chrom1 = new Chromosome(conf, genes1);
111     Gene cgene2 = new IntegerGene(conf, 1, 10);
112     cgene2.setAllele(new Integer JavaDoc(8));
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     Chromosome[] population2 = (Chromosome[]) population.clone();
129     GeneticOperator op = new AveragingCrossoverOperator(conf);
130     op.operate(new Population(conf, population), chroms);
131     op.operate(new Population(conf, population2), chroms);
132     assertTrue(isChromosomesEqual(population, population2));
133   }
134
135   /**
136    * Using CompositeGene as first crossover candidate.
137    * @throws Exception
138    *
139    * @author Klaus Meffert
140    * @since 2.0
141    */

142   public void testOperate_2()
143       throws Exception JavaDoc {
144     DefaultConfiguration conf = new DefaultConfiguration();
145 // conf.addGeneticOperator(op);
146
RandomGeneratorForTest rand = new RandomGeneratorForTest();
147     rand.setNextIntSequence(new int[] {
148                             0, 1, 0, 1, 2});
149     conf.setRandomGenerator(rand);
150     conf.setFitnessFunction(new TestFitnessFunction());
151     Gene sampleGene = new IntegerGene(conf, 1, 10);
152     CompositeGene compGene = new CompositeGene(conf);
153     compGene.addGene(sampleGene);
154     Chromosome chrom = new Chromosome(conf, compGene, 3);
155     conf.setSampleChromosome(chrom);
156     conf.setPopulationSize(6);
157     Gene cgene1 = new IntegerGene(conf, 1, 10);
158     cgene1.setAllele(new Integer JavaDoc(6));
159     compGene = new CompositeGene(conf);
160     compGene.addGene(cgene1);
161     Gene[] genes1 = new Gene[] {
162         compGene};
163     Chromosome chrom1 = new Chromosome(conf, genes1);
164     Gene cgene2 = new IntegerGene(conf, 1, 10);
165     cgene2.setAllele(new Integer JavaDoc(8));
166     Gene[] genes2 = new Gene[] {
167         cgene2};
168     Chromosome chrom2 = new Chromosome(conf, genes2);
169     Chromosome[] population = new Chromosome[] {
170         chrom1, chrom2};
171     List chroms = new Vector();
172     Gene gene1 = new IntegerGene(conf, 1, 10);
173     gene1.setAllele(new Integer JavaDoc(5));
174     chroms.add(gene1);
175     Gene gene2 = new IntegerGene(conf, 1, 10);
176     gene2.setAllele(new Integer JavaDoc(7));
177     chroms.add(gene2);
178     Gene gene3 = new IntegerGene(conf, 1, 10);
179     gene3.setAllele(new Integer JavaDoc(4));
180     chroms.add(gene3);
181     GeneticOperator op = new AveragingCrossoverOperator(conf);
182     op.operate(new Population(conf, population), chroms);
183     assertEquals(5, chroms.size());
184     Chromosome target = (Chromosome) chroms.get(4);
185     assertEquals(6, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
186     target = (Chromosome) chroms.get(3);
187     CompositeGene result = (CompositeGene) target.getGene(0);
188     assertEquals(8, ( (Integer JavaDoc) ( (Vector) result.getAllele()).get(0))
189                  .intValue());
190   }
191
192   /**
193    * Considers crossover rate calculator
194    * @throws Exception
195    *
196    * @author Klaus Meffert
197    * @since 2.6
198    */

199   public void testOperate_3()
200       throws Exception JavaDoc {
201     DefaultConfiguration conf = new DefaultConfiguration();
202 // conf.addGeneticOperator(op);
203
RandomGeneratorForTest rand = new RandomGeneratorForTest();
204     rand.setNextIntSequence(new int[] {
205                             0, 1, 0, 1, 2});
206     conf.setRandomGenerator(rand);
207     conf.setFitnessFunction(new TestFitnessFunction());
208     Gene sampleGene = new IntegerGene(conf, 1, 10);
209     Chromosome chrom = new Chromosome(conf, sampleGene, 2);
210     conf.setSampleChromosome(chrom);
211     conf.setPopulationSize(6);
212     Gene cgene1 = new IntegerGene(conf, 1, 10);
213     cgene1.setAllele(new Integer JavaDoc(6));
214     Gene[] genes1 = new Gene[] {
215         cgene1};
216     Chromosome chrom1 = new Chromosome(conf, genes1);
217     Gene cgene2 = new IntegerGene(conf, 1, 10);
218     cgene2.setAllele(new Integer JavaDoc(8));
219     Gene[] genes2 = new Gene[] {
220         cgene2};
221     Chromosome chrom2 = new Chromosome(conf, genes2);
222     Chromosome[] population = new Chromosome[] {
223         chrom1, chrom2};
224     List chroms = new Vector();
225     Gene gene1 = new IntegerGene(conf, 1, 10);
226     gene1.setAllele(new Integer JavaDoc(5));
227     chroms.add(gene1);
228     Gene gene2 = new IntegerGene(conf, 1, 10);
229     gene2.setAllele(new Integer JavaDoc(7));
230     chroms.add(gene2);
231     Gene gene3 = new IntegerGene(conf, 1, 10);
232     gene3.setAllele(new Integer JavaDoc(4));
233     chroms.add(gene3);
234     GeneticOperator op = new AveragingCrossoverOperator(conf, new
235         DefaultCrossoverRateCalculator(conf));
236     op.operate(new Population(conf, population), chroms);
237     assertEquals(3 + 2, chroms.size());
238     Chromosome target = (Chromosome) chroms.get(4);
239     assertEquals(6, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
240     target = (Chromosome) chroms.get(3);
241     assertEquals(8, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
242     op.operate(new Population(conf, population), chroms);
243     assertEquals(3 + 2 + 2, chroms.size());
244   }
245
246   /**
247    * Using CompositeGene as second crossover candidate.
248    * @throws Exception
249    *
250    * @author Klaus Meffert
251    * @since 2.6
252    */

253   public void testOperate_4()
254       throws Exception JavaDoc {
255 // conf.addGeneticOperator(op);
256
RandomGeneratorForTest rand = new RandomGeneratorForTest();
257     rand.setNextIntSequence(new int[] {
258                             0, 1, 0, 1, 2});
259     conf.setRandomGenerator(rand);
260     conf.setFitnessFunction(new TestFitnessFunction());
261     Gene sampleGene = new IntegerGene(conf, 1, 10);
262     CompositeGene compGene = new CompositeGene(conf);
263     compGene.addGene(sampleGene);
264     Chromosome chrom = new Chromosome(conf, compGene, 3);
265     conf.setSampleChromosome(chrom);
266     conf.setPopulationSize(6);
267     Gene cgene1 = new IntegerGene(conf, 1, 10);
268     cgene1.setAllele(new Integer JavaDoc(6));
269     compGene = new CompositeGene(conf);
270     compGene.addGene(cgene1);
271     Gene[] genes1 = new Gene[] {
272         compGene};
273     Chromosome chrom1 = new Chromosome(conf, genes1);
274     Gene cgene2 = new IntegerGene(conf, 1, 10);
275     cgene2.setAllele(new Integer JavaDoc(8));
276     Gene[] genes2 = new Gene[] {
277         cgene2};
278     Chromosome chrom2 = new Chromosome(conf, genes2);
279     Chromosome[] population = new Chromosome[] {
280         chrom2, chrom1};
281     List chroms = new Vector();
282     Gene gene1 = new IntegerGene(conf, 1, 10);
283     gene1.setAllele(new Integer JavaDoc(5));
284     chroms.add(gene1);
285     Gene gene2 = new IntegerGene(conf, 1, 10);
286     gene2.setAllele(new Integer JavaDoc(7));
287     chroms.add(gene2);
288     Gene gene3 = new IntegerGene(conf, 1, 10);
289     gene3.setAllele(new Integer JavaDoc(4));
290     chroms.add(gene3);
291     GeneticOperator op = new AveragingCrossoverOperator(conf);
292     op.operate(new Population(conf, population), chroms);
293     assertEquals(5, chroms.size());
294     Chromosome target = (Chromosome) chroms.get(3);
295     assertEquals(6, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
296     target = (Chromosome) chroms.get(4);
297     CompositeGene result = (CompositeGene) target.getGene(0);
298     assertEquals(8, ( (Integer JavaDoc) ( (Vector) result.getAllele()).get(0))
299                  .intValue());
300   }
301
302   /**
303    * Following should be possible without exception.
304    * @throws Exception
305    *
306    * @author Klaus Meffert
307    * @since 2.2
308    */

309   public void testConstruct_0()
310       throws Exception JavaDoc {
311     DefaultConfiguration conf = new DefaultConfiguration();
312     new AveragingCrossoverOperator(conf, (RandomGenerator)null);
313   }
314
315   /**
316    * Following should be possible without exception.
317    * @throws Exception
318    *
319    * @author Klaus Meffert
320    * @since 2.2
321    */

322   public void testConstruct_1()
323       throws Exception JavaDoc {
324     DefaultConfiguration conf = new DefaultConfiguration();
325     new AveragingCrossoverOperator(conf, (IUniversalRateCalculator)null);
326   }
327
328   /**
329    * @throws Exception
330    *
331    * @author Klaus Meffert
332    * @since 3.1
333    */

334   public void testConstruct_2()
335       throws Exception JavaDoc {
336     Genotype.setStaticConfiguration(conf);
337     AveragingCrossoverOperator op = new AveragingCrossoverOperator();
338     assertSame(conf, op.getConfiguration());
339   }
340
341   /**
342    * Ensures Object is implementing Serializable.
343    * @throws Exception
344    *
345    * @author Klaus Meffert
346    * @since 2.6
347    */

348   public void testIsSerializable_0()
349       throws Exception JavaDoc {
350     AveragingCrossoverOperator op = new AveragingCrossoverOperator(conf);
351     assertTrue(isSerializable(op));
352   }
353
354   /**
355    * Ensures that Object and all objects contained implement Serializable
356    * @throws Exception
357    *
358    * @author Klaus Meffert
359    * @since 2.6
360    */

361   public void testDoSerialize_0()
362       throws Exception JavaDoc {
363     // construct object to be serialized
364
IUniversalRateCalculator calc = new DefaultCrossoverRateCalculator(conf);
365     AveragingCrossoverOperator op = new AveragingCrossoverOperator(conf, calc);
366     Object JavaDoc o = doSerialize(op);
367     assertEquals(o, op);
368   }
369
370   /**
371    * Test equals with classcast object.
372    * @throws Exception
373    *
374    * @author Klaus Meffert
375    * @since 2.6
376    */

377   public void testEquals_0()
378       throws Exception JavaDoc {
379     GeneticOperator op = new AveragingCrossoverOperator(conf);
380     assertFalse(op.equals(new Chromosome(conf)));
381   }
382
383   /**
384    * @throws Exception
385    *
386    * @author Klaus Meffert
387    * @since 2.6
388    */

389   public void testCompareTo_0()
390       throws Exception JavaDoc {
391     AveragingCrossoverOperator op = new AveragingCrossoverOperator(conf);
392     assertEquals(1, op.compareTo(null));
393     AveragingCrossoverOperator op2 = new AveragingCrossoverOperator(conf);
394     assertEquals(0, op.compareTo(op2));
395     op = new AveragingCrossoverOperator(conf, new StockRandomGenerator());
396     assertEquals(0, op.compareTo(op2));
397     assertEquals(0, op2.compareTo(op));
398     op = new AveragingCrossoverOperator(conf,
399                                         new DefaultCrossoverRateCalculator(conf));
400     assertEquals(1, op.compareTo(op2));
401     assertEquals( -1, op2.compareTo(op));
402     op2 = new AveragingCrossoverOperator(conf,
403                                          new
404                                          DefaultCrossoverRateCalculator(conf));
405     assertEquals(0, op.compareTo(op2));
406   }
407
408   /**
409    * Crossover rate (setter introduced in 2.6).
410    * @throws Exception
411    *
412    * @author Klaus Meffert
413    * @since 2.6
414    */

415   public void testCompareTo_1()
416       throws Exception JavaDoc {
417     AveragingCrossoverOperator op = new AveragingCrossoverOperator(conf);
418     op.setCrossoverRate(3);
419     assertEquals(1, op.compareTo(null));
420     AveragingCrossoverOperator op2 = new AveragingCrossoverOperator(conf);
421     assertEquals(1, op.compareTo(op2));
422     assertEquals( -1, op2.compareTo(op));
423     op2.setCrossoverRate(3);
424     assertEquals(0, op.compareTo(op2));
425     op = new AveragingCrossoverOperator(conf, new StockRandomGenerator());
426     assertEquals( -1, op.compareTo(op2));
427     assertEquals(1, op2.compareTo(op));
428   }
429 }
430
Popular Tags