1 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 26 public class GPProgramTest 27 extends GPTestCase { 28 29 private final static String 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 46 public void testExecute_0() 47 throws Exception { 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", CommandGene.IntegerClass); 53 pc1.getFunctions()[2] = CMD_CONST1; 54 pc1.getFunctions()[3] = new StoreTerminal(m_gpconf, "mem1", CommandGene.IntegerClass); 56 pc1.getFunctions()[4] = CMD_CONST1; 57 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, CMD_FOR, CMD_NOP, vx = Variable.create(m_gpconf, "X", CommandGene.IntegerClass), new Increment(m_gpconf, CommandGene.IntegerClass), new AddAndStore(m_gpconf, CommandGene.IntegerClass, "mem2"), new TransferMemory(m_gpconf, "mem2", "mem1"), new TransferMemory(m_gpconf, "mem1", "mem0"), new ReadTerminal(m_gpconf, CommandGene.IntegerClass, "mem0"), new ReadTerminal(m_gpconf, CommandGene.IntegerClass, "mem1"), }; 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 [] noargs = new Object [0]; 88 prog.execute_void(0, noargs); 89 vx.set(new Integer (2)); 90 prog.execute_void(1, noargs); 91 int result = prog.execute_int(2, noargs); 92 assertEquals(3, result); 93 prog.execute_void(0, noargs); 95 vx.set(new Integer (3)); 96 prog.execute_void(1, noargs); 97 result = prog.execute_int(2, noargs); 98 assertEquals(5, result); 99 prog.execute_void(0, noargs); 101 vx.set(new Integer (4)); 102 prog.execute_void(1, noargs); 103 result = prog.execute_int(2, noargs); 104 assertEquals(8, result); 105 prog.execute_void(0, noargs); 107 vx.set(new Integer (7)); 108 prog.execute_void(1, noargs); 109 result = prog.execute_int(2, noargs); 110 assertEquals(34, result); 111 } 112 113 119 public void testSerialize_0() 120 throws Exception { 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 |