1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import org.jgap.data.config.*; 15 16 32 public class MutationOperator 33 extends BaseGeneticOperator implements Configurable { 34 35 private final static String CVS_REVISION = "$Revision: 1.42 $"; 36 37 42 private IUniversalRateCalculator m_mutationRateCalc; 43 44 private MutationOperatorConfigurable m_config = new 45 MutationOperatorConfigurable(); 46 47 60 public MutationOperator() 61 throws InvalidConfigurationException { 62 this(Genotype.getStaticConfiguration()); 63 } 64 65 72 public MutationOperator(final Configuration a_conf) 73 throws InvalidConfigurationException { 74 super(a_conf); 75 setMutationRateCalc(new DefaultMutationRateCalculator(a_conf)); 76 } 77 78 90 public MutationOperator(final Configuration a_config, 91 final IUniversalRateCalculator 92 a_mutationRateCalculator) 93 throws InvalidConfigurationException { 94 super(a_config); 95 setMutationRateCalc(a_mutationRateCalculator); 96 } 97 98 112 public MutationOperator(final Configuration a_config, 113 final int a_desiredMutationRate) 114 throws InvalidConfigurationException { 115 super(a_config); 116 m_config.m_mutationRate = a_desiredMutationRate; 117 setMutationRateCalc(null); 118 } 119 120 133 public void operate(final Population a_population, 134 final List a_candidateChromosomes) { 135 if (a_population == null || a_candidateChromosomes == null) { 136 return; 140 } 141 if (m_config.m_mutationRate == 0 && m_mutationRateCalc == null) { 142 return; 146 } 147 boolean mutate = false; 152 RandomGenerator generator = getConfiguration().getRandomGenerator(); 153 int size = Math.min(getConfiguration().getPopulationSize(), 158 a_population.size()); 159 IGeneticOperatorConstraint constraint = getConfiguration(). 160 getJGAPFactory().getGeneticOperatorConstraint(); 161 for (int i = 0; i < size; i++) { 162 IChromosome chrom = a_population.getChromosome(i); 163 Gene[] genes = chrom.getGenes(); 164 IChromosome copyOfChromosome = null; 165 for (int j = 0; j < genes.length; j++) { 168 if (m_mutationRateCalc != null) { 169 mutate = m_mutationRateCalc.toBePermutated(chrom, j); 173 } 174 else { 175 mutate = (generator.nextInt(m_config.m_mutationRate) == 0); 179 } 180 if (mutate) { 181 184 if (constraint != null) { 185 List v = new Vector(); 186 v.add(chrom); 187 if (!constraint.isValid(a_population, v, this)) { 188 continue; 189 } 190 } 191 if (copyOfChromosome == null) { 200 copyOfChromosome = (IChromosome) chrom.clone(); 203 a_candidateChromosomes.add(copyOfChromosome); 206 genes = copyOfChromosome.getGenes(); 209 } 210 if (genes[j] instanceof ICompositeGene) { 215 ICompositeGene compositeGene = (ICompositeGene) genes[j]; 216 for (int k = 0; k < compositeGene.size(); k++) { 217 mutateGene(compositeGene.geneAt(k), generator); 218 } 219 } 220 else { 221 mutateGene(genes[j], generator); 222 } 223 } 224 } 225 } 226 } 227 228 236 private void mutateGene(final Gene a_gene, final RandomGenerator a_generator) { 237 for (int k = 0; k < a_gene.size(); k++) { 238 double percentage = -1 + a_generator.nextDouble() * 2; 242 a_gene.applyMutation(k, percentage); 245 } 246 } 247 248 254 public IUniversalRateCalculator getMutationRateCalc() { 255 return m_mutationRateCalc; 256 } 257 258 266 public void setMutationRateCalc(final IUniversalRateCalculator 267 a_mutationRateCalc) { 268 m_mutationRateCalc = a_mutationRateCalc; 269 if (m_mutationRateCalc != null) { 270 m_config.m_mutationRate = 0; 271 } 272 } 273 274 285 public boolean equals(final Object a_other) { 286 try { 287 return compareTo(a_other) == 0; 288 } catch (ClassCastException cex) { 289 return false; 290 } 291 } 292 293 304 public int compareTo(Object a_other) { 305 if (a_other == null) { 306 return 1; 307 } 308 MutationOperator op = (MutationOperator) a_other; 309 if (m_mutationRateCalc == null) { 310 if (op.m_mutationRateCalc != null) { 311 return -1; 312 } 313 } 314 else { 315 if (op.m_mutationRateCalc == null) { 316 return 1; 317 } 318 else { 319 } 320 } 321 if (m_config.m_mutationRate != op.m_config.m_mutationRate) { 322 if (m_config.m_mutationRate > op.m_config.m_mutationRate) { 323 return 1; 324 } 325 else { 326 return -1; 327 } 328 } 329 return 0; 332 } 333 334 public int getMutationRate() { 335 return m_config.m_mutationRate; 336 } 337 338 class MutationOperatorConfigurable 339 implements java.io.Serializable { 340 346 public int m_mutationRate; 347 } 348 } 349 | Popular Tags |