KickJava   Java API By Example, From Geeks To Geeks.

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


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 InversionOperator class.
19  *
20  * @author Klaus Meffert
21  * @since 2.3
22  */

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

43   public void testConstruct_0()
44       throws Exception JavaDoc {
45     new InversionOperator(conf);
46   }
47
48   /**
49    * @throws Exception
50    *
51    * @author Klaus Meffert
52    * @since 3.1
53    */

54   public void testConstruct_1()
55       throws Exception JavaDoc {
56     Genotype.setStaticConfiguration(conf);
57     InversionOperator op = new InversionOperator();
58     assertSame(conf, op.getConfiguration());
59   }
60
61   /**
62    * @throws Exception
63    *
64    * @author Klaus Meffert
65    * @since 1.1
66    */

67   public void testOperate_0()
68       throws Exception JavaDoc {
69     DefaultConfiguration conf = new DefaultConfiguration();
70     RandomGeneratorForTest rand = new RandomGeneratorForTest();
71     // tupels: (index of chromosome; locus), see InversionOperator.operate
72
rand.setNextIntSequence(new int[] {
73                             0, 1, 1, 1});
74     conf.setRandomGenerator(rand);
75     conf.setFitnessFunction(new TestFitnessFunction());
76     Gene sampleGene = new IntegerGene(conf, 1, 10);
77     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
78     conf.setSampleChromosome(chrom);
79     conf.setPopulationSize(6);
80     InversionOperator op = new InversionOperator(conf);
81     Gene cgene1 = new IntegerGene(conf, 1, 10);
82     cgene1.setAllele(new Integer JavaDoc(6));
83     Gene[] genes1 = new Gene[] {
84         cgene1};
85     Chromosome chrom1 = new Chromosome(conf, genes1);
86     Gene cgene2 = new IntegerGene(conf, 1, 10);
87     cgene2.setAllele(new Integer JavaDoc(8));
88     Gene cgene3 = new IntegerGene(conf, 2, 120);
89     cgene3.setAllele(new Integer JavaDoc(99));
90     Gene[] genes2 = new Gene[] {
91         cgene2, cgene3};
92     Chromosome chrom2 = new Chromosome(conf, genes2);
93     Chromosome[] population = new Chromosome[] {
94         chrom1, chrom2};
95     List chroms = new Vector();
96     Gene gene1 = new IntegerGene(conf, 1, 10);
97     gene1.setAllele(new Integer JavaDoc(5));
98     chroms.add(gene1);
99     Gene gene2 = new IntegerGene(conf, 1, 10);
100     gene2.setAllele(new Integer JavaDoc(7));
101     chroms.add(gene2);
102     Gene gene3 = new IntegerGene(conf, 1, 10);
103     gene3.setAllele(new Integer JavaDoc(4));
104     chroms.add(gene3);
105     final int size = chroms.size();
106     op.operate(new Population(conf, population), chroms);
107     assertEquals(size + 1, chroms.size());
108     Chromosome target = (Chromosome) chroms.get(size);
109     assertEquals(6, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
110     op.operate(new Population(conf, population), chroms);
111     assertEquals(size + 2, chroms.size());
112     target = (Chromosome) chroms.get(size + 1);
113     assertEquals(99, ( (Integer JavaDoc) target.getGene(0).getAllele()).intValue());
114     assertEquals(8, ( (Integer JavaDoc) target.getGene(1).getAllele()).intValue());
115   }
116
117   /**
118    * Tests if operator produces same results for two operate-runs.
119    * @throws Exception
120    *
121    * @author Klaus Meffert
122    * @since 2.0
123    */

124   public void testOperate_1()
125       throws Exception JavaDoc {
126     DefaultConfiguration conf = new DefaultConfiguration();
127     GeneticOperator op = new InversionOperator(conf);
128     conf.addGeneticOperator(op);
129     RandomGeneratorForTest rand = new RandomGeneratorForTest();
130     rand.setNextIntSequence(new int[] {
131                             0, 1, 0, 1, 2});
132     conf.setRandomGenerator(rand);
133     conf.setFitnessFunction(new TestFitnessFunction());
134     Gene sampleGene = new IntegerGene(conf, 1, 10);
135     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
136     conf.setSampleChromosome(chrom);
137     conf.setPopulationSize(6);
138     Gene cgene1 = new IntegerGene(conf, 1, 10);
139     cgene1.setAllele(new Integer JavaDoc(6));
140     Gene[] genes1 = new Gene[] {
141         cgene1};
142     Chromosome chrom1 = new Chromosome(conf, genes1);
143     Gene cgene2 = new IntegerGene(conf, 1, 10);
144     cgene2.setAllele(new Integer JavaDoc(8));
145     Gene[] genes2 = new Gene[] {
146         cgene2};
147     Chromosome chrom2 = new Chromosome(conf, genes2);
148     Chromosome[] population = new Chromosome[] {
149         chrom1, chrom2};
150     List chroms = new Vector();
151     Gene gene1 = new IntegerGene(conf, 1, 10);
152     gene1.setAllele(new Integer JavaDoc(5));
153     chroms.add(gene1);
154     Gene gene2 = new IntegerGene(conf, 1, 10);
155     gene2.setAllele(new Integer JavaDoc(7));
156     chroms.add(gene2);
157     Gene gene3 = new IntegerGene(conf, 1, 10);
158     gene3.setAllele(new Integer JavaDoc(4));
159     chroms.add(gene3);
160     Chromosome[] population2 = (Chromosome[]) population.clone();
161     op.operate(new Population(conf, population), chroms);
162     op.operate(new Population(conf, population2), chroms);
163     assertTrue(isChromosomesEqual(population, population2));
164   }
165
166   /**
167    * Test with CompositeGene.
168    * @throws Exception
169    *
170    * @author Klaus Meffert
171    * @since 2.1
172    */

173   public void testOperate_2()
174       throws Exception JavaDoc {
175     DefaultConfiguration conf = new DefaultConfiguration();
176     RandomGeneratorForTest rand = new RandomGeneratorForTest();
177     // tupels: (index of chromosome; locus), see InversionOperator.operate
178
rand.setNextIntSequence(new int[] {
179                             0, 1, 1, 1, 2});
180     conf.setRandomGenerator(rand);
181     conf.setFitnessFunction(new TestFitnessFunction());
182     Gene sampleGene = new IntegerGene(conf, 1, 10);
183     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
184     conf.setSampleChromosome(chrom);
185     conf.setPopulationSize(6);
186     InversionOperator op = new InversionOperator(conf);
187     Gene cgene1 = new IntegerGene(conf, 1, 10);
188     cgene1.setAllele(new Integer JavaDoc(6));
189     CompositeGene compGene = new CompositeGene(conf);
190     compGene.addGene(cgene1);
191     Gene[] genes1 = new Gene[] {
192         compGene};
193     Chromosome chrom1 = new Chromosome(conf, genes1);
194     Gene cgene2 = new IntegerGene(conf, 1, 10);
195     cgene2.setAllele(new Integer JavaDoc(8));
196     Gene[] genes2 = new Gene[] {
197         cgene2};
198     Chromosome chrom2 = new Chromosome(conf, genes2);
199     Chromosome[] population = new Chromosome[] {
200         chrom1, chrom2};
201     List chroms = new Vector();
202     Gene gene1 = new IntegerGene(conf, 1, 10);
203     gene1.setAllele(new Integer JavaDoc(5));
204     chroms.add(gene1);
205     Gene gene2 = new IntegerGene(conf, 1, 10);
206     gene2.setAllele(new Integer JavaDoc(7));
207     chroms.add(gene2);
208     Gene gene3 = new IntegerGene(conf, 1, 10);
209     gene3.setAllele(new Integer JavaDoc(4));
210     chroms.add(gene3);
211     int size = chroms.size();
212     op.operate(new Population(conf, population), chroms);
213     assertEquals(size + 1, chroms.size());
214     Chromosome target = (Chromosome) chroms.get(size);
215     CompositeGene cog = (CompositeGene) target.getGene(0);
216     assertEquals(6, ( (Integer JavaDoc) ( (Vector) cog.getAllele()).get(0)).intValue());
217     op.operate(new Population(conf, population), chroms);
218     assertEquals(size + 2, chroms.size());
219     target = (Chromosome) chroms.get(size + 1);
220     IntegerGene result = (IntegerGene) target.getGene(0);
221     assertEquals(8, ( (Integer JavaDoc) result.getAllele()).intValue());
222   }
223
224   /**
225    * Test equals with classcast object.
226    *
227    * @throws Exception
228    * @author Klaus Meffert
229    * @since 2.6
230    */

231   public void testEquals_0()
232       throws Exception JavaDoc {
233     GeneticOperator op = new InversionOperator(conf);
234     assertFalse(op.equals(new Chromosome(conf)));
235   }
236
237   /**
238    * @throws Exception
239    *
240    * @author Klaus Meffert
241    * @since 2.6
242    */

243   public void testOperate_3()
244       throws Exception JavaDoc {
245     DefaultConfiguration conf = new DefaultConfiguration();
246     RandomGeneratorForTest rand = new RandomGeneratorForTest();
247     // tupels: (index of chromosome; locus), see InversionOperator.operate
248
rand.setNextIntSequence(new int[] {
249                             0, 1, 1, 1, 2});
250     conf.setPopulationSize(6);
251     conf.setRandomGenerator(rand);
252     Gene sampleGene = new IntegerGene(conf, 1, 10);
253     Chromosome chrom = new Chromosome(conf, sampleGene, 3);
254     conf.setSampleChromosome(chrom);
255     Gene gene1 = new IntegerGene(conf, 1, 10);
256     gene1.setAllele(new Integer JavaDoc(5));
257     Gene cgene1 = new IntegerGene(conf, 1, 10);
258     cgene1.setAllele(new Integer JavaDoc(6));
259     Gene cgene1_2 = new IntegerGene(conf, 1, 10);
260     cgene1_2.setAllele(new Integer JavaDoc(9));
261     CompositeGene compGene = new CompositeGene(conf);
262     compGene.addGene(cgene1);
263     Gene[] genes1 = new Gene[] {
264         compGene};
265     Chromosome chrom1 = new Chromosome(conf, genes1);
266     chrom1.setConstraintChecker(new TestConstraintChecker());
267     Gene cgene2 = new IntegerGene(conf, 1, 10);
268     cgene2.setAllele(new Integer JavaDoc(8));
269     Gene[] genes2 = new Gene[] {
270         cgene2, cgene1_2};
271     Chromosome chrom2 = new Chromosome(conf);
272     chrom2.setGenes(genes2);
273     chrom2.setConstraintChecker(new TestConstraintChecker());
274     Chromosome[] population = new Chromosome[] {
275         chrom1, chrom2};
276     List chroms = new Vector();
277     chroms.add(gene1);
278     Gene gene2 = new IntegerGene(conf, 1, 10);
279     gene2.setAllele(new Integer JavaDoc(7));
280     chroms.add(gene2);
281     Gene gene3 = new IntegerGene(conf, 1, 10);
282     gene3.setAllele(new Integer JavaDoc(4));
283     chroms.add(gene3);
284     InversionOperator op = new InversionOperator(conf);
285     try {
286       op.operate(new Population(conf, population), chroms);
287       fail();
288     } catch (Error JavaDoc iex) {
289       ; //this is OK
290
}
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     InversionOperator op = new InversionOperator(conf);
302     assertEquals(1, op.compareTo(null));
303     InversionOperator op2 = new InversionOperator(conf);
304     assertEquals(0, op.compareTo(op2));
305     assertEquals(0, op2.compareTo(op));
306   }
307
308   public class TestConstraintChecker
309       implements IGeneConstraintChecker {
310     private int m_callcount;
311
312     public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
313                           IChromosome a_chrom, int a_index) {
314       if (m_callcount++ >= 2) {
315         return false;
316       }
317       else {
318         return true;
319       }
320     }
321   }
322 }
323
Popular Tags