KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > audit > CoinsExampleFitnessFunction


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.audit;
11
12 import org.jgap.*;
13
14 /**
15  * Sample fitness function for the coins example.
16  *
17  * @author Klaus Meffert
18  * @since 2.2
19  */

20 public class CoinsExampleFitnessFunction
21     extends FitnessFunction {
22   /** String containing the CVS revision. Read out via reflection!*/
23   private final static String JavaDoc CVS_REVISION = "$Revision: 1.3 $";
24
25   private final int m_targetAmount;
26
27   public static final int MAX_BOUND = 1000;
28
29   public CoinsExampleFitnessFunction(int a_targetAmount) {
30     if (a_targetAmount < 1 || a_targetAmount >= MAX_BOUND) {
31       throw new IllegalArgumentException JavaDoc(
32           "Change amount must be between 1 and " + MAX_BOUND + " cents.");
33     }
34     m_targetAmount = a_targetAmount;
35   }
36
37   /**
38    * Determine the fitness of the given Chromosome instance. The higher the
39    * return value, the more fit the instance. This method should always
40    * return the same fitness value for two equivalent Chromosome instances.
41    *
42    * @param a_subject the Chromosome instance to evaluate
43    *
44    * @return a positive integer reflecting the fitness rating of the given
45    * Chromosome
46    *
47    * @author Klaus Meffert
48    * @since 2.2
49    */

50   public double evaluate(IChromosome a_subject) {
51     // The fitness value measures both how close the value is to the
52
// target amount supplied by the user and the total number of coins
53
// represented by the solution. We do this in two steps: first,
54
// we consider only the represented amount of change vs. the target
55
// amount of change and return higher fitness values for amounts
56
// closer to the target, and lower fitness values for amounts further
57
// away from the target. If the amount equals the target, then we go
58
// to step 2, which returns a higher fitness value for solutions
59
// representing fewer total coins, and lower fitness values for
60
// solutions representing more total coins.
61
// ------------------------------------------------------------------
62
int changeAmount = amountOfChange(a_subject);
63     int totalCoins = getTotalNumberOfCoins(a_subject);
64     int changeDifference = Math.abs(m_targetAmount - changeAmount);
65     // Step 1: Determine distance of amount represented by solution from
66
// the target amount. Since we know the maximum amount of change is
67
// 999 cents, we'll subtract the difference in change between the
68
// solution amount and the target amount. That will give the desired effect
69
// of returning higher values for amounts closer to the target amount and
70
// lower values for amounts further away from the target amount.
71
// -----------------------------------------------------------------
72
double fitness = (MAX_BOUND - 1 - changeDifference * 20);
73     // Step 2: If the solution amount equals the target amount, then
74
// we add additional fitness points for solutions representing fewer
75
// total coins.
76
// -----------------------------------------------------------------
77
if (changeDifference == 0) {
78       fitness += computeCoinNumberBonus(totalCoins);
79     }
80     // Make sure fitness value is always positive.
81
// -------------------------------------------
82
return Math.max(1.0d, fitness);
83   }
84
85   protected double computeCoinNumberBonus(int a_coins) {
86     return MAX_BOUND - ( (MAX_BOUND * 0.05d) * a_coins);
87   }
88
89   /**
90    * Calculates the total amount of change (in cents) represented by
91    * the given potential solution and returns that amount.
92    *
93    * @param a_potentialSolution the pontential solution to evaluate
94    * @return the total amount of change (in cents) represented by the
95    * given solution
96    *
97    * @author Klaus Meffert
98    * @since 2.2
99    */

100   public static int amountOfChange(IChromosome a_potentialSolution) {
101     int numQuarters = getNumberOfCoinsAtGene(a_potentialSolution, 0);
102     int numDimes = getNumberOfCoinsAtGene(a_potentialSolution, 1);
103     int numNickels = getNumberOfCoinsAtGene(a_potentialSolution, 2);
104     int numPennies = getNumberOfCoinsAtGene(a_potentialSolution, 3);
105     return (numQuarters * 25) + (numDimes * 10) + (numNickels * 5) +
106         numPennies;
107   }
108
109   /**
110    * Retrieves the number of coins represented by the given potential
111    * solution at the given gene position.
112    *
113    * @param a_potentialSolution the potential solution to evaluate
114    * @param a_position the gene position to evaluate
115    * @return the number of coins represented by the potential solution
116    * at the given gene position.
117    *
118    * @author Klaus Meffert
119    * @since 2.2
120    */

121   public static int getNumberOfCoinsAtGene(IChromosome a_potentialSolution,
122                                            int a_position) {
123     Integer JavaDoc numCoins =
124         (Integer JavaDoc) a_potentialSolution.getGene(a_position).getAllele();
125     return numCoins.intValue();
126   }
127
128   /**
129    * Returns the total number of coins represented by all of the genes in
130    * the given potential solution.
131    *
132    * @param a_potentialsolution The potential solution to evaluate.
133    * @return The total number of coins represented by the given Chromosome.
134    *
135    * @since 2.2
136    */

137   public static int getTotalNumberOfCoins(IChromosome a_potentialsolution) {
138     int totalCoins = 0;
139     int numberOfGenes = a_potentialsolution.size();
140     for (int i = 0; i < numberOfGenes; i++) {
141       totalCoins += getNumberOfCoinsAtGene(a_potentialsolution, i);
142     }
143     return totalCoins;
144   }
145 }
146
Popular Tags