KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > GPFitnessFunction


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 org.jgap.gp;
11
12 import org.jgap.*;
13
14 /**
15  * Fitness function for GP-Programs.
16  *
17  * @author Klaus Meffert
18  * @since 3.0
19  */

20 public abstract class GPFitnessFunction
21     implements java.io.Serializable JavaDoc {
22   /** String containing the CVS revision. Read out via reflection!*/
23   private final static String JavaDoc CVS_REVISION = "$Revision: 1.5 $";
24
25   public final static double NO_FITNESS_VALUE = -1.0000000d;
26
27   public final static double MAX_FITNESS_VALUE = Double.MAX_VALUE / 2;
28
29   /**
30    * The fitness value computed during the previous run
31    */

32   private double m_lastComputedFitnessValue = NO_FITNESS_VALUE;
33
34   /**
35    * Retrieves the fitness value of the given GPProgram. The fitness
36    * value will be a positive double.
37    *
38    * @param a_program the GPProgram for which to compute and return the
39    * fitness value
40    * @return the fitness value of the given GPProgram
41    *
42    * @author Klaus Meffert
43    * @since 3.0
44    */

45   public final double getFitnessValue(final IGPProgram a_program) {
46     // Delegate to the evaluate() method to actually compute the
47
// fitness value. If the returned value is less than one,
48
// then we throw a runtime exception.
49
// ---------------------------------------------------------
50
double fitnessValue = evaluate(a_program);
51     if (fitnessValue < 0.00000000d) {
52       throw new RuntimeException JavaDoc(
53           "Fitness values must be positive! Received value: "
54           + fitnessValue);
55     }
56     m_lastComputedFitnessValue = fitnessValue;
57     return fitnessValue;
58   }
59
60   /**
61    * @return the last fitness value computed via method getFitnessValue(
62    * Chromosome), or NO_FITNES_VALUE if the former method has not been called
63    * yet
64    *
65    * @author Klaus Meffert
66    * @since 2.4
67    */

68   public double getLastComputedFitnessValue() {
69     return m_lastComputedFitnessValue;
70   }
71
72   /**
73    * Determine the fitness of the given GPProgram instance. The higher the
74    * return value, the more fit the instance. This method should always
75    * return the same fitness value for two equivalent GPProgram instances.
76    *
77    * @param a_subject the GPProgram instance to evaluate
78    *
79    * @return positive double reflecting the fitness rating of the given
80    * GPProgram. Note that if a non-positive double is returned, a
81    * RuntimeException should be generated
82    *
83    * @author Klaus Meffert
84    * @since 3.0
85    */

86   protected abstract double evaluate(IGPProgram a_subject);
87 }
88
Popular Tags