KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

54   public void testConstruct_1()
55       throws Exception JavaDoc {
56     MutationOperator mutOp = new MutationOperator(conf);
57     assertEquals(0, mutOp.getMutationRate());
58     assertNotNull(mutOp.getMutationRateCalc());
59   }
60
61   /**
62    * @throws Exception
63    *
64    * @author Klaus Meffert
65    */

66   public void testConstruct_2()
67       throws Exception JavaDoc {
68     MutationOperator mutOp = new MutationOperator(conf, null);
69     assertEquals(0, mutOp.getMutationRate());
70     assertNull(mutOp.getMutationRateCalc());
71   }
72
73   /**
74    * @throws Exception
75    *
76    * @author Klaus Meffert
77    */

78   public void testConstruct_3()
79       throws Exception JavaDoc {
80     IUniversalRateCalculator calc = new DefaultMutationRateCalculator(conf);
81     MutationOperator mutOp = new MutationOperator(conf, calc);
82     assertEquals(0, mutOp.getMutationRate());
83     assertEquals(calc, mutOp.getMutationRateCalc());
84   }
85
86   /**
87    * Ensure that size of mutated set of chromosomes equals the size of original
88    * chromosomes.
89    * @throws Exception
90    *
91    * @author Klaus Meffert
92    */

93   public void testOperate_0()
94       throws Exception JavaDoc {
95     Configuration conf = new DefaultConfiguration();
96     conf.setFitnessFunction(new TestFitnessFunction());
97     MutationOperator mutOp = new MutationOperator(conf);
98     List candChroms = new Vector();
99     Chromosome[] population = new Chromosome[] {};
100     mutOp.operate(new Population(conf, population), candChroms);
101     assertEquals(candChroms.size(), population.length);
102   }
103
104   /**
105    * Ensure that size of mutated set of chromosomes equals the size of original
106    * chromosomes.
107    *
108    * @throws Exception
109    *
110    * @author Klaus Meffert
111    */

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

149   public void testOperate_1()
150       throws Exception JavaDoc {
151     List candChroms = new Vector();
152     Configuration conf = new Configuration();
153     conf.setPopulationSize(3);
154     conf.setRandomGenerator(new StockRandomGenerator());
155     MutationOperator mutOp = new MutationOperator(conf,
156                                                   new
157                                                   DefaultMutationRateCalculator(
158         conf));
159     Chromosome[] population = new Chromosome[] {
160         new Chromosome(conf, new BooleanGene(conf), 9),
161         (new Chromosome(conf, new IntegerGene(conf), 4))};
162     mutOp.operate(new Population(conf, population), candChroms);
163   }
164
165   /**
166    * NullpointerException because of null Configuration.
167    * @throws Exception
168    *
169    * @author Klaus Meffert
170    */

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

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

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

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

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

381   public void testOperate_5_2()
382       throws Exception JavaDoc {
383     Configuration conf = new Configuration();
384     conf.setPopulationSize(5);
385     BooleanGene gene1 = new BooleanGene(conf);
386     CompositeGene comp1 = new CompositeGene(conf);
387     comp1.addGene(gene1);
388     Chromosome chrom1 = new Chromosome(conf, comp1, 1);
389     ( (CompositeGene) chrom1.getGene(0)).geneAt(0).setAllele(
390         Boolean.valueOf(false));
391     IntegerGene gene2 = new IntegerGene(conf, 0, 10);
392     CompositeGene comp2 = new CompositeGene(conf);
393     comp2.addGene(gene2);
394     Chromosome chrom2 = new Chromosome(conf, comp2, 1);
395     ( (CompositeGene) chrom2.getGene(0)).geneAt(0).setAllele(new Integer JavaDoc(3));
396     Chromosome[] chroms = new Chromosome[] {
397         chrom1, chrom2};
398     MutationOperator mutOp = new MutationOperator(conf,
399                                                   new
400                                                   DefaultMutationRateCalculator(
401         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).getGene(0)).
425                   geneAt(0)).intValue());
426   }
427
428   /**
429    * Following should be possible without exception.
430    * @throws Exception
431    *
432    * @author Klaus Meffert
433    * @since 2.2
434    */

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

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

466   public void testOperate_8()
467       throws Exception JavaDoc {
468     Configuration conf = new Configuration();
469     conf.setPopulationSize(5);
470     RandomGeneratorForTest rn = new RandomGeneratorForTest();
471     rn.setNextInt(0);
472     rn.setNextInt(0);
473     rn.setNextDouble(0.8d);
474     conf.setRandomGenerator(rn);
475     BooleanGene gene1 = new BooleanGene(conf);
476     Chromosome chrom1 = new Chromosome(conf, gene1, 1);
477     chrom1.getGene(0).setAllele(Boolean.valueOf(false));
478     IntegerGene gene2 = new IntegerGene(conf, 0, 10);
479     Chromosome chrom2 = new Chromosome(conf, gene2, 1);
480     chrom2.getGene(0).setAllele(new Integer JavaDoc(3));
481     Chromosome[] chroms = new Chromosome[] {
482         chrom1, chrom2};
483     MutationOperator mutOp = new MutationOperator(conf,
484                                                   new
485                                                   DefaultMutationRateCalculator(
486         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 2.6
511    */

512   public void testIsSerializable_0()
513       throws Exception JavaDoc {
514     MutationOperator op = new MutationOperator(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 2.6
524    */

525   public void testDoSerialize_0()
526       throws Exception JavaDoc {
527     // construct object to be serialized
528
IUniversalRateCalculator calc = new DefaultCrossoverRateCalculator(conf);
529     MutationOperator op = new MutationOperator(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 2.6
549    */

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

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