KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > audit > PermutingConfigurationTest


1 package org.jgap.audit;
2
3 import java.util.*;
4 import org.jgap.*;
5 import org.jgap.event.*;
6 import org.jgap.impl.*;
7 import junit.framework.*;
8
9 /**
10  * Tests the PermutingConfiguration class
11  *
12  * @author Klaus Meffert
13  * @since 2.2
14  */

15 public class PermutingConfigurationTest
16     extends JGAPTestCase {
17   /** String containing the CVS revision. Read out via reflection!*/
18   private static final String JavaDoc CVS_REVISION = "$Revision: 1.5 $";
19
20   public void setUp() {
21     super.setUp();
22     Configuration.reset();
23   }
24
25   public static Test suite() {
26     TestSuite suite = new TestSuite(PermutingConfigurationTest.class);
27     return suite;
28   }
29
30   /**
31    * Test construction with empty fitness function, population size 0, random
32    * generator initial, sample chromosome initial of reference configuration.
33    * No error should occur.
34    * @throws Exception
35    *
36    * @author Klaus Meffert
37    * @since 2.2
38    */

39   public void testConstruct_0()
40       throws Exception JavaDoc {
41     Configuration conf1 = new DefaultConfiguration();
42     new PermutingConfiguration(conf1);
43   }
44
45   /**
46    * @throws Exception
47    *
48    * @author Klaus Meffert
49    * @since 2.2
50    */

51   public void testConstruct_1()
52       throws Exception JavaDoc {
53     Configuration conf1 = new DefaultConfiguration();
54     conf1.setFitnessFunction(new StaticFitnessFunction(0.5d));
55     PermutingConfiguration conf = new PermutingConfiguration(conf1);
56     assertEquals(StaticFitnessFunction.class,
57                  conf.getFitnessFunction().getClass());
58     assertEquals(EventManager.class, conf.getEventManager().getClass());
59     assertEquals(DefaultFitnessEvaluator.class,
60                  conf.getFitnessEvaluator().getClass());
61     assertEquals(0, conf.getNaturalSelectorsSize(true));
62     assertEquals(StockRandomGenerator.class,
63                  conf.getRandomGenerator().getClass());
64     assertEquals(ChromosomePool.class, conf.getChromosomePool().getClass());
65     assertEquals(0, conf.getGeneticOperators().size());
66     assertNull(conf.getBulkFitnessFunction());
67   }
68
69   /**
70    * Tests if the correct number of permutations appears and that they are
71    * unique.
72    * @throws Exception
73    *
74    * @author Klaus Meffert
75    * @since 2.2
76    */

77   public void testPermute_0()
78       throws Exception JavaDoc {
79     Map cache = new Hashtable();
80     Configuration conf1 = new DefaultConfiguration();
81     PermutingConfiguration conf = new PermutingConfiguration(conf1);
82     // 2 possible combinations (because only 1 RandomGenerator a time possible)
83
conf.addRandomGeneratorSlot(new StockRandomGenerator());
84     conf.addRandomGeneratorSlot(new GaussianRandomGenerator());
85     // 3 possible combinations (multiple NaturalSelector's possible)
86
conf.addNaturalSelectorSlot(new BestChromosomesSelector(conf));
87     conf.addNaturalSelectorSlot(new WeightedRouletteSelector(conf));
88     // 3 possible combinations (multiple GeneticOperator's possible)
89
conf.addGeneticOperatorSlot(new MutationOperator(conf));
90     conf.addGeneticOperatorSlot(new CrossoverOperator(conf));
91     // 1 possible combinations
92
conf.addFitnessFunctionSlot(new StaticFitnessFunction(0.5d));
93     assertTrue(conf.hasNext());
94     Configuration c;
95     for (int i = 0; i < 18; i++) { // 18 = 2 * 3 * 3 * 1
96
c = conf.next();
97       assertNotNull(c);
98       // add to cache and check that object is unique among cached ones
99
int hash = c.getRandomGenerator().hashCode()
100           + c.getFitnessFunction().hashCode();
101       for (int j = 0; j < c.getNaturalSelectorsSize(true); j++) {
102         hash += c.getNaturalSelector(true, j).hashCode();
103       }
104       for (int j = 0; j < c.getGeneticOperators().size(); j++) {
105         hash += c.getGeneticOperators().get(j).hashCode();
106       }
107       Integer JavaDoc hashI = new Integer JavaDoc(hash);
108       assertFalse(cache.containsKey(hashI));
109       cache.put(hashI, hashI);
110     }
111     assertFalse(conf.hasNext());
112   }
113
114   /**
115    * Permutation without chromosome pool
116    * @throws Exception
117    *
118    * @author Klaus Meffert
119    * @since 2.6
120    */

121   public void testPermute_1()
122       throws Exception JavaDoc {
123     Configuration conf1 = new DefaultConfiguration();
124     conf1.setChromosomePool(null);
125     conf1.setFitnessFunction(new StaticFitnessFunction(0.5d));
126     PermutingConfiguration conf = new PermutingConfiguration(conf1);
127     assertFalse(conf.hasNext());
128     conf.addFitnessFunctionSlot(new StaticFitnessFunction(0.5d));
129     conf.addRandomGeneratorSlot(new StockRandomGenerator());
130     conf.addNaturalSelectorSlot(new BestChromosomesSelector(conf));
131     conf.addGeneticOperatorSlot(new MutationOperator(conf));
132     assertTrue(conf.hasNext());
133     Configuration c = conf.next();
134     assertNull(c.getChromosomePool());
135   }
136
137   /**
138    * Ensures that not supported methods throw an exception
139    * @throws Exception
140    *
141    * @author Klaus Meffert
142    * @since 2.2
143    */

144   public void testNotSupported_1()
145       throws Exception JavaDoc {
146     Configuration conf1 = new DefaultConfiguration();
147     conf1.setFitnessFunction(new StaticFitnessFunction(0.5d));
148     PermutingConfiguration conf = new PermutingConfiguration(conf1);
149     try {
150       conf.addNaturalSelector(null, true);
151       fail();
152     } catch (UnsupportedOperationException JavaDoc uex) {
153       ;//this is OK
154
}
155     try {
156       conf.addGeneticOperator(null);
157       fail();
158     } catch (UnsupportedOperationException JavaDoc uex) {
159       ;//this is OK
160
}
161   }
162 }
163
Popular Tags