KickJava   Java API By Example, From Geeks To Geeks.

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


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

22 public class GaussianMutationOperatorTest
23     extends JGAPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private static final String JavaDoc CVS_REVISION = "$Revision: 1.13 $";
26
27   public static Test suite() {
28     TestSuite suite = new TestSuite(GaussianMutationOperatorTest.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     DefaultConfiguration conf = new DefaultConfiguration();
46     GeneticOperator op = new GaussianMutationOperator(conf, 0.15d);
47     conf.addGeneticOperator(op);
48     RandomGeneratorForTest rand = new RandomGeneratorForTest();
49     rand.setNextDouble(0.45d);
50     conf.setRandomGenerator(rand);
51     conf.setFitnessFunction(new TestFitnessFunction());
52     Gene sampleGene = new IntegerGene(conf, 1, 10);
53     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
54     conf.setSampleChromosome(chrom);
55     conf.setPopulationSize(6);
56     Gene cgene1 = new IntegerGene(conf, 1, 10);
57     cgene1.setAllele(new Integer JavaDoc(6));
58     Gene[] genes1 = new Gene[] {
59         cgene1};
60     Chromosome chrom1 = new Chromosome(conf, genes1);
61     Gene cgene2 = new IntegerGene(conf, 1, 10);
62     cgene2.setAllele(new Integer JavaDoc(9));
63     Gene[] genes2 = new Gene[] {
64         cgene2};
65     Chromosome chrom2 = new Chromosome(conf, genes2);
66     Chromosome[] population = new Chromosome[] {
67         chrom1, chrom2};
68     List chroms = new Vector();
69     Gene gene1 = new IntegerGene(conf, 1, 10);
70     gene1.setAllele(new Integer JavaDoc(5));
71     chroms.add(gene1);
72     Gene gene2 = new IntegerGene(conf, 1, 10);
73     gene2.setAllele(new Integer JavaDoc(7));
74     chroms.add(gene2);
75     Gene gene3 = new IntegerGene(conf, 1, 10);
76     gene3.setAllele(new Integer JavaDoc(4));
77     chroms.add(gene3);
78     op.operate(new Population(conf, population), chroms);
79     Chromosome target = (Chromosome) chroms.get(4);
80     assertEquals(Math.round( (10 - 1) * (0.45d * 0.15d) + 9),
81                  ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
82     target = (Chromosome) chroms.get(3);
83     assertEquals(Math.round( (10 - 1) * (0.45d * 0.15d) + 6),
84                  ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
85   }
86
87   /**
88    * Tests if mutation produces same results for two operate-runs
89    * @throws Exception
90    *
91    * @author Klaus Meffert
92    * @since 2.0
93    */

94   public void testOperate_1()
95       throws Exception JavaDoc {
96     DefaultConfiguration conf = new DefaultConfiguration();
97     GeneticOperator op = new GaussianMutationOperator(conf);
98     conf.addGeneticOperator(op);
99     RandomGeneratorForTest rand = new RandomGeneratorForTest();
100     rand.setNextIntSequence(new int[] {
101                             0, 1, 0, 1, 2});
102     conf.setRandomGenerator(rand);
103     conf.setFitnessFunction(new TestFitnessFunction());
104     Gene sampleGene = new IntegerGene(conf, 1, 10);
105     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
106     conf.setSampleChromosome(chrom);
107     conf.setPopulationSize(6);
108     Gene cgene1 = new IntegerGene(conf, 1, 10);
109     cgene1.setAllele(new Integer JavaDoc(6));
110     Gene[] genes1 = new Gene[] {
111         cgene1};
112     Chromosome chrom1 = new Chromosome(conf, genes1);
113     Gene cgene2 = new IntegerGene(conf, 1, 10);
114     cgene2.setAllele(new Integer JavaDoc(8));
115     Gene[] genes2 = new Gene[] {
116         cgene2};
117     Chromosome chrom2 = new Chromosome(conf, genes2);
118     Chromosome[] population = new Chromosome[] {
119         chrom1, chrom2};
120     List chroms = new Vector();
121     Gene gene1 = new IntegerGene(conf, 1, 10);
122     gene1.setAllele(new Integer JavaDoc(5));
123     chroms.add(gene1);
124     Gene gene2 = new IntegerGene(conf, 1, 10);
125     gene2.setAllele(new Integer JavaDoc(7));
126     chroms.add(gene2);
127     Gene gene3 = new IntegerGene(conf, 1, 10);
128     gene3.setAllele(new Integer JavaDoc(4));
129     chroms.add(gene3);
130     Chromosome[] population2 = (Chromosome[]) population.clone();
131     op.operate(new Population(conf, population), chroms);
132     op.operate(new Population(conf, population2), chroms);
133     assertTrue(isChromosomesEqual(population, population2));
134   }
135
136   /**
137    * Tests if mutation works for CompositeGene.
138    * @throws Exception
139    *
140    * @author Klaus Meffert
141    * @since 2.1
142    */

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

199   public void testOperate_4()
200       throws Exception JavaDoc {
201     DefaultConfiguration conf = new DefaultConfiguration();
202     GeneticOperator op = new GaussianMutationOperator(conf, 0.15d);
203     conf.addGeneticOperator(op);
204     RandomGeneratorForTest rand = new RandomGeneratorForTest();
205     rand.setNextDouble(0.45d);
206     conf.setRandomGenerator(rand);
207     conf.setFitnessFunction(new TestFitnessFunction());
208     Gene sampleGene = new IntegerGene(conf, 1, 10);
209     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
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(9));
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     assertEquals(3, chroms.size());
235     Population pop = new Population(conf, population);
236     op.operate(pop, chroms);
237     assertEquals(2, pop.size());
238     assertEquals(3 + 2, chroms.size());
239     op.operate(pop, chroms);
240     assertEquals(2, pop.size());
241     assertEquals(3 + 2 + 2, chroms.size());
242   }
243
244   /**
245    * @throws Exception
246    *
247    * @author Klaus Meffert
248    * @since 3.1
249    */

250   public void testConstruct_0()
251       throws Exception JavaDoc {
252     GaussianMutationOperator mutOp = new GaussianMutationOperator(conf, 234);
253     assertEquals(234, mutOp.getDeviation(), DELTA);
254   }
255
256   /**
257    * @throws Exception
258    *
259    * @author Klaus Meffert
260    * @since 3.1
261    */

262   public void testConstruct_1()
263       throws Exception JavaDoc {
264     GaussianMutationOperator mutOp = new GaussianMutationOperator(conf);
265     assertEquals(0.05d, mutOp.getDeviation(), DELTA);
266   }
267
268   /**
269    * @throws Exception
270    *
271    * @author Klaus Meffert
272    * @since 3.1
273    */

274   public void testConstruct_2()
275       throws Exception JavaDoc {
276     Genotype.setStaticConfiguration(conf);
277     GaussianMutationOperator mutOp = new GaussianMutationOperator();
278     assertSame(conf, mutOp.getConfiguration());
279   }
280
281   /**
282    * @throws Exception
283    *
284    * @author Klaus Meffert
285    * @since 3.1
286    */

287   public void testConstruct_3()
288       throws Exception JavaDoc {
289     GaussianMutationOperator mutOp = new GaussianMutationOperator(conf, 1.0d);
290     assertEquals(1.0d, mutOp.getDeviation(), DELTA);
291   }
292
293   /**
294    * @throws Exception
295    *
296    * @author Klaus Meffert
297    * @since 3.1
298    */

299   public void testCompareTo_0()
300       throws Exception JavaDoc {
301     GaussianMutationOperator op = new GaussianMutationOperator(conf);
302     assertEquals(1, op.compareTo(null));
303     GaussianMutationOperator op2 = new GaussianMutationOperator(conf);
304     assertEquals(0, op.compareTo(op2));
305     op = new GaussianMutationOperator(conf, 0.03d);
306     assertEquals( -1, op.compareTo(op2));
307     assertEquals(1, op2.compareTo(op));
308     op = new GaussianMutationOperator(conf, 0.05d);
309     assertEquals(0, op.compareTo(op2));
310   }
311 }
312
Popular Tags