KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > config > MaximizingFunctionFitnessFunction


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.config;
11
12 import org.jgap.*;
13
14 /**
15  * Sample Fitness function for the MaximizingFunction problem.
16  */

17 public class MaximizingFunctionFitnessFunction
18     extends FitnessFunction {
19   /** String containing the CVS revision. Read out via reflection!*/
20   private final static String JavaDoc CVS_REVISION = "$Revision: 1.4 $";
21
22   /**
23    * Determine the fitness of the given Chromosome instance. The higher the
24    * return value, the more fit the instance. This method should always
25    * return the same fitness value for two equivalent Chromosome instances.
26    * @author Siddhartha Azad.
27    * @param a_chromosome the Chromosome instance to evaluate
28    * @return a positive integer reflecting the fitness rating of the given
29    * Chromosome
30    */

31   public double evaluate(IChromosome a_chromosome) {
32     int numGenes = a_chromosome.size();
33     if (numGenes != 3) {
34       throw new IllegalArgumentException JavaDoc("Chromosome for " +
35                                          "MaximizingFunction must have "
36                                          + "exactly 3 genes.");
37     }
38     Integer JavaDoc aVal = (Integer JavaDoc) a_chromosome.getGene(0).getAllele();
39     Integer JavaDoc bVal = (Integer JavaDoc) a_chromosome.getGene(1).getAllele();
40     Integer JavaDoc cVal = (Integer JavaDoc) a_chromosome.getGene(2).getAllele();
41     return (aVal.intValue() - bVal.intValue() + cVal.intValue());
42   }
43 }
44
Popular Tags