Exemplo n.º 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;
 }