KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > impl > fitness > TruthTableFitnessFunctionTest


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.fitness;
11
12 import java.util.*;
13 import junit.framework.*;
14 import org.jgap.*;
15
16 /**
17  * Tests the TruthTableFitnessFunction class.
18  *
19  * @author Klaus Meffert
20  * @since 1.1
21  */

22 public class TruthTableFitnessFunctionTest
23     extends JGAPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.4 $";
26
27   public static Test suite() {
28     TestSuite suite = new TestSuite(TruthTableFitnessFunctionTest.class);
29     return suite;
30   }
31
32   /**
33    * @throws Exception
34    *
35    * @author Klaus Meffert
36    * @since 2.6
37    */

38   public void testCalcFitness_0()
39       throws Exception JavaDoc {
40     Map inout = new HashMap();
41     inout.put(new Double JavaDoc(2), new Double JavaDoc(3));
42     inout.put(new Double JavaDoc(9), new Double JavaDoc(11));
43     TruthTableFitnessFunctionImpl fitfunc = new TruthTableFitnessFunctionImpl(
44         conf, 7);
45     Map truthTable = new HashMap();
46     truthTable.put(new Double JavaDoc(2), new Double JavaDoc(4));
47     truthTable.put(new Double JavaDoc(8), new Double JavaDoc(1));
48     truthTable.put(new Double JavaDoc(9), new Double JavaDoc(11));
49     fitfunc.setTruthTable(truthTable);
50     assertEquals(2.0d, fitfunc.calcFitness(inout), DELTA);
51   }
52
53   /**
54    * @throws Exception
55    *
56    * @author Klaus Meffert
57    * @since 2.6
58    */

59   public void testCalcFitness_1()
60       throws Exception JavaDoc {
61     Map inout = new HashMap();
62     inout.put(new Double JavaDoc(2), new Double JavaDoc(3));
63     inout.put(new Double JavaDoc(9), new Double JavaDoc(11));
64     Map truthTable = new HashMap();
65     truthTable.put(new Double JavaDoc(2), new Double JavaDoc(4));
66     truthTable.put(new Double JavaDoc(8), new Double JavaDoc(1));
67     truthTable.put(new Double JavaDoc(9), new Double JavaDoc(11));
68     TruthTableFitnessFunctionImpl fitfunc = new TruthTableFitnessFunctionImpl(
69         conf, 7, truthTable);
70     assertEquals(2.0d, fitfunc.calcFitness(inout), DELTA);
71   }
72
73   /**
74    * Calc fitness with one value being a NaN.
75    * @throws Exception
76    *
77    * @author Klaus Meffert
78    * @since 2.6
79    */

80   public void testCalcFitness_2()
81       throws Exception JavaDoc {
82     Map inout = new HashMap();
83     inout.put(new Double JavaDoc(2), new Double JavaDoc(Double.NaN));
84     inout.put(new Double JavaDoc(9), new Double JavaDoc(11));
85     Map truthTable = new HashMap();
86     truthTable.put(new Double JavaDoc(2), new Double JavaDoc(4));
87     truthTable.put(new Double JavaDoc(8), new Double JavaDoc(1));
88     TruthTableFitnessFunctionImpl fitfunc = new TruthTableFitnessFunctionImpl(
89         conf, 7, truthTable);
90     assertTrue(Double.isNaN(fitfunc.calcFitness(inout)));
91   }
92
93   /**
94    * @throws Exception
95    *
96    * @author Klaus Meffert
97    * @since 3.1
98    */

99   public void testgetConfiguration_0()
100       throws Exception JavaDoc {
101     Map truthTable = new HashMap();
102     truthTable.put(new Double JavaDoc(9), new Double JavaDoc(11));
103     TruthTableFitnessFunctionImpl fitfunc = new TruthTableFitnessFunctionImpl(
104         conf, 7, truthTable);
105     assertSame(conf, fitfunc.getConfiguration());
106   }
107
108   /**
109    * @throws Exception
110    *
111    * @author Klaus Meffert
112    * @since 3.1
113    */

114   public void testConstruct_0()
115       throws Exception JavaDoc {
116     Genotype.setStaticConfiguration(conf);
117     TruthTableFitnessFunctionImpl fitfunc = new TruthTableFitnessFunctionImpl();
118     assertSame(conf, fitfunc.getConfiguration());
119   }
120
121   /**
122    * Implementing class of abstract TruthTableFitnessFunction class.
123    *
124    * @author Klaus Meffert
125    * @since 2.6
126    */

127   private class TruthTableFitnessFunctionImpl
128       extends TruthTableFitnessFunction {
129     /**
130      * @since 2.0 (until 1.1: type int)
131      */

132     private double m_evaluationValue;
133
134     public TruthTableFitnessFunctionImpl() {
135       super();
136     }
137
138     public TruthTableFitnessFunctionImpl(Configuration a_config,
139         double a_evaluationValue, Map a_values) {
140       super(a_config, a_values);
141       m_evaluationValue = a_evaluationValue;
142     }
143
144     public TruthTableFitnessFunctionImpl(Configuration a_config,
145         double a_evaluationValue) {
146       super(a_config);
147       m_evaluationValue = a_evaluationValue;
148     }
149
150     protected double evaluate(IChromosome a_subject) {
151       return m_evaluationValue;
152     }
153   }
154 }
155
Popular Tags