KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > multiobjective > MultiObjectiveFitnessFunction


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  * Sample fitness function for the multiobjective problem.
18  *
19  * @author Klaus Meffert
20  * @since 2.6
21  */

22 public class MultiObjectiveFitnessFunction
23     extends BulkFitnessFunction {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.2 $";
26
27   public static final int MAX_BOUND = 4000;
28
29   public static final double MIN_X = -10;
30
31   public static final double MAX_X = 10;
32
33   /**
34    * Determine the fitness of the given Chromosome instance. The higher the
35    * returned value, the fitter the instance. This method should always
36    * return the same fitness value for two equivalent Chromosome instances.
37    *
38    * @param a_subject the population of chromosomes to evaluate
39    *
40    * @author Klaus Meffert
41    * @since 2.6
42    */

43   public void evaluate(Population a_subject) {
44     Iterator it = a_subject.getChromosomes().iterator();
45     while (it.hasNext()) {
46       IChromosome a_chrom1 = (IChromosome) it.next();
47       // evaluate values to fill vector of multiobjectives with
48
DoubleGene g1 = (DoubleGene) a_chrom1.getGene(0);
49       double d = g1.doubleValue();
50       double y1 = formula(1, d);
51       List l = new Vector();
52       l.add(new Double JavaDoc(y1));
53       double y2 = formula(2, d);
54       l.add(new Double JavaDoc(y2));
55       ( (Chromosome) a_chrom1).setMultiObjectives(l);
56     }
57   }
58
59   public static Vector getVector(IChromosome a_chrom) {
60     Vector result = new Vector();
61 // Gene g1 = a_chrom.getGene(0);
62
// result.add(g1);
63
// Gene g2 = a_chrom.getGene(1);
64
// result.add(g2);
65
List mo = ( (Chromosome) a_chrom).getMultiObjectives();
66     Double JavaDoc d = (Double JavaDoc) mo.get(0);
67     result.add(d);
68     d = (Double JavaDoc) mo.get(1);
69     result.add(d);
70     return result;
71   }
72
73   private double formula(int a_index, double a_x) {
74     if (a_index == 1) {
75       return a_x * a_x;
76     }
77     else {
78       return (a_x - 2) * (a_x - 2);
79     }
80   }
81
82   /**
83    * @return deep clone of the current instance
84    *
85    * @author Klaus Meffert
86    * @since 3.2
87    */

88   public Object JavaDoc clone() {
89     return new MultiObjectiveFitnessFunction();
90   }
91 }
92
Popular Tags