⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpleexample.java

📁 jGAp 遗传算法 提不错的一款软件 最新的更新
💻 JAVA
字号:
/*
 * This file is part of JGAP.
 *
 * JGAP offers a dual license model containing the LGPL as well as the MPL.
 *
 * For licensing information please see the file license.txt included with JGAP
 * or have a look at the top of class org.jgap.Chromosome which representatively
 * includes the JGAP license policy applicable for any file delivered with JGAP.
 */
package examples.simpleBooleanThreaded;

import org.jgap.*;
import org.jgap.impl.*;
import org.jgap.event.*;

/**
 * Simple class that demonstrates the basic usage of JGAP together with
 * multithreaded processing.
 *
 * @author Klaus Meffert
 * @since 3.01
 */
public class SimpleExample {
  /** String containing the CVS revision. Read out via reflection!*/
  private static final String CVS_REVISION = "$Revision: 1.3 $";

  /**
   * Starts the example.
   * @param args ignored here
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.01
   */
  public static void main(String[] args)
      throws Exception {
    final int numEvolutions = 500;
    final int numThreads = 5;
    int chromeSize = 20;
    if (chromeSize > 32) {
      System.err.println("This example does not handle " +
                         "Chromosomes greater than 32 bits in length.");
      System.exit( -1);
    }
    for (int i = 0; i < numThreads; i++) {
      final int j = i;
      Configuration gaConf = new DefaultConfiguration(i + "", "no name");
      gaConf.setPreservFittestIndividual(i % 2 == 0);
      gaConf.setKeepPopulationSizeConstant(i % 2 != 0);
      IChromosome sampleChromosome = new Chromosome(gaConf,
          new BooleanGene(gaConf), chromeSize);
      gaConf.setSampleChromosome(sampleChromosome);
      gaConf.setPopulationSize(gaConf.getRandomGenerator().nextInt(500));
      gaConf.setFitnessFunction(new MaxFunction());
      Genotype genotype = null;
      try {
        genotype = Genotype.randomInitialGenotype(gaConf);
      } catch (InvalidConfigurationException e) {
        e.printStackTrace();
        System.exit( -2);
      }
      final Thread t1 = new Thread(genotype);
      gaConf.getEventManager().addEventListener(GeneticEvent.
          GENOTYPE_EVOLVED_EVENT, new GeneticEventListener() {
        public void geneticEventFired(GeneticEvent a_firedEvent) {
          Genotype genotype = (Genotype) a_firedEvent.getSource();
          int evno = genotype.getConfiguration().getGenerationNr();
          if (evno % 10 == 0) {
            double bestFitness = genotype.getFittestChromosome().
                getFitnessValue();
            System.out.println(t1.getName() + ": Evolving generation " + evno
                               + ", best fitness: " + bestFitness);
          }
          if (evno > numEvolutions) {
            t1.stop();
          }
          else {
            try {
              t1.sleep( (j + 1) * 3);
            } catch (InterruptedException iex) {
              iex.printStackTrace();
              System.exit(1);
            }
          }
        }
      });
      t1.setPriority( (i % 2) + 1);
      t1.start();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -