1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 15 22 public class ThresholdSelector 23 extends NaturalSelector { 24 25 private final static String CVS_REVISION = "$Revision: 1.14 $"; 26 27 30 private List m_chromosomes; 31 32 35 private boolean m_needsSorting; 36 37 40 private FitnessValueComparator m_fitnessValueComparator; 41 42 private ThresholdSelectorConfigurable m_config 43 = new ThresholdSelectorConfigurable(); 44 45 53 public ThresholdSelector() { 54 this(Genotype.getStaticConfiguration(), 0.3d); 55 } 56 57 67 public ThresholdSelector(final Configuration a_config, 68 final double a_bestChromosomes_Percentage) { 69 super(a_config); 70 if (a_bestChromosomes_Percentage < 0.0000000d 71 || a_bestChromosomes_Percentage > 1.0000000d) { 72 throw new IllegalArgumentException ("Percentage must be between 0.0" 73 + " and 1.0 !"); 74 } 75 m_config.m_bestChroms_Percentage = a_bestChromosomes_Percentage; 76 m_chromosomes = new Vector(); 77 m_needsSorting = false; 78 m_fitnessValueComparator = new FitnessValueComparator(); 79 } 80 81 93 public void select(final int a_howManyToSelect, final Population a_from_pop, 94 Population a_to_pop) { 95 if (a_from_pop != null) { 96 int size = a_from_pop.size(); 97 for (int i = 0; i < size; i++) { 98 add(a_from_pop.getChromosome(i)); 99 } 100 } 101 int canBeSelected; 102 if (a_howManyToSelect > m_chromosomes.size()) { 103 canBeSelected = m_chromosomes.size(); 104 } 105 else { 106 canBeSelected = a_howManyToSelect; 107 } 108 if (m_needsSorting) { 112 Collections.sort(m_chromosomes, m_fitnessValueComparator); 113 m_needsSorting = false; 114 } 115 int bestToBeSelected = (int) Math.round(canBeSelected 117 * m_config.m_bestChroms_Percentage); 118 for (int i = 0; i < bestToBeSelected; i++) { 119 a_to_pop.addChromosome( (IChromosome) m_chromosomes.get(i)); 120 } 121 int missing = a_howManyToSelect - bestToBeSelected; 123 RandomGenerator rn = getConfiguration().getRandomGenerator(); 124 int index; 125 int size = m_chromosomes.size(); 126 for (int i = 0; i < missing; i++) { 127 index = rn.nextInt(size); 128 a_to_pop.addChromosome( (IChromosome) m_chromosomes.get(index)); 129 } 130 } 131 132 138 public boolean returnsUniqueChromosomes() { 139 return false; 140 } 141 142 public void empty() { 143 m_chromosomes.clear(); 144 m_needsSorting = false; 145 } 146 147 154 protected void add(final IChromosome a_chromosomeToAdd) { 155 m_chromosomes.add(a_chromosomeToAdd); 156 m_needsSorting = true; 157 } 158 159 166 private class FitnessValueComparator 167 implements Comparator { 168 public int compare(final Object a_first, final Object a_second) { 169 IChromosome chrom1 = (IChromosome) a_first; 170 IChromosome chrom2 = (IChromosome) a_second; 171 if (getConfiguration().getFitnessEvaluator().isFitter(chrom2. 172 getFitnessValue(), chrom1.getFitnessValue())) { 173 return 1; 174 } 175 else if (getConfiguration().getFitnessEvaluator().isFitter( 176 chrom1.getFitnessValue(), chrom2.getFitnessValue())) { 177 return -1; 178 } 179 else { 180 return 0; 181 } 182 } 183 } 184 class ThresholdSelectorConfigurable { 185 190 public double m_bestChroms_Percentage; 191 } 192 } 193 | Popular Tags |