1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 15 33 public class CrossoverOperator 34 extends BaseGeneticOperator implements Comparable { 35 36 private final static String CVS_REVISION = "$Revision: 1.32 $"; 37 38 41 private int m_crossoverRate; 42 43 47 private IUniversalRateCalculator m_crossoverRateCalc; 48 49 60 public CrossoverOperator() 61 throws InvalidConfigurationException { 62 super(Genotype.getStaticConfiguration()); 63 m_crossoverRate = 2; 65 setCrossoverRateCalc(null); 66 } 67 68 79 public CrossoverOperator(final Configuration a_configuration) 80 throws InvalidConfigurationException { 81 super(a_configuration); 82 m_crossoverRate = 2; 85 setCrossoverRateCalc(null); 86 } 87 88 102 public CrossoverOperator(final Configuration a_configuration, 103 final IUniversalRateCalculator 104 a_crossoverRateCalculator) 105 throws InvalidConfigurationException { 106 super(a_configuration); 107 setCrossoverRateCalc(a_crossoverRateCalculator); 108 } 109 110 122 public CrossoverOperator(final Configuration a_configuration, 123 final int a_desiredCrossoverRate) 124 throws InvalidConfigurationException { 125 super(a_configuration); 126 if (a_desiredCrossoverRate < 1) { 127 throw new IllegalArgumentException ("Crossover rate must be greater zero"); 128 } 129 m_crossoverRate = a_desiredCrossoverRate; 130 setCrossoverRateCalc(null); 131 } 132 133 138 public void operate(final Population a_population, 139 final List a_candidateChromosomes) { 140 int size = Math.min(getConfiguration().getPopulationSize(), 143 a_population.size()); 144 int numCrossovers = 0; 145 if (m_crossoverRateCalc == null) { 146 numCrossovers = size / m_crossoverRate; 147 } 148 else { 149 numCrossovers = size / m_crossoverRateCalc.calculateCurrentRate(); 150 } 151 RandomGenerator generator = getConfiguration().getRandomGenerator(); 152 IGeneticOperatorConstraint constraint = getConfiguration(). 153 getJGAPFactory().getGeneticOperatorConstraint(); 154 int index1, index2; 160 for (int i = 0; i < numCrossovers; i++) { 161 index1 = generator.nextInt(size); 162 index2 = generator.nextInt(size); 163 IChromosome chrom1 = a_population.getChromosome(index1); 164 IChromosome chrom2 = a_population.getChromosome(index2); 165 if (constraint != null) { 168 List v = new Vector(); 169 v.add(chrom1); 170 v.add(chrom2); 171 if (!constraint.isValid(a_population, v, this)) { 172 continue; 173 } 174 } 175 IChromosome firstMate = (IChromosome) chrom1.clone(); 178 IChromosome secondMate = (IChromosome) chrom2.clone(); 179 doCrossover(firstMate, secondMate, a_candidateChromosomes, generator); 180 } 181 } 182 183 private void doCrossover(IChromosome firstMate, IChromosome secondMate, 184 List a_candidateChromosomes, 185 RandomGenerator generator) { 186 Gene[] firstGenes = firstMate.getGenes(); 187 Gene[] secondGenes = secondMate.getGenes(); 188 int locus = generator.nextInt(firstGenes.length); 189 Gene gene1; 192 Gene gene2; 193 Object firstAllele; 194 for (int j = locus; j < firstGenes.length; j++) { 195 if (firstGenes[j] instanceof ICompositeGene) { 198 int index1 = generator.nextInt(firstGenes[j].size()); 201 gene1 = ( (ICompositeGene) firstGenes[j]).geneAt(index1); 202 } 203 else { 204 gene1 = firstGenes[j]; 205 } 206 if (secondGenes[j] instanceof ICompositeGene) { 209 int index2 = generator.nextInt(secondGenes[j].size()); 212 gene2 = ( (ICompositeGene) secondGenes[j]).geneAt(index2); 213 } 214 else { 215 gene2 = secondGenes[j]; 216 } 217 firstAllele = gene1.getAllele(); 218 gene1.setAllele(gene2.getAllele()); 219 gene2.setAllele(firstAllele); 220 } 221 a_candidateChromosomes.add(firstMate); 226 a_candidateChromosomes.add(secondMate); 227 } 228 229 237 private void setCrossoverRateCalc(final IUniversalRateCalculator 238 a_crossoverRateCalculator) { 239 m_crossoverRateCalc = a_crossoverRateCalculator; 240 } 241 242 253 public int compareTo(final Object a_other) { 254 255 if (a_other == null) { 256 return 1; 257 } 258 CrossoverOperator op = (CrossoverOperator) a_other; 259 if (m_crossoverRateCalc == null) { 260 if (op.m_crossoverRateCalc != null) { 261 return -1; 262 } 263 } 264 else { 265 if (op.m_crossoverRateCalc == null) { 266 return 1; 267 } 268 } 269 if (m_crossoverRate != op.m_crossoverRate) { 270 if (m_crossoverRate > op.m_crossoverRate) { 271 return 1; 272 } 273 else { 274 return -1; 275 } 276 } 277 return 0; 280 } 281 } 282 | Popular Tags |