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 licensing 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:
018: import org.jgap.gp.*;
019:
020: /**
021: * Tests the BranchTypingCross class.
022: *
023: * @author Klaus Meffert
024: * @since 3.0
025: */
026: public class BranchTypingCrossTest extends GPTestCase {
027: /** String containing the CVS revision. Read out via reflection!*/
028: private final static String CVS_REVISION = "$Revision: 1.6 $";
029:
030: public static Test suite() {
031: TestSuite suite = new TestSuite(BranchTypingCrossTest.class);
032: return suite;
033: }
034:
035: public void setUp() {
036: super .setUp();
037: }
038:
039: /**
040: * See if crossover chooses a correct function for a function selected
041: * priorly. Simple case: De facto replace subtrees (from "FOR" on, see above
042: * code). Cross over at functions in this test case.
043: *
044: * @throws Exception
045: *
046: * @author Klaus Meffert
047: * @since 3.0
048: */
049: public void testOperate_0() throws Exception {
050: BranchTypingCross btc = new BranchTypingCross(m_gpconf);
051: Class[] types = new Class[] { Add.class };//needed for init. only
052: // First program.
053: // --------------
054: GPProgram prog1 = new GPProgram(m_gpconf, 1);
055: ProgramChromosome pc1 = new ProgramChromosome(m_gpconf, 50,
056: prog1);
057: pc1.setGene(0, CMD_SUB_V_I);
058: pc1.setGene(1, CMD_FOR);
059: pc1.setGene(2, CMD_CONST2);
060: pc1.setGene(3, CMD_NOP);
061: pc1.setGene(4, CMD_CONST3);
062: pc1.redepth();
063: prog1.setChromosome(0, pc1);
064: prog1.setTypes(types);
065: // Second program.
066: // ---------------
067: GPProgram prog2 = new GPProgram(m_gpconf, 1);
068: ProgramChromosome pc2 = new ProgramChromosome(m_gpconf, 50,
069: prog2);
070: prog2.setTypes(types);
071: pc2.setGene(0, CMD_SUB_V_I);
072: pc2.setGene(1, CMD_FOR);
073: pc2.setGene(2, CMD_ADD);
074: pc2.setGene(3, CMD_CONST0);
075: pc2.setGene(4, CMD_CONST1);
076: pc2.setGene(5, CMD_NOP);
077: pc2.setGene(6, CMD_CONST3);
078: pc2.redepth();
079: prog2.setChromosome(0, pc2);
080: // Do crossing over.
081: // -----------------
082: rn.setNextIntSequence(new int[] { 1, // a node in pc1
083: 1, // index of function to choose (p0 = CMD_FOR)
084: 1 // index of function to choose (p1 = CMD_ADD)
085: });
086: rn.setNextFloatSequence(new float[] { 0.5f, // Choose a function when crossing over
087: 0.5f // Choose a function when crossing over
088: });
089: IGPProgram[] result = btc.operate(prog1, prog2);
090: assertEquals(2, result.length);
091: IGPProgram p1 = result[0];
092: ProgramChromosome chrom1 = p1.getChromosome(0);
093: IGPProgram p2 = result[1];
094: ProgramChromosome chrom2 = p2.getChromosome(0);
095: assertSame(CMD_SUB_V_I, chrom1.getGene(0));
096: // Next, FOR is assumed because it's the only function with return type void
097: // in the above compilation for pc2
098: assertSame(CMD_FOR, chrom1.getGene(1));
099: assertSame(CMD_ADD, chrom1.getGene(2));
100: assertSame(CMD_CONST0, chrom1.getGene(3));
101: assertSame(CMD_CONST1, chrom1.getGene(4));
102: assertSame(CMD_NOP, chrom1.getGene(5));
103: assertSame(CMD_CONST3, chrom1.getGene(6));
104:
105: assertSame(CMD_SUB_V_I, chrom2.getGene(0));
106: assertSame(CMD_FOR, chrom2.getGene(1));
107: assertSame(CMD_CONST2, chrom2.getGene(2));
108: assertSame(CMD_NOP, chrom2.getGene(3));
109: assertSame(CMD_CONST3, chrom2.getGene(4));
110: }
111:
112: /**
113: * Cross over a function with a terminal.
114: *
115: * @throws Exception
116: *
117: * @author Klaus Meffert
118: * @since 3.0
119: */
120: public void testOperate_1() throws Exception {
121: BranchTypingCross btc = new BranchTypingCross(m_gpconf);
122: Class[] types = new Class[] { Add.class };//needed for init. only
123: // First program.
124: // --------------
125: GPProgram prog1 = new GPProgram(m_gpconf, 1);
126: ProgramChromosome pc1 = new ProgramChromosome(m_gpconf, 50,
127: prog1);
128: pc1.setGene(0, CMD_SUB_V_I);
129: pc1.setGene(1, CMD_FOR);
130: pc1.setGene(2, CMD_CONST2);
131: pc1.setGene(3, CMD_NOP);
132: pc1.setGene(4, CMD_CONST3);
133: pc1.redepth();
134: prog1.setChromosome(0, pc1);
135: prog1.setTypes(types);
136: // Second program.
137: // ---------------
138: GPProgram prog2 = new GPProgram(m_gpconf, 1);
139: ProgramChromosome pc2 = new ProgramChromosome(m_gpconf, 50,
140: prog2);
141: pc2.setGene(0, CMD_SUB_V_I);
142: pc2.setGene(1, CMD_FOR);
143: pc2.setGene(2, CMD_ADD);
144: pc2.setGene(3, CMD_CONST0);
145: pc2.setGene(4, CMD_CONST1);
146: pc2.setGene(5, CMD_NOP);
147: pc2.setGene(6, CMD_CONST3);
148: pc2.redepth();
149: prog2.setChromosome(0, pc2);
150: prog2.setTypes(types);
151: // Do crossing over.
152: // -----------------
153: rn.setNextIntSequence(new int[] { 1, // a node in pc1
154: 1, // index of function to choose (p0 = CMD_FOR)
155: 2 // index of terminal to choose (p1 = NOP)
156: });
157: rn.setNextFloatSequence(new float[] { 0.50f, // Choose a function when crossing over
158: 0.95f // Choose a terminal when crossing over
159: });
160: IGPProgram[] result = btc.operate(prog1, prog2);
161: assertEquals(2, result.length);
162: IGPProgram p1 = result[0];
163: ProgramChromosome chrom1 = p1.getChromosome(0);
164: IGPProgram p2 = result[1];
165: ProgramChromosome chrom2 = p2.getChromosome(0);
166: assertSame(CMD_SUB_V_I, chrom1.getGene(0));
167: // NOP from other chromosome instead of FOR, CONST2, NOP (the for-loop).
168: assertSame(CMD_NOP, chrom1.getGene(1));
169: assertSame(CMD_CONST3, chrom1.getGene(2));
170:
171: assertSame(CMD_SUB_V_I, chrom2.getGene(0));
172: assertSame(CMD_FOR, chrom2.getGene(1));
173: assertSame(CMD_ADD, chrom2.getGene(2));
174: assertSame(CMD_CONST0, chrom2.getGene(3));
175: assertSame(CMD_CONST1, chrom2.getGene(4));
176: // FOR, CONST2, NOP (the for-loop) from other chromosome.
177: assertSame(CMD_FOR, chrom2.getGene(5));
178: assertSame(CMD_CONST2, chrom2.getGene(6));
179: assertSame(CMD_NOP, chrom2.getGene(7));
180: assertSame(CMD_CONST3, chrom2.getGene(8));
181: }
182:
183: /**
184: * Cross over a terminal with a terminal.
185: *
186: * @throws Exception
187: *
188: * @author Klaus Meffert
189: * @since 3.0
190: */
191: public void testOperate_2() throws Exception {
192: BranchTypingCross btc = new BranchTypingCross(m_gpconf);
193: Class[] types = new Class[] { Add.class };//needed for init. only
194: // First program.
195: // --------------
196: GPProgram prog1 = new GPProgram(m_gpconf, 1);
197: ProgramChromosome pc1 = new ProgramChromosome(m_gpconf, 50,
198: prog1);
199: pc1.setGene(0, CMD_FOR);
200: pc1.setGene(1, CMD_CONST2);
201: pc1.setGene(2, CMD_NOP);
202: pc1.redepth();
203: prog1.setChromosome(0, pc1);
204: prog1.setTypes(types);
205: // Second program.
206: // ---------------
207: GPProgram prog2 = new GPProgram(m_gpconf, 1);
208: ProgramChromosome pc2 = new ProgramChromosome(m_gpconf, 50,
209: prog2);
210: pc2.setGene(0, CMD_SUB_V_I);
211: pc2.setGene(1, CMD_FOR);
212: pc2.setGene(2, CMD_ADD);
213: pc2.setGene(3, CMD_CONST0);
214: pc2.setGene(4, CMD_CONST1);
215: pc2.setGene(5, CMD_NOP);
216: pc2.setGene(6, CMD_CONST3);
217: pc2.redepth();
218: prog2.setChromosome(0, pc2);
219: prog2.setTypes(types);
220: // Do crossing over.
221: // -----------------
222: rn.setNextIntSequence(new int[] { 1, // a node in pc1
223: 0, // index of terminal to choose (p0 => CMD_CONST2)
224: 2 // index of terminal to choose (p1 => CMD_CONST3)
225: });
226: rn.setNextFloatSequence(new float[] { 0.95f, // Choose a terminal when crossing over
227: 0.95f // Choose a terminal when crossing over
228: });
229: IGPProgram[] result = btc.operate(prog1, prog2);
230: assertEquals(2, result.length);
231: IGPProgram p1 = result[0];
232: ProgramChromosome chrom1 = p1.getChromosome(0);
233: IGPProgram p2 = result[1];
234: ProgramChromosome chrom2 = p2.getChromosome(0);
235: assertSame(CMD_FOR, chrom1.getGene(0));
236: assertSame(CMD_CONST3, chrom1.getGene(1));
237: assertSame(CMD_NOP, chrom1.getGene(2));
238:
239: assertSame(CMD_SUB_V_I, chrom2.getGene(0));
240: assertSame(CMD_FOR, chrom2.getGene(1));
241: assertSame(CMD_ADD, chrom2.getGene(2));
242: assertSame(CMD_CONST0, chrom2.getGene(3));
243: assertSame(CMD_CONST1, chrom2.getGene(4));
244: assertSame(CMD_NOP, chrom2.getGene(5));
245: assertSame(CMD_CONST2, chrom2.getGene(6));
246: }
247:
248: /**
249: * @throws Exception
250: *
251: * @author Klaus Meffert
252: * @since 3.0
253: */
254: public void testSerialize_0() throws Exception {
255: BranchTypingCross btc = new BranchTypingCross(m_gpconf);
256: BranchTypingCross btc2 = (BranchTypingCross) doSerialize(btc);
257: assertEquals(btc, btc2);
258: }
259: }
|