1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 15 25 public class TournamentSelector 26 extends NaturalSelector { 27 28 private final static String CVS_REVISION = "$Revision: 1.19 $"; 29 30 private TournamentSelectorConfigurable m_config 31 = new TournamentSelectorConfigurable(); 32 33 private List m_chromosomes; 34 35 38 private FitnessValueComparator m_fitnessValueComparator; 39 40 48 public TournamentSelector() { 49 super(Genotype.getStaticConfiguration()); 50 init(); 51 } 52 53 private void init() { 54 m_chromosomes = new Vector(); 55 m_fitnessValueComparator = new FitnessValueComparator(); 56 } 57 58 66 public TournamentSelector(final Configuration a_config, 67 final int a_tournament_size, 68 final double a_probability) { 69 super(a_config); 70 init(); 71 if (a_tournament_size < 1) { 72 throw new IllegalArgumentException ("Tournament size must be at least 1!"); 73 } 74 if (a_probability <= 0.0d || a_probability > 1.0d) { 75 throw new IllegalArgumentException ("Probability must be greater 0.0 and" 76 + " less or equal than 1.0!"); 77 } 78 m_config.m_tournament_size = a_tournament_size; 79 m_config.m_probability = a_probability; 80 } 81 82 public void setTournamentSize(final int a_tournament_size) { 83 if (a_tournament_size < 1) { 84 throw new IllegalArgumentException ("Tournament size must be at least 1!"); 85 } 86 m_config.m_tournament_size = a_tournament_size; 87 } 88 89 public int getTournamentSize() { 90 return m_config.m_tournament_size; 91 } 92 93 public double getProbability() { 94 return m_config.m_probability; 95 } 96 97 public void setProbability(final double a_probability) { 98 if (a_probability <= 0.0d || a_probability > 1.0d) { 99 throw new IllegalArgumentException ("Probability must be greater 0.0 and" 100 + " less or equal than 1.0!"); 101 } 102 m_config.m_probability = a_probability; 103 } 104 105 117 public void select(final int a_howManyToSelect, final Population a_from_pop, 118 Population a_to_pop) { 119 if (a_from_pop != null) { 120 for (int i = 0; i < a_from_pop.size(); i++) { 121 add(a_from_pop.getChromosome(i)); 122 } 123 } 124 List tournament = new Vector(); 125 RandomGenerator rn = getConfiguration().getRandomGenerator(); 126 int size = m_chromosomes.size(); 127 if (size == 0) { 128 return; 129 } 130 int k; 131 for (int i = 0; i < a_howManyToSelect; i++) { 132 tournament.clear(); 134 for (int j = 0; j < m_config.m_tournament_size; j++) { 135 k = rn.nextInt(size); 136 tournament.add(m_chromosomes.get(k)); 137 } 138 Collections.sort(tournament, m_fitnessValueComparator); 139 double prob = rn.nextDouble(); 140 double probAccumulated = m_config.m_probability; 141 int index = 0; 142 if (m_config.m_tournament_size > 1) { 144 do { 145 if (prob <= probAccumulated) { 146 break; 147 } 148 else { 149 probAccumulated += probAccumulated * (1 - m_config.m_probability); 150 index++; 151 } 152 } 153 while (index < m_config.m_tournament_size - 1); 154 } 155 a_to_pop.addChromosome( (IChromosome) tournament.get(index)); 156 } 157 } 158 159 165 public boolean returnsUniqueChromosomes() { 166 return false; 167 } 168 169 public void empty() { 170 m_chromosomes.clear(); 171 } 172 173 180 protected void add(final IChromosome a_chromosomeToAdd) { 181 m_chromosomes.add(a_chromosomeToAdd); 182 } 183 184 191 private class FitnessValueComparator 192 implements Comparator { 193 public int compare(final Object a_first, final Object a_second) { 194 IChromosome chrom1 = (IChromosome) a_first; 195 IChromosome chrom2 = (IChromosome) a_second; 196 if (getConfiguration().getFitnessEvaluator().isFitter(chrom2. 197 getFitnessValue(), chrom1.getFitnessValue())) { 198 return 1; 199 } 200 else if (getConfiguration().getFitnessEvaluator().isFitter( 201 chrom1.getFitnessValue(), chrom2.getFitnessValue())) { 202 return -1; 203 } 204 else { 205 return 0; 206 } 207 } 208 } 209 210 class TournamentSelectorConfigurable { 211 216 public double m_probability; 217 218 222 public int m_tournament_size; 223 224 } 225 } 226 | Popular Tags |