1 10 package org.jgap; 11 12 import java.io.*; 13 import java.util.*; 14 import org.jgap.event.*; 15 16 30 public class Genotype 31 implements Serializable, Runnable { 32 33 private final static String CVS_REVISION = "$Revision: 1.93 $"; 34 35 39 private transient Configuration m_activeConfiguration; 40 41 private transient static Configuration m_staticConfiguration; 42 43 47 private Population m_population; 48 49 66 public Genotype(Configuration a_configuration, 67 IChromosome[] a_initialChromosomes) 68 throws InvalidConfigurationException { 69 this(a_configuration, new Population(a_configuration, a_initialChromosomes)); 70 } 71 72 88 public Genotype(Configuration a_configuration, Population a_population) 89 throws InvalidConfigurationException { 90 if (a_configuration == null) { 94 throw new IllegalArgumentException ( 95 "The Configuration instance may not be null."); 96 } 97 if (a_population == null) { 98 throw new IllegalArgumentException ( 99 "The Population may not be null."); 100 } 101 for (int i = 0; i < a_population.size(); i++) { 102 if (a_population.getChromosome(i) == null) { 103 throw new IllegalArgumentException ( 104 "The Chromosome instance at index " + i + " of the array of " + 105 "Chromosomes is null. No Chromosomes instance in this array " + 106 "may be null."); 107 } 108 } 109 m_population = a_population; 110 a_configuration.lockSettings(); 114 m_activeConfiguration = a_configuration; 115 } 116 117 126 public Genotype(Configuration a_configuration) 127 throws InvalidConfigurationException { 128 129 } 130 131 142 public synchronized IChromosome[] getChromosomes() { 143 Iterator it = getPopulation().iterator(); 144 IChromosome[] result = new Chromosome[getPopulation().size()]; 145 int i = 0; 146 while (it.hasNext()) { 147 result[i++] = (IChromosome) it.next(); 148 } 149 return result; 150 } 151 152 158 public Population getPopulation() { 159 return m_population; 160 } 161 162 173 public synchronized IChromosome getFittestChromosome() { 174 return getPopulation().determineFittestChromosome(); 175 } 176 177 189 public synchronized IChromosome getFittestChromosome(int a_startIndex, 190 int a_endIndex) { 191 return getPopulation().determineFittestChromosome(a_startIndex, a_endIndex); 192 } 193 194 205 public synchronized List getFittestChromosomes(int a_numberOfChromosomes) { 206 return getPopulation().determineFittestChromosomes(a_numberOfChromosomes); 207 } 208 209 222 public synchronized void evolve() { 223 if (m_activeConfiguration == null) { 224 throw new IllegalStateException ( 225 "The Configuration object must be set on this" 226 + " Genotype prior to evolution."); 227 } 228 if (m_activeConfiguration.isKeepPopulationSizeConstant()) { 238 keepPopSizeConstant(getPopulation(), 239 m_activeConfiguration.getPopulationSize()); 240 } 241 applyNaturalSelectors(true); 244 applyGeneticOperators(); 247 int originalPopSize = m_activeConfiguration.getPopulationSize(); 254 int currentPopSize = getPopulation().size(); 255 for (int i = originalPopSize; i < currentPopSize; i++) { 256 IChromosome chrom = getPopulation().getChromosome(i); 257 chrom.setFitnessValueDirectly(FitnessFunction.NO_FITNESS_VALUE); 258 } 259 applyNaturalSelectors(false); 262 BulkFitnessFunction bulkFunction = 265 m_activeConfiguration.getBulkFitnessFunction(); 266 if (bulkFunction != null) { 267 bulkFunction.evaluate(getPopulation()); 268 } 269 if (m_activeConfiguration.getMinimumPopSizePercent() > 0) { 273 int sizeWanted = m_activeConfiguration.getPopulationSize(); 274 int popSize; 275 int minSize = (int) Math.round(sizeWanted * 276 (double) getConfiguration(). 277 getMinimumPopSizePercent() 278 / 100); 279 popSize = getPopulation().size(); 280 if (popSize < minSize) { 281 IChromosome newChrom; 282 IChromosome sampleChrom = m_activeConfiguration.getSampleChromosome(); 284 Class sampleChromClass = sampleChrom.getClass(); 285 IInitializer chromIniter = m_activeConfiguration.getJGAPFactory(). 286 getInitializerFor(sampleChrom, sampleChromClass); 287 while (getPopulation().size() < minSize) { 288 try { 289 newChrom = (IChromosome) chromIniter.perform(sampleChrom, 290 sampleChromClass, null); 291 getPopulation().addChromosome(newChrom); 293 }catch (Exception ex) { 294 throw new RuntimeException (ex); 295 } 296 } 297 } 302 } 303 if (m_activeConfiguration.isPreserveFittestIndividual()) { 304 IChromosome fittest = getFittestChromosome(0, 305 m_activeConfiguration. 306 getPopulationSize() - 1); 307 if (m_activeConfiguration.isKeepPopulationSizeConstant()) { 308 keepPopSizeConstant(getPopulation(), 309 m_activeConfiguration.getPopulationSize()); 310 } 311 if (!getPopulation().contains(fittest)) { 314 getPopulation().addChromosome(fittest); 317 } 318 } 319 m_activeConfiguration.incrementGenerationNr(); 322 m_activeConfiguration.getEventManager().fireGeneticEvent( 325 new GeneticEvent(GeneticEvent.GENOTYPE_EVOLVED_EVENT, this)); 326 } 327 328 339 public void evolve(int a_numberOfEvolutions) { 340 for (int i = 0; i < a_numberOfEvolutions; i++) { 341 evolve(); 342 } 343 if (m_activeConfiguration.isKeepPopulationSizeConstant()) { 344 keepPopSizeConstant(getPopulation(), 345 m_activeConfiguration.getPopulationSize()); 346 } 347 } 348 349 356 public String toString() { 357 StringBuffer buffer = new StringBuffer (); 358 for (int i = 0; i < getPopulation().size(); i++) { 359 buffer.append(getPopulation().getChromosome(i).toString()); 360 buffer.append(" ["); 361 buffer.append(getPopulation().getChromosome(i).getFitnessValueDirectly()); 362 buffer.append("]\n"); 363 } 364 return buffer.toString(); 365 } 366 367 385 public static Genotype randomInitialGenotype(Configuration 386 a_configuration) 387 throws InvalidConfigurationException { 388 if (a_configuration == null) { 389 throw new IllegalArgumentException ( 390 "The Configuration instance may not be null."); 391 } 392 a_configuration.lockSettings(); 393 int populationSize = a_configuration.getPopulationSize(); 401 IChromosome sampleChrom = a_configuration.getSampleChromosome(); 402 Population pop = new Population(a_configuration, populationSize); 403 Genotype result = new Genotype(a_configuration, pop); 406 result.fillPopulation(populationSize); 407 return result; 408 } 409 410 419 public void fillPopulation(final int a_num) 420 throws InvalidConfigurationException { 421 IChromosome sampleChrom = getConfiguration().getSampleChromosome(); 422 Class sampleClass = sampleChrom.getClass(); 423 IInitializer chromIniter = getConfiguration().getJGAPFactory(). 424 getInitializerFor(sampleChrom, sampleClass); 425 if (chromIniter == null) { 426 throw new InvalidConfigurationException("No initializer found for class " 427 + sampleClass); 428 } 429 try { 430 for (int i = 0; i < a_num; i++) { 431 getPopulation().addChromosome( (IChromosome) chromIniter.perform(sampleChrom, 432 sampleClass, null)); 433 } 434 } 435 catch (Exception ex) { 436 throw new IllegalStateException (ex.getMessage()); 437 } 438 439 } 440 441 456 public boolean equals(Object a_other) { 457 try { 458 if (a_other == null) { 461 return false; 462 } 463 Genotype otherGenotype = (Genotype) a_other; 464 if (getPopulation().size() != otherGenotype.getPopulation().size()) { 468 return false; 469 } 470 Collections.sort(getPopulation().getChromosomes()); 478 Collections.sort(otherGenotype.getPopulation().getChromosomes()); 479 for (int i = 0; i < getPopulation().size(); i++) { 480 if (! (getPopulation().getChromosome(i).equals( 481 otherGenotype.getPopulation().getChromosome(i)))) { 482 return false; 483 } 484 } 485 return true; 486 } 487 catch (ClassCastException e) { 488 return false; 489 } 490 } 491 492 502 protected void applyNaturalSelectors( 503 boolean a_processBeforeGeneticOperators) { 504 505 try { 506 int selectorSize = m_activeConfiguration.getNaturalSelectorsSize( 510 a_processBeforeGeneticOperators); 511 if (selectorSize > 0) { 512 int m_population_size = m_activeConfiguration.getPopulationSize(); 513 int m_single_selection_size; 514 Population m_new_population; 515 m_new_population = new Population(m_activeConfiguration, 516 m_population_size); 517 NaturalSelector selector; 518 for (int i = 0; i < selectorSize; i++) { 522 selector = m_activeConfiguration.getNaturalSelector( 523 a_processBeforeGeneticOperators, i); 524 if (i == selectorSize - 1 && i > 0) { 525 m_single_selection_size = m_population_size - getPopulation().size(); 528 } 529 else { 530 m_single_selection_size = m_population_size / selectorSize; 531 } 532 selector.select(m_single_selection_size, getPopulation(), 535 m_new_population); 536 selector.empty(); 539 } 540 setPopulation(new Population(m_activeConfiguration)); 541 getPopulation().addChromosomes(m_new_population); 542 } 543 } 544 catch (InvalidConfigurationException iex) { 545 throw new IllegalStateException (iex.getMessage()); 547 } 548 } 549 550 556 public void applyGeneticOperators() { 557 List geneticOperators = m_activeConfiguration.getGeneticOperators(); 558 Iterator operatorIterator = geneticOperators.iterator(); 559 while (operatorIterator.hasNext()) { 560 GeneticOperator operator = (GeneticOperator) operatorIterator.next(); 561 applyGeneticOperator(operator, getPopulation(), 562 getPopulation().getChromosomes()); 563 564 } 568 } 569 570 576 public static Configuration getStaticConfiguration() { 577 return m_staticConfiguration; 578 } 579 580 587 public static void setStaticConfiguration(Configuration a_configuration) { 588 m_staticConfiguration = a_configuration; 589 } 590 591 public Configuration getConfiguration() { 592 return m_activeConfiguration; 593 } 594 595 614 public int hashCode() { 615 int i, size = getPopulation().size(); 616 IChromosome s; 617 int twopower = 1; 618 int localHashCode = -573; 622 for (i = 0; i < size; i++, twopower = 2 * twopower) { 623 s = getPopulation().getChromosome(i); 624 localHashCode = 31 * localHashCode + s.hashCode(); 625 } 626 return localHashCode; 627 } 628 629 protected void setPopulation(Population a_pop) { 630 m_population = a_pop; 631 } 632 633 646 protected void applyGeneticOperator(GeneticOperator a_operator, 647 Population a_population, 648 List a_chromosomes) { 649 a_operator.operate(a_population, a_chromosomes); 650 } 651 652 661 protected void keepPopSizeConstant(Population a_pop, int a_maxSize) { 662 int popSize = a_pop.size(); 663 while (popSize > a_maxSize) { 666 a_pop.removeChromosome(0); 669 popSize--; 670 } 671 } 672 673 682 public void run() { 683 while (!Thread.currentThread().interrupted()) { 684 evolve(); 685 } 686 } 687 } 688 | Popular Tags |