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.equalDistribution;
11:
12: import org.jgap.*;
13: import org.jgap.impl.*;
14:
15: /**
16: * Fitness function for our example. See method evaluate(..).
17: *
18: * @author Klaus Meffert
19: * @since 3.2
20: */
21: public class SampleFitnessFunction extends FitnessFunction {
22: /** String containing the CVS revision. Read out via reflection!*/
23: private final static String CVS_REVISION = "$Revision: 1.1 $";
24:
25: private Vent[] m_vents;
26:
27: public SampleFitnessFunction(Vent[] a_vents) {
28: m_vents = a_vents;
29: }
30:
31: /**
32: * Calculates the difference in weight between the 8 groups of vents. The
33: * lower the different the better the solution.
34: *
35: * @param a_subject the Chromosome to be evaluated
36: * @return fitness of our problem (smaller is better here)
37: *
38: * @author Klaus Meffert
39: * @since 3.2
40: */
41: public double evaluate(IChromosome a_subject) {
42: double[] groupWeights = new double[8];
43: double squaredDiff = 0.0d;
44: for (int i = 0; i < 8; i++) {
45: double groupWeight = 0.0d;
46: for (int j = 0; j < 8; j++) {
47: IntegerGene ventIndex = (IntegerGene) a_subject
48: .getGene((i * 8 + j));
49: Vent vent = (Vent) m_vents[ventIndex.intValue()];
50: groupWeight += vent.getWeight();
51: }
52: if (i > 0) {
53: for (int k = 0; k < i; k++) {
54: double diff = Math.abs(groupWeight
55: - groupWeights[k]);
56: squaredDiff += diff * diff;
57: }
58: }
59: groupWeights[i] = groupWeight;
60: }
61: return squaredDiff;
62: }
63: }
|