KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > multiobjective > MOFitnessEvaluator


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.multiobjective;
11
12 import java.util.*;
13 import org.jgap.*;
14 import org.jgap.impl.*;
15
16 /**
17  * Fitness evaluator for multi objectives examples.
18  *
19  * @author Klaus Meffert
20  * @since 2.6
21  */

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