1 10 package examples.multidimension; 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 33 public class KnapsackMain { 34 35 private final static String CVS_REVISION = "$Revision: 1.1 $"; 36 37 40 private static final int MAX_ALLOWED_EVOLUTIONS = 170; 41 42 43 public final static double[] itemVolumes = { 44 50.2d, 14.8d, 27.5d, 6800.0d, 25.0d, 4.75d, 95.36d, 1500.7d, 18365.9d, 45 83571.1d}; 46 47 48 public final static String [] itemNames = { 49 "Torch", "Banana", "Miniradio", "TV", "Gameboy", "Small thingie", 50 "Medium thingie", "Big thingie", "Huge thingie", "Gigantic thingie"}; 51 52 public final static String [] COLORS = { 53 "red", "green", "blue", "yellow", "brown", "orange", 54 "mint", "purple", "black", "white"}; 55 56 57 71 public static void findItemsForVolume(double a_knapsackVolume, int a_numCols) 72 throws Exception { 73 Configuration conf = new DefaultConfiguration(); 77 conf.setPreservFittestIndividual(true); 78 FitnessFunction myFunc = 82 new KnapsackFitnessFunction(a_knapsackVolume); 83 conf.setFitnessFunction(myFunc); 84 Gene[] sampleGenes = new Gene[itemVolumes.length]; 96 for (int i = 0; i < itemVolumes.length; i++) { 97 CompositeGene compositeGene = new CompositeGene(conf); 98 IntegerGene color = new IntegerGene(conf, 0, a_numCols - 1); 99 IntegerGene item = new IntegerGene(conf, 0, 100 (int) Math.ceil(a_knapsackVolume / 101 itemVolumes[i])); 102 compositeGene.addGene(color); 103 compositeGene.addGene(item); 104 sampleGenes[i] = compositeGene; 105 } 106 IChromosome sampleChromosome = new Chromosome(conf, sampleGenes); 107 conf.setSampleChromosome(sampleChromosome); 108 conf.setPopulationSize(80); 115 Genotype population = Genotype.randomInitialGenotype(conf); 118 for (int i = 0; i < MAX_ALLOWED_EVOLUTIONS; i++) { 122 population.evolve(); 123 } 124 128 DataTreeBuilder builder = DataTreeBuilder.getInstance(); 131 IDataCreators doc2 = builder.representGenotypeAsDocument(population); 132 XMLDocumentBuilder docbuilder = new XMLDocumentBuilder(); 135 Document xmlDoc = (Document) docbuilder.buildDocument(doc2); 136 XMLManager.writeFile(xmlDoc, new File("knapsackJGAP.xml")); 137 IChromosome bestSolutionSoFar = population.getFittestChromosome(); 140 System.out.println("The best solution has a fitness value of " + 141 bestSolutionSoFar.getFitnessValue()); 142 System.out.println("It contained the following: "); 143 int count; 144 double totalVolume = 0.0d; 145 for (int i = 0; i < bestSolutionSoFar.size(); i++) { 146 CompositeGene comp = (CompositeGene)bestSolutionSoFar.getGene(i); 147 IntegerGene color = (IntegerGene)comp.geneAt(0); 148 IntegerGene item = (IntegerGene)comp.geneAt(1); 149 count = ( (Integer ) item.getAllele()).intValue(); 150 if (count > 0) { 151 String colorName = COLORS[color.intValue()]; 152 System.out.println("\t " + count + " x " + itemNames[i] + " color "+colorName); 153 totalVolume += itemVolumes[i] * count; 154 } 155 } 156 System.out.println("\nFor a total volume of " + totalVolume + " ccm"); 157 System.out.println("Expected volume was " + a_knapsackVolume + " ccm"); 158 System.out.println("Volume difference is " + 159 Math.abs(totalVolume - a_knapsackVolume) + " ccm"); 160 } 161 162 172 public static void main(String [] args) { 173 if (args.length != 2) { 174 System.out.println("Syntax: " + KnapsackMain.class.getName() + 175 " <volume> <number of colors>"); 176 } 177 else { 178 try { 179 double volume = Double.parseDouble(args[0]); 180 if (volume < 1 || 181 volume >= KnapsackFitnessFunction.MAX_BOUND) { 182 System.out.println("The <volume> argument must be between 1 and " 183 + 184 (KnapsackFitnessFunction.MAX_BOUND - 1) 185 + " and can be a decimal."); 186 } 187 else { 188 int colors = Integer.parseInt(args[1]); 189 if (colors < 1 || colors > KnapsackMain.COLORS.length) { 190 System.out.println("The <number of colors> argument must be between" 191 +" 1 and "+ 192 KnapsackMain.COLORS.length); 193 } 194 else { 195 try { 196 findItemsForVolume(volume, colors); 197 } 198 catch (Exception e) { 199 e.printStackTrace(); 200 } 201 } 202 } 203 } 204 catch (NumberFormatException e) { 205 System.out.println( 206 "The <volume> argument must be a valid double value," 207 +" <colors> must be a valid integer number."); 208 } 209 } 210 } 211 } 212 | Popular Tags |