1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 15 28 public class TwoWayMutationOperator 29 extends BaseGeneticOperator { 30 31 private final static String CVS_REVISION = "$Revision: 1.6 $"; 32 33 39 private int m_mutationRate; 40 41 46 private IUniversalRateCalculator m_mutationRateCalc; 47 48 60 public TwoWayMutationOperator() 61 throws InvalidConfigurationException { 62 this(Genotype.getStaticConfiguration(), 63 new DefaultMutationRateCalculator(Genotype.getStaticConfiguration())); 64 } 65 66 77 public TwoWayMutationOperator(final Configuration a_config) 78 throws InvalidConfigurationException { 79 this(a_config, new DefaultMutationRateCalculator(a_config)); 80 } 81 82 94 public TwoWayMutationOperator(final Configuration a_config, 95 final IUniversalRateCalculator 96 a_mutationRateCalculator) 97 throws InvalidConfigurationException { 98 super(a_config); 99 setMutationRateCalc(a_mutationRateCalculator); 100 } 101 102 116 public TwoWayMutationOperator(final Configuration a_config, 117 final int a_desiredMutationRate) 118 throws InvalidConfigurationException { 119 super(a_config); 120 m_mutationRate = a_desiredMutationRate; 121 setMutationRateCalc(null); 122 } 123 124 128 public void operate(final Population a_population, 129 final List a_candidateChromosomes) { 130 if (a_population == null || a_candidateChromosomes == null) { 131 return; 135 } 136 if (m_mutationRate == 0 && m_mutationRateCalc == null) { 137 return; 141 } 142 boolean mutate = false; 147 RandomGenerator generator = getConfiguration().getRandomGenerator(); 148 int size = Math.min(getConfiguration().getPopulationSize(), 153 a_population.size()); 154 IGeneticOperatorConstraint constraint = getConfiguration(). 155 getJGAPFactory().getGeneticOperatorConstraint(); 156 for (int i = 0; i < size; i++) { 157 IChromosome chrom = a_population.getChromosome(i); 158 Gene[] genes = chrom.getGenes(); 159 IChromosome copyOfChromosome = null; 160 double d = generator.nextDouble(); 163 int geneIndex; 164 166 if (d >= (1 - 0.7462d)) { 167 geneIndex = 3; 168 } 169 else if (d >= (1 - 0.7462d - 0.1492537d)) { 170 geneIndex = 2; 171 } 172 else if (d >= (1 - 0.7462d - 0.1492537d - 0.07462686d)) { 173 geneIndex = 1; 174 } 175 else { 176 geneIndex = 0; 177 } 178 if (geneIndex >= genes.length) { 179 geneIndex = genes.length - 1; 180 } 181 182 if (m_mutationRateCalc != null) { 183 mutate = m_mutationRateCalc.toBePermutated(chrom, geneIndex); 187 } 188 else { 189 mutate = (generator.nextInt(m_mutationRate) == 0); 193 } 194 if (mutate) { 195 203 if (constraint != null) { 204 List v = new Vector(); 205 v.add(chrom); 206 if (!constraint.isValid(a_population, v, this)) { 207 continue; 208 } 209 } 210 if (copyOfChromosome == null) { 219 copyOfChromosome = (IChromosome) chrom.clone(); 222 a_candidateChromosomes.add(copyOfChromosome); 225 genes = copyOfChromosome.getGenes(); 228 } 229 if (genes[geneIndex] instanceof ICompositeGene) { 234 ICompositeGene compositeGene = (ICompositeGene) genes[geneIndex]; 235 for (int k = 0; k < compositeGene.size(); k++) { 236 mutateGene(compositeGene.geneAt(k), generator); 237 } 238 } 239 else { 240 mutateGene(genes[geneIndex], generator); 241 } 242 } 243 } 244 } 245 246 254 private void mutateGene(final Gene a_gene, final RandomGenerator a_generator) { 255 for (int k = 0; k < a_gene.size(); k++) { 256 260 double percentage = -1 + a_generator.nextDouble() * 2; 264 a_gene.applyMutation(k, percentage); 267 } 268 } 269 270 276 public IUniversalRateCalculator getMutationRateCalc() { 277 return m_mutationRateCalc; 278 } 279 280 288 public void setMutationRateCalc(final IUniversalRateCalculator 289 a_mutationRateCalc) { 290 m_mutationRateCalc = a_mutationRateCalc; 291 if (m_mutationRateCalc != null) { 292 m_mutationRate = 0; 293 } 294 } 295 296 307 public boolean equals(final Object a_other) { 308 try { 309 return compareTo(a_other) == 0; 310 } 311 catch (ClassCastException cex) { 312 return false; 313 } 314 } 315 316 327 public int compareTo(Object a_other) { 328 if (a_other == null) { 329 return 1; 330 } 331 TwoWayMutationOperator op = (TwoWayMutationOperator) a_other; 332 if (m_mutationRateCalc == null) { 333 if (op.m_mutationRateCalc != null) { 334 return -1; 335 } 336 } 337 else { 338 if (op.m_mutationRateCalc == null) { 339 return 1; 340 } 341 else { 342 } 343 } 344 if (m_mutationRate != op.m_mutationRate) { 345 if (m_mutationRate > op.m_mutationRate) { 346 return 1; 347 } 348 else { 349 return -1; 350 } 351 } 352 return 0; 355 } 356 357 public int getMutationRate() { 358 return m_mutationRate; 359 } 360 } 361 | Popular Tags |