1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 15 23 public class InversionOperator 24 extends BaseGeneticOperator { 25 26 private final static String CVS_REVISION = "$Revision: 1.9 $"; 27 28 36 public InversionOperator() 37 throws InvalidConfigurationException { 38 this(Genotype.getStaticConfiguration()); 39 } 40 41 48 public InversionOperator(Configuration a_config) 49 throws InvalidConfigurationException { 50 super(a_config); 51 } 52 53 57 public void operate(final Population a_population, 58 final List a_candidateChromosomes) { 59 int size = Math.min(getConfiguration().getPopulationSize(), 62 a_population.size()); 63 RandomGenerator generator = getConfiguration().getRandomGenerator(); 64 int index1; 70 index1 = generator.nextInt(size); 71 IChromosome chrom1 = a_population.getChromosome(index1); 72 IChromosome firstMate = (IChromosome) chrom1.clone(); 73 Gene[] firstGenes = firstMate.getGenes(); 74 int locus = generator.nextInt(firstGenes.length); 75 Gene[] invertedGenes = new Gene[firstGenes.length]; 78 int index = 0; 79 int len = firstGenes.length; 80 for (int j = locus; j < len; j++) { 81 invertedGenes[index++] = firstGenes[j]; 82 } 83 for (int j = 0; j < locus; j++) { 84 invertedGenes[index++] = firstGenes[j]; 85 } 86 try { 87 firstMate.setGenes(invertedGenes); 88 } 89 catch (InvalidConfigurationException cex) { 90 throw new Error (cex); 93 } 94 a_candidateChromosomes.add(firstMate); 98 } 99 100 111 public int compareTo(final Object a_other) { 112 if (a_other == null) { 113 return 1; 114 } 115 InversionOperator op = (InversionOperator) a_other; 116 return 0; 119 } 120 } 121 | Popular Tags |