1 10 package examples.knapsack; 11 12 import java.io.*; 13 import org.jgap.*; 14 import org.jgap.data.*; 15 import org.jgap.impl.*; 16 import org.jgap.xml.*; 17 import org.w3c.dom.*; 18 19 30 public class KnapsackMain { 31 32 private final static String CVS_REVISION = "$Revision: 1.9 $"; 33 34 37 private static final int MAX_ALLOWED_EVOLUTIONS = 140; 38 39 40 public final static double[] itemVolumes = { 41 50.2d, 14.8d, 27.5d, 6800.0d, 25.0d, 4.75d, 95.36d, 1500.7d, 18365.9d, 42 83571.1d}; 43 44 45 public final static String [] itemNames = { 46 "Torch", "Banana", "Miniradio", "TV", "Gameboy", "Small thingie", 47 "Medium thingie", "Big thingie", "Huge thingie", "Gigantic thingie"}; 48 49 62 public static void findItemsForVolume(double a_knapsackVolume) 63 throws Exception { 64 Configuration conf = new DefaultConfiguration(); 68 conf.setPreservFittestIndividual(true); 69 FitnessFunction myFunc = 73 new KnapsackFitnessFunction(a_knapsackVolume); 74 conf.setFitnessFunction(myFunc); 75 Gene[] sampleGenes = new Gene[itemVolumes.length]; 87 for (int i = 0; i < itemVolumes.length; i++) { 88 sampleGenes[i] = new IntegerGene(conf, 0, 89 (int) Math.ceil(a_knapsackVolume / 90 itemVolumes[i])); 91 } 92 IChromosome sampleChromosome = new Chromosome(conf, sampleGenes); 93 conf.setSampleChromosome(sampleChromosome); 94 conf.setPopulationSize(50); 101 Genotype population; 106 try { 107 Document doc = XMLManager.readFile(new File("knapsackJGAP.xml")); 108 population = XMLManager.getGenotypeFromDocument(conf, doc); 109 } 110 catch (FileNotFoundException fex) { 111 population = Genotype.randomInitialGenotype(conf); 112 } 113 population = Genotype.randomInitialGenotype(conf); 114 for (int i = 0; i < MAX_ALLOWED_EVOLUTIONS; i++) { 118 population.evolve(); 119 } 120 124 DataTreeBuilder builder = DataTreeBuilder.getInstance(); 127 IDataCreators doc2 = builder.representGenotypeAsDocument(population); 128 XMLDocumentBuilder docbuilder = new XMLDocumentBuilder(); 131 Document xmlDoc = (Document) docbuilder.buildDocument(doc2); 132 XMLManager.writeFile(xmlDoc, new File("knapsackJGAP.xml")); 133 IChromosome bestSolutionSoFar = population.getFittestChromosome(); 136 System.out.println("The best solution has a fitness value of " + 137 bestSolutionSoFar.getFitnessValue()); 138 System.out.println("It contained the following: "); 139 int count; 140 double totalVolume = 0.0d; 141 for (int i = 0; i < bestSolutionSoFar.size(); i++) { 142 count = ( (Integer ) bestSolutionSoFar.getGene(i).getAllele()).intValue(); 143 if (count > 0) { 144 System.out.println("\t " + count + " x " + itemNames[i]); 145 totalVolume += itemVolumes[i] * count; 146 } 147 } 148 System.out.println("\nFor a total volume of " + totalVolume + " ccm"); 149 System.out.println("Expected volume was " + a_knapsackVolume + " ccm"); 150 System.out.println("Volume difference is " + 151 Math.abs(totalVolume - a_knapsackVolume) + " ccm"); 152 } 153 154 164 public static void main(String [] args) { 165 if (args.length != 1) { 166 System.out.println("Syntax: " + KnapsackMain.class.getName() + 167 " <volume>"); 168 } 169 else { 170 try { 171 double volume = Double.parseDouble(args[0]); 172 if (volume < 1 || 173 volume >= KnapsackFitnessFunction.MAX_BOUND) { 174 System.out.println("The <volume> argument must be between 1 and " 175 + 176 (KnapsackFitnessFunction.MAX_BOUND - 1) 177 + " and can be a decimal."); 178 } 179 else { 180 try { 181 findItemsForVolume(volume); 182 } 183 catch (Exception e) { 184 e.printStackTrace(); 185 } 186 } 187 } 188 catch (NumberFormatException e) { 189 System.out.println( 190 "The <volume> argument must be a valid double value"); 191 } 192 } 193 } 194 } 195 | Popular Tags |