KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.jgap.gp.*;
18
19 /**
20  * Tests the ProgramChromosome class.
21  *
22  * @author Klaus Meffert
23  * @since 3.0
24  */

25 public class ProgramChromosomeTest
26     extends GPTestCase {
27   /** String containing the CVS revision. Read out via reflection!*/
28   private final static String JavaDoc CVS_REVISION = "$Revision: 1.5 $";
29
30   public static Test suite() {
31     TestSuite suite = new TestSuite(ProgramChromosomeTest.class);
32     return suite;
33   }
34
35   public void setUp() {
36     super.setUp();
37   }
38
39   /**
40    * Produce a valid program. Random numbers preset to optimum (= hit at first
41    * number returned by generator).
42    *
43    * @throws Exception
44    *
45    * @author Klaus Meffert
46    * @since 3.0
47    */

48   public void testGrowNode_0()
49       throws Exception JavaDoc {
50     ProgramChromosome pc = new ProgramChromosome(m_gpconf, 50, null);
51     CommandGene[] funcSet = new CommandGene[] {
52         CMD_SUB_V_I, //0
53
CMD_FOR, //1
54
CMD_NOP, //2
55
CMD_ADD, //3
56
CMD_CONST2, //4
57
CMD_CONST3, //5
58
CMD_CONST4, //6
59
};
60     rn.setNextIntSequence(new int[] {1, 4, 2, 5});
61     pc.growOrFullNode(0, 5, CommandGene.IntegerClass, 0, funcSet, CMD_SUB_V_I,
62                       0, true, -1, false);
63     pc.redepth();
64     assertEquals(CMD_SUB_V_I, pc.getNode(0));
65     assertSame(CMD_FOR, pc.getNode(1));
66     assertSame(CMD_CONST2, pc.getNode(2));
67     assertSame(CMD_NOP, pc.getNode(3));
68     assertSame(CMD_CONST3, pc.getNode(4));
69   }
70
71   /**
72    * Produce a valid program. Random numbers preset to sub-optimum (multiple
73    * requests to generator necessary).
74    *
75    * @throws Exception
76    *
77    * @author Klaus Meffert
78    * @since 3.0
79    */

80   public void testGrowNode_1()
81       throws Exception JavaDoc {
82     ProgramChromosome pc = new ProgramChromosome(m_gpconf, 50, null);
83     CommandGene[] funcSet = new CommandGene[] {
84         CMD_SUB_V_I, //0
85
CMD_FOR, //1
86
CMD_NOP, //2
87
CMD_ADD, //3
88
CMD_CONST2, //4
89
CMD_CONST3, //5
90
CMD_CONST4, //6
91
};
92     // The next sequences contain two numbers with "-1".
93
// "-1" is because we use a UniqueRandomGenerator that
94
// removes each invalid try to avoid duplicate tries.
95
// ---------------------------------------------------
96
rn.setNextIntSequence(new int[] {1, 2, 6-1, 2, 2, 5-1});
97     pc.growOrFullNode(0, 5, CommandGene.IntegerClass, 0, funcSet, CMD_SUB_V_I,
98                       0, true, -1, false);
99     pc.redepth();
100     assertEquals(CMD_SUB_V_I, pc.getNode(0));
101     assertSame(CMD_FOR, pc.getNode(1));
102     assertSame(CMD_CONST4, pc.getNode(2));
103     assertSame(CMD_NOP, pc.getNode(3));
104     assertSame(CMD_CONST3, pc.getNode(4));
105   }
106
107   /**
108    * Produce a valid program that is similar to computing Fibonacci.
109    *
110    * @throws Exception
111    *
112    * @author Klaus Meffert
113    * @since 3.0
114    */

115   public void testGrowNode_2()
116       throws Exception JavaDoc {
117     ProgramChromosome pc = new ProgramChromosome(m_gpconf, 50, null);
118     CommandGene[] funcSet = new CommandGene[] {
119         CMD_SUB_V_V_V, //0
120
CMD_FORX, //1
121
CMD_NOP, //2
122
Variable.create(m_gpconf, "X", CommandGene.IntegerClass), //3
123
new Increment(m_gpconf, CommandGene.IntegerClass), //4
124
new AddAndStore(m_gpconf, CommandGene.IntegerClass, "mem2"), //5
125
new TransferMemory(m_gpconf, "mem2", "mem1"), //6
126
new TransferMemory(m_gpconf, "mem1", "mem0"), //7
127
new ReadTerminal(m_gpconf, CommandGene.IntegerClass, "mem0"), //8
128
new ReadTerminal(m_gpconf, CommandGene.IntegerClass, "mem1"), //9
129
};
130     rn.setNextIntSequence(new int[] {0, 5, 8, 9, 6, 7});
131     pc.growOrFullNode(0, 5, CommandGene.IntegerClass, 0, funcSet, CMD_FORX, 0, true,
132                       -1, false);
133     pc.redepth();
134     assertSame(CMD_FORX, pc.getNode(0));
135     assertEquals(CMD_SUB_V_V_V, pc.getNode(1));
136     assertEquals(AddAndStore.class, pc.getNode(2).getClass());
137     assertEquals(ReadTerminal.class, pc.getNode(3).getClass());
138     assertEquals(ReadTerminal.class, pc.getNode(4).getClass());
139     assertEquals(TransferMemory.class, pc.getNode(5).getClass());
140     assertEquals(TransferMemory.class, pc.getNode(6).getClass());
141   }
142
143   /**
144    * Produce a valid program that is similar to computing Fibonacci.
145    *
146    * @throws Exception
147    *
148    * @author Klaus Meffert
149    * @since 3.0
150    */

151   public void testGrowNode_3()
152       throws Exception JavaDoc {
153     ProgramChromosome pc = new ProgramChromosome(m_gpconf, 50, null);
154     CommandGene[] funcSet = new CommandGene[] {
155         CMD_SUB_V_V_V, //0
156
CMD_FOR, //1
157
CMD_NOP, //2
158
Variable.create(m_gpconf, "X", CommandGene.IntegerClass), //3
159
new Increment(m_gpconf, CommandGene.IntegerClass), //4
160
new AddAndStore(m_gpconf, CommandGene.IntegerClass, "mem2"), //5
161
new TransferMemory(m_gpconf, "mem2", "mem1"), //6
162
new TransferMemory(m_gpconf, "mem1", "mem0"), //7
163
new ReadTerminal(m_gpconf, CommandGene.IntegerClass, "mem0"), //8
164
new ReadTerminal(m_gpconf, CommandGene.IntegerClass, "mem1"), //9
165
};
166     rn.setNextIntSequence(new int[] {3, 0, 5, 8, 9, 6, 7});
167     pc.growOrFullNode(0, 5, CommandGene.IntegerClass, 0, funcSet, CMD_FOR, 0, true,
168                       -1, false);
169     pc.redepth();
170     assertEquals(3, pc.getDepth(0));
171     assertSame(CMD_FOR, pc.getNode(0));
172     assertEquals(Variable.class, pc.getNode(1).getClass());
173     assertEquals(CMD_SUB_V_V_V, pc.getNode(2));
174     assertEquals(AddAndStore.class, pc.getNode(3).getClass());
175     assertEquals(ReadTerminal.class, pc.getNode(4).getClass());
176     assertEquals(ReadTerminal.class, pc.getNode(5).getClass());
177     assertEquals(TransferMemory.class, pc.getNode(6).getClass());
178     assertEquals(TransferMemory.class, pc.getNode(7).getClass());
179   }
180
181   /**
182    * @throws Exception
183    *
184    * @author Klaus Meffert
185    * @since 3.0
186    */

187   public void testToStringNorm_0()
188       throws Exception JavaDoc {
189     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
190     pc.setGene(0, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
191     pc.redepth();
192     assertEquals("X", pc.toStringNorm(0));
193   }
194
195   /**
196    * @throws Exception
197    *
198    * @author Klaus Meffert
199    * @since 3.0
200    */

201   public void testToStringNorm_1()
202       throws Exception JavaDoc {
203     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
204     pc.setGene(0, new Increment(m_gpconf, CommandGene.IntegerClass));
205     pc.setGene(1, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
206     pc.redepth();
207     String JavaDoc s = pc.toStringNorm(0);
208     assertEquals("INC(X)", s);
209   }
210
211   /**
212    * @throws Exception
213    *
214    * @author Klaus Meffert
215    * @since 3.0
216    */

217   public void testToStringNorm_2()
218       throws Exception JavaDoc {
219     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
220     pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass));
221     pc.setGene(1, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
222     pc.setGene(2, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
223     pc.redepth();
224     String JavaDoc s = pc.toStringNorm(0);
225     assertEquals("X + Y", s);
226   }
227
228   /**
229    * @throws Exception
230    *
231    * @author Klaus Meffert
232    * @since 3.0
233    */

234   public void testToStringNorm_3()
235       throws Exception JavaDoc {
236     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
237     pc.setGene(0, new Modulo(m_gpconf, CommandGene.IntegerClass));
238     pc.setGene(1, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
239     pc.setGene(2, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
240     pc.redepth();
241     String JavaDoc s = pc.toStringNorm(0);
242     assertEquals("X % Y", s);
243   }
244
245   /**
246    * @throws Exception
247    *
248    * @author Klaus Meffert
249    * @since 3.0
250    */

251   public void testToStringNorm_4()
252       throws Exception JavaDoc {
253     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
254     pc.setGene(0, new Modulo(m_gpconf, CommandGene.IntegerClass));
255     pc.setGene(1, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
256     pc.setGene(2, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
257     pc.redepth();
258     String JavaDoc s = pc.toStringNorm(1);
259     assertEquals("X", s);
260     s = pc.toStringNorm(2);
261     assertEquals("Y", s);
262   }
263
264   /**
265    * @throws Exception
266    *
267    * @author Klaus Meffert
268    * @since 3.0
269    */

270   public void testToStringNorm_5()
271       throws Exception JavaDoc {
272     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
273     pc.setGene(0, new Modulo(m_gpconf, CommandGene.IntegerClass));
274     pc.setGene(1, new Subtract(m_gpconf, CommandGene.IntegerClass));
275     pc.setGene(2, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
276     pc.setGene(3, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
277     pc.setGene(4, new Variable(m_gpconf, "Z", CommandGene.IntegerClass));
278     pc.redepth();
279     String JavaDoc s = pc.toStringNorm(0);
280     assertEquals("(X - Y) % Z", s);
281     s = pc.toStringNorm(1);
282     assertEquals("(X - Y)", s);
283   }
284
285   /**
286    * @throws Exception
287    *
288    * @author Klaus Meffert
289    * @since 3.0
290    */

291   public void testToStringNorm_6()
292       throws Exception JavaDoc {
293     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
294     pc.setGene(0, new Multiply(m_gpconf, CommandGene.IntegerClass));
295     pc.setGene(1, new Push(m_gpconf, CommandGene.IntegerClass));
296     pc.setGene(2, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
297     pc.setGene(3, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
298     pc.redepth();
299     String JavaDoc s = pc.toStringNorm(1);
300     assertEquals("(push X)", s);
301     s = pc.toStringNorm(0);
302     assertEquals("(push X) * X", s);
303   }
304
305   /**
306    * @throws Exception
307    *
308    * @author Klaus Meffert
309    * @since 3.0
310    */

311   public void testToStringNorm_7()
312       throws Exception JavaDoc {
313     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
314     pc.setGene(0,
315                new SubProgram(m_gpconf,
316                               new Class JavaDoc[] {CommandGene.IntegerClass,
317                               CommandGene.IntegerClass,
318                               CommandGene.IntegerClass}));
319     pc.setGene(1, new Multiply(m_gpconf, CommandGene.IntegerClass));
320     pc.setGene(2, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
321     pc.setGene(3, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
322     pc.setGene(4, new Push(m_gpconf, CommandGene.IntegerClass));
323     pc.setGene(5, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
324     pc.setGene(6,
325                new Constant(m_gpconf, CommandGene.IntegerClass, new Integer JavaDoc(7)));
326     pc.redepth();
327     String JavaDoc s = pc.toStringNorm(0);
328     assertEquals("sub[(X * Y) --> (push X) --> 7]", s);
329   }
330
331   /**
332    * @throws Exception
333    *
334    * @author Klaus Meffert
335    * @since 3.0
336    */

337   public void testToStringNorm_8()
338       throws Exception JavaDoc {
339     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
340     pc.setGene(0,
341                new SubProgram(m_gpconf,
342                               new Class JavaDoc[] {CommandGene.IntegerClass,
343                               CommandGene.IntegerClass}));
344     pc.setGene(1, new Multiply(m_gpconf, CommandGene.IntegerClass));
345     pc.setGene(2, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
346     pc.setGene(3, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
347     pc.setGene(4, new Push(m_gpconf, CommandGene.IntegerClass));
348     pc.setGene(5,
349                new Constant(m_gpconf, CommandGene.IntegerClass, new Integer JavaDoc(9)));
350     pc.redepth();
351     String JavaDoc s = pc.toStringNorm(0);
352     assertEquals("sub[(X * Y) --> (push 9)]", s);
353   }
354
355   /**
356    * @throws Exception
357    *
358    * @author Klaus Meffert
359    * @since 3.0
360    */

361   public void testToString_0()
362       throws Exception JavaDoc {
363     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
364     pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass));
365     pc.setGene(1, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
366     pc.setGene(2, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
367     pc.redepth();
368     String JavaDoc s = pc.toString(0);
369     assertEquals("+ ( X Y )", s);
370   }
371
372   /**
373    * @throws Exception
374    *
375    * @author Klaus Meffert
376    * @since 3.0
377    */

378   public void testToString_1()
379       throws Exception JavaDoc {
380     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
381     pc.setGene(0, new Subtract(m_gpconf, CommandGene.IntegerClass));
382     pc.setGene(1, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
383     pc.setGene(2, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
384     pc.redepth();
385     String JavaDoc s = pc.toString(1);
386     assertEquals("X ", s);
387     s = pc.toString(2);
388     assertEquals("Y ", s);
389   }
390
391   /**
392    * @throws Exception
393    *
394    * @author Klaus Meffert
395    * @since 3.0
396    */

397   public void testRedepth_0()
398       throws Exception JavaDoc {
399     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
400     pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass));
401     pc.setGene(1, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
402     try {
403       pc.redepth();
404       fail();
405     } catch (IllegalStateException JavaDoc ise) {
406       ; //this i expected
407
}
408   }
409
410   /**
411    * @throws Exception
412    *
413    * @author Klaus Meffert
414    * @since 3.0
415    */

416   public void testGetDepth_0()
417       throws Exception JavaDoc {
418     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
419     pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass)); //Node 0
420
pc.setGene(1, new Variable(m_gpconf, "Y", CommandGene.IntegerClass)); //Node 1
421
pc.setGene(2, new Variable(m_gpconf, "Z", CommandGene.IntegerClass)); // Node 2
422
pc.redepth();
423     assertEquals(1, pc.getDepth(0)); //1 = one level below node 0
424
assertEquals(0, pc.getDepth(1));
425     assertEquals(0, pc.getDepth(2));
426   }
427
428   /**
429    * @throws Exception
430    *
431    * @author Klaus Meffert
432    * @since 3.0
433    */

434   public void testGetDepth_1()
435       throws Exception JavaDoc {
436     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
437     pc.setGene(0, new IfElse(m_gpconf, CommandGene.IntegerClass)); //Node 0
438
pc.setGene(1, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
439     pc.setGene(2, new Variable(m_gpconf, "Z", CommandGene.IntegerClass));
440     pc.setGene(3, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
441     pc.redepth();
442     assertEquals(1, pc.getDepth(0)); //1 = one level below node 0
443
assertEquals(0, pc.getDepth(1));
444     assertEquals(0, pc.getDepth(2));
445     assertEquals(0, pc.getDepth(3));
446   }
447
448   /**
449    * @throws Exception
450    *
451    * @author Klaus Meffert
452    * @since 3.0
453    */

454   public void testGetDepth_2()
455       throws Exception JavaDoc {
456     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
457     pc.setGene(0, new IfElse(m_gpconf, CommandGene.IntegerClass)); //Node 0
458
pc.setGene(1, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
459     pc.setGene(2, new Add(m_gpconf, CommandGene.IntegerClass)); // Node 2
460
pc.setGene(3, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
461     pc.setGene(4,
462                new Constant(m_gpconf, CommandGene.IntegerClass, new Integer JavaDoc(3)));
463     pc.setGene(5, new Variable(m_gpconf, "Z", CommandGene.IntegerClass));
464     pc.redepth();
465     assertEquals(2, pc.getDepth(0)); //2 = one level below node 0
466
assertEquals(0, pc.getDepth(1));
467     assertEquals(1, pc.getDepth(2)); //1 = one level below node 2
468
assertEquals(0, pc.getDepth(3));
469     assertEquals(0, pc.getDepth(4));
470     assertEquals(0, pc.getDepth(5));
471   }
472
473   /**
474    * @throws Exception
475    *
476    * @author Klaus Meffert
477    * @since 3.0
478    */

479   public void testSerialize_0()
480       throws Exception JavaDoc {
481     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
482     pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass));
483     pc.setGene(1, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
484     pc.setGene(2, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
485     pc.redepth();
486     ProgramChromosome pc2 = (ProgramChromosome) doSerialize(pc);
487     assertEquals(pc, pc2);
488   }
489 }
490
Popular Tags