1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 15 23 public class BestChromosomesSelector 24 extends NaturalSelector { 25 26 private final static String CVS_REVISION = "$Revision: 1.41 $"; 27 28 31 private Population m_chromosomes; 32 33 36 private boolean m_doublettesAllowed; 37 38 41 private boolean m_needsSorting; 42 43 46 private FitnessValueComparator m_fitnessValueComparator; 47 48 private BestChromosomesSelectorConfig m_config = new 49 BestChromosomesSelectorConfig(); 50 51 60 public BestChromosomesSelector() 61 throws InvalidConfigurationException { 62 this(Genotype.getStaticConfiguration()); 63 } 64 65 73 public BestChromosomesSelector(final Configuration a_config) 74 throws InvalidConfigurationException { 75 this(a_config, 1.0d); 76 } 77 78 public BestChromosomesSelector(final Configuration a_config, 79 final double a_originalRate) 80 throws InvalidConfigurationException { 81 super(a_config); 82 m_chromosomes = new Population(a_config); 83 m_needsSorting = false; 84 m_doublettesAllowed = false; 85 setOriginalRate(a_originalRate); 86 m_fitnessValueComparator = new FitnessValueComparator(); 87 } 88 89 96 protected void add(final IChromosome a_chromosomeToAdd) { 97 if (!getDoubletteChromosomesAllowed() 101 && m_chromosomes.getChromosomes().contains(a_chromosomeToAdd)) { 102 return; 103 } 104 a_chromosomeToAdd.setIsSelectedForNextGeneration(false); 106 m_chromosomes.addChromosome(a_chromosomeToAdd); 107 m_needsSorting = true; 110 } 111 112 124 public void select(final int a_howManyToSelect, 125 final Population a_from_pop, 126 final Population a_to_pop) { 127 if (a_from_pop != null) { 128 int popSize = a_from_pop.size(); 129 for (int i = 0; i < popSize; i++) { 130 add(a_from_pop.getChromosome(i)); 131 } 132 } 133 int canBeSelected; 134 int chromsSize = m_chromosomes.size(); 135 if (a_howManyToSelect > chromsSize) { 136 canBeSelected = chromsSize; 137 } 138 else { 139 canBeSelected = a_howManyToSelect; 140 } 141 int neededSize = a_howManyToSelect; 142 double origRate; 143 origRate = m_config.m_originalRate; 144 if (origRate < 1.0d) { 145 canBeSelected = (int) Math.round( (double) canBeSelected * 146 origRate); 147 if (canBeSelected < 1) { 148 canBeSelected = 1; 149 } 150 } 151 if (m_needsSorting) { 155 Collections.sort(m_chromosomes.getChromosomes(), 156 m_fitnessValueComparator); 157 m_needsSorting = false; 158 } 159 IChromosome selectedChromosome; 162 for (int i = 0; i < canBeSelected; i++) { 163 selectedChromosome = m_chromosomes.getChromosome(i); 164 selectedChromosome.setIsSelectedForNextGeneration(true); 165 a_to_pop.addChromosome(selectedChromosome); 166 } 167 if (getDoubletteChromosomesAllowed()) { 168 int toAdd; 169 toAdd = neededSize - a_to_pop.size(); 170 for (int i = 0; i < toAdd; i++) { 174 175 selectedChromosome = m_chromosomes.getChromosome(i % chromsSize); 176 selectedChromosome.setIsSelectedForNextGeneration(true); 177 a_to_pop.addChromosome(selectedChromosome); 178 } 179 } 180 } 181 182 188 public void empty() { 189 m_chromosomes.getChromosomes().clear(); 192 m_needsSorting = false; 193 } 194 195 204 public void setDoubletteChromosomesAllowed( 205 final boolean a_doublettesAllowed) { 206 m_doublettesAllowed = a_doublettesAllowed; 207 } 208 209 215 public boolean getDoubletteChromosomesAllowed() { 216 return m_doublettesAllowed; 217 } 218 219 225 public boolean returnsUniqueChromosomes() { 226 return true; 227 } 228 229 243 public void setOriginalRate(final double a_originalRate) { 244 if (a_originalRate < 0.0d || a_originalRate > 1.0d) { 245 throw new IllegalArgumentException ("Original rate must be greater than" 246 + " zero and not greater than one!"); 247 } 248 m_config.m_originalRate = a_originalRate; 249 } 250 251 258 public double getOriginalRate() { 259 return m_config.m_originalRate; 260 } 261 262 class BestChromosomesSelectorConfig 263 implements java.io.Serializable { 264 268 public double m_originalRate; 269 } 270 } 271 | Popular Tags |