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.multiobjective;
11:
12: import java.util.*;
13: import org.jgap.*;
14: import org.jgap.impl.*;
15:
16: /**
17: * Sample fitness function for the multiobjective problem.
18: *
19: * @author Klaus Meffert
20: * @since 2.6
21: */
22: public class MultiObjectiveFitnessFunction extends BulkFitnessFunction {
23: /** String containing the CVS revision. Read out via reflection!*/
24: private final static String CVS_REVISION = "$Revision: 1.2 $";
25:
26: public static final int MAX_BOUND = 4000;
27:
28: public static final double MIN_X = -10;
29:
30: public static final double MAX_X = 10;
31:
32: /**
33: * Determine the fitness of the given Chromosome instance. The higher the
34: * returned value, the fitter the instance. This method should always
35: * return the same fitness value for two equivalent Chromosome instances.
36: *
37: * @param a_subject the population of chromosomes to evaluate
38: *
39: * @author Klaus Meffert
40: * @since 2.6
41: */
42: public void evaluate(Population a_subject) {
43: Iterator it = a_subject.getChromosomes().iterator();
44: while (it.hasNext()) {
45: IChromosome a_chrom1 = (IChromosome) it.next();
46: // evaluate values to fill vector of multiobjectives with
47: DoubleGene g1 = (DoubleGene) a_chrom1.getGene(0);
48: double d = g1.doubleValue();
49: double y1 = formula(1, d);
50: List l = new Vector();
51: l.add(new Double(y1));
52: double y2 = formula(2, d);
53: l.add(new Double(y2));
54: ((Chromosome) a_chrom1).setMultiObjectives(l);
55: }
56: }
57:
58: public static Vector getVector(IChromosome a_chrom) {
59: Vector result = new Vector();
60: // Gene g1 = a_chrom.getGene(0);
61: // result.add(g1);
62: // Gene g2 = a_chrom.getGene(1);
63: // result.add(g2);
64: List mo = ((Chromosome) a_chrom).getMultiObjectives();
65: Double d = (Double) mo.get(0);
66: result.add(d);
67: d = (Double) mo.get(1);
68: result.add(d);
69: return result;
70: }
71:
72: private double formula(int a_index, double a_x) {
73: if (a_index == 1) {
74: return a_x * a_x;
75: } else {
76: return (a_x - 2) * (a_x - 2);
77: }
78: }
79:
80: /**
81: * @return deep clone of the current instance
82: *
83: * @author Klaus Meffert
84: * @since 3.2
85: */
86: public Object clone() {
87: return new MultiObjectiveFitnessFunction();
88: }
89: }
|