1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 15 22 public class GaussianMutationOperator 23 extends BaseGeneticOperator { 24 25 private static final String CVS_REVISION = "$Revision: 1.21 $"; 26 27 private double m_deviation; 28 29 private RandomGenerator m_rg; 30 31 40 public GaussianMutationOperator() 41 throws InvalidConfigurationException { 42 this(Genotype.getStaticConfiguration()); 43 } 44 45 54 public GaussianMutationOperator(Configuration a_config) 55 throws InvalidConfigurationException { 56 this(a_config, 0.05d); 57 } 58 59 68 public GaussianMutationOperator(final Configuration a_configuration, 69 final double a_deviation) 70 throws InvalidConfigurationException { 71 super(a_configuration); 72 m_deviation = a_deviation; 73 } 74 75 84 public void operate(final Population a_population, 85 final List a_candidateChromosomes) { 86 int size = Math.min(getConfiguration().getPopulationSize(), 87 a_population.size()); 88 if (m_rg == null) { 89 RandomGenerator rn = getConfiguration().getRandomGenerator(); 90 m_rg = rn; 91 } 92 for (int i = 0; i < size; i++) { 99 Gene[] genes = a_population.getChromosome(i).getGenes(); 100 IChromosome copyOfChromosome = null; 101 for (int j = 0; j < genes.length; j++) { 104 double nextGaussian = m_rg.nextDouble(); 105 double diff = nextGaussian * m_deviation; 106 if (copyOfChromosome == null) { 109 copyOfChromosome = (IChromosome) a_population.getChromosome(i).clone(); 110 a_candidateChromosomes.add(copyOfChromosome); 113 genes = copyOfChromosome.getGenes(); 115 } 116 if (genes[j] instanceof CompositeGene) { 121 CompositeGene compositeGene = (CompositeGene) genes[j]; 122 for (int k = 0; k < compositeGene.size(); k++) { 123 mutateGene(compositeGene.geneAt(k), diff); 124 } 125 } 126 else { 127 mutateGene(genes[j], diff); 128 } 129 } 130 } 131 } 132 133 142 private void mutateGene(final Gene a_gene, final double a_percentage) { 143 for (int k = 0; k < a_gene.size(); k++) { 144 a_gene.applyMutation(k, a_percentage); 147 } 148 } 149 150 private void setRandomGenerator(final RandomGenerator a_rg) { 151 m_rg = a_rg; 152 } 153 154 166 public int compareTo(final Object a_other) { 167 if (a_other == null) { 168 return 1; 169 } 170 GaussianMutationOperator op = (GaussianMutationOperator) a_other; 171 if (m_deviation != op.m_deviation) { 172 if (m_deviation > op.m_deviation) { 173 return 1; 174 } 175 else { 176 return -1; 177 } 178 } 179 return 0; 182 } 183 184 190 public double getDeviation() { 191 return m_deviation; 192 } 193 } 194 | Popular Tags |