01: /*
02: * This file is part of JGAP.
03: *
04: * JGAP offers a dual license model containing the LGPL as well as the MPL.
05: *
06: * For licencing information please see the file license.txt included with JGAP
07: * or have a look at the top of class org.jgap.Chromosome which representatively
08: * includes the JGAP license policy applicable for any file delivered with JGAP.
09: */
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 extends FitnessFunction {
23: /** String containing the CVS revision. Read out via reflection!*/
24: private final static String CVS_REVISION = "$Revision: 1.5 $";
25:
26: /**
27: * This example implementation calculates the fitness value of Chromosomes
28: * using BooleanAllele implementations. It simply returns a fitness value
29: * equal to the numeric binary value of the bits. In other words, it
30: * optimizes the numeric value of the genes interpreted as bits. It should
31: * be noted that, for clarity, this function literally returns the binary
32: * value of the Chromosome's genes interpreted as bits. However, it would
33: * be better to return the value raised to a fixed power to exaggerate the
34: * difference between the higher values. For example, the difference
35: * between 254 and 255 is only about .04%, which isn't much incentive for
36: * the selector to choose 255 over 254. However, if you square the values,
37: * you then get 64516 and 65025, which is a difference of 0.8% -- twice
38: * as much and, therefore, twice the incentive to select the higher
39: * value.
40: *
41: * @param a_subject the Chromosome to be evaluated
42: * @return defect rate of our problem
43: *
44: * @author Neil Rotstan
45: * @author Klaus Meffert
46: * @since 2.0
47: */
48: public double evaluate(IChromosome a_subject) {
49: int total = 0;
50:
51: for (int i = 0; i < a_subject.size(); i++) {
52: BooleanGene value = (BooleanGene) a_subject
53: .getGene(a_subject.size() - (i + 1));
54: if (value.booleanValue()) {
55: total += Math.pow(2.0, (double) i);
56: }
57: }
58:
59: return total;
60: }
61: }
|