1 10 package examples.supergene; 11 12 import org.jgap.*; 13 import org.jgap.impl.*; 14 15 23 public abstract class AbstractSupergeneTest { 24 25 private static final String CVS_REVISION = "$Revision: 1.3 $"; 26 27 private transient Configuration m_conf; 28 29 32 public static final int DIMES = 0; 33 34 37 public static final int QUARTERS = 1; 38 39 42 public static final int NICKELS = 2; 43 44 47 public static final int PENNIES = 3; 48 49 52 public static int MAX_ALLOWED_EVOLUTIONS = 200; 53 54 57 public static int POPULATION_SIZE = 2000; 58 59 public static boolean REPORT_ENABLED = true; 60 61 64 protected Gene getDimesGene(Configuration a_conf) { 65 try { 66 return new IntegerGene(a_conf, 0, 2); } 68 catch (InvalidConfigurationException iex) { 69 throw new IllegalStateException (iex.getMessage()); 70 } 71 }; 72 73 76 protected Gene getNickelsGene(Configuration a_conf) { 77 try { 78 return new IntegerGene(a_conf, 0, 5); 79 } 80 catch (InvalidConfigurationException iex) { 81 throw new IllegalStateException (iex.getMessage()); 82 } 83 } 84 85 88 protected Gene getPenniesGene(Configuration a_conf) { 89 try { 90 return new IntegerGene(a_conf, 0, 7); 91 } 92 catch (InvalidConfigurationException iex) { 93 throw new IllegalStateException (iex.getMessage()); 94 } 95 } 96 97 100 protected Gene getQuartersGene(Configuration a_conf) { 101 try { 102 return new IntegerGene(a_conf, 0, 3); 103 } 104 catch (InvalidConfigurationException iex) { 105 throw new IllegalStateException (iex.getMessage()); 106 } 107 } 108 109 110 public static int amountOfChange(int a_numQuarters, int a_numDimes, 111 int a_numNickels, int a_numPennies) { 112 return (a_numQuarters * 25) + (a_numDimes * 10) + (a_numNickels * 5) 113 + a_numPennies; 114 }; 115 116 128 public abstract int makeChangeForAmount(int a_targetChangeAmount) 129 throws Exception ; 130 131 137 public IChromosome report(SupergeneChangeFitnessFunction a_fitnessFunction, 138 Genotype a_population) { 139 IChromosome bestSolutionSoFar = a_population.getFittestChromosome(); 140 if (!REPORT_ENABLED) { 141 return bestSolutionSoFar; 142 } 143 System.out.println("\nThe best solution has a fitness value of " 144 + bestSolutionSoFar.getFitnessValue()); 145 System.out.println("It contained the following: "); 146 System.out.println("\t" + a_fitnessFunction.getNumberOfCoinsAtGene( 147 bestSolutionSoFar, QUARTERS) + " quarters."); 148 System.out.println("\t" + a_fitnessFunction.getNumberOfCoinsAtGene( 149 bestSolutionSoFar, DIMES) + " dimes."); 150 System.out.println("\t" + a_fitnessFunction.getNumberOfCoinsAtGene( 151 bestSolutionSoFar, NICKELS) + " nickels."); 152 System.out.println("\t" + a_fitnessFunction.getNumberOfCoinsAtGene( 153 bestSolutionSoFar, PENNIES) + " pennies."); 154 System.out.println("For a total of " + a_fitnessFunction.amountOfChange( 155 bestSolutionSoFar) + " cents in " 156 + a_fitnessFunction.getTotalNumberOfCoins( 157 bestSolutionSoFar) + " coins."); 158 return bestSolutionSoFar; 159 } 160 161 165 public static boolean EXISTING_SOLUTIONS_ONLY = false; 166 167 172 public int test() { 173 int s = 0; 174 int e; 175 Test: 176 for (int amount = 20; amount < 100; amount++) { 177 try { 178 if (REPORT_ENABLED) { 179 System.out.println("EXCANGING " + amount + " "); 180 } 181 if (EXISTING_SOLUTIONS_ONLY) { 183 if (!Force.solve(amount)) { 184 continue Test; 185 } 186 } 187 DefaultConfiguration.reset(); 191 e = makeChangeForAmount(amount); 192 if (REPORT_ENABLED) { 193 System.out.println(" err " + e); 194 System.out.println("---------------"); 195 } 196 s = s + e; 197 } 198 catch (Exception ex) { 199 ex.printStackTrace(); 200 s += 1000; 201 } 202 } 203 if (REPORT_ENABLED) { 204 System.out.println("Sum of errors " + s); 205 } 206 return s; 207 } 208 209 213 protected int solve(Configuration a_conf, int a_targetChangeAmount, 214 SupergeneChangeFitnessFunction a_fitnessFunction, 215 Gene[] a_sampleGenes) 216 throws InvalidConfigurationException { 217 IChromosome sampleChromosome = new Chromosome(a_conf, a_sampleGenes); 218 a_conf.setSampleChromosome(sampleChromosome); 219 a_conf.setPopulationSize(POPULATION_SIZE); 227 Genotype population = Genotype.randomInitialGenotype(a_conf); 230 int s; 231 Evolution: 232 for (int i = 0; i < MAX_ALLOWED_EVOLUTIONS; i++) { 235 population.evolve(); 236 s = Math.abs(a_fitnessFunction.amountOfChange(population. 237 getFittestChromosome()) 238 - a_targetChangeAmount); 239 if (s == 0) { 240 break Evolution; 241 } 242 } 243 IChromosome bestSolutionSoFar = report(a_fitnessFunction, population); 246 return Math.abs(a_fitnessFunction.amountOfChange(bestSolutionSoFar) 247 - a_targetChangeAmount); 248 } 249 250 } 251 | Popular Tags |