KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > FitnessFunctionTest


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;
11
12 import junit.framework.*;
13
14 /**
15  * Tests the FitnessFunction class.
16  *
17  * @author Klaus Meffert
18  * @since 1.1
19  */

20 public class FitnessFunctionTest
21     extends JGAPTestCase {
22   /** String containing the CVS revision. Read out via reflection!*/
23   private final static String JavaDoc CVS_REVISION = "$Revision: 1.14 $";
24
25   public static Test suite() {
26     TestSuite suite = new TestSuite(FitnessFunctionTest.class);
27     return suite;
28   }
29
30   public void testGetFitnessValue_0() {
31     FitnessFunctionImpl fitfunc = new FitnessFunctionImpl(7);
32     assertEquals(7.0d, fitfunc.getFitnessValue(null), 0.00000001d);
33   }
34
35   public void testGetFitnessValue_1() {
36     try {
37       FitnessFunctionImpl fitfunc = new FitnessFunctionImpl( -7);
38       fitfunc.getFitnessValue(null);
39       fail();
40     }
41     catch (RuntimeException JavaDoc cause) {
42       ; // This is expected since non-positive fitness values are illegal.
43
}
44   }
45
46   public void testGetFitnessValue_2() {
47     try {
48       FitnessFunctionImpl fitfunc = new FitnessFunctionImpl(0);
49       fitfunc.getFitnessValue(null);
50     }
51     catch (RuntimeException JavaDoc cause) {
52       ; // This is expected since non-positive fitness values are illegal.
53
}
54   }
55
56   /**
57    * @throws Exception
58    *
59    * @author Klaus Meffert
60    * @since 2.6
61    */

62   public void testLastComputedValue_0()
63       throws Exception JavaDoc {
64     FitnessFunctionImpl fitfunc = new FitnessFunctionImpl(47.15d);
65     assertEquals(FitnessFunction.NO_FITNESS_VALUE,
66                  fitfunc.getLastComputedFitnessValue(), DELTA);
67     IChromosome chrom = new Chromosome(new ConfigurationForTest());
68     fitfunc.getFitnessValue(chrom);
69     assertEquals(47.15d, fitfunc.getLastComputedFitnessValue(), DELTA);
70   }
71
72   /**
73    * Implementing class of abstract FitnessFunction class.
74    *
75    * @author Klaus Meffert
76    * @since 1.1
77    */

78   private class FitnessFunctionImpl
79       extends FitnessFunction {
80     /**
81      * @since 2.0 (until 1.1: type int)
82      */

83     private double m_evaluationValue;
84
85     public FitnessFunctionImpl(double a_evaluationValue) {
86       m_evaluationValue = a_evaluationValue;
87     }
88
89     protected double evaluate(IChromosome a_subject) {
90       return m_evaluationValue;
91     }
92   }
93 }
94
Popular Tags