KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

43   public void testConstruct_0()
44       throws Exception JavaDoc {
45     TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, 234);
46     assertEquals(234, mutOp.getMutationRate());
47     assertNull(mutOp.getMutationRateCalc());
48   }
49
50   /**
51    * @throws Exception
52    *
53    * @author Klaus Meffert
54    * @since 3.1
55    */

56   public void testConstruct_1()
57       throws Exception JavaDoc {
58     Genotype.setStaticConfiguration(conf);
59     TwoWayMutationOperator mutOp = new TwoWayMutationOperator();
60     assertEquals(0, mutOp.getMutationRate());
61     assertNotNull(mutOp.getMutationRateCalc());
62     assertSame(conf, mutOp.getConfiguration());
63   }
64
65   /**
66    * @throws Exception
67    *
68    * @author Klaus Meffert
69    * @since 3.1
70    */

71   public void testConstruct_2()
72       throws Exception JavaDoc {
73     TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, null);
74     assertEquals(0, mutOp.getMutationRate());
75     assertNull(mutOp.getMutationRateCalc());
76   }
77
78   /**
79    * @throws Exception
80    *
81    * @author Klaus Meffert
82    * @since 3.1
83    */

84   public void testConstruct_3()
85       throws Exception JavaDoc {
86     IUniversalRateCalculator calc = new DefaultMutationRateCalculator(conf);
87     TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, calc);
88     assertEquals(0, mutOp.getMutationRate());
89     assertEquals(calc, mutOp.getMutationRateCalc());
90   }
91
92   /**
93    * Ensure that size of mutated set of chromosomes equals the size of original
94    * chromosomes.
95    * @throws Exception
96    *
97    * @author Klaus Meffert
98    * @since 3.1
99    */

100   public void testOperate_0()
101       throws Exception JavaDoc {
102     Configuration conf = new DefaultConfiguration();
103     conf.setFitnessFunction(new TestFitnessFunction());
104     TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf);
105     List candChroms = new Vector();
106     Chromosome[] population = new Chromosome[] {};
107     mutOp.operate(new Population(conf, population), candChroms);
108     assertEquals(candChroms.size(), population.length);
109   }
110
111   /**
112    * Ensure that size of mutated set of chromosomes equals the size of original
113    * chromosomes.
114    *
115    * @throws Exception
116    *
117    * @author Klaus Meffert
118    * @since 3.1
119    */

120   public void testOperate_0_2()
121       throws Exception JavaDoc {
122     Configuration conf = new DefaultConfiguration();
123     conf.setFitnessFunction(new TestFitnessFunction());
124     TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf,
125         new DefaultMutationRateCalculator(conf));
126     List candChroms = new Vector();
127     RandomGeneratorForTest gen = new RandomGeneratorForTest();
128     gen.setNextInt(9);
129     conf.setRandomGenerator(gen);
130     Chromosome c1 = new Chromosome(conf, new BooleanGene(conf), 9);
131     conf.setSampleChromosome(c1);
132     conf.addNaturalSelector(new BestChromosomesSelector(conf), true);
133     conf.setPopulationSize(5);
134     for (int i = 0; i < c1.getGenes().length; i++) {
135       c1.getGene(i).setAllele(Boolean.TRUE);
136     }
137     Chromosome c2 = new Chromosome(conf, new IntegerGene(conf), 4);
138     for (int i = 0; i < c2.getGenes().length; i++) {
139       c2.getGene(i).setAllele(new Integer JavaDoc(27));
140     }
141     Chromosome[] population = new Chromosome[] {
142         c1, c2};
143     mutOp.operate(new Population(conf, population), candChroms);
144     assertEquals(candChroms.size(), population.length);
145   }
146
147   /**
148    * Mutating with different types of genes contained in the population should
149    * be possible without exception.
150    *
151    * @throws Exception
152    *
153    * @author Klaus Meffert
154    * @since 3.1
155    */

156   public void testOperate_1()
157       throws Exception JavaDoc {
158     List candChroms = new Vector();
159     Configuration conf = new Configuration();
160     conf.setPopulationSize(3);
161     conf.setRandomGenerator(new StockRandomGenerator());
162     TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf,
163         new DefaultMutationRateCalculator(conf));
164     Chromosome[] population = new Chromosome[] {
165         new Chromosome(conf, new BooleanGene(conf), 9),
166         (new Chromosome(conf, new IntegerGene(conf), 4))};
167     mutOp.operate(new Population(conf, population), candChroms);
168   }
169
170   /**
171    * NullpointerException because of null Configuration.
172    *
173    * @throws Exception
174    *
175    * @author Klaus Meffert
176    * @since 3.1
177    */

178   public void testOperate_2()
179       throws Exception JavaDoc {
180     TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf);
181     List candChroms = new Vector();
182     Chromosome[] population = new Chromosome[] {
183         new Chromosome(conf, new BooleanGene(conf), 9),
184         (new Chromosome(conf, new IntegerGene(conf), 4))};
185     try {
186       mutOp.operate(new Population(null, population), candChroms);
187       fail();
188     } catch (InvalidConfigurationException nex) {
189       ; //this is OK
190
}
191   }
192
193   /**
194    * Tests if population size grows expectedly after two consecutive calls.
195    *
196    * @throws Exception
197    *
198    * @author Klaus Meffert
199    * @since 3.1
200    */

201   public void testOperate_3()
202       throws Exception JavaDoc {
203     DefaultConfiguration conf = new DefaultConfiguration();
204     TwoWayMutationOperator op = new TwoWayMutationOperator(conf,
205         new DefaultMutationRateCalculator(conf));
206     conf.addGeneticOperator(op);
207     RandomGeneratorForTest rand = new RandomGeneratorForTest();
208     rand.setNextInt(0);
209     conf.setRandomGenerator(rand);
210     conf.setFitnessFunction(new TestFitnessFunction());
211     Gene sampleGene = new IntegerGene(conf, 1, 10);
212     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
213     conf.setSampleChromosome(chrom);
214     conf.setPopulationSize(6);
215     Gene cgene1 = new IntegerGene(conf, 1, 10);
216     cgene1.setAllele(new Integer JavaDoc(6));
217     Gene[] genes1 = new Gene[] {
218         cgene1};
219     Chromosome chrom1 = new Chromosome(conf, genes1);
220     Gene cgene2 = new IntegerGene(conf, 1, 10);
221     cgene2.setAllele(new Integer JavaDoc(9));
222     Gene[] genes2 = new Gene[] {
223         cgene2};
224     Chromosome chrom2 = new Chromosome(conf, genes2);
225     Chromosome[] population = new Chromosome[] {
226         chrom1, chrom2};
227     List chroms = new Vector();
228     Gene gene1 = new IntegerGene(conf, 1, 10);
229     gene1.setAllele(new Integer JavaDoc(5));
230     chroms.add(gene1);
231     Gene gene2 = new IntegerGene(conf, 1, 10);
232     gene2.setAllele(new Integer JavaDoc(7));
233     chroms.add(gene2);
234     Gene gene3 = new IntegerGene(conf, 1, 10);
235     gene3.setAllele(new Integer JavaDoc(4));
236     chroms.add(gene3);
237     assertEquals(3, chroms.size());
238     Population pop = new Population(conf, population);
239     op.operate(pop, chroms);
240     assertEquals(2, pop.size());
241     assertEquals(3 + 2, chroms.size());
242     op.operate(pop, chroms);
243     assertEquals(2, pop.size());
244     assertEquals(3 + 2 + 2, chroms.size());
245   }
246
247   /**
248    * @throws Exception
249    *
250    * @author Klaus Meffert (adapted from Dan Clark's test of MutationOperator)
251    * @since 3.1
252    */

253   public void testOperate_3_1()
254       throws Exception JavaDoc {
255     DefaultConfiguration conf = new DefaultConfiguration();
256     GeneticOperator op = new TwoWayMutationOperator(conf, 10);
257     conf.addGeneticOperator(op);
258     RandomGeneratorForTest rand = new RandomGeneratorForTest();
259     // 0 in this sequence represents a gene to be mutated
260
// thus, the middle gene of each chromosome should be mutated
261
rand.setNextIntSequence(new int[] {0, 0, 1});
262     rand.setNextDouble(0.7d);
263     conf.setRandomGenerator(rand);
264     conf.setFitnessFunction(new TestFitnessFunction());
265     Gene sampleGene = new IntegerGene(conf, 0, 9);
266     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
267     conf.setSampleChromosome(chrom);
268     conf.setPopulationSize(6);
269     Gene[] genes1 = new Gene[3];
270     for (int i = 0; i < genes1.length; i++) {
271       genes1[i] = new IntegerGene(conf, 0, 9);
272       genes1[i].setAllele(new Integer JavaDoc(i));
273     }
274     Chromosome chrom1 = new Chromosome(conf, genes1);
275     Gene[] genes2 = new Gene[3];
276     for (int i = 0; i < genes2.length; i++) {
277       genes2[i] = new IntegerGene(conf, 0, 9);
278       genes2[i].setAllele(new Integer JavaDoc(i + 3));
279     }
280     Chromosome chrom2 = new Chromosome(conf, genes2);
281     Chromosome[] population = new Chromosome[] {
282         chrom1, chrom2};
283     // this will cause an increase of 4 in mutated values
284
// original + (0.7*2 - 1) * 10 (range of allele values)
285
List chroms = new Vector();
286     Population pop = new Population(conf, population);
287     op.operate(pop, chroms);
288     assertEquals(2, chroms.size());
289     // test chromosome 1 - 3rd gene should be different
290
Chromosome c1 = (Chromosome) chroms.get(0);
291     assertEquals(new Integer JavaDoc(0), c1.getGene(0).getAllele());
292     assertEquals(new Integer JavaDoc(1), c1.getGene(1).getAllele());
293     assertEquals(new Integer JavaDoc(2 + 4), c1.getGene(2).getAllele());
294     // test chromosome 2 - 2nd gene should be different
295
Chromosome c2 = (Chromosome) chroms.get(1);
296     assertEquals(new Integer JavaDoc(3), c2.getGene(0).getAllele());
297     assertEquals(new Integer JavaDoc(4), c2.getGene(1).getAllele());
298     assertEquals(new Integer JavaDoc(5 + 4), c2.getGene(2).getAllele());
299     op.operate(pop, chroms);
300     assertEquals(3, chroms.size());
301   }
302
303   /**
304    * Ensure that nothing is done.
305    * @throws Exception
306    *
307    * @author Klaus Meffert
308    * @since 3.1
309    */

310   public void testOperate_4()
311       throws Exception JavaDoc {
312     DefaultConfiguration conf = new DefaultConfiguration();
313     TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, 0);
314     mutOp.setMutationRateCalc(null);
315     List candChroms = new Vector();
316     BooleanGene gene1 = new BooleanGene(conf);
317     Chromosome chrom1 = new Chromosome(conf, gene1, 1);
318     chrom1.getGene(0).setAllele(Boolean.valueOf(false));
319     IntegerGene gene2 = new IntegerGene(conf, 0, 10);
320     Chromosome chrom2 = new Chromosome(conf, gene2, 1);
321     chrom2.getGene(0).setAllele(new Integer JavaDoc(3));
322     candChroms.add(chrom1);
323     candChroms.add(chrom2);
324     mutOp.operate(null, candChroms);
325     assertEquals(2, candChroms.size());
326     assertEquals(chrom1, candChroms.get(0));
327     assertEquals(chrom2, candChroms.get(1));
328   }
329
330   /**
331    * Mutation, especially tested for an IntegerGene.
332    *
333    * @throws Exception
334    *
335    * @author Klaus Meffert
336    * @since 3.1
337    */

338   public void testOperate_5()
339       throws Exception JavaDoc {
340     Configuration conf = new Configuration();
341     conf.setPopulationSize(5);
342     RandomGeneratorForTest rn = new RandomGeneratorForTest();
343     rn.setNextInt(0);
344     rn.setNextDouble(0.8d); //C
345
conf.setRandomGenerator(rn);
346     BooleanGene gene1 = new BooleanGene(conf);
347     Chromosome chrom1 = new Chromosome(conf, gene1, 1);
348     chrom1.getGene(0).setAllele(Boolean.valueOf(false));
349     IntegerGene gene2 = new IntegerGene(conf, 0, 10); //B: B1, B2
350
Chromosome chrom2 = new Chromosome(conf, gene2, 1);
351     chrom2.getGene(0).setAllele(new Integer JavaDoc(3)); //A
352
Chromosome[] chroms = new Chromosome[] {
353         chrom1, chrom2};
354     TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf,
355         new DefaultMutationRateCalculator(conf));
356     Population pop = new Population(conf, chroms);
357     mutOp.operate(pop, pop.getChromosomes());
358     // now we should have the double number of chromosomes because the target
359
// list is the same as the source list of chromosomes
360
assertEquals(2 + 2, pop.getChromosomes().size());
361     //old gene
362
assertFalse( ( (BooleanGene) pop.getChromosome(0).getGene(0))
363                 .booleanValue());
364     //mutated gene
365
assertTrue( ( (BooleanGene) pop.getChromosome(2).getGene(0)).booleanValue());
366     //old gene
367
assertEquals(3, ( (IntegerGene) pop.getChromosome(1).getGene(0)).intValue());
368     //mutated gene: A + (B2-B1) * (-1 + C * 2) --> see IntegerGene.applyMutation
369
// A, B1, B2, C: see comments above
370
// -1 + C * 2: see IntegerGene.applyMutation
371
assertEquals( (int) Math.round(3 + (10 - 0) * ( -1 + 0.8d * 2)),
372                  ( (IntegerGene) pop.getChromosome(3).getGene(0)).intValue());
373   }
374
375   /**
376    * Mutation, especially tested for an IntegerGene. Uses a CompositeGene.
377    *
378    * @throws Exception
379    *
380    * @author Klaus Meffert
381    * @since 3.1
382    */

383   public void testOperate_5_2()
384       throws Exception JavaDoc {
385     Configuration conf = new Configuration();
386     conf.setPopulationSize(5);
387     BooleanGene gene1 = new BooleanGene(conf);
388     CompositeGene comp1 = new CompositeGene(conf);
389     comp1.addGene(gene1);
390     Chromosome chrom1 = new Chromosome(conf, comp1, 1);
391     ( (CompositeGene) chrom1.getGene(0)).geneAt(0).setAllele(
392         Boolean.valueOf(false));
393     IntegerGene gene2 = new IntegerGene(conf, 0, 10);
394     CompositeGene comp2 = new CompositeGene(conf);
395     comp2.addGene(gene2);
396     Chromosome chrom2 = new Chromosome(conf, comp2, 1);
397     ( (CompositeGene) chrom2.getGene(0)).geneAt(0).setAllele(new Integer JavaDoc(3));
398     Chromosome[] chroms = new Chromosome[] {
399         chrom1, chrom2};
400     TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf,
401         new DefaultMutationRateCalculator(conf));
402     RandomGeneratorForTest rn = new RandomGeneratorForTest();
403     rn.setNextInt(0);
404     rn.setNextDouble(0.8d);
405     conf.setRandomGenerator(rn);
406     Population pop = new Population(conf, chroms);
407     mutOp.operate(pop, pop.getChromosomes());
408     assertEquals(2 + 2, pop.getChromosomes().size());
409     //old gene
410
assertFalse( ( (BooleanGene) ( (CompositeGene) pop.getChromosome(0).getGene(
411         0)).geneAt(0)).booleanValue());
412     //mutated gene
413
assertTrue( ( (BooleanGene) ( (CompositeGene) pop.getChromosome(2).getGene(
414         0)).geneAt(0)).booleanValue());
415     //old gene
416
assertEquals(3,
417                  ( (IntegerGene) ( (CompositeGene) pop.getChromosome(1).
418                                   getGene(0)).geneAt(0)).intValue());
419     //mutated gene: A + (B2-B1) * (-1 + C * 2) --> see IntegerGene.applyMutation
420
// A, B1, B2, C: see comments above
421
// -1 + C * 2: see IntegerGene.applyMutation
422
assertEquals( (int) Math.round(3 + (10 - 0) * ( -1 + 0.8d * 2)),
423                  ( (
424                      IntegerGene) ( (CompositeGene) pop.getChromosome(3).
425                                    getGene(0)).
426                   geneAt(0)).intValue());
427   }
428
429   /**
430    * Following should be possible without exception.
431    *
432    * @throws Exception
433    *
434    * @author Klaus Meffert
435    * @since 3.1
436    */

437   public void testOperate_6()
438       throws Exception JavaDoc {
439     Configuration conf = new DefaultConfiguration();
440     MutationOperator mutOp = new MutationOperator(conf, 0);
441     mutOp.setMutationRateCalc(null);
442     mutOp.operate(null, null);
443   }
444
445   /**
446    * @throws Exception
447    *
448    * @author Klaus Meffert
449    * @since 3.1
450    */

451   public void testOperate_6_2()
452       throws Exception JavaDoc {
453     Configuration conf = new DefaultConfiguration();
454     TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, 0);
455     mutOp.setMutationRateCalc(new DefaultMutationRateCalculator(conf));
456     mutOp.operate(null, null);
457   }
458
459   /**
460    * Considers IGeneticOperatorConstraint. Here, the mutation of a BooleanGene
461    * is forbidden by that constraint.
462    *
463    * @throws Exception
464    *
465    * @author Klaus Meffert
466    * @since 3.1
467    */

468   public void testOperate_8()
469       throws Exception JavaDoc {
470     Configuration conf = new Configuration();
471     conf.setPopulationSize(5);
472     RandomGeneratorForTest rn = new RandomGeneratorForTest();
473     rn.setNextInt(0);
474     rn.setNextInt(0);
475     rn.setNextDouble(0.8d);
476     conf.setRandomGenerator(rn);
477     BooleanGene gene1 = new BooleanGene(conf);
478     Chromosome chrom1 = new Chromosome(conf, gene1, 1);
479     chrom1.getGene(0).setAllele(Boolean.valueOf(false));
480     IntegerGene gene2 = new IntegerGene(conf, 0, 10);
481     Chromosome chrom2 = new Chromosome(conf, gene2, 1);
482     chrom2.getGene(0).setAllele(new Integer JavaDoc(3));
483     Chromosome[] chroms = new Chromosome[] {
484         chrom1, chrom2};
485     TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf,
486         new DefaultMutationRateCalculator(conf));
487     IGeneticOperatorConstraint constraint = new
488         GeneticOperatorConstraintForTest();
489     conf.getJGAPFactory().setGeneticOperatorConstraint(
490         constraint);
491     Population pop = new Population(conf, chroms);
492     mutOp.operate(pop, pop.getChromosomes());
493     // +1 (not +2) because only IntegerGene should have been mutated.
494
assertEquals(2 + 1, pop.getChromosomes().size());
495     //old gene
496
assertFalse( ( (BooleanGene) pop.getChromosome(0).getGene(0)).booleanValue());
497     //old gene
498
assertEquals(3, ( (IntegerGene) pop.getChromosome(1).
499                      getGene(0)).intValue());
500     //mutated gene
501
assertEquals( (int) Math.round(3 + (10 - 0) * ( -1 + 0.8d * 2)),
502                  ( (IntegerGene) pop.getChromosome(2).getGene(0)).intValue());
503   }
504
505   /**
506    * Ensures operator is implementing Serializable.
507    * @throws Exception
508    *
509    * @author Klaus Meffert
510    * @since 3.1
511    */

512   public void testIsSerializable_0()
513       throws Exception JavaDoc {
514     TwoWayMutationOperator op = new TwoWayMutationOperator(conf);
515     assertTrue(isSerializable(op));
516   }
517
518   /**
519    * Ensures that operator and all objects contained implement Serializable.
520    * @throws Exception
521    *
522    * @author Klaus Meffert
523    * @since 3.1
524    */

525   public void testDoSerialize_0()
526       throws Exception JavaDoc {
527     // construct object to be serialized
528
IUniversalRateCalculator calc = new DefaultCrossoverRateCalculator(conf);
529     TwoWayMutationOperator op = new TwoWayMutationOperator(conf, calc);
530     Object JavaDoc o = doSerialize(op);
531     assertEquals(o, op);
532   }
533
534   public class GeneticOperatorConstraintForTest
535       implements IGeneticOperatorConstraint {
536     public boolean isValid(Population a_pop, List a_chromosomes,
537                            GeneticOperator a_caller) {
538       Chromosome chrom = (Chromosome) a_chromosomes.get(0);
539       Gene gene = chrom.getGene(0);
540       return gene.getClass() != BooleanGene.class;
541     }
542   }
543   /**
544    * Test equals with classcast object.
545    *
546    * @throws Exception
547    * @author Klaus Meffert
548    * @since 3.1
549    */

550   public void testEquals_0()
551       throws Exception JavaDoc {
552     GeneticOperator op = new TwoWayMutationOperator(conf);
553     assertFalse(op.equals(new Chromosome(conf)));
554   }
555
556   /**
557    * @throws Exception
558    *
559    * @author Klaus Meffert
560    * @since 3.1
561    */

562   public void testCompareTo_0()
563       throws Exception JavaDoc {
564     TwoWayMutationOperator op = new TwoWayMutationOperator(conf);
565     assertEquals(1, op.compareTo(null));
566     TwoWayMutationOperator op2 = new TwoWayMutationOperator(conf);
567     assertEquals(0, op.compareTo(op2));
568     op = new TwoWayMutationOperator(conf, 3);
569     assertEquals( -1, op.compareTo(op2));
570     assertEquals(1, op2.compareTo(op));
571     op = new TwoWayMutationOperator(conf,
572                                     new DefaultMutationRateCalculator(conf));
573     assertEquals(0, op.compareTo(op2));
574     op = new TwoWayMutationOperator(conf, 3);
575     op2 = new TwoWayMutationOperator(conf, 4);
576     assertEquals( -1, op.compareTo(op2));
577     assertEquals(1, op2.compareTo(op));
578   }
579 }
580
Popular Tags