001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licencing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package org.jgap.gp.impl;
011:
012: import junit.framework.*;
013: import org.jgap.*;
014: import org.jgap.impl.*;
015: import org.jgap.gp.terminal.*;
016: import org.jgap.gp.function.*;
017: import org.jgap.gp.*;
018:
019: /**
020: * Tests the ProgramChromosome class.
021: *
022: * @author Klaus Meffert
023: * @since 3.0
024: */
025: public class ProgramChromosomeTest extends GPTestCase {
026: /** String containing the CVS revision. Read out via reflection!*/
027: private final static String CVS_REVISION = "$Revision: 1.6 $";
028:
029: public static Test suite() {
030: TestSuite suite = new TestSuite(ProgramChromosomeTest.class);
031: return suite;
032: }
033:
034: public void setUp() {
035: super .setUp();
036: }
037:
038: /**
039: * @throws Exception
040: *
041: * @author Klaus Meffert
042: * @since 3.3
043: */
044: public void testConstruct_0() throws Exception {
045: try {
046: ProgramChromosome pc = new ProgramChromosome(m_gpconf, 50,
047: null);
048: fail();
049: } catch (IllegalArgumentException iex) {
050: ;//this is OK
051: }
052: }
053:
054: /**
055: * Produce a valid program. Random numbers preset to optimum (= hit at first
056: * number returned by generator).
057: *
058: * @throws Exception
059: *
060: * @author Klaus Meffert
061: * @since 3.0
062: */
063: public void testGrowNode_0() throws Exception {
064: IGPProgram ind = new GPProgram(m_gpconf, 5);
065: ProgramChromosome pc = new ProgramChromosome(m_gpconf, 50, ind);
066: CommandGene[] funcSet = new CommandGene[] { CMD_SUB_V_I, //0
067: CMD_FOR, //1
068: CMD_NOP, //2
069: CMD_ADD, //3
070: CMD_CONST2, //4
071: CMD_CONST3, //5
072: CMD_CONST4, //6
073: };
074: rn.setNextIntSequence(new int[] { 1, 4, 2, 5 });
075: pc.growOrFullNode(0, 5, CommandGene.IntegerClass, 0, funcSet,
076: CMD_SUB_V_I, 0, true, -1, false);
077: pc.redepth();
078: assertEquals(CMD_SUB_V_I, pc.getNode(0));
079: assertSame(CMD_FOR, pc.getNode(1));
080: assertSame(CMD_CONST2, pc.getNode(2));
081: assertSame(CMD_NOP, pc.getNode(3));
082: assertSame(CMD_CONST3, pc.getNode(4));
083: }
084:
085: /**
086: * Produce a valid program. Random numbers preset to sub-optimum (multiple
087: * requests to generator necessary).
088: *
089: * @throws Exception
090: *
091: * @author Klaus Meffert
092: * @since 3.0
093: */
094: public void testGrowNode_1() throws Exception {
095: IGPProgram ind = new GPProgram(m_gpconf, 5);
096: ProgramChromosome pc = new ProgramChromosome(m_gpconf, 50, ind);
097: CommandGene[] funcSet = new CommandGene[] { CMD_SUB_V_I, //0
098: CMD_FOR, //1
099: CMD_NOP, //2
100: CMD_ADD, //3
101: CMD_CONST2, //4
102: CMD_CONST3, //5
103: CMD_CONST4, //6
104: };
105: // The next sequences contain two numbers with "-1".
106: // "-1" is because we use a UniqueRandomGenerator that
107: // removes each invalid try to avoid duplicate tries.
108: // ---------------------------------------------------
109: rn.setNextIntSequence(new int[] { 1, 2, 6 - 1, 2, 2, 5 - 1 });
110: pc.growOrFullNode(0, 5, CommandGene.IntegerClass, 0, funcSet,
111: CMD_SUB_V_I, 0, true, -1, false);
112: pc.redepth();
113: assertEquals(CMD_SUB_V_I, pc.getNode(0));
114: assertSame(CMD_FOR, pc.getNode(1));
115: assertSame(CMD_CONST4, pc.getNode(2));
116: assertSame(CMD_NOP, pc.getNode(3));
117: assertSame(CMD_CONST3, pc.getNode(4));
118: }
119:
120: /**
121: * Produce a valid program that is similar to computing Fibonacci.
122: *
123: * @throws Exception
124: *
125: * @author Klaus Meffert
126: * @since 3.0
127: */
128: public void testGrowNode_2() throws Exception {
129: IGPProgram ind = new GPProgram(m_gpconf, 5);
130: ProgramChromosome pc = new ProgramChromosome(m_gpconf, 50, ind);
131: CommandGene[] funcSet = new CommandGene[] {
132: CMD_SUB_V_V_V, //0
133: CMD_FORX, //1
134: CMD_NOP, //2
135: Variable
136: .create(m_gpconf, "X", CommandGene.IntegerClass), //3
137: new Increment(m_gpconf, CommandGene.IntegerClass), //4
138: new AddAndStore(m_gpconf, CommandGene.IntegerClass,
139: "mem2"), //5
140: new TransferMemory(m_gpconf, "mem2", "mem1"), //6
141: new TransferMemory(m_gpconf, "mem1", "mem0"), //7
142: new ReadTerminal(m_gpconf, CommandGene.IntegerClass,
143: "mem0"), //8
144: new ReadTerminal(m_gpconf, CommandGene.IntegerClass,
145: "mem1"), //9
146: };
147: rn.setNextIntSequence(new int[] { 0, 5, 8, 9, 6, 7 });
148: pc.growOrFullNode(0, 5, CommandGene.IntegerClass, 0, funcSet,
149: CMD_FORX, 0, true, -1, false);
150: pc.redepth();
151: assertSame(CMD_FORX, pc.getNode(0));
152: assertEquals(CMD_SUB_V_V_V, pc.getNode(1));
153: assertEquals(AddAndStore.class, pc.getNode(2).getClass());
154: assertEquals(ReadTerminal.class, pc.getNode(3).getClass());
155: assertEquals(ReadTerminal.class, pc.getNode(4).getClass());
156: assertEquals(TransferMemory.class, pc.getNode(5).getClass());
157: assertEquals(TransferMemory.class, pc.getNode(6).getClass());
158: }
159:
160: /**
161: * Produce a valid program that is similar to computing Fibonacci.
162: *
163: * @throws Exception
164: *
165: * @author Klaus Meffert
166: * @since 3.0
167: */
168: public void testGrowNode_3() throws Exception {
169: IGPProgram ind = new GPProgram(m_gpconf, 5);
170: ProgramChromosome pc = new ProgramChromosome(m_gpconf, 50, ind);
171: CommandGene[] funcSet = new CommandGene[] {
172: CMD_SUB_V_V_V, //0
173: CMD_FOR, //1
174: CMD_NOP, //2
175: Variable
176: .create(m_gpconf, "X", CommandGene.IntegerClass), //3
177: new Increment(m_gpconf, CommandGene.IntegerClass), //4
178: new AddAndStore(m_gpconf, CommandGene.IntegerClass,
179: "mem2"), //5
180: new TransferMemory(m_gpconf, "mem2", "mem1"), //6
181: new TransferMemory(m_gpconf, "mem1", "mem0"), //7
182: new ReadTerminal(m_gpconf, CommandGene.IntegerClass,
183: "mem0"), //8
184: new ReadTerminal(m_gpconf, CommandGene.IntegerClass,
185: "mem1"), //9
186: };
187: rn.setNextIntSequence(new int[] { 3, 0, 5, 8, 9, 6, 7 });
188: pc.growOrFullNode(0, 5, CommandGene.IntegerClass, 0, funcSet,
189: CMD_FOR, 0, true, -1, false);
190: pc.redepth();
191: assertEquals(3, pc.getDepth(0));
192: assertSame(CMD_FOR, pc.getNode(0));
193: assertEquals(Variable.class, pc.getNode(1).getClass());
194: assertEquals(CMD_SUB_V_V_V, pc.getNode(2));
195: assertEquals(AddAndStore.class, pc.getNode(3).getClass());
196: assertEquals(ReadTerminal.class, pc.getNode(4).getClass());
197: assertEquals(ReadTerminal.class, pc.getNode(5).getClass());
198: assertEquals(TransferMemory.class, pc.getNode(6).getClass());
199: assertEquals(TransferMemory.class, pc.getNode(7).getClass());
200: }
201:
202: /**
203: * @throws Exception
204: *
205: * @author Klaus Meffert
206: * @since 3.0
207: */
208: public void testToStringNorm_0() throws Exception {
209: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
210: pc.setGene(0, new Variable(m_gpconf, "X",
211: CommandGene.IntegerClass));
212: pc.redepth();
213: assertEquals("X", pc.toStringNorm(0));
214: }
215:
216: /**
217: * @throws Exception
218: *
219: * @author Klaus Meffert
220: * @since 3.0
221: */
222: public void testToStringNorm_1() throws Exception {
223: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
224: pc
225: .setGene(0, new Increment(m_gpconf,
226: CommandGene.IntegerClass));
227: pc.setGene(1, new Variable(m_gpconf, "X",
228: CommandGene.IntegerClass));
229: pc.redepth();
230: String s = pc.toStringNorm(0);
231: assertEquals("INC(X)", s);
232: }
233:
234: /**
235: * @throws Exception
236: *
237: * @author Klaus Meffert
238: * @since 3.0
239: */
240: public void testToStringNorm_2() throws Exception {
241: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
242: pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass));
243: pc.setGene(1, new Variable(m_gpconf, "X",
244: CommandGene.IntegerClass));
245: pc.setGene(2, new Variable(m_gpconf, "Y",
246: CommandGene.IntegerClass));
247: pc.redepth();
248: String s = pc.toStringNorm(0);
249: assertEquals("X + Y", s);
250: }
251:
252: /**
253: * @throws Exception
254: *
255: * @author Klaus Meffert
256: * @since 3.0
257: */
258: public void testToStringNorm_3() throws Exception {
259: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
260: pc.setGene(0, new Modulo(m_gpconf, CommandGene.IntegerClass));
261: pc.setGene(1, new Variable(m_gpconf, "X",
262: CommandGene.IntegerClass));
263: pc.setGene(2, new Variable(m_gpconf, "Y",
264: CommandGene.IntegerClass));
265: pc.redepth();
266: String s = pc.toStringNorm(0);
267: assertEquals("X % Y", s);
268: }
269:
270: /**
271: * @throws Exception
272: *
273: * @author Klaus Meffert
274: * @since 3.0
275: */
276: public void testToStringNorm_4() throws Exception {
277: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
278: pc.setGene(0, new Modulo(m_gpconf, CommandGene.IntegerClass));
279: pc.setGene(1, new Variable(m_gpconf, "X",
280: CommandGene.IntegerClass));
281: pc.setGene(2, new Variable(m_gpconf, "Y",
282: CommandGene.IntegerClass));
283: pc.redepth();
284: String s = pc.toStringNorm(1);
285: assertEquals("X", s);
286: s = pc.toStringNorm(2);
287: assertEquals("Y", s);
288: }
289:
290: /**
291: * @throws Exception
292: *
293: * @author Klaus Meffert
294: * @since 3.0
295: */
296: public void testToStringNorm_5() throws Exception {
297: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
298: pc.setGene(0, new Modulo(m_gpconf, CommandGene.IntegerClass));
299: pc.setGene(1, new Subtract(m_gpconf, CommandGene.IntegerClass));
300: pc.setGene(2, new Variable(m_gpconf, "X",
301: CommandGene.IntegerClass));
302: pc.setGene(3, new Variable(m_gpconf, "Y",
303: CommandGene.IntegerClass));
304: pc.setGene(4, new Variable(m_gpconf, "Z",
305: CommandGene.IntegerClass));
306: pc.redepth();
307: String s = pc.toStringNorm(0);
308: assertEquals("(X - Y) % Z", s);
309: s = pc.toStringNorm(1);
310: assertEquals("(X - Y)", s);
311: }
312:
313: /**
314: * @throws Exception
315: *
316: * @author Klaus Meffert
317: * @since 3.0
318: */
319: public void testToStringNorm_6() throws Exception {
320: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
321: pc.setGene(0, new Multiply(m_gpconf, CommandGene.IntegerClass));
322: pc.setGene(1, new Push(m_gpconf, CommandGene.IntegerClass));
323: pc.setGene(2, new Variable(m_gpconf, "X",
324: CommandGene.IntegerClass));
325: pc.setGene(3, new Variable(m_gpconf, "X",
326: CommandGene.IntegerClass));
327: pc.redepth();
328: String s = pc.toStringNorm(1);
329: assertEquals("(push X)", s);
330: s = pc.toStringNorm(0);
331: assertEquals("(push X) * X", s);
332: }
333:
334: /**
335: * @throws Exception
336: *
337: * @author Klaus Meffert
338: * @since 3.0
339: */
340: public void testToStringNorm_7() throws Exception {
341: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
342: pc.setGene(0, new SubProgram(m_gpconf, new Class[] {
343: CommandGene.IntegerClass, CommandGene.IntegerClass,
344: CommandGene.IntegerClass }));
345: pc.setGene(1, new Multiply(m_gpconf, CommandGene.IntegerClass));
346: pc.setGene(2, new Variable(m_gpconf, "X",
347: CommandGene.IntegerClass));
348: pc.setGene(3, new Variable(m_gpconf, "Y",
349: CommandGene.IntegerClass));
350: pc.setGene(4, new Push(m_gpconf, CommandGene.IntegerClass));
351: pc.setGene(5, new Variable(m_gpconf, "X",
352: CommandGene.IntegerClass));
353: pc.setGene(6, new Constant(m_gpconf, CommandGene.IntegerClass,
354: new Integer(7)));
355: pc.redepth();
356: String s = pc.toStringNorm(0);
357: assertEquals("sub[(X * Y) --> (push X) --> 7]", s);
358: }
359:
360: /**
361: * @throws Exception
362: *
363: * @author Klaus Meffert
364: * @since 3.0
365: */
366: public void testToStringNorm_8() throws Exception {
367: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
368: pc.setGene(0, new SubProgram(m_gpconf, new Class[] {
369: CommandGene.IntegerClass, CommandGene.IntegerClass }));
370: pc.setGene(1, new Multiply(m_gpconf, CommandGene.IntegerClass));
371: pc.setGene(2, new Variable(m_gpconf, "X",
372: CommandGene.IntegerClass));
373: pc.setGene(3, new Variable(m_gpconf, "Y",
374: CommandGene.IntegerClass));
375: pc.setGene(4, new Push(m_gpconf, CommandGene.IntegerClass));
376: pc.setGene(5, new Constant(m_gpconf, CommandGene.IntegerClass,
377: new Integer(9)));
378: pc.redepth();
379: String s = pc.toStringNorm(0);
380: assertEquals("sub[(X * Y) --> (push 9)]", s);
381: }
382:
383: /**
384: * @throws Exception
385: *
386: * @author Klaus Meffert
387: * @since 3.0
388: */
389: public void testToString_0() throws Exception {
390: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
391: pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass));
392: pc.setGene(1, new Variable(m_gpconf, "X",
393: CommandGene.IntegerClass));
394: pc.setGene(2, new Variable(m_gpconf, "Y",
395: CommandGene.IntegerClass));
396: pc.redepth();
397: String s = pc.toString(0);
398: assertEquals("+ ( X Y )", s);
399: }
400:
401: /**
402: * @throws Exception
403: *
404: * @author Klaus Meffert
405: * @since 3.0
406: */
407: public void testToString_1() throws Exception {
408: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
409: pc.setGene(0, new Subtract(m_gpconf, CommandGene.IntegerClass));
410: pc.setGene(1, new Variable(m_gpconf, "X",
411: CommandGene.IntegerClass));
412: pc.setGene(2, new Variable(m_gpconf, "Y",
413: CommandGene.IntegerClass));
414: pc.redepth();
415: String s = pc.toString(1);
416: assertEquals("X ", s);
417: s = pc.toString(2);
418: assertEquals("Y ", s);
419: }
420:
421: /**
422: * @throws Exception
423: *
424: * @author Klaus Meffert
425: * @since 3.0
426: */
427: public void testRedepth_0() throws Exception {
428: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
429: pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass));
430: pc.setGene(1, new Variable(m_gpconf, "X",
431: CommandGene.IntegerClass));
432: try {
433: pc.redepth();
434: fail();
435: } catch (IllegalStateException ise) {
436: ; //this i expected
437: }
438: }
439:
440: /**
441: * @throws Exception
442: *
443: * @author Klaus Meffert
444: * @since 3.0
445: */
446: public void testGetDepth_0() throws Exception {
447: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
448: pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass)); //Node 0
449: pc.setGene(1, new Variable(m_gpconf, "Y",
450: CommandGene.IntegerClass)); //Node 1
451: pc.setGene(2, new Variable(m_gpconf, "Z",
452: CommandGene.IntegerClass)); // Node 2
453: pc.redepth();
454: assertEquals(1, pc.getDepth(0)); //1 = one level below node 0
455: assertEquals(0, pc.getDepth(1));
456: assertEquals(0, pc.getDepth(2));
457: }
458:
459: /**
460: * @throws Exception
461: *
462: * @author Klaus Meffert
463: * @since 3.0
464: */
465: public void testGetDepth_1() throws Exception {
466: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
467: pc.setGene(0, new IfElse(m_gpconf, CommandGene.IntegerClass)); //Node 0
468: pc.setGene(1, new Variable(m_gpconf, "Y",
469: CommandGene.IntegerClass));
470: pc.setGene(2, new Variable(m_gpconf, "Z",
471: CommandGene.IntegerClass));
472: pc.setGene(3, new Variable(m_gpconf, "X",
473: CommandGene.IntegerClass));
474: pc.redepth();
475: assertEquals(1, pc.getDepth(0)); //1 = one level below node 0
476: assertEquals(0, pc.getDepth(1));
477: assertEquals(0, pc.getDepth(2));
478: assertEquals(0, pc.getDepth(3));
479: }
480:
481: /**
482: * @throws Exception
483: *
484: * @author Klaus Meffert
485: * @since 3.0
486: */
487: public void testGetDepth_2() throws Exception {
488: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
489: pc.setGene(0, new IfElse(m_gpconf, CommandGene.IntegerClass)); //Node 0
490: pc.setGene(1, new Variable(m_gpconf, "Y",
491: CommandGene.IntegerClass));
492: pc.setGene(2, new Add(m_gpconf, CommandGene.IntegerClass)); // Node 2
493: pc.setGene(3, new Variable(m_gpconf, "X",
494: CommandGene.IntegerClass));
495: pc.setGene(4, new Constant(m_gpconf, CommandGene.IntegerClass,
496: new Integer(3)));
497: pc.setGene(5, new Variable(m_gpconf, "Z",
498: CommandGene.IntegerClass));
499: pc.redepth();
500: assertEquals(2, pc.getDepth(0)); //2 = one level below node 0
501: assertEquals(0, pc.getDepth(1));
502: assertEquals(1, pc.getDepth(2)); //1 = one level below node 2
503: assertEquals(0, pc.getDepth(3));
504: assertEquals(0, pc.getDepth(4));
505: assertEquals(0, pc.getDepth(5));
506: }
507:
508: /**
509: * @throws Exception
510: *
511: * @author Klaus Meffert
512: * @since 3.0
513: */
514: public void testSerialize_0() throws Exception {
515: ProgramChromosome pc = new ProgramChromosome(m_gpconf);
516: pc.setGene(0, new Add(m_gpconf, CommandGene.IntegerClass));
517: pc.setGene(1, new Variable(m_gpconf, "X",
518: CommandGene.IntegerClass));
519: pc.setGene(2, new Variable(m_gpconf, "Y",
520: CommandGene.IntegerClass));
521: pc.redepth();
522: ProgramChromosome pc2 = (ProgramChromosome) doSerialize(pc);
523: assertEquals(pc, pc2);
524: }
525: }
|