1 10 package examples; 11 12 import java.io.*; 13 14 import org.jgap.*; 15 import org.jgap.data.*; 16 import org.jgap.impl.*; 17 import org.jgap.xml.*; 18 import org.w3c.dom.*; 19 20 39 public class MinimizingMakeChange { 40 41 private final static String CVS_REVISION = "$Revision: 1.19 $"; 42 43 46 private static final int MAX_ALLOWED_EVOLUTIONS = 200; 47 48 61 public static void makeChangeForAmount(int a_targetChangeAmount) 62 throws Exception { 63 Configuration conf = new DefaultConfiguration(); 67 conf.setPreservFittestIndividual(true); 68 conf.setKeepPopulationSizeConstant(false); 69 FitnessFunction myFunc = 74 new MinimizingMakeChangeFitnessFunction(a_targetChangeAmount); 75 conf.setBulkFitnessFunction(new BulkFitnessOffsetRemover(myFunc)); 77 82 Gene[] sampleGenes = new Gene[4]; 94 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); IChromosome sampleChromosome = new Chromosome(conf, sampleGenes); 99 conf.setSampleChromosome(sampleChromosome); 100 conf.setPopulationSize(80); 107 108 Genotype population; 113 try { 114 Document doc = XMLManager.readFile(new File("JGAPExample26.xml")); 115 population = XMLManager.getGenotypeFromDocument(conf, doc); 116 } 117 catch (UnsupportedRepresentationException uex) { 118 population = Genotype.randomInitialGenotype(conf); 120 } 121 catch (FileNotFoundException fex) { 122 population = Genotype.randomInitialGenotype(conf); 123 } 124 population = Genotype.randomInitialGenotype(conf); 127 for (int i = 0; i < MAX_ALLOWED_EVOLUTIONS; i++) { 131 population.evolve(); 132 } 133 137 DataTreeBuilder builder = DataTreeBuilder.getInstance(); 139 IDataCreators doc2 = builder.representGenotypeAsDocument(population); 140 XMLDocumentBuilder docbuilder = new XMLDocumentBuilder(); 142 Document xmlDoc = (Document) docbuilder.buildDocument(doc2); 143 XMLManager.writeFile(xmlDoc, new File("JGAPExample26.xml")); 144 IChromosome bestSolutionSoFar = population.getFittestChromosome(); 147 System.out.println("The best solution has a fitness value of " + 148 bestSolutionSoFar.getFitnessValue()); 149 System.out.println("It contained the following: "); 150 System.out.println("\t" + 151 MinimizingMakeChangeFitnessFunction. 152 getNumberOfCoinsAtGene( 153 bestSolutionSoFar, 0) + " quarters."); 154 System.out.println("\t" + 155 MinimizingMakeChangeFitnessFunction. 156 getNumberOfCoinsAtGene( 157 bestSolutionSoFar, 1) + " dimes."); 158 System.out.println("\t" + 159 MinimizingMakeChangeFitnessFunction. 160 getNumberOfCoinsAtGene( 161 bestSolutionSoFar, 2) + " nickels."); 162 System.out.println("\t" + 163 MinimizingMakeChangeFitnessFunction. 164 getNumberOfCoinsAtGene( 165 bestSolutionSoFar, 3) + " pennies."); 166 System.out.println("For a total of " + 167 MinimizingMakeChangeFitnessFunction.amountOfChange( 168 bestSolutionSoFar) + " cents in " + 169 MinimizingMakeChangeFitnessFunction. 170 getTotalNumberOfCoins( 171 bestSolutionSoFar) + " coins."); 172 } 173 174 186 public static void main(String [] args) 187 throws Exception { 188 if (args.length != 1) { 189 System.out.println("Syntax: MinimizingMakeChange <amount>"); 190 } 191 else { 192 int amount = 0; 193 try { 194 amount = Integer.parseInt(args[0]); 195 } 196 catch (NumberFormatException e) { 197 System.out.println( 198 "The <amount> argument must be a valid integer value"); 199 System.exit(1); 200 } 201 if (amount < 1 || 202 amount >= MinimizingMakeChangeFitnessFunction.MAX_BOUND) { 203 System.out.println("The <amount> argument must be between 1 and " 204 + 205 (MinimizingMakeChangeFitnessFunction.MAX_BOUND - 1) 206 + "."); 207 } 208 else { 209 makeChangeForAmount(amount); 210 } 211 } 212 } 213 214 } 215 | Popular Tags |