KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > simpleBooleanThreaded > MaxFunction


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.simpleBooleanThreaded;
11
12 import org.jgap.*;
13 import org.jgap.impl.*;
14
15 /**
16  * Fitness function for our example. See evolve() method for details
17  *
18  * @author Klaus Meffert
19  * @since 3.01
20  */

21 public class MaxFunction
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    * This example implementation calculates the fitness value of Chromosomes
28    * using BooleanAllele implementations.
29    *
30    * @param a_subject the Chromosome to be evaluated
31    * @return defect rate of our problem
32    *
33    * @author Klaus Meffert
34    * @since 3.01
35    */

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