KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > simpleBoolean > 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.simpleBoolean;
11
12 import org.jgap.*;
13 import org.jgap.impl.*;
14
15 /**
16  * Fitness function for our example. See evaluate(...) method for details.
17  *
18  * @author Neil Rotstan
19  * @author Klaus Meffert
20  * @since 2.0
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.5 $";
26
27   /**
28    * This example implementation calculates the fitness value of Chromosomes
29    * using BooleanAllele implementations. It simply returns a fitness value
30    * equal to the numeric binary value of the bits. In other words, it
31    * optimizes the numeric value of the genes interpreted as bits. It should
32    * be noted that, for clarity, this function literally returns the binary
33    * value of the Chromosome's genes interpreted as bits. However, it would
34    * be better to return the value raised to a fixed power to exaggerate the
35    * difference between the higher values. For example, the difference
36    * between 254 and 255 is only about .04%, which isn't much incentive for
37    * the selector to choose 255 over 254. However, if you square the values,
38    * you then get 64516 and 65025, which is a difference of 0.8% -- twice
39    * as much and, therefore, twice the incentive to select the higher
40    * value.
41    *
42    * @param a_subject the Chromosome to be evaluated
43    * @return defect rate of our problem
44    *
45    * @author Neil Rotstan
46    * @author Klaus Meffert
47    * @since 2.0
48    */

49   public double evaluate(IChromosome a_subject) {
50     int total = 0;
51
52     for (int i = 0; i < a_subject.size(); i++) {
53       BooleanGene value = (BooleanGene) a_subject.getGene(a_subject.size() -
54           (i + 1));
55       if (value.booleanValue()) {
56         total += Math.pow(2.0, (double) i);
57       }
58     }
59
60     return total;
61   }
62 }
63
Popular Tags