KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jgap.*;
13 import junit.framework.*;
14
15 /**
16  * Tests for the BulkFitnessOffsetRemover class.
17  *
18  * @author Achim Westermann
19  * @since 2.2
20  */

21 public class BulkFitnessOffsetRemoverTest
22     extends JGAPTestCase {
23   /** String containing the CVS revision. Read out via reflection! */
24   private final static String JavaDoc CVS_REVISION = "$Revision: 1.11 $";
25
26   // A plainforward implementation for this test.
27
//---------------------------------------------
28
private final transient FitnessFunction m_fitnessFunction =
29       new DummyFitnessFunction();
30
31   // The instance that is tested.
32
//-----------------------------
33
private final transient BulkFitnessFunction m_bulkFitnessFunction =
34       new BulkFitnessOffsetRemover(m_fitnessFunction);
35
36   public static Test suite() {
37     TestSuite suite = new TestSuite(BulkFitnessOffsetRemoverTest.class);
38     return suite;
39   }
40
41   /**
42    * Test the inner class DummyFitnessFunction that is needed for this test with
43    * IntegerGenes in a Chromosome.
44    *
45    * @throws Exception
46    */

47   public void testDummyFitnessFunction_0()
48       throws Exception JavaDoc {
49     // setting up a Chromosome and assign values:
50
//-------------------------------------------
51
NumberGene[] genes = {
52         new IntegerGene(conf),
53         new IntegerGene(conf),
54         new IntegerGene(conf)};
55     Number JavaDoc[] values = {
56         new Integer JavaDoc(100),
57         new Integer JavaDoc(200),
58         new Integer JavaDoc(300)};
59     for (int i = 0; i < genes.length; i++) {
60       genes[i].setAllele(values[i]);
61     }
62     Chromosome chromosome = new Chromosome(conf, genes);
63     // Using the DummyFitnessFunction:
64
//--------------------------------
65
assertNotNull(m_fitnessFunction);
66     double fitness = m_fitnessFunction.getFitnessValue(chromosome);
67     assertEquals( (double) (100 + 200 + 300), fitness, 0.0);
68   }
69
70   /**
71    * Test the inner class DummyFitnessFunction that is needed for this test with
72    * a mix of IntegerGenes and DoubleGenes in a Chromosome.
73    *
74    * @throws Exception
75    */

76   public void testDummyFitnessFunction_1()
77       throws Exception JavaDoc {
78     // setting up a Chromosome and assign values:
79
//-------------------------------------------
80
NumberGene[] genes = {
81         new IntegerGene(conf),
82         new DoubleGene(conf),
83         new DoubleGene(conf)};
84     Number JavaDoc[] values = {
85         new Integer JavaDoc(100),
86         new Double JavaDoc(2000.25),
87         new Double JavaDoc(0.75e11)};
88     for (int i = 0; i < genes.length; i++) {
89       genes[i].setAllele(values[i]);
90     }
91     Chromosome chromosome = new Chromosome(conf, genes);
92     // Using the DummyFitnessFunction:
93
//--------------------------------
94
assertNotNull(m_fitnessFunction);
95     double fitness = m_fitnessFunction.getFitnessValue(chromosome);
96     assertEquals( (double) (100 + 2000.25 + 0.75e11), fitness, 0.0);
97   }
98
99   public void testConstructor_0() {
100     assertNotNull(m_fitnessFunction);
101     new BulkFitnessOffsetRemover(m_fitnessFunction);
102   }
103
104   public void testConstructor_1() {
105     try {
106       BulkFitnessOffsetRemover test = new BulkFitnessOffsetRemover(null);
107       fail("The constructor of "
108            + test.getClass().getName() + " allows a null value!");
109     }
110     catch (Throwable JavaDoc f) {
111       ; //this is OK
112
}
113   }
114
115   /**
116    * Tests the method BulkFitnessOffsetRemover.evaluate(java.util.List) with a
117    * list of two Chromosomes with each one IntegerGene with the allele 100.
118    *
119    * @author Achim Westermann
120    * @since 2.2
121    * @throws Exception
122    */

123   public void testEvaluate_0()
124       throws Exception JavaDoc {
125     assertNotNull(m_fitnessFunction);
126     assertNotNull(m_bulkFitnessFunction);
127     // setting up two Chromosomes and assign values:
128
//--------------------------------------------------
129
Gene g100A = new IntegerGene(conf);
130     g100A.setAllele(new Integer JavaDoc(100));
131     Gene g100B = new IntegerGene(conf);
132     g100B.setAllele(new Integer JavaDoc(100));
133     Chromosome c100A = new Chromosome(conf, new Gene[] {g100A});
134     Chromosome c100B = new Chromosome(conf, new Gene[] {g100B});
135     // Running the evaluate method.
136
// It will remove the common offset 100 and add 1.
137
//-------------------------------------------------
138
Population chromosomeList = new Population(conf);
139     chromosomeList.addChromosome(c100A);
140     chromosomeList.addChromosome(c100B);
141     m_bulkFitnessFunction.evaluate(chromosomeList);
142     // The offset should have been removed:
143
//-------------------------------------
144
assertEquals(1.0, c100A.getFitnessValue(), 0d);
145     assertEquals(1.0, c100B.getFitnessValue(), 0d);
146   }
147
148   /**
149    * Tests the method BulkFitnessOffsetRemover.evaluate(List) with a list of two
150    * Chromosomes with one IntegerGene with the allele 100. The IntegerGene is
151    * the same instance in both Chromosomes.
152    *
153    * @author Achim Westermann
154    * @since 2.2
155    * @throws Exception
156    */

157   public void testEvaluate_1()
158       throws Exception JavaDoc {
159     assertNotNull(m_fitnessFunction);
160     assertNotNull(m_bulkFitnessFunction);
161     // setting up two Chromosomes and assign values:
162
//--------------------------------------------------
163
Gene G100 = new IntegerGene(conf);
164     G100.setAllele(new Integer JavaDoc(100));
165     Chromosome C100A = new Chromosome(conf, new Gene[] {G100});
166     Chromosome C100B = new Chromosome(conf, new Gene[] {G100});
167     // Running the evaluate method.
168
// It will remove the common offset 100 and add 1.
169
//-------------------------------------------------
170
Population chromosomeList = new Population(conf);
171     chromosomeList.addChromosome(C100A);
172     chromosomeList.addChromosome(C100B);
173     m_bulkFitnessFunction.evaluate(chromosomeList);
174     // The offset should have been removed:
175
//-------------------------------------
176
assertEquals(1.0, C100A.getFitnessValue(), 0d);
177     assertEquals(1.0, C100B.getFitnessValue(), 0d);
178   }
179
180   /**
181    * Tests the method BulkFitnessOffsetRemover.evaluate(List) with a list of two
182    * Chromosomes with each one IntegerGene with the allele 200. The Chromosomes
183    * ar the same instance in the List that is evaluated.
184    *
185    * @author Achim Westermann
186    * @since 2.2
187    * @throws Exception
188    */

189   public void testEvaluate_2()
190       throws Exception JavaDoc {
191     assertNotNull(m_fitnessFunction);
192     assertNotNull(m_bulkFitnessFunction);
193     // setting up two Chromosomes and assign values:
194
//--------------------------------------------------
195
Gene G200 = new IntegerGene(conf);
196     G200.setAllele(new Integer JavaDoc(200));
197     Chromosome C200 = new Chromosome(conf, new Gene[] {G200});
198     // Running the evaluate method.
199
// It will remove the common offset 100 and add 1.
200
//-------------------------------------------------
201
Population chromosomeList = new Population(conf);
202     chromosomeList.addChromosome(C200);
203     chromosomeList.addChromosome(C200);
204     m_bulkFitnessFunction.evaluate(chromosomeList);
205     // The offset should have been removed:
206
//-------------------------------------
207
assertEquals(1.0, C200.getFitnessValue(), 0d);
208   }
209
210   /**
211    * Tests the method BulkFitnessOffsetRemover.evaluate(List) with a list of two
212    * Chromosomes with each two NumberGenes. <pre>
213    * chromosomeA
214    * / \
215    * IntegerGene DoubleGene => Fitness 4100.15
216    * | |
217    * 100 4000.15
218    * chromosomeB
219    * / \
220    * IntegerGene DoubleGene =>; Fitness 1234 + 14e-5
221    * | |
222    * 1234 14e-5
223    * </pre>
224    *
225    * @author Achim Westermann
226    * @since 2.2
227    * @throws Exception
228    */

229   public void testEvaluate_3()
230       throws Exception JavaDoc {
231     assertNotNull(m_fitnessFunction);
232     assertNotNull(m_bulkFitnessFunction);
233     // setting up a two Chromosome instances and assign values:
234
//---------------------------------------------------------
235
NumberGene[] genesA = {
236         new IntegerGene(conf), new DoubleGene(conf)};
237     Number JavaDoc[] valuesA = {
238         new Integer JavaDoc(100), new Double JavaDoc(4000.15)};
239     for (int i = 0; i < genesA.length; i++) {
240       genesA[i].setAllele(valuesA[i]);
241     }
242     Chromosome chromosomeA = new Chromosome(conf, new Gene[] {
243                                             genesA[0],
244                                             genesA[1]});
245     NumberGene[] genesB = {
246         new IntegerGene(conf), new DoubleGene(conf)};
247     Number JavaDoc[] valuesB = {
248         new Integer JavaDoc(1234), new Double JavaDoc(14.e-5)};
249     for (int i = 0; i < genesA.length; i++) {
250       genesB[i].setAllele(valuesB[i]);
251     }
252     Chromosome chromosomeB = new Chromosome(conf, new Gene[] {
253                                             genesB[0],
254                                             genesB[1]});
255     // Calculation of the estimated result:
256
//--------------------------------------
257
double offset = Math.min( ( (IntegerGene) genesA[0]).intValue()
258                              + ( (DoubleGene) genesA[1]).doubleValue(),
259                              ( (IntegerGene) genesB[0]).intValue()
260                              + ( (DoubleGene) genesB[1]).doubleValue());
261     double estimateFitnessA = ( (IntegerGene) genesA[0]).intValue()
262         + ( (DoubleGene) genesA[1]).doubleValue() - offset + 1d;
263     double estimateFitnessB = ( (IntegerGene) genesB[0]).intValue()
264         + ( (DoubleGene) genesB[1]).doubleValue() - offset + 1d;
265     // Running the evaluate method.
266
// It will remove the common offset.
267
//-------------------------------------------------
268
Population chromosomeList = new Population(conf);
269     chromosomeList.addChromosome(chromosomeA);
270     chromosomeList.addChromosome(chromosomeB);
271     m_bulkFitnessFunction.evaluate(chromosomeList);
272     // The offset should have been removed:
273
//-------------------------------------
274
assertEquals(estimateFitnessA, chromosomeA.getFitnessValue(), 0d);
275     assertEquals(estimateFitnessB, chromosomeB.getFitnessValue(), 0d);
276   }
277
278   /**
279    *
280    * @author Klaus Meffert
281    * @since 2.2
282    * @throws Exception
283    */

284   public void testGetAbsoluteFitness_0()
285       throws Exception JavaDoc {
286     BulkFitnessOffsetRemover remover = new BulkFitnessOffsetRemover(
287         new StaticFitnessFunction(33.345d));
288     NumberGene[] genes = {
289         new IntegerGene(conf),
290         new IntegerGene(conf),
291         new IntegerGene(conf)};
292     Number JavaDoc[] values = {
293         new Integer JavaDoc(100),
294         new Integer JavaDoc(200),
295         new Integer JavaDoc(300)};
296     for (int i = 0; i < genes.length; i++) {
297       genes[i].setAllele(values[i]);
298     }
299     Chromosome chrom = new Chromosome(conf, genes);
300     double fitness = remover.getAbsoluteFitness(chrom);
301     assertEquals(33.345d, chrom.getFitnessValue(), DELTA);
302     assertEquals(33.345d, fitness, DELTA);
303     fitness = remover.getAbsoluteFitness(chrom);
304     assertEquals(33.345d, chrom.getFitnessValue(), DELTA);
305     assertEquals(33.345d, fitness, DELTA);
306   }
307
308   /**
309    * <p>
310    * This class is a helper to allow testing class
311    * {@link BulkFitnessOffsetRemover}in a plainforward way: It only works
312    * with {@link NumberGene}instances and returns the addition of their
313    * primitive values as the fitness value. This way we can configure a
314    * Chromosome with numerical value genes knowing their fitness and see what
315    * fitness values should be installed by the method
316    * {@link BulkFitnessOffsetRemover}.
317    * </p>
318    * <p>
319    * Do not copy or use this implementation it is for this test only. It is
320    * tested itself by the outer class ({@link #testDummyFitnessFunction()})
321    * to ensure valid test results.
322    * </p>
323    *
324    * @author Achim Westermann
325    * @since 2.2
326    */

327   private class DummyFitnessFunction
328       extends FitnessFunction {
329     /**
330      * Casts every Gene to the subtype of NumberGene and calculates the sum
331      * of the numerical values obtained.
332      *
333      * @param a_subject the Chromosome to be evaluated
334      * @return the sum of all numerical values of the Genes within the
335      * given Chromosome that only consists of NumberGene instances
336      * @throws ClassCastException if a Gene within the Chromosome is not derived
337      * from {@link NumberGene}
338      *
339      * @see org.jgap.FitnessFunction#evaluate(org.jgap.Chromosome)
340      */

341     protected double evaluate(IChromosome a_subject) {
342       double ret = 0d;
343       Gene[] genes = a_subject.getGenes();
344       for (int i = genes.length - 1; i > -1; i--) {
345         if (genes[i] instanceof DoubleGene) {
346           ret += ( (DoubleGene) genes[i]).doubleValue();
347         }
348         else if (genes[i] instanceof IntegerGene) {
349           ret += ( (IntegerGene) genes[i]).intValue();
350         }
351         else {
352           throw new ClassCastException JavaDoc(
353               "The FitnessFunction "
354               + getClass().getName()
355               + " is for testing purposes only and only handles DoubleGene "
356               + "and NumberGene instances (used: "
357               + genes[i].getClass().getName() + ")");
358         }
359       }
360       return ret;
361     }
362   }
363 }
364
Popular Tags