KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > grid > evolutionDistributed > SampleFitnessFunction


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package examples.grid.evolutionDistributed;
11
12 import org.jgap.*;
13 import org.jgap.impl.*;
14
15 /**
16  * Fitness function for our example.
17  *
18  * @author Klaus Meffert
19  * @since 3.2
20  */

21 public class SampleFitnessFunction
22     extends FitnessFunction {
23   /** String containing the CVS revision. Read out via reflection!*/
24   private final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
25
26   /**
27    * The fitness evaluation routine.
28    *
29    * @param a_subject the Chromosome to be evaluated
30    * @return defect rate of our problem
31    *
32    * @author Klaus Meffert
33    * @since 3.2
34    */

35   public double evaluate(IChromosome a_subject) {
36     int total = 0;
37     for (int i = 0; i < a_subject.size(); i++) {
38       BooleanGene value = (BooleanGene) a_subject.getGene(a_subject.size() -
39           (i + 1));
40       if (value.booleanValue()) {
41         if (i % 3 == 0) {
42           total += Math.pow(2.0, (double) i);
43         }
44         else if (i % 3 == 1) {
45           total -= Math.pow(2.0, (double) i);
46         }
47         else {
48           total += Math.pow(1.1, (double) i);
49         }
50       }
51     }
52     if (total < 0) {
53       total = 0;
54     }
55     return total;
56   }
57 }
58
Popular Tags