KickJava   Java API By Example, From Geeks To Geeks.

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


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 BranchTypingCross class.
22  *
23  * @author Klaus Meffert
24  * @since 3.0
25  */

26 public class BranchTypingCrossTest
27     extends GPTestCase {
28   /** String containing the CVS revision. Read out via reflection!*/
29   private final static String JavaDoc CVS_REVISION = "$Revision: 1.5 $";
30
31   public static Test suite() {
32     TestSuite suite = new TestSuite(BranchTypingCrossTest.class);
33     return suite;
34   }
35
36   public void setUp() {
37     super.setUp();
38   }
39
40   /**
41    * See if crossover chooses a correct function for a function selected
42    * priorly. Simple case: De facto replace subtrees (from "FOR" on, see above
43    * code). Cross over at functions in this test case.
44    *
45    * @throws Exception
46    *
47    * @author Klaus Meffert
48    * @since 3.0
49    */

50   public void testOperate_0()
51       throws Exception JavaDoc {
52     BranchTypingCross btc = new BranchTypingCross(m_gpconf);
53     Class JavaDoc[] types = new Class JavaDoc[]{Add.class};//needed for init. only
54
// First program.
55
// --------------
56
GPProgram prog1 = new GPProgram(m_gpconf, 1);
57     ProgramChromosome pc1 = new ProgramChromosome(m_gpconf, 50, prog1);
58     pc1.setGene(0, CMD_SUB_V_I);
59     pc1.setGene(1, CMD_FOR);
60     pc1.setGene(2, CMD_CONST2);
61     pc1.setGene(3, CMD_NOP);
62     pc1.setGene(4, CMD_CONST3);
63     pc1.redepth();
64     prog1.setChromosome(0, pc1);
65     prog1.setTypes(types);
66     // Second program.
67
// ---------------
68
GPProgram prog2 = new GPProgram(m_gpconf, 1);
69     ProgramChromosome pc2 = new ProgramChromosome(m_gpconf, 50, prog2);
70     prog2.setTypes(types);
71     pc2.setGene(0, CMD_SUB_V_I);
72     pc2.setGene(1, CMD_FOR);
73     pc2.setGene(2, CMD_ADD);
74     pc2.setGene(3, CMD_CONST0);
75     pc2.setGene(4, CMD_CONST1);
76     pc2.setGene(5, CMD_NOP);
77     pc2.setGene(6, CMD_CONST3);
78     pc2.redepth();
79     prog2.setChromosome(0, pc2);
80     // Do crossing over.
81
// -----------------
82
rn.setNextIntSequence(new int[] {
83                           1, // a node in pc1
84
1, // index of function to choose (p0 = CMD_FOR)
85
1 // index of function to choose (p1 = CMD_ADD)
86
});
87     rn.setNextFloatSequence(new float[] {
88                             0.5f, // Choose a function when crossing over
89
0.5f // Choose a function when crossing over
90
});
91     IGPProgram[] result = btc.operate(prog1, prog2);
92     assertEquals(2, result.length);
93     IGPProgram p1 = result[0];
94     ProgramChromosome chrom1 = p1.getChromosome(0);
95     IGPProgram p2 = result[1];
96     ProgramChromosome chrom2 = p2.getChromosome(0);
97     assertSame(CMD_SUB_V_I, chrom1.getGene(0));
98     // Next, FOR is assumed because it's the only function with return type void
99
// in the above compilation for pc2
100
assertSame(CMD_FOR, chrom1.getGene(1));
101     assertSame(CMD_ADD, chrom1.getGene(2));
102     assertSame(CMD_CONST0, chrom1.getGene(3));
103     assertSame(CMD_CONST1, chrom1.getGene(4));
104     assertSame(CMD_NOP, chrom1.getGene(5));
105     assertSame(CMD_CONST3, chrom1.getGene(6));
106
107     assertSame(CMD_SUB_V_I, chrom2.getGene(0));
108     assertSame(CMD_FOR, chrom2.getGene(1));
109     assertSame(CMD_CONST2, chrom2.getGene(2));
110     assertSame(CMD_NOP, chrom2.getGene(3));
111     assertSame(CMD_CONST3, chrom2.getGene(4));
112   }
113
114   /**
115    * Cross over a function with a terminal.
116    *
117    * @throws Exception
118    *
119    * @author Klaus Meffert
120    * @since 3.0
121    */

122   public void testOperate_1()
123       throws Exception JavaDoc {
124     BranchTypingCross btc = new BranchTypingCross(m_gpconf);
125     Class JavaDoc[] types = new Class JavaDoc[]{Add.class};//needed for init. only
126
// First program.
127
// --------------
128
GPProgram prog1 = new GPProgram(m_gpconf, 1);
129     ProgramChromosome pc1 = new ProgramChromosome(m_gpconf, 50, prog1);
130     pc1.setGene(0, CMD_SUB_V_I);
131     pc1.setGene(1, CMD_FOR);
132     pc1.setGene(2, CMD_CONST2);
133     pc1.setGene(3, CMD_NOP);
134     pc1.setGene(4, CMD_CONST3);
135     pc1.redepth();
136     prog1.setChromosome(0, pc1);
137     prog1.setTypes(types);
138     // Second program.
139
// ---------------
140
GPProgram prog2 = new GPProgram(m_gpconf, 1);
141     ProgramChromosome pc2 = new ProgramChromosome(m_gpconf, 50, prog2);
142     pc2.setGene(0, CMD_SUB_V_I);
143     pc2.setGene(1, CMD_FOR);
144     pc2.setGene(2, CMD_ADD);
145     pc2.setGene(3, CMD_CONST0);
146     pc2.setGene(4, CMD_CONST1);
147     pc2.setGene(5, CMD_NOP);
148     pc2.setGene(6, CMD_CONST3);
149     pc2.redepth();
150     prog2.setChromosome(0, pc2);
151     prog2.setTypes(types);
152     // Do crossing over.
153
// -----------------
154
rn.setNextIntSequence(new int[] {
155                           1, // a node in pc1
156
1, // index of function to choose (p0 = CMD_FOR)
157
2 // index of terminal to choose (p1 = NOP)
158
});
159     rn.setNextFloatSequence(new float[] {
160                             0.50f, // Choose a function when crossing over
161
0.95f // Choose a terminal when crossing over
162
});
163     IGPProgram[] result = btc.operate(prog1, prog2);
164     assertEquals(2, result.length);
165     IGPProgram p1 = result[0];
166     ProgramChromosome chrom1 = p1.getChromosome(0);
167     IGPProgram p2 = result[1];
168     ProgramChromosome chrom2 = p2.getChromosome(0);
169     assertSame(CMD_SUB_V_I, chrom1.getGene(0));
170     // NOP from other chromosome instead of FOR, CONST2, NOP (the for-loop).
171
assertSame(CMD_NOP, chrom1.getGene(1));
172     assertSame(CMD_CONST3, chrom1.getGene(2));
173
174     assertSame(CMD_SUB_V_I, chrom2.getGene(0));
175     assertSame(CMD_FOR, chrom2.getGene(1));
176     assertSame(CMD_ADD, chrom2.getGene(2));
177     assertSame(CMD_CONST0, chrom2.getGene(3));
178     assertSame(CMD_CONST1, chrom2.getGene(4));
179     // FOR, CONST2, NOP (the for-loop) from other chromosome.
180
assertSame(CMD_FOR, chrom2.getGene(5));
181     assertSame(CMD_CONST2, chrom2.getGene(6));
182     assertSame(CMD_NOP, chrom2.getGene(7));
183     assertSame(CMD_CONST3, chrom2.getGene(8));
184   }
185
186   /**
187    * Cross over a terminal with a terminal.
188    *
189    * @throws Exception
190    *
191    * @author Klaus Meffert
192    * @since 3.0
193    */

194   public void testOperate_2()
195       throws Exception JavaDoc {
196     BranchTypingCross btc = new BranchTypingCross(m_gpconf);
197     Class JavaDoc[] types = new Class JavaDoc[]{Add.class};//needed for init. only
198
// First program.
199
// --------------
200
GPProgram prog1 = new GPProgram(m_gpconf, 1);
201     ProgramChromosome pc1 = new ProgramChromosome(m_gpconf, 50, prog1);
202     pc1.setGene(0, CMD_FOR);
203     pc1.setGene(1, CMD_CONST2);
204     pc1.setGene(2, CMD_NOP);
205     pc1.redepth();
206     prog1.setChromosome(0, pc1);
207     prog1.setTypes(types);
208     // Second program.
209
// ---------------
210
GPProgram prog2 = new GPProgram(m_gpconf, 1);
211     ProgramChromosome pc2 = new ProgramChromosome(m_gpconf, 50, prog2);
212     pc2.setGene(0, CMD_SUB_V_I);
213     pc2.setGene(1, CMD_FOR);
214     pc2.setGene(2, CMD_ADD);
215     pc2.setGene(3, CMD_CONST0);
216     pc2.setGene(4, CMD_CONST1);
217     pc2.setGene(5, CMD_NOP);
218     pc2.setGene(6, CMD_CONST3);
219     pc2.redepth();
220     prog2.setChromosome(0, pc2);
221     prog2.setTypes(types);
222     // Do crossing over.
223
// -----------------
224
rn.setNextIntSequence(new int[] {
225                           1, // a node in pc1
226
0, // index of terminal to choose (p0 => CMD_CONST2)
227
2 // index of terminal to choose (p1 => CMD_CONST3)
228
});
229     rn.setNextFloatSequence(new float[] {
230                             0.95f, // Choose a terminal when crossing over
231
0.95f // Choose a terminal when crossing over
232
});
233     IGPProgram[] result = btc.operate(prog1, prog2);
234     assertEquals(2, result.length);
235     IGPProgram p1 = result[0];
236     ProgramChromosome chrom1 = p1.getChromosome(0);
237     IGPProgram p2 = result[1];
238     ProgramChromosome chrom2 = p2.getChromosome(0);
239     assertSame(CMD_FOR, chrom1.getGene(0));
240     assertSame(CMD_CONST3, chrom1.getGene(1));
241     assertSame(CMD_NOP, chrom1.getGene(2));
242
243     assertSame(CMD_SUB_V_I, chrom2.getGene(0));
244     assertSame(CMD_FOR, chrom2.getGene(1));
245     assertSame(CMD_ADD, chrom2.getGene(2));
246     assertSame(CMD_CONST0, chrom2.getGene(3));
247     assertSame(CMD_CONST1, chrom2.getGene(4));
248     assertSame(CMD_NOP, chrom2.getGene(5));
249     assertSame(CMD_CONST2, chrom2.getGene(6));
250   }
251
252   /**
253    * @throws Exception
254    *
255    * @author Klaus Meffert
256    * @since 3.0
257    */

258   public void testSerialize_0()
259       throws Exception JavaDoc {
260     BranchTypingCross btc = new BranchTypingCross(m_gpconf);
261     BranchTypingCross btc2 = (BranchTypingCross) doSerialize(btc);
262     assertEquals(btc, btc2);
263   }
264 }
265
Popular Tags