KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > FitnessFunction


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;
11
12 import java.io.*;
13 import org.jgap.util.*;
14
15 /**
16  * Fitness functions are used to determine how optimal a particular solution
17  * is relative to other solutions. This abstract class should be extended and
18  * the evaluate() method implemented. The fitness function is given a Chromosome
19  * to evaluate and should return a positive double that reflects its fitness
20  * value. The higher the value, the more fit the Chromosome. The actual range
21  * of fitness values isn't important (other than the fact that they must be
22  * positive doubles): it's the relative difference as a percentage that
23  * tends to determine the success or failure of a Chromosome. So in other words,
24  * two Chromosomes with respective fitness values of 1 and 100 have the same
25  * relative fitness to each other as two Chromosomes with respective fitness
26  * values of 10 and 1000 (in each case, the first is 1% as fit as the second).
27  * <p>
28  * Note: Two Chromosomes with equivalent sets of genes should always be
29  * assigned the same fitness value by any implementation of this interface.
30  *
31  * @author Neil Rotstan
32  * @author Klaus Meffert
33  * @since 1.0
34  */

35 public abstract class FitnessFunction
36     implements Serializable, ICloneable {
37   /** String containing the CVS revision. Read out via reflection!*/
38   private final static String JavaDoc CVS_REVISION = "$Revision: 1.20 $";
39
40   public final static double NO_FITNESS_VALUE = -1.0000000d;
41
42   public final static double DELTA = 0.0000001d;
43
44   /**
45    * The fitness value computed during the previous run
46    */

47   private double m_lastComputedFitnessValue = NO_FITNESS_VALUE;
48
49   /**
50    * Retrieves the fitness value of the given Chromosome. The fitness
51    * value will be a positive double.
52    *
53    * @param a_subject the Chromosome for which to compute and return the
54    * fitness value
55    * @return the fitness value of the given Chromosome
56    *
57    * @author Neil Rotstan
58    * @author Klaus Meffert
59    * @since 2.0 (until 1.1: return type int)
60    */

61   public final double getFitnessValue(final IChromosome a_subject) {
62     // Delegate to the evaluate() method to actually compute the
63
// fitness value. If the returned value is less than one,
64
// then we throw a runtime exception.
65
// ---------------------------------------------------------
66
double fitnessValue = evaluate(a_subject);
67     if (fitnessValue < 0.00000000d) {
68       throw new RuntimeException JavaDoc(
69           "Fitness values must be positive! Received value: "
70           + fitnessValue);
71     }
72     m_lastComputedFitnessValue = fitnessValue;
73     return fitnessValue;
74   }
75
76   /**
77    * @return the last fitness value computed via method getFitnessValue(
78    * Chromosome), or NO_FITNES_VALUE if the former method has not been called
79    * yet
80    *
81    * @author Klaus Meffert
82    * @since 2.4
83    */

84   public double getLastComputedFitnessValue() {
85     return m_lastComputedFitnessValue;
86   }
87
88   /**
89    * Determine the fitness of the given Chromosome instance. The higher the
90    * return value, the more fit the instance. This method should always
91    * return the same fitness value for two equivalent Chromosome instances.
92    *
93    * @param a_subject the Chromosome instance to evaluate
94    *
95    * @return positive double reflecting the fitness rating of the given
96    * Chromosome. Note that if a non-positive double is returned, a
97    * RuntimeException should be generated
98    *
99    * @author Neil Rotstan
100    * @author Klaus Meffert
101    * @since 2.0 (until 1.1: return type int)
102    */

103   protected abstract double evaluate(IChromosome a_subject);
104
105   /**
106    * Please override in your implementations!
107    *
108    * @return deep clone of the current instance
109    *
110    * @author Klaus Meffert
111    * @since 3.2
112    */

113   public Object JavaDoc clone() {
114     try {
115       return super.clone();
116     } catch (CloneNotSupportedException JavaDoc cex) {
117       throw new CloneException(cex);
118     }
119   }
120 }
121
Popular Tags