KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > impl > GPPopulationTest


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.gp.impl;
11
12 import junit.framework.*;
13 import org.jgap.*;
14 import org.jgap.impl.*;
15 import org.jgap.gp.terminal.*;
16 import org.jgap.gp.function.*;
17
18 import org.jgap.gp.*;
19
20 /**
21  * Tests the GPPopulation class.
22  *
23  * @author Klaus Meffert
24  * @since 3.0
25  */

26 public class GPPopulationTest
27     extends GPTestCase {
28   /** String containing the CVS revision. Read out via reflection!*/
29   private final static String JavaDoc CVS_REVISION = "$Revision: 1.2 $";
30
31   public static Test suite() {
32     TestSuite suite = new TestSuite(GPPopulationTest.class);
33     return suite;
34   }
35
36   public void setUp() {
37     super.setUp();
38   }
39
40   /**
41    * @throws Exception
42    *
43    * @author Klaus Meffert
44    * @since 3.0
45    */

46   public void testConstruct_0()
47       throws Exception JavaDoc {
48     GPPopulation gppop = new GPPopulation(m_gpconf, 10);
49     assertEquals(10, gppop.getPopSize());
50     assertEquals(10, gppop.size());
51   }
52
53   /**
54    * Create population with no nodes available.
55    *
56    * @throws Exception
57    *
58    * @author Klaus Meffert
59    * @since 3.0
60    */

61   public void testCreate_0()
62       throws Exception JavaDoc {
63     GPPopulation gppop = new GPPopulation(m_gpconf, 10);
64     Class JavaDoc[] types = {
65         CommandGene.IntegerClass};
66     Class JavaDoc[][] argTypes = { {}
67     };
68     CommandGene[][] nodeSets = { {
69     }
70     };
71     try {
72       gppop.create(types, argTypes, nodeSets, new int[] {1}, new int[] {1}, 1,
73                    true, 10, new boolean[] {true});
74       fail();
75     } catch (IllegalStateException JavaDoc iex) {
76       ; //this is OK
77
}
78   }
79
80   /**
81    * @throws Exception
82    *
83    * @author Klaus Meffert
84    * @since 3.0
85    */

86   public void testCreate_1()
87       throws Exception JavaDoc {
88     GPPopulation gppop = new GPPopulation(m_gpconf, 10);
89     Class JavaDoc[] types = {
90         CommandGene.IntegerClass};
91     Class JavaDoc[][] argTypes = { {}
92     };
93     CommandGene[][] nodeSets = { {
94         new Add(m_gpconf, CommandGene.IntegerClass),
95         Variable.create(m_gpconf, "X", CommandGene.IntegerClass),
96         new Constant(m_gpconf, CommandGene.IntegerClass, new Integer JavaDoc(1)),
97     }
98     };
99     rn.setNextInt(2); //Constant
100
/**@todo adapt*/
101 // ProgramChromosome pc = gppop.create(types, argTypes, nodeSets, 1, true);
102
// assertEquals(0, pc.getDepth(0));
103
// assertEquals(1, pc.size());
104
// assertEquals(Constant.class, pc.getGene(0).getClass());
105
}
106
107   /**
108    * @throws Exception
109    *
110    * @author Klaus Meffert
111    * @since 3.0
112    */

113   public void testCreate_2()
114       throws Exception JavaDoc {
115     GPPopulation gppop = new GPPopulation(m_gpconf, 10);
116     Class JavaDoc[] types = {
117         CommandGene.IntegerClass};
118     Class JavaDoc[][] argTypes = { {}
119     };
120     CommandGene[][] nodeSets = { {
121         new Add(m_gpconf, CommandGene.IntegerClass),
122         Variable.create(m_gpconf, "X", CommandGene.IntegerClass),
123         new Constant(m_gpconf, CommandGene.IntegerClass, new Integer JavaDoc(1)),
124     }
125     };
126     rn.setNextIntSequence(new int[] {0, 2, 1}); //AddCommand, Constant, Variable
127
/**@todo adapt*/
128
129 // ProgramChromosome pc = gppop.create(types, argTypes, nodeSets, 3, true);
130
// assertEquals(1, pc.getDepth(0)); //AddCommand
131
// assertEquals(0, pc.getDepth(1)); //Constant
132
// assertEquals(0, pc.getDepth(2)); //Variable
133
// assertEquals(3, pc.size());
134
// assertEquals(AddCommand.class, pc.getGene(0).getClass());
135
// assertEquals(Constant.class, pc.getGene(1).getClass());
136
// assertEquals(Variable.class, pc.getGene(2).getClass());
137
}
138
139   /**
140    * Given nodeset doesn't allow to build a valid program.
141    *
142    * @throws Exception
143    *
144    * @author Klaus Meffert
145    * @since 3.0
146    */

147   public void testCreate_3()
148       throws Exception JavaDoc {
149     GPPopulation gppop = new GPPopulation(m_gpconf, 10);
150     Class JavaDoc[] types = {
151         CommandGene.IntegerClass};
152     Class JavaDoc[][] argTypes = { {}
153     };
154     CommandGene[][] nodeSets = { {
155         new Add(m_gpconf, CommandGene.IntegerClass),
156         new Subtract(m_gpconf, CommandGene.IntegerClass),
157         new Push(m_gpconf, CommandGene.IntegerClass),
158     }
159     };
160     m_gpconf.setRandomGenerator(new StockRandomGenerator());
161     try {
162       gppop.create(types, argTypes, nodeSets, new int[] {1}, new int[] {1}, 3,
163                    true, 10, new boolean[] {true});
164       fail();
165     } catch (IllegalStateException JavaDoc iex) {
166       ; //this is OK
167
}
168   }
169
170   /**
171    * @throws Exception
172    *
173    * @author Klaus Meffert
174    * @since 3.0
175    */

176   public void testSerialize_0()
177       throws Exception JavaDoc {
178     GPPopulation pop = new GPPopulation(m_gpconf, 2);
179     GPProgram prog = new GPProgram(m_gpconf, 2);
180     ProgramChromosome chrom = new ProgramChromosome(m_gpconf, 2, prog);
181     prog.setChromosome(0, chrom);
182     prog.setChromosome(1, chrom);
183     pop.setGPProgram(0, prog);
184     pop.setGPProgram(1, prog);
185     // Serialize population to a file.
186
// -------------------------------
187
GPPopulation pop2 = (GPPopulation)doSerialize(pop);
188     assertEquals(pop, pop2);
189   }
190 }
191
Popular Tags