1 10 package examples.dynamicMutation; 11 12 import org.jgap.*; 13 import org.jgap.impl.*; 14 15 24 public class DynamicMutationExample { 25 26 private final static String CVS_REVISION = "$Revision: 1.4 $"; 27 28 31 private static final int MAX_ALLOWED_EVOLUTIONS = 200; 32 33 46 public static void makeChangeForAmount(int a_targetChangeAmount) 47 throws Exception { 48 Configuration conf = new DefaultConfiguration(); 52 conf.getGeneticOperators().clear(); 54 TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, 7); 56 conf.addGeneticOperator(mutOp); 57 conf.addGeneticOperator(new CrossoverOperator()); 58 conf.setPreservFittestIndividual(!true); 59 conf.setKeepPopulationSizeConstant(false); 60 FitnessFunction myFunc = 65 new DynamicMutationFitnessFunction(a_targetChangeAmount); 66 conf.setFitnessFunction(myFunc); 67 conf.setFitnessEvaluator(new DeltaFitnessEvaluator()); 69 Gene[] sampleGenes = new Gene[4]; 81 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(null, sampleGenes); 86 conf.setSampleChromosome(sampleChromosome); 87 conf.setPopulationSize(80); 94 Genotype population; 99 population = Genotype.randomInitialGenotype(conf); 101 for (int i = 0; i < MAX_ALLOWED_EVOLUTIONS; i++) { 105 population.evolve(); 106 } 107 IChromosome bestSolutionSoFar = population.getFittestChromosome(); 110 System.out.println("The best solution has a fitness value of " + 111 bestSolutionSoFar.getFitnessValue()); 112 System.out.println("It contained the following: "); 113 System.out.println("\t" + 114 DynamicMutationFitnessFunction. 115 getNumberOfCoinsAtGene( 116 bestSolutionSoFar, 0) + " quarters."); 117 System.out.println("\t" + 118 DynamicMutationFitnessFunction. 119 getNumberOfCoinsAtGene( 120 bestSolutionSoFar, 1) + " dimes."); 121 System.out.println("\t" + 122 DynamicMutationFitnessFunction. 123 getNumberOfCoinsAtGene( 124 bestSolutionSoFar, 2) + " nickels."); 125 System.out.println("\t" + 126 DynamicMutationFitnessFunction. 127 getNumberOfCoinsAtGene( 128 bestSolutionSoFar, 3) + " pennies."); 129 System.out.println("For a total of " + 130 DynamicMutationFitnessFunction.amountOfChange( 131 bestSolutionSoFar) + " cents in " + 132 DynamicMutationFitnessFunction. 133 getTotalNumberOfCoins( 134 bestSolutionSoFar) + " coins."); 135 } 136 137 149 public static void main(String [] args) 150 throws Exception { 151 if (args.length != 1) { 152 System.out.println("Syntax: DynamicMutationExample <amount>"); 153 } 154 else { 155 int amount = 0; 156 try { 157 amount = Integer.parseInt(args[0]); 158 } 159 catch (NumberFormatException e) { 160 System.out.println( 161 "The <amount> argument must be a valid integer value"); 162 System.exit(1); 163 } 164 if (amount < 1 || 165 amount >= DynamicMutationFitnessFunction.MAX_BOUND) { 166 System.out.println("The <amount> argument must be between 1 and " 167 + 168 (DynamicMutationFitnessFunction.MAX_BOUND - 1) 169 + "."); 170 } 171 else { 172 makeChangeForAmount(amount); 173 } 174 } 175 } 176 177 183 public static class CoinsMutationRateCalc 184 implements IUniversalRateCalculator { 185 private int m_evolution; 186 187 private double m_rate0 = 0.2d; 188 189 private double m_rate1 = 0.6d; 190 191 private double m_rate2 = 0.7d; 192 193 private double m_rate3 = 1.0d; 194 195 public int calculateCurrentRate() { 196 int size; 197 size = 15; 198 if (size < 1) { 199 size = 1; 200 } 201 return size; 202 } 203 204 public boolean toBePermutated(IChromosome a_chrom, int a_geneIndex) { 205 RandomGenerator generator 206 = a_chrom.getConfiguration().getRandomGenerator(); 207 double mult = 0.0d; 208 switch (a_geneIndex) { 209 case 0: 210 mult = get(0); 211 break; 212 case 1: 213 mult = m_rate1; 214 break; 215 case 2: 216 mult = m_rate2; 217 break; 218 case 3: 219 mult = m_rate3; 220 m_evolution++; 221 break; 222 } 223 return (generator.nextDouble() < (1 / calculateCurrentRate()) * mult); 224 } 225 226 private double get(int a_index) { 227 if (m_evolution > 90) { 228 m_rate0 = 1.0d; 229 } 230 else if (m_evolution > 60) { 231 m_rate0 = 0.75d; 232 } 233 else if (m_evolution > 30) { 234 m_rate0 = 0.5d; 235 } 236 else if (m_evolution > 15) { 237 m_rate0 = 0.4d; 238 } 239 return m_rate0; 240 } 241 } 242 } 243 | Popular Tags |