KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > chromInit > 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.chromInit;
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 Neil Rotstan
19  * @author Klaus Meffert
20  * @since 2.4
21  */

22 public class MaxFunction
23     extends FitnessFunction {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.3 $";
26
27   /**
28    * See examples.simpleBoolean.MaxFunction for description.
29    * Please notice that in this example here, we have chromosomes with one and
30    * Chromosomes with two Genes, so that the description from the original
31    * MaxFunction does not apply directly here.
32    * @param a_subject the Chromosome to be evaluated
33    * @return defect rate of our problem
34    *
35    * @author Klaus Meffert
36    * @since 2.4
37    */

38   public double evaluate(IChromosome a_subject) {
39     int total = 0;
40
41     for (int i = 0; i < a_subject.size(); i++) {
42       BooleanGene value = (BooleanGene) a_subject.getGene(a_subject.size() -
43           (i + 1));
44       if (value.booleanValue()) {
45         total += Math.pow(2.0, (double) i);
46       }
47     }
48
49     return total;
50   }
51 }
52
Popular Tags