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 licensing 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: * Fitness evaluator for multi objectives example.
18: *
19: * @author Klaus Meffert
20: * @since 2.6
21: */
22: public class MOFitnessEvaluator implements FitnessEvaluator {
23: /** String containing the CVS revision. Read out via reflection!*/
24: private final static String CVS_REVISION = "$Revision: 1.2 $";
25:
26: /**
27: * Not to be called in multi-objectives context!
28: *
29: * @param a_fitness_value1 ignored
30: * @param a_fitness_value2 ignored
31: * @return always a RuntimeException
32: *
33: * @author Klaus Meffert
34: * @since 2.6
35: */
36: public boolean isFitter(final double a_fitness_value1,
37: final double a_fitness_value2) {
38: throw new RuntimeException(
39: "Not supported for multi-objectives!");
40: }
41:
42: public boolean isFitter(IChromosome a_chrom1, IChromosome a_chrom2) {
43: // Evaluate values to fill vector of multiobjectives with.
44: DoubleGene g1 = (DoubleGene) a_chrom1.getGene(0);
45: double d = g1.doubleValue();
46: double y1 = formula(1, d);
47: List l = new Vector();
48: l.add(new Double(y1));
49: double y2 = formula(2, d);
50: l.add(new Double(y2));
51: ((Chromosome) a_chrom1).setMultiObjectives(l);
52:
53: l.clear();
54: g1 = (DoubleGene) a_chrom2.getGene(0);
55: d = g1.doubleValue();
56: y1 = formula(1, d);
57: l.add(new Double(y1));
58: y2 = formula(2, d);
59: l.add(new Double(y2));
60: ((Chromosome) a_chrom2).setMultiObjectives(l);
61:
62: List v1 = ((Chromosome) a_chrom1).getMultiObjectives();
63: List v2 = ((Chromosome) a_chrom2).getMultiObjectives();
64: int size = v1.size();
65: if (size != v2.size()) {
66: throw new RuntimeException(
67: "Size of objectives inconsistent!");
68: }
69: boolean better = false;
70: for (int i = 0; i < size; i++) {
71: double d1 = ((Double) v1.get(i)).doubleValue();
72: double d2 = ((Double) v2.get(i)).doubleValue();
73: if (d1 > d2) {
74: better = true;
75: } else if (d1 < d2) {
76: return false;
77: }
78: }
79: return better;
80: }
81:
82: private double formula(int a_index, double a_x) {
83: if (a_index == 1) {
84: return a_x * a_x;
85: } else {
86: return (a_x - 2) * (a_x - 2);
87: }
88: }
89: }
|