/** * Verifies that terminals are passed as clones during evolution and not as references. Thanx a * lot Javier for pointing this out nicely! * * @throws Exception * @author Klaus Meffert * @since 3.2 */ public void testReferenceProblem_0() throws Exception { m_gpconf.setPopulationSize(30); m_gpconf.setGPFitnessEvaluator(new DeltaGPFitnessEvaluator()); m_gpconf.setFitnessFunction(new TerminalsOnly()); m_gpconf.setRandomGenerator(new StockRandomGenerator()); // Class[] types = {CommandGene.IntegerClass}; Class[][] argTypes = {{}}; CommandGene[][] nodeSets = { { new Increment(m_gpconf, CommandGene.IntegerClass, 1), new Terminal(m_gpconf, CommandGene.IntegerClass, 1.0d, 10000.0d), } }; GPGenotype gen = GPGenotype.randomInitialGenotype(m_gpconf, types, argTypes, nodeSets, 10, false); gen.getGPPopulation().sort(new TerminalsFirstComparator()); IGPProgram prog1 = gen.getGPPopulation().getGPProgram(0); ProgramChromosome chrom1 = prog1.getChromosome(0); Terminal gene1 = (Terminal) chrom1.getGene(0); IGPProgram prog2 = gen.getGPPopulation().getGPProgram(1); ProgramChromosome chrom2 = prog2.getChromosome(0); Terminal gene2 = (Terminal) chrom2.getGene(0); assertNotSame(gene1, gene2); }
/** * @throws Exception * @author Klaus Meffert * @since 3.0 */ public void testConstruct_1() throws Exception { GPPopulation pop = new GPPopulation(m_gpconf, 2); GPProgram prog = new GPProgram(m_gpconf, 2); ProgramChromosome chrom = new ProgramChromosome(m_gpconf, 2, prog); prog.setChromosome(0, chrom); prog.setChromosome(1, chrom); pop.setGPProgram(0, prog); pop.setGPProgram(1, prog); GPGenotype gen = new GPGenotype(m_gpconf, pop, null, null, null, null, null, 1); assertSame(pop, gen.getGPPopulation()); }
public void testRandomInitialize_0() throws Exception { Variable vx; Class[] types = {CommandGene.VoidClass, CommandGene.VoidClass, CommandGene.IntegerClass}; Class[][] argTypes = {{}, {}, {}}; int[] minDepths = new int[] {3, 4, 1}; int[] maxDepths = new int[] {3, 4, 1}; CommandGene[][] nodeSets = { { CMD_SUB_V_V, // 0 CMD_CONST1, // 1 new StoreTerminal(m_gpconf, "mem0", CommandGene.IntegerClass), // 2 new StoreTerminal(m_gpconf, "mem1", CommandGene.IntegerClass), // 3 }, { vx = Variable.create(m_gpconf, "X", CommandGene.IntegerClass), // 0 new AddAndStore(m_gpconf, CommandGene.IntegerClass, "mem2"), // 1 CMD_FOR, // 2 new TransferMemory(m_gpconf, "mem2", "mem1"), // 3 new TransferMemory(m_gpconf, "mem1", "mem0"), // 4 new ReadTerminal(m_gpconf, CommandGene.IntegerClass, "mem0"), // 5 new ReadTerminal(m_gpconf, CommandGene.IntegerClass, "mem1"), // 6 CMD_SUB_V_V_V, // 7 new Increment(m_gpconf, CommandGene.IntegerClass, -1), // 8 }, {} }; // Add commands working with internal memory. // ------------------------------------------ nodeSets[2] = CommandFactory.createReadOnlyCommands( nodeSets[2], m_gpconf, CommandGene.IntegerClass, "mem", 1, 2, !true); // Execute the functionality to test. // ---------------------------------- rn.setNextIntSequence(new int[] {0, 2, 1, 3, 1, 2, 8, 0, 7, 1, 5, 6, 4, 3}); m_gpconf.setPopulationSize(1); GPGenotype gen = GPGenotype.randomInitialGenotype( m_gpconf, types, argTypes, nodeSets, minDepths, maxDepths, 200, new boolean[] {true, true, false}, false); GPPopulation pop = gen.getGPPopulation(); assertEquals(m_gpconf.getPopulationSize(), pop.size()); // Evaluate program 1 // ------------------ IGPProgram p = pop.getGPProgram(0); assertEquals(8, p.getChromosome(0).size()); assertEquals(CMD_SUB_V_V, p.getChromosome(0).getNode(0)); assertEquals(StoreTerminal.class, p.getChromosome(0).getNode(1).getClass()); assertSame(CMD_CONST1, p.getChromosome(0).getNode(2)); assertEquals(SubProgram.class, p.getChromosome(0).getNode(3).getClass()); // assertEquals(StoreTerminal.class, p.getChromosome(0).getNode(3).getClass()); assertSame(CMD_CONST1, p.getChromosome(0).getNode(4)); // Evaluate program 2 // ------------------ int node = 0; assertEquals(9, p.getChromosome(1).size()); assertEquals(CMD_FOR, p.getChromosome(1).getNode(node++)); assertEquals(Increment.class, p.getChromosome(1).getNode(node++).getClass()); assertEquals(Variable.class, p.getChromosome(1).getNode(node++).getClass()); assertEquals(CMD_SUB_V_V_V, p.getChromosome(1).getNode(node++)); assertEquals(AddAndStore.class, p.getChromosome(1).getNode(node++).getClass()); assertEquals(ReadTerminal.class, p.getChromosome(1).getNode(node++).getClass()); assertEquals(ReadTerminal.class, p.getChromosome(1).getNode(node++).getClass()); assertEquals(TransferMemory.class, p.getChromosome(1).getNode(node++).getClass()); assertEquals(TransferMemory.class, p.getChromosome(1).getNode(node++).getClass()); // Evaluate program 3 // ------------------ assertEquals(1, p.getChromosome(2).size()); assertEquals(ReadTerminal.class, p.getChromosome(2).getNode(0).getClass()); assertEquals(0.0, computeFitness(p, vx), DELTA); }