コード例 #1
0
 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;
 }
コード例 #2
0
 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);
 }