KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > perf > TestOverallPerformanceFitnessFunc


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

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

45   public double evaluate(IChromosome a_subject) {
46     // The fitness value measures both how close the value is to the
47
// target amount supplied by the user and the total number of coins
48
// represented by the solution. We do this in two steps: first,
49
// we consider only the represented amount of change vs. the target
50
// amount of change and return higher fitness values for amounts
51
// closer to the target, and lower fitness values for amounts further
52
// away from the target. If the amount equals the target, then we go
53
// to step 2, which returns a higher fitness value for solutions
54
// representing fewer total coins, and lower fitness values for
55
// solutions representing more total coins.
56
// ------------------------------------------------------------------
57
int changeAmount = amountOfChange(a_subject);
58     int totalCoins = getTotalNumberOfCoins(a_subject);
59     int changeDifference = Math.abs(m_targetAmount - changeAmount);
60     // Step 1: Determine distance of amount represented by solution from
61
// the target amount. Since we know the maximum amount of change is
62
// 99 cents, we'll subtract the difference in change between the
63
// solution amount and the target amount from 99. That will give
64
// the desired effect of returning higher values for amounts
65
// closer to the target amount and lower values for amounts
66
// further away from the target amount.
67
// -----------------------------------------------------------------
68
int fitness = (99 - changeDifference);
69     // Step 2: If the solution amount equals the target amount, then
70
// we add additional fitness points for solutions representing fewer
71
// total coins.
72
// -----------------------------------------------------------------
73
if (changeAmount == m_targetAmount) {
74       fitness += 100 - (10 * totalCoins);
75     }
76     // Make sure fitness value is always positive.
77
// -------------------------------------------
78
return Math.max(1, fitness);
79   }
80
81   /**
82    * Calculates the total amount of change (in cents) represented by
83    * the given potential solution and returns that amount.
84    * Here we use "fantasy" coins just to have more genes and bloat the time
85    * consumed for test performance test
86    *
87    * @param a_potentialSolution the pontential solution to evaluate
88    * @return the total amount of change (in cents) represented by the
89    * given solution
90    */

91   public static int amountOfChange(IChromosome a_potentialSolution) {
92     int numQuarters = getNumberOfCoinsAtGene(a_potentialSolution, 0);
93     int numDimes = getNumberOfCoinsAtGene(a_potentialSolution, 1);
94     int numNickels = getNumberOfCoinsAtGene(a_potentialSolution, 2);
95     int numPennies = getNumberOfCoinsAtGene(a_potentialSolution, 3);
96     int A = getNumberOfCoinsAtGene(a_potentialSolution, 4);
97     int B = getNumberOfCoinsAtGene(a_potentialSolution, 5);
98     int C = getNumberOfCoinsAtGene(a_potentialSolution, 6);
99     int D = getNumberOfCoinsAtGene(a_potentialSolution, 7);
100     int E = getNumberOfCoinsAtGene(a_potentialSolution, 8);
101     int F = getNumberOfCoinsAtGene(a_potentialSolution, 9);
102     return (numQuarters * 25) + (numDimes * 10) + (numNickels * 5)
103         + numPennies + (A * 29) + (B * 31) + (C * 37) + (D * 41) + (E * 43)
104         + (F * 47);
105   }
106
107   /**
108    * Retrieves the number of coins represented by the given potential
109    * solution at the given gene position.
110    *
111    * @param a_potentialSolution the potential solution to evaluate
112    * @param a_position the gene position to evaluate
113    * @return the number of coins represented by the potential solution
114    * at the given gene position
115    */

116   public static int getNumberOfCoinsAtGene(IChromosome a_potentialSolution,
117                                            int a_position) {
118     Integer JavaDoc numCoins =
119         (Integer JavaDoc) a_potentialSolution.getGene(a_position).getAllele();
120     return numCoins.intValue();
121   }
122
123   /**
124    * Returns the total number of coins represented by all of the genes in
125    * the given potential solution.
126    *
127    * @param a_potentialsolution the potential solution to evaluate
128    * @return the total number of coins represented by the given Chromosome
129    */

130   public static int getTotalNumberOfCoins(IChromosome a_potentialsolution) {
131     int totalCoins = 0;
132     int numberOfGenes = a_potentialsolution.size();
133     for (int i = 0; i < numberOfGenes; i++) {
134       totalCoins += getNumberOfCoinsAtGene(a_potentialsolution, i);
135     }
136     return totalCoins;
137   }
138 }
139
Popular Tags