1 10 package examples.audit; 11 12 16 import org.jgap.*; 20 import org.jgap.impl.*; 21 import org.jgap.audit.*; 22 23 30 public class CoinsExample { 31 32 private final static String CVS_REVISION = "$Revision: 1.22 $"; 33 34 37 private static final int MAX_ALLOWED_EVOLUTIONS = 80; 38 39 52 public static void makeChangeForAmount(int a_targetChangeAmount) 53 throws Exception { 54 Configuration conf = new DefaultConfiguration(); 58 conf.setPreservFittestIndividual(true); 59 FitnessFunction myFunc = 64 new CoinsExampleFitnessFunction(a_targetChangeAmount); 65 conf.setFitnessFunction(myFunc); 66 Gene[] sampleGenes = new Gene[4]; 78 sampleGenes[0] = new IntegerGene(conf, 0, 3 * 10); sampleGenes[1] = new IntegerGene(conf, 0, 2 * 10); sampleGenes[2] = new IntegerGene(conf, 0, 1 * 10); sampleGenes[3] = new IntegerGene(conf, 0, 4 * 10); Chromosome sampleChromosome = new Chromosome(conf, sampleGenes); 83 conf.setSampleChromosome(sampleChromosome); 84 conf.setPopulationSize(50); 91 PermutingConfiguration pconf = new PermutingConfiguration(conf); 96 pconf.addGeneticOperatorSlot(new CrossoverOperator(conf)); 97 pconf.addGeneticOperatorSlot(new MutationOperator(conf)); 98 pconf.addNaturalSelectorSlot(new BestChromosomesSelector(conf)); 99 pconf.addNaturalSelectorSlot(new WeightedRouletteSelector(conf)); 100 pconf.addRandomGeneratorSlot(new StockRandomGenerator()); 101 RandomGeneratorForTest rn = new RandomGeneratorForTest(); 102 rn.setNextDouble(0.7d); 103 rn.setNextInt(2); 104 pconf.addRandomGeneratorSlot(rn); 105 pconf.addRandomGeneratorSlot(new GaussianRandomGenerator()); 106 pconf.addFitnessFunctionSlot(new CoinsExampleFitnessFunction( 107 a_targetChangeAmount)); 108 Evaluator eval = new Evaluator(pconf); 109 121 int permutation = 0; 122 while (eval.hasNext()) { 123 Genotype population = Genotype.randomInitialGenotype(eval.next()); 126 for (int run = 0; run < 10; run++) { 127 for (int i = 0; i < MAX_ALLOWED_EVOLUTIONS; i++) { 131 population.evolve(); 132 double fitness = population.getFittestChromosome().getFitnessValue(); 134 if (i % 3 == 0) { 135 String s = String.valueOf(i); 136 eval.setValue(permutation, run, fitness, "" + permutation, s); 146 eval.storeGenotype(permutation, run, population); 147 } 149 } 150 } 151 IChromosome bestSolutionSoFar = population.getFittestChromosome(); 154 System.out.println("The best solution has a fitness value of " + 155 bestSolutionSoFar.getFitnessValue()); 156 System.out.println("It contained the following: "); 157 System.out.println("\t" + 158 CoinsExampleFitnessFunction. 159 getNumberOfCoinsAtGene( 160 bestSolutionSoFar, 0) + " quarters."); 161 System.out.println("\t" + 162 CoinsExampleFitnessFunction. 163 getNumberOfCoinsAtGene( 164 bestSolutionSoFar, 1) + " dimes."); 165 System.out.println("\t" + 166 CoinsExampleFitnessFunction. 167 getNumberOfCoinsAtGene( 168 bestSolutionSoFar, 2) + " nickels."); 169 System.out.println("\t" + 170 CoinsExampleFitnessFunction. 171 getNumberOfCoinsAtGene( 172 bestSolutionSoFar, 3) + " pennies."); 173 System.out.println("For a total of " + 174 CoinsExampleFitnessFunction.amountOfChange( 175 bestSolutionSoFar) + " cents in " + 176 CoinsExampleFitnessFunction. 177 getTotalNumberOfCoins( 178 bestSolutionSoFar) + " coins."); 179 permutation++; 180 } 181 184 196 207 int maxPerm = permutation - 1; 210 double avgBestFitness = 0.0d; 211 int avgBestGen = 0; 212 double avgAvgFitness = 0.0d; 213 double avgAvgDiv = 0.0d; 214 double avgAvgBestD = 0.0d; 215 for (int i = 0; i < maxPerm; i++) { 216 Evaluator.GenotypeDataAvg dataAvg = eval.calcPerformance(i); 218 System.err.println("-----------------------------"); 219 System.err.println("Perm " + i); 220 System.err.println("Best Fitness " + dataAvg.bestFitnessValue); 221 System.err.println(" Generation " + dataAvg.bestFitnessValueGeneration); 222 System.err.println(" BestFit/Gen " + 223 dataAvg.bestFitnessValue / dataAvg.bestFitnessValueGeneration); 224 System.err.println("Avg. Fitness " + dataAvg.avgFitnessValue); 225 System.err.println("Avg. Div. " + dataAvg.avgDiversityFitnessValue); 226 System.err.println("Avg. BestD " + dataAvg.avgBestDeltaFitnessValue); 227 avgBestFitness += dataAvg.bestFitnessValue; 228 avgBestGen += dataAvg.bestFitnessValueGeneration; 229 avgAvgFitness += dataAvg.avgFitnessValue; 230 avgAvgDiv += dataAvg.avgDiversityFitnessValue; 231 avgAvgBestD += dataAvg.avgBestDeltaFitnessValue; 232 } 233 System.err.println("\nOverall Statistics for all permutations"); 236 System.err.println("----------------------------------------"); 237 System.err.println("Avg. Best Fitness " + avgBestFitness / maxPerm); 238 System.err.println("Avg. Best Generation " + avgBestGen / maxPerm); 239 System.err.println("Avg. Avg. Fitness " + avgAvgFitness / maxPerm); 240 System.err.println("Avg. Avg. Diversity " + avgAvgDiv / maxPerm); 241 System.err.println("Avg. Avg. BestD " + avgAvgBestD / maxPerm); 242 245 } 264 265 public static void main(String [] args) { 266 if (args.length != 1) { 267 System.out.println("Syntax: CoinsExample <amount>"); 268 } 269 else { 270 try { 271 int amount = Integer.parseInt(args[0]); 272 if (amount < 1 || 273 amount >= CoinsExampleFitnessFunction.MAX_BOUND) { 274 System.out.println("The <amount> argument must be between 1 and " 275 + 276 (CoinsExampleFitnessFunction.MAX_BOUND - 1) 277 + "."); 278 } 279 else { 280 try { 281 makeChangeForAmount(amount); 282 } 283 catch (Exception e) { 284 e.printStackTrace(); 285 } 286 } 287 } 288 catch (NumberFormatException e) { 289 System.out.println( 290 "The <amount> argument must be a valid integer value"); 291 } 292 } 293 } 294 } 295 | Popular Tags |