KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jgap.JGAPTestCase.*;
15 import junit.framework.*;
16
17 /**
18  * Tests the SwappingMutationOperator class.
19  *
20  * @author Klaus Meffert
21  * @since 2.1
22  */

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

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

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

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

83   public void testConstruct_3()
84       throws Exception JavaDoc {
85     IUniversalRateCalculator calc = new DefaultMutationRateCalculator(conf);
86     SwappingMutationOperator mutOp = new SwappingMutationOperator(conf, calc);
87     assertEquals(0, mutOp.getMutationRate());
88     assertEquals(calc, mutOp.getMutationRateCalc());
89   }
90
91   /**
92    * @throws Exception
93    *
94    * @author Klaus Meffert
95    * @since 3.1
96    */

97   public void testConstruct_4()
98       throws Exception JavaDoc {
99     Genotype.setStaticConfiguration(conf);
100     SwappingMutationOperator op = new SwappingMutationOperator();
101     assertSame(conf, op.getConfiguration());
102   }
103
104   /**
105    * @throws Exception
106    *
107    * @author Klaus Meffert
108    * @since 2.1
109    */

110   public void testOperate_0()
111       throws Exception JavaDoc {
112     Configuration conf = new DefaultConfiguration();
113     conf.setFitnessFunction(new TestFitnessFunction());
114     SwappingMutationOperator mutOp = new SwappingMutationOperator(conf,
115         new DefaultMutationRateCalculator(conf));
116     List candChroms = new Vector();
117     Chromosome[] population = new Chromosome[] {};
118     mutOp.operate(new Population(conf, population), candChroms);
119     assertEquals(candChroms.size(), population.length);
120     candChroms.clear();
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     population = new Chromosome[] {
136         c1, c2};
137     mutOp.operate(new Population(conf, population), candChroms);
138     assertEquals(candChroms.size(), population.length);
139     assertEquals(c1, candChroms.get(0));
140     assertFalse(candChroms.get(0) == c1);
141     assertEquals(c2, candChroms.get(1));
142     assertFalse(candChroms.get(1) == c2);
143   }
144
145   /**
146    * @throws Exception
147    *
148    * @author Klaus Meffert
149    * @since 2.1
150    */

151   public void testOperate_1()
152       throws Exception JavaDoc {
153     List candChroms = new Vector();
154     Configuration conf = new Configuration();
155     conf.setRandomGenerator(new StockRandomGenerator());
156     SwappingMutationOperator mutOp = new SwappingMutationOperator(conf, null);
157     Chromosome[] population = new Chromosome[] {
158         new Chromosome(conf, new BooleanGene(conf), 9),
159         (new Chromosome(conf, new IntegerGene(conf), 4))};
160     mutOp.operate(new Population(conf, population), candChroms);
161     /**@todo assert result is correct*/
162   }
163
164   /**
165    * @throws Exception
166    *
167    * @author Klaus Meffert
168    * @since 2.1
169    */

170   public void testOperate_2()
171       throws Exception JavaDoc {
172     SwappingMutationOperator mutOp = new SwappingMutationOperator(conf);
173     List candChroms = new Vector();
174     Chromosome[] population = new Chromosome[] {
175         new Chromosome(conf, new BooleanGene(conf), 9),
176         (new Chromosome(conf, new IntegerGene(conf), 4))};
177     try {
178       mutOp.operate(new Population(null, population), candChroms);
179       fail();
180     }
181     catch (InvalidConfigurationException nex) {
182       ; //this is OK
183
}
184   }
185
186   /**
187    * Tests if population size grows expectedly after two consecutive calls.
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     SwappingMutationOperator op = new SwappingMutationOperator(conf,
197         new DefaultMutationRateCalculator(conf));
198     op.setStartOffset(0);
199     conf.addGeneticOperator(op);
200     RandomGeneratorForTest rand = new RandomGeneratorForTest();
201     rand.setNextDouble(0.45d);
202     rand.setNextInt(0);
203     conf.setRandomGenerator(rand);
204     conf.setFitnessFunction(new TestFitnessFunction());
205     Gene sampleGene = new IntegerGene(conf, 1, 10);
206     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
207     conf.setSampleChromosome(chrom);
208     conf.setPopulationSize(6);
209     Gene cgene1 = new IntegerGene(conf, 1, 10);
210     cgene1.setAllele(new Integer JavaDoc(6));
211     Gene[] genes1 = new Gene[] {
212         cgene1};
213     Chromosome chrom1 = new Chromosome(conf, genes1);
214     Gene cgene2 = new IntegerGene(conf, 1, 10);
215     cgene2.setAllele(new Integer JavaDoc(9));
216     Gene[] genes2 = new Gene[] {
217         cgene2};
218     Chromosome chrom2 = new Chromosome(conf, genes2);
219     Chromosome[] population = new Chromosome[] {
220         chrom1, chrom2};
221     List chroms = new Vector();
222     Gene gene1 = new IntegerGene(conf, 1, 10);
223     gene1.setAllele(new Integer JavaDoc(5));
224     chroms.add(gene1);
225     Gene gene2 = new IntegerGene(conf, 1, 10);
226     gene2.setAllele(new Integer JavaDoc(7));
227     chroms.add(gene2);
228     Gene gene3 = new IntegerGene(conf, 1, 10);
229     gene3.setAllele(new Integer JavaDoc(4));
230     chroms.add(gene3);
231     assertEquals(3, chroms.size());
232     Population pop = new Population(conf, population);
233     op.operate(pop, chroms);
234     assertEquals(2, pop.size());
235     assertEquals(3 + 2, chroms.size());
236     op.operate(pop, chroms);
237     assertEquals(2, pop.size());
238     assertEquals(3 + 2 + 2, chroms.size());
239   }
240
241   /**
242    * Check if none of the genes is lost during swapping.
243    * The checksum must stay the same. This step tests the swapping part,
244    * not the the whole operator.
245    * @throws Exception
246    *
247    * @author Audrius Meskauskas
248    * @since 2.0
249    */

250   public void testOperate_4()
251       throws Exception JavaDoc {
252     Configuration conf = new DefaultConfiguration();
253     conf.setFitnessFunction(new TestFitnessFunction());
254     int n_iterations = 20;
255     RandomGenerator generator = new StockRandomGenerator();
256     SwappingMutationOperator mutOp = new SwappingMutationOperator(conf);
257     mutOp.setStartOffset(0);
258     for (int n_genes = 0; n_genes < 20; n_genes++) {
259       Gene[] genes = new IntegerGene[n_genes];
260       for (int i = 0; i < genes.length; i++) {
261         genes[i] = new IntegerGene(conf, -1000, 1000);
262         genes[i].setToRandomValue(generator);
263       }
264       final long checksum = checksum(genes);
265       Gene[] prev = new Gene[genes.length];
266       for (int i = 0; i < n_iterations; i++) {
267         for (int gene = 0; gene < genes.length; gene++) {
268           System.arraycopy(genes, 0, prev, 0, genes.length);
269           genes = mutOp.operate(generator, gene, genes);
270           // checksum constant:
271
assertEquals(checksum, checksum(genes));
272         }
273       }
274     }
275   }
276
277   /**
278    * Nothing to do. Test that nothing is done.
279    * @throws Exception
280    *
281    * @author Klaus Meffert
282    * @since 2.6
283    */

284   public void testOperate_5()
285       throws Exception JavaDoc {
286     Configuration conf = new DefaultConfiguration();
287     SwappingMutationOperator mutOp = new SwappingMutationOperator(conf, 0);
288     mutOp.setMutationRateCalc(null);
289     List candChroms = new Vector();
290     BooleanGene gene1 = new BooleanGene(conf);
291     Chromosome chrom1 = new Chromosome(conf, gene1, 1);
292     chrom1.getGene(0).setAllele(Boolean.valueOf(false));
293     IntegerGene gene2 = new IntegerGene(conf, 0, 10);
294     Chromosome chrom2 = new Chromosome(conf, gene2, 1);
295     chrom2.getGene(0).setAllele(new Integer JavaDoc(3));
296     candChroms.add(chrom1);
297     candChroms.add(chrom2);
298     mutOp.operate(null, candChroms);
299     assertEquals(2, candChroms.size());
300     assertEquals(chrom1, candChroms.get(0));
301     assertEquals(chrom2, candChroms.get(1));
302   }
303
304   private long checksum(Gene[] a_genes) {
305     long s = 0;
306     for (int i = 0; i < a_genes.length; i++) {
307       s += ( (IntegerGene) a_genes[i]).intValue();
308     }
309     return s;
310   }
311
312   /**
313    * @throws Exception
314    *
315    * @author Klaus Meffert
316    * @since 2.2
317    */

318   public void testStartoffset_0()
319       throws Exception JavaDoc {
320     SwappingMutationOperator op = new SwappingMutationOperator(conf);
321     assertEquals(1, op.getStartOffset());
322     op.setStartOffset(2);
323     assertEquals(2, op.getStartOffset());
324     op.setStartOffset(1);
325     assertEquals(1, op.getStartOffset());
326     op.setStartOffset(0);
327     assertEquals(0, op.getStartOffset());
328   }
329
330   /**
331    * Ensures the operator is implementing Serializable
332    * @throws Exception
333    *
334    * @author Klaus Meffert
335    * @since 2.6
336    */

337   public void testIsSerializable_0()
338       throws Exception JavaDoc {
339     SwappingMutationOperator op = new SwappingMutationOperator(conf);
340     assertTrue(isSerializable(op));
341   }
342
343   /**
344    * Ensures that the operator and all objects contained implement Serializable
345    * @throws Exception
346    *
347    * @author Klaus Meffert
348    * @since 2.6
349    */

350   public void testDoSerialize_0()
351       throws Exception JavaDoc {
352     // construct object to be serialized
353
SwappingMutationOperator op = new SwappingMutationOperator(conf,
354         new DefaultCrossoverRateCalculator(conf));
355     SwappingMutationOperator o = (SwappingMutationOperator) doSerialize(op);
356     assertEquals(o, op);
357   }
358
359   /**
360    * Test equals with classcast object.
361    * @throws Exception
362    *
363    * @author Klaus Meffert
364    * @since 2.6
365    */

366   public void testEquals_0()
367       throws Exception JavaDoc {
368     GeneticOperator op = new SwappingMutationOperator(conf);
369     assertFalse(op.equals(new Chromosome(conf)));
370   }
371 }
372
Popular Tags