1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 15 28 public class AveragingCrossoverOperator 29 extends BaseGeneticOperator { 30 31 private final static String CVS_REVISION = "$Revision: 1.26 $"; 32 33 36 private RandomGenerator m_crossoverGenerator; 37 38 41 private int m_crossoverRate; 42 43 46 private Map m_loci; 47 48 52 private IUniversalRateCalculator m_crossoverRateCalc; 53 54 private void init() { 55 m_loci = new Hashtable(); 56 m_crossoverRate = 2; 57 } 58 59 68 public AveragingCrossoverOperator() 69 throws InvalidConfigurationException { 70 this(Genotype.getStaticConfiguration(), (RandomGenerator)null); 71 } 72 73 81 public AveragingCrossoverOperator(final Configuration a_configuration) 82 throws InvalidConfigurationException { 83 this(a_configuration, (RandomGenerator)null); 84 } 85 86 95 public AveragingCrossoverOperator(final Configuration a_configuration, 96 final RandomGenerator 97 a_generatorForAveraging) 98 throws InvalidConfigurationException { 99 super(a_configuration); 100 init(); 101 m_crossoverGenerator = a_generatorForAveraging; 102 } 103 104 115 public AveragingCrossoverOperator(final Configuration a_configuration, 116 final IUniversalRateCalculator 117 a_crossoverRateCalculator) 118 throws InvalidConfigurationException { 119 super(a_configuration); 120 init(); 121 setCrossoverRateCalc(a_crossoverRateCalculator); 122 } 123 124 131 private void setCrossoverRateCalc(final IUniversalRateCalculator 132 a_crossoverRateCalculator) { 133 m_crossoverRateCalc = a_crossoverRateCalculator; 134 } 135 136 151 public void operate(final Population a_population, 152 final List a_candidateChromosomes) { 153 int size = Math.min(getConfiguration().getPopulationSize(), 155 a_population.size()); 156 int numCrossovers = 0; 157 if (m_crossoverRateCalc == null) { 158 numCrossovers = size / m_crossoverRate; 159 } 160 else { 161 numCrossovers = size / m_crossoverRateCalc.calculateCurrentRate(); 162 } 163 RandomGenerator generator = getConfiguration().getRandomGenerator(); 164 if (m_crossoverGenerator == null) { 165 m_crossoverGenerator = generator; 166 } 167 int index1, index2; 173 for (int i = 0; i < numCrossovers; i++) { 174 index1 = generator.nextInt(size); 175 index2 = generator.nextInt(size); 176 IChromosome firstMate = a_population.getChromosome(index1); 177 IChromosome secondMate = a_population.getChromosome(index2); 178 Gene[] firstGenes = firstMate.getGenes(); 179 Gene[] secondGenes = secondMate.getGenes(); 180 int locus = getLocus(m_crossoverGenerator, i, firstGenes.length); 181 Gene gene1; 184 Gene gene2; 185 Object firstAllele; 186 for (int j = locus; j < firstGenes.length; j++) { 187 if (firstGenes[j] instanceof ICompositeGene) { 189 index1 = generator.nextInt(firstGenes[j].size()); 191 gene1 = ( (ICompositeGene) firstGenes[j]).geneAt(index1); 192 } 193 else { 194 gene1 = firstGenes[j]; 195 } 196 if (secondGenes[j] instanceof CompositeGene) { 198 index2 = generator.nextInt(secondGenes[j].size()); 200 gene2 = ( (ICompositeGene) secondGenes[j]).geneAt(index2); 201 } 202 else { 203 gene2 = secondGenes[j]; 204 } 205 firstAllele = gene1.getAllele(); 206 gene1.setAllele(gene2.getAllele()); 207 gene2.setAllele(firstAllele); 208 } 209 a_candidateChromosomes.add(firstMate); 214 a_candidateChromosomes.add(secondMate); 215 } 216 } 217 218 231 protected int getLocus(final RandomGenerator a_generator, final int a_index, 232 final int a_max) { 233 Integer locus = (Integer ) m_loci.get(new Integer (a_index)); 234 if (locus == null) { 235 locus = new Integer (a_generator.nextInt(a_max)); 236 m_loci.put(new Integer (a_index), locus); 237 } 238 return locus.intValue(); 239 } 240 241 252 public boolean equals(final Object a_other) { 253 try { 254 return compareTo(a_other) == 0; 255 } 256 catch (ClassCastException cex) { 257 return false; 258 } 259 } 260 261 272 public int compareTo(final Object a_other) { 273 if (a_other == null) { 274 return 1; 275 } 276 AveragingCrossoverOperator op = (AveragingCrossoverOperator) a_other; 277 if (m_crossoverRateCalc == null) { 278 if (op.m_crossoverRateCalc != null) { 279 return -1; 280 } 281 } 282 else { 283 if (op.m_crossoverRateCalc == null) { 284 return 1; 285 } 286 } 287 if (m_crossoverRate != op.m_crossoverRate) { 288 if (m_crossoverRate > op.m_crossoverRate) { 289 return 1; 290 } 291 else { 292 return -1; 293 } 294 } 295 return 0; 298 } 299 300 305 public void setCrossoverRate(int a_rate) { 306 m_crossoverRate = a_rate; 307 } 308 } 309 | Popular Tags |