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