1 10 package org.jgap.impl.salesman; 11 12 import org.jgap.*; 13 import org.jgap.impl.*; 14 import org.jgap.event.*; 15 16 45 public abstract class Salesman 46 implements java.io.Serializable { 47 48 private final static String CVS_REVISION = "$Revision: 1.19 $"; 49 50 private Configuration m_config; 51 52 private int m_maxEvolution = 128; 53 54 private int m_populationSize = 512; 55 56 private int m_acceptableCost = -1; 57 58 70 public abstract double distance(Gene a_from, Gene a_to); 71 72 89 public abstract IChromosome createSampleChromosome(Object a_initial_data); 90 91 102 public FitnessFunction createFitnessFunction(final Object a_initial_data) { 103 return new SalesmanFitnessFunction(this); 104 } 105 106 120 public Configuration createConfiguration(final Object a_initial_data) 121 throws InvalidConfigurationException { 122 Configuration config = new Configuration(); 125 BestChromosomesSelector bestChromsSelector = 126 new BestChromosomesSelector(config, 1.0d); 127 bestChromsSelector.setDoubletteChromosomesAllowed(false); 128 config.addNaturalSelector(bestChromsSelector, true); 129 config.setRandomGenerator(new StockRandomGenerator()); 130 config.setMinimumPopSizePercent(0); 131 config.setEventManager(new EventManager()); 132 config.setFitnessEvaluator(new DefaultFitnessEvaluator()); 133 config.setChromosomePool(new ChromosomePool()); 134 config.addGeneticOperator(new GreedyCrossover(config)); 137 config.addGeneticOperator(new SwappingMutationOperator(config, 20)); 138 return config; 139 } 140 141 152 public int getAcceptableCost() { 153 return m_acceptableCost; 154 } 155 156 public void setAcceptableCost(final int a_acceptableCost) { 157 m_acceptableCost = a_acceptableCost; 158 } 159 160 166 public int getMaxEvolution() { 167 return m_maxEvolution; 168 } 169 170 177 public void setMaxEvolution(final int a_maxEvolution) { 178 m_maxEvolution = a_maxEvolution; 179 } 180 181 186 public int getPopulationSize() { 187 return m_populationSize; 188 } 189 190 197 public void setPopulationSize(final int a_populationSize) { 198 m_populationSize = a_populationSize; 199 } 200 201 216 public IChromosome findOptimalPath(final Object a_initial_data) 217 throws Exception { 218 m_config = createConfiguration(a_initial_data); 219 FitnessFunction myFunc = createFitnessFunction(a_initial_data); 220 m_config.setFitnessFunction(myFunc); 221 IChromosome sampleChromosome = createSampleChromosome(a_initial_data); 227 m_config.setSampleChromosome(sampleChromosome); 228 m_config.setPopulationSize(getPopulationSize()); 236 239 IChromosome[] chromosomes = 243 new IChromosome[m_config.getPopulationSize()]; 244 Gene[] samplegenes = sampleChromosome.getGenes(); 245 for (int i = 0; i < chromosomes.length; i++) { 246 Gene[] genes = new Gene[samplegenes.length]; 247 for (int k = 0; k < genes.length; k++) { 248 genes[k] = samplegenes[k].newGene(); 249 genes[k].setAllele(samplegenes[k].getAllele()); 250 } 251 shuffle(genes); 252 chromosomes[i] = new Chromosome(m_config, genes); 253 } 254 Genotype population = new Genotype(m_config, 255 new Population(m_config, chromosomes)); 256 IChromosome best = null; 257 Evolution: 261 for (int i = 0; i < getMaxEvolution(); i++) { 262 population.evolve(); 263 best = population.getFittestChromosome(); 264 if (best.getFitnessValue() >= getAcceptableCost()) { 265 break Evolution; 266 } 267 } 268 return best; 271 } 272 273 protected void shuffle(final Gene[] a_genes) { 274 Gene t; 275 for (int r = 0; r < 10 * a_genes.length; r++) { 277 for (int i = m_startOffset; i < a_genes.length; i++) { 278 int p = m_startOffset 279 + m_config.getRandomGenerator(). 280 nextInt(a_genes.length - m_startOffset); 281 t = a_genes[i]; 282 a_genes[i] = a_genes[p]; 283 a_genes[p] = t; 284 } 285 } 286 } 287 288 private int m_startOffset = 1; 289 290 300 public void setStartOffset(final int a_offset) { 301 m_startOffset = a_offset; 302 } 303 304 314 public int getStartOffset() { 315 return m_startOffset; 316 } 317 318 public Configuration getConfiguration() { 319 return m_config; 320 } 321 } 322 | Popular Tags |