1 10 package examples.equalDistribution; 11 12 import java.util.*; 13 import org.jgap.*; 14 import org.jgap.impl.*; 15 16 28 public class MainClass { 29 30 private final static String CVS_REVISION = "$Revision: 1.1 $"; 31 32 35 private Vent[] m_vents; 36 37 private int m_numEvolutions; 38 39 47 public MainClass() 48 throws Exception { 49 makeVents(); 50 Genotype genotype = configureJGAP(); 51 doEvolution(genotype); 52 } 53 54 62 protected Genotype configureJGAP() 63 throws Exception { 64 m_numEvolutions = 50; 65 Configuration gaConf = new DefaultConfiguration(); 66 gaConf.resetProperty(Configuration.PROPERTY_FITEVAL_INST); 67 gaConf.setFitnessEvaluator(new DeltaFitnessEvaluator()); 68 gaConf.getGeneticOperators().clear(); 71 SwappingMutationOperator swapper = new SwappingMutationOperator(gaConf); 72 gaConf.addGeneticOperator(swapper); 73 gaConf.setPreservFittestIndividual(true); 76 gaConf.setKeepPopulationSizeConstant(false); 77 gaConf.setPopulationSize(50); 80 int chromeSize = m_vents.length; 81 Genotype genotype = null; 82 try { 83 IChromosome sampleChromosome = new Chromosome(gaConf, 87 new IntegerGene(gaConf), chromeSize); 88 gaConf.setSampleChromosome(sampleChromosome); 89 gaConf.setFitnessFunction(new SampleFitnessFunction(m_vents)); 92 genotype = Genotype.randomInitialGenotype(gaConf); 94 List chromosomes = genotype.getPopulation().getChromosomes(); 100 for (int i = 0; i < chromosomes.size(); i++) { 101 IChromosome chrom = (IChromosome) chromosomes.get(i); 102 for (int j = 0; j < chrom.size(); j++) { 103 Gene gene = (Gene) chrom.getGene(j); 104 gene.setAllele(new Integer (j)); 105 } 106 } 107 } catch (InvalidConfigurationException e) { 108 e.printStackTrace(); 109 System.exit( -2); 110 } 111 return genotype; 112 } 113 114 120 public void doEvolution(Genotype genotype) { 121 int progress = 0; 122 int percentEvolution = m_numEvolutions / 100; 123 for (int i = 0; i < m_numEvolutions; i++) { 124 genotype.evolve(); 125 if (percentEvolution > 0 && i % percentEvolution == 0) { 128 progress++; 129 IChromosome fittest = genotype.getFittestChromosome(); 130 double fitness = fittest.getFitnessValue(); 131 System.out.println("Currently best solution has fitness " + 132 fitness); 133 printSolution(fittest); 134 } 135 } 136 IChromosome fittest = genotype.getFittestChromosome(); 139 System.out.println("Best solution has fitness " + 140 fittest.getFitnessValue()); 141 printSolution(fittest); 142 } 143 144 150 public void printSolution(IChromosome a_solution) { 151 double groupWeights = 0.0d; 152 for (int i = 0; i < 8; i++) { 153 System.out.println("\nGroup " + i); 154 System.out.println("-------"); 155 double groupWeight = 0.0d; 156 for (int j = 0; j < 8; j++) { 157 IntegerGene ventIndex = (IntegerGene) a_solution.getGene( (i * 8 + j)); 158 Vent vent = (Vent) m_vents[ventIndex.intValue()]; 159 double weight = vent.getWeight(); 160 groupWeight += weight; 161 System.out.println(" Vent at index " 162 + ventIndex.intValue() 163 + " with weight " 164 + weight); 165 } 166 groupWeights += groupWeight; 167 System.out.println(" --> Group weight: " + groupWeight); 168 } 169 System.out.println("\n Average group weight: " + groupWeights / 8); 170 } 171 172 178 public void makeVents() { 179 m_vents = new Vent[64]; 180 for (int i = 0; i < m_vents.length; i++) { 181 double weight = 290 + Math.random() * 20; 183 Vent vent = new Vent(weight); 184 m_vents[i] = vent; 185 } 186 } 187 188 195 public static void main(String [] args) { 196 try { 197 new MainClass(); 198 } catch (Throwable t) { 199 t.printStackTrace(); 200 System.exit(1); 201 } 202 } 203 } 204 | Popular Tags |