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.*; 18 19 25 public class ProgramChromosomeTest 26 extends GPTestCase { 27 28 private final static String 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 48 public void testGrowNode_0() 49 throws Exception { 50 ProgramChromosome pc = new ProgramChromosome(m_gpconf, 50, null); 51 CommandGene[] funcSet = new CommandGene[] { 52 CMD_SUB_V_I, CMD_FOR, CMD_NOP, CMD_ADD, CMD_CONST2, CMD_CONST3, CMD_CONST4, }; 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 80 public void testGrowNode_1() 81 throws Exception { 82 ProgramChromosome pc = new ProgramChromosome(m_gpconf, 50, null); 83 CommandGene[] funcSet = new CommandGene[] { 84 CMD_SUB_V_I, CMD_FOR, CMD_NOP, CMD_ADD, CMD_CONST2, CMD_CONST3, CMD_CONST4, }; 92 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 115 public void testGrowNode_2() 116 throws Exception { 117 ProgramChromosome pc = new ProgramChromosome(m_gpconf, 50, null); 118 CommandGene[] funcSet = new CommandGene[] { 119 CMD_SUB_V_V_V, CMD_FORX, CMD_NOP, 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"), }; 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 151 public void testGrowNode_3() 152 throws Exception { 153 ProgramChromosome pc = new ProgramChromosome(m_gpconf, 50, null); 154 CommandGene[] funcSet = new CommandGene[] { 155 CMD_SUB_V_V_V, CMD_FOR, CMD_NOP, 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"), }; 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 187 public void testToStringNorm_0() 188 throws Exception { 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 201 public void testToStringNorm_1() 202 throws Exception { 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 s = pc.toStringNorm(0); 208 assertEquals("INC(X)", s); 209 } 210 211 217 public void testToStringNorm_2() 218 throws Exception { 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 s = pc.toStringNorm(0); 225 assertEquals("X + Y", s); 226 } 227 228 234 public void testToStringNorm_3() 235 throws Exception { 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 s = pc.toStringNorm(0); 242 assertEquals("X % Y", s); 243 } 244 245 251 public void testToStringNorm_4() 252 throws Exception { 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 s = pc.toStringNorm(1); 259 assertEquals("X", s); 260 s = pc.toStringNorm(2); 261 assertEquals("Y", s); 262 } 263 264 270 public void testToStringNorm_5() 271 throws Exception { 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 s = pc.toStringNorm(0); 280 assertEquals("(X - Y) % Z", s); 281 s = pc.toStringNorm(1); 282 assertEquals("(X - Y)", s); 283 } 284 285 291 public void testToStringNorm_6() 292 throws Exception { 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 s = pc.toStringNorm(1); 300 assertEquals("(push X)", s); 301 s = pc.toStringNorm(0); 302 assertEquals("(push X) * X", s); 303 } 304 305 311 public void testToStringNorm_7() 312 throws Exception { 313 ProgramChromosome pc = new ProgramChromosome(m_gpconf); 314 pc.setGene(0, 315 new SubProgram(m_gpconf, 316 new Class [] {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 (7))); 326 pc.redepth(); 327 String s = pc.toStringNorm(0); 328 assertEquals("sub[(X * Y) --> (push X) --> 7]", s); 329 } 330 331 337 public void testToStringNorm_8() 338 throws Exception { 339 ProgramChromosome pc = new ProgramChromosome(m_gpconf); 340 pc.setGene(0, 341 new SubProgram(m_gpconf, 342 new Class [] {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 (9))); 350 pc.redepth(); 351 String s = pc.toStringNorm(0); 352 assertEquals("sub[(X * Y) --> (push 9)]", s); 353 } 354 355 361 public void testToString_0() 362 throws Exception { 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 s = pc.toString(0); 369 assertEquals("+ ( X Y )", s); 370 } 371 372 378 public void testToString_1() 379 throws Exception { 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 s = pc.toString(1); 386 assertEquals("X ", s); 387 s = pc.toString(2); 388 assertEquals("Y ", s); 389 } 390 391 397 public void testRedepth_0() 398 throws Exception { 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 ise) { 406 ; } 408 } 409 410 416 public void testGetDepth_0() 417 throws Exception { 418 ProgramChromosome pc = new ProgramChromosome(m_gpconf); 419 pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass)); pc.setGene(1, new Variable(m_gpconf, "Y", CommandGene.IntegerClass)); pc.setGene(2, new Variable(m_gpconf, "Z", CommandGene.IntegerClass)); pc.redepth(); 423 assertEquals(1, pc.getDepth(0)); assertEquals(0, pc.getDepth(1)); 425 assertEquals(0, pc.getDepth(2)); 426 } 427 428 434 public void testGetDepth_1() 435 throws Exception { 436 ProgramChromosome pc = new ProgramChromosome(m_gpconf); 437 pc.setGene(0, new IfElse(m_gpconf, CommandGene.IntegerClass)); 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)); assertEquals(0, pc.getDepth(1)); 444 assertEquals(0, pc.getDepth(2)); 445 assertEquals(0, pc.getDepth(3)); 446 } 447 448 454 public void testGetDepth_2() 455 throws Exception { 456 ProgramChromosome pc = new ProgramChromosome(m_gpconf); 457 pc.setGene(0, new IfElse(m_gpconf, CommandGene.IntegerClass)); pc.setGene(1, new Variable(m_gpconf, "Y", CommandGene.IntegerClass)); 459 pc.setGene(2, new Add(m_gpconf, CommandGene.IntegerClass)); pc.setGene(3, new Variable(m_gpconf, "X", CommandGene.IntegerClass)); 461 pc.setGene(4, 462 new Constant(m_gpconf, CommandGene.IntegerClass, new Integer (3))); 463 pc.setGene(5, new Variable(m_gpconf, "Z", CommandGene.IntegerClass)); 464 pc.redepth(); 465 assertEquals(2, pc.getDepth(0)); assertEquals(0, pc.getDepth(1)); 467 assertEquals(1, pc.getDepth(2)); assertEquals(0, pc.getDepth(3)); 469 assertEquals(0, pc.getDepth(4)); 470 assertEquals(0, pc.getDepth(5)); 471 } 472 473 479 public void testSerialize_0() 480 throws Exception { 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 |