KickJava   Java API By Example, From Geeks To Geeks.

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


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

26 public class GPProgramTest
27     extends GPTestCase {
28   /** String containing the CVS revision. Read out via reflection!*/
29   private final static String JavaDoc CVS_REVISION = "$Revision: 1.3 $";
30
31   public static Test suite() {
32     TestSuite suite = new TestSuite(GPProgramTest.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 testExecute_0()
47       throws Exception JavaDoc {
48     GPProgram prog = new GPProgram(m_gpconf, 3);
49     ProgramChromosome pc1 = new ProgramChromosome(m_gpconf, 50, prog);
50     pc1.getFunctions()[0] = CMD_SUB_V_V;
51     pc1.getFunctions()[1] = new StoreTerminal(m_gpconf, "mem0", //a
52
CommandGene.IntegerClass);
53     pc1.getFunctions()[2] = CMD_CONST1;
54     pc1.getFunctions()[3] = new StoreTerminal(m_gpconf, "mem1", //b
55
CommandGene.IntegerClass);
56     pc1.getFunctions()[4] = CMD_CONST1;
57 // pc1.getFunctions()[5] = new StoreTerminal(m_gpconf, "mem2",//x
58
// CommandGene.IntegerClass);
59
// pc1.getFunctions()[6] = CMD_CONST0;
60
pc1.redepth();
61     assertEquals(2, pc1.getDepth(0));
62     prog.setChromosome(0, pc1);
63     ProgramChromosome pc2 = new ProgramChromosome(m_gpconf, 50, prog);
64     Variable vx;
65     CommandGene[] funcSet2 = new CommandGene[] {
66         CMD_SUB_V_V_V, //0
67
CMD_FOR, //1
68
CMD_NOP, //2
69
vx = Variable.create(m_gpconf, "X", CommandGene.IntegerClass), //3
70
new Increment(m_gpconf, CommandGene.IntegerClass), //4
71
new AddAndStore(m_gpconf, CommandGene.IntegerClass, "mem2"), //5
72
new TransferMemory(m_gpconf, "mem2", "mem1"), //6
73
new TransferMemory(m_gpconf, "mem1", "mem0"), //7
74
new ReadTerminal(m_gpconf, CommandGene.IntegerClass, "mem0"), //8
75
new ReadTerminal(m_gpconf, CommandGene.IntegerClass, "mem1"), //9
76
};
77     rn.setNextIntSequence(new int[] {3, 0, 5, 8, 9, 7, 6});
78     pc2.growOrFullNode(0, 5, CommandGene.IntegerClass, 0, funcSet2, CMD_FOR, 0, true,
79                        -1, false);
80     pc2.redepth();
81     prog.setChromosome(1, pc2);
82     ProgramChromosome pc3 = new ProgramChromosome(m_gpconf, 50, prog);
83     pc3.getFunctions()[0] = new ReadTerminal(m_gpconf, CommandGene.IntegerClass,
84         "mem2");
85     pc3.redepth();
86     prog.setChromosome(2, pc3);
87     Object JavaDoc[] noargs = new Object JavaDoc[0];
88     prog.execute_void(0, noargs);
89     vx.set(new Integer JavaDoc(2));
90     prog.execute_void(1, noargs);
91     int result = prog.execute_int(2, noargs);
92     assertEquals(3, result);
93     // 3
94
prog.execute_void(0, noargs);
95     vx.set(new Integer JavaDoc(3));
96     prog.execute_void(1, noargs);
97     result = prog.execute_int(2, noargs);
98     assertEquals(5, result);
99     // 4
100
prog.execute_void(0, noargs);
101     vx.set(new Integer JavaDoc(4));
102     prog.execute_void(1, noargs);
103     result = prog.execute_int(2, noargs);
104     assertEquals(8, result);
105     // 7
106
prog.execute_void(0, noargs);
107     vx.set(new Integer JavaDoc(7));
108     prog.execute_void(1, noargs);
109     result = prog.execute_int(2, noargs);
110     assertEquals(34, result);
111   }
112
113   /**
114    * @throws Exception
115    *
116    * @author Klaus Meffert
117    * @since 3.0
118    */

119   public void testSerialize_0()
120       throws Exception JavaDoc {
121     GPProgram prog = new GPProgram(m_gpconf, 1);
122     ProgramChromosome pc = new ProgramChromosome(m_gpconf);
123     pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass));
124     pc.setGene(1, new Variable(m_gpconf, "X", CommandGene.IntegerClass));
125     pc.setGene(2, new Variable(m_gpconf, "Y", CommandGene.IntegerClass));
126     pc.redepth();
127     prog.setChromosome(0, pc);
128     GPProgram prog2 = (GPProgram) doSerialize(prog);
129     assertEquals(prog, prog2);
130   }
131 }
132
Popular Tags