1 10 package examples; 11 12 import org.jgap.*; 13 14 21 public class MinimizingMakeChangeFitnessFunction 22 extends FitnessFunction { 23 24 private final static String CVS_REVISION = "$Revision: 1.17 $"; 25 26 private final int m_targetAmount; 27 28 public static final int MAX_BOUND = 4000; 29 30 public MinimizingMakeChangeFitnessFunction(int a_targetAmount) { 31 if (a_targetAmount < 1 || a_targetAmount >= MAX_BOUND) { 32 throw new IllegalArgumentException ( 33 "Change amount must be between 1 and " + MAX_BOUND + " cents."); 34 } 35 m_targetAmount = a_targetAmount; 36 } 37 38 50 public double evaluate(IChromosome a_subject) { 51 boolean defaultComparation = a_subject.getConfiguration(). 56 getFitnessEvaluator().isFitter(2, 1); 57 58 int changeAmount = amountOfChange(a_subject); 69 int totalCoins = getTotalNumberOfCoins(a_subject); 70 int changeDifference = Math.abs(m_targetAmount - changeAmount); 71 double fitness; 72 if (defaultComparation) { 73 fitness = 0.0d; 74 } 75 else { 76 fitness = MAX_BOUND/2; 77 } 78 if (defaultComparation) { 89 fitness += changeDifferenceBonus(MAX_BOUND/2, changeDifference); 90 } 91 else { 92 fitness -= changeDifferenceBonus(MAX_BOUND/2, changeDifference); 93 } 94 if (defaultComparation) { 101 fitness -= computeCoinNumberPenalty(MAX_BOUND/2, totalCoins); 102 } 103 else { 104 fitness += computeCoinNumberPenalty(MAX_BOUND/2, totalCoins); 105 } 106 return Math.max(1.0d, fitness); 109 } 110 111 120 protected double changeDifferenceBonus(double a_maxFitness, 121 int a_changeDifference) { 122 if (a_changeDifference == 0) { 123 return a_maxFitness; 124 } 125 else { 126 if (a_changeDifference * a_changeDifference >= a_maxFitness / 2) { 129 return 0.0d; 130 } 131 else { 132 return a_maxFitness / 2 - a_changeDifference * a_changeDifference; 133 } 134 } 135 } 136 137 148 protected double computeCoinNumberPenalty(double a_maxFitness, int a_coins) { 149 if (a_coins == 1) { 150 return 0; 152 } 153 else { 154 return (Math.min(a_maxFitness, a_coins * a_coins)); 158 } 159 } 160 161 172 public static int amountOfChange(IChromosome a_potentialSolution) { 173 int numQuarters = getNumberOfCoinsAtGene(a_potentialSolution, 0); 174 int numDimes = getNumberOfCoinsAtGene(a_potentialSolution, 1); 175 int numNickels = getNumberOfCoinsAtGene(a_potentialSolution, 2); 176 int numPennies = getNumberOfCoinsAtGene(a_potentialSolution, 3); 177 return (numQuarters * 25) + (numDimes * 10) + (numNickels * 5) + 178 numPennies; 179 } 180 181 193 public static int getNumberOfCoinsAtGene(IChromosome a_potentialSolution, 194 int a_position) { 195 Integer numCoins = 196 (Integer ) a_potentialSolution.getGene(a_position).getAllele(); 197 return numCoins.intValue(); 198 } 199 200 210 public static int getTotalNumberOfCoins(IChromosome a_potentialsolution) { 211 int totalCoins = 0; 212 int numberOfGenes = a_potentialsolution.size(); 213 for (int i = 0; i < numberOfGenes; i++) { 214 totalCoins += getNumberOfCoinsAtGene(a_potentialsolution, i); 215 } 216 return totalCoins; 217 } 218 } 219
| Popular Tags
|