public int compare(Object o1, Object o2) { IGPProgram prog1 = (IGPProgram) o1; IGPProgram prog2 = (IGPProgram) o2; CommandGene gene1 = prog1.getChromosome(0).getGene(0); CommandGene gene2 = prog2.getChromosome(0).getGene(0); boolean o1is, o2is; if (gene1 instanceof Terminal) { o1is = true; } else { o1is = false; } if (gene2 instanceof Terminal) { o2is = true; } else { o2is = false; } if (o1is) { if (o2is) { return 0; } else { return -1; } } else { if (o2is) { return 1; } else { return 0; } } }
/** * 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); }
private double computeFitness(IGPProgram a_program, Variable vx) { double error = 0.0f; Object[] noargs = new Object[0]; // Initialize local stores. // ------------------------ a_program.getGPConfiguration().clearStack(); a_program.getGPConfiguration().clearMemory(); // Compute fitness for each program. // --------------------------------- for (int i = 2; i < 15; i++) { for (int j = 0; j < a_program.size(); j++) { vx.set(new Integer(i)); try { try { // Only evaluate after whole GP program was run. // --------------------------------------------- if (j == a_program.size() - 1) { double result = a_program.execute_int(j, noargs); error += Math.abs(result - fib_iter(i)); } else { a_program.execute_void(j, noargs); } } catch (IllegalStateException iex) { error = Double.MAX_VALUE / 2; break; } } catch (ArithmeticException ex) { System.out.println("Arithmetic Exception with x = " + i); System.out.println(a_program.getChromosome(j)); throw ex; } } } return error; }
protected double evaluate(IGPProgram a_subject) { ProgramChromosome chrom1 = a_subject.getChromosome(0); CommandGene gene1 = chrom1.getGene(0); if (gene1 instanceof Terminal) { return gene1.execute_double(null, 0, new Object[] {}); } else { return 999999.0d; } }
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); }