1 10 package examples.energy; 11 12 import org.jgap.*; 13 import org.jgap.impl.*; 14 15 26 public class CoinsEnergy { 27 28 private final static String CVS_REVISION = "$Revision: 1.8 $"; 29 30 33 private static final int MAX_ALLOWED_EVOLUTIONS = 200; 34 35 49 public static void makeChangeForAmount(int a_targetChangeAmount, 50 double a_maxWeight) 51 throws Exception { 52 Configuration conf = new DefaultConfiguration(); 56 conf.setPreservFittestIndividual(true); 57 conf.setKeepPopulationSizeConstant(false); 58 FitnessFunction myFunc = 63 new CoinsEnergyFitnessFunction(a_targetChangeAmount, a_maxWeight); 64 conf.setBulkFitnessFunction(new BulkFitnessOffsetRemover(myFunc)); 66 Gene[] sampleGenes = new Gene[4]; 78 IntegerGene gene = new IntegerGene(conf, 0, 3 * 10); 79 gene.setConstraintChecker(new EnergyGeneConstraintChecker()); 80 sampleGenes[0] = gene; sampleGenes[0].setEnergy(20.0d); 86 sampleGenes[1] = new IntegerGene(conf, 0, 2 * 10); sampleGenes[1].setEnergy(10.0d); 88 sampleGenes[2] = new IntegerGene(conf, 0, 1 * 10); sampleGenes[2].setEnergy(11.0d); 90 sampleGenes[3] = new IntegerGene(conf, 0, 4 * 10); sampleGenes[3].setEnergy(7.0d); 92 IChromosome sampleChromosome = new Chromosome(conf, sampleGenes); 93 conf.setSampleChromosome(sampleChromosome); 94 conf.setPopulationSize(80); 101 Genotype population = Genotype.randomInitialGenotype(conf); 104 105 for (int i = 0; i < MAX_ALLOWED_EVOLUTIONS; i++) { 109 population.evolve(); 110 } 111 IChromosome bestSolutionSoFar = population.getFittestChromosome(); 114 System.out.println("The best solution has a fitness value of " 115 + bestSolutionSoFar.getFitnessValue()); 116 System.out.println("It contains the following: "); 117 System.out.println("\t" + CoinsEnergyFitnessFunction.getNumberOfCoinsAtGene( 118 bestSolutionSoFar, 0) + " quarters."); 119 System.out.println("\t" + CoinsEnergyFitnessFunction.getNumberOfCoinsAtGene( 120 bestSolutionSoFar, 1) + " dimes."); 121 System.out.println("\t" + CoinsEnergyFitnessFunction.getNumberOfCoinsAtGene( 122 bestSolutionSoFar, 2) + " nickels."); 123 System.out.println("\t" + CoinsEnergyFitnessFunction.getNumberOfCoinsAtGene( 124 bestSolutionSoFar, 3) + " pennies."); 125 System.out.println("For a total of " 126 + CoinsEnergyFitnessFunction.amountOfChange( 127 bestSolutionSoFar) 128 + " cents in " 129 + CoinsEnergyFitnessFunction.getTotalNumberOfCoins( 130 bestSolutionSoFar) 131 + " coins with a total weight of " 132 + CoinsEnergyFitnessFunction.getTotalWeight( 133 bestSolutionSoFar) 134 + ")"); 135 } 136 137 149 public static void main(String [] args) 150 throws Exception { 151 if (args.length != 2) { 152 System.out.println("Syntax: CoinsEnergy <amount> <max weight>"); 153 } 154 else { 155 int amount = getValue(args, 0); 156 int weight = getValue(args, 1); 157 makeChangeForAmount(amount, weight); 158 } 159 } 160 161 protected static int getValue(String [] args, int index) { 162 int value; 163 try { 164 value = Integer.parseInt(args[index]); 165 return value; 166 } 167 catch (NumberFormatException e) { 168 System.out.println( 169 "The " + (index + 1) + ". argument must be a valid integer value"); 170 System.exit(1); 171 return -1; } 173 } 174 175 180 public static class EnergyGeneConstraintChecker 181 implements IGeneConstraintChecker { 182 public final static double[] coinWeights = { 183 1.0d, 2.0d, 8.0d, 3.0d}; 184 185 198 public boolean verify(Gene a_gene, final Object a_alleleValue, 199 final IChromosome a_chrom, final int a_geneIndex) 200 throws RuntimeException { 201 double computedWeight = 0.0d; 202 208 return true; 212 } 213 } 214 } 215 | Popular Tags |