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

📄 .#gpconfiguration.java.1.46

📁 JGAP是一种遗传算法和遗传规划的组成部分提供了一个Java框架。它提供了基本的遗传机制
💻 46
📖 第 1 页 / 共 3 页
字号:
/*
 * 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 org.jgap.gp.impl;

import java.util.*;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.jgap.*;
import org.jgap.impl.*;
import org.jgap.distr.*;
import org.jgap.event.*;
import org.jgap.gp.*;
import org.jgap.gp.terminal.Variable;
import org.jgap.util.ICloneable;
import org.jgap.util.CloneException;
import java.io.*;

/**
 * Configuration for a GP.
 *
 * @author Klaus Meffert
 * @since 3.0
 */
public class GPConfiguration
    extends Configuration {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.46 $";

  /**@todo introduce lock for configuration*/
  /**
   * References the current fitness function that will be used to evaluate
   * chromosomes during the natural selection process.
   */
  private GPFitnessFunction m_objectiveFunction;

  /**
   * Internal stack, see PushCommand for example.
   */
  private /*transient*/ Stack m_stack = new Stack();

  /**
   * Internal memory, see StoreTerminalCommand for example.
   */
  private transient Culture m_memory = new Culture(50);

  /**@todo make 50 configurable*/
  private transient Hashtable<String, char[][]> m_matrices;

  /**
   * The probability that a crossover operation is chosen during evolution. Must
   * be between 0.0d and 1.0d, inclusive.
   */
  private double m_crossoverProb = 0.9d;

  /**
   * The probability that a reproduction operation is chosen during evolution.
   * Must be between 0.0d and 1.0d. crossoverProb + reproductionProb must equal
   * 1.0d.
   */
  private double m_reproductionProb = 0.1d;

  /**
   * The probability that a node is mutated during growing a program.
   */
  private double m_mutationProb = 0.1d;

  /**
   * The probability that the arity of a node is changed during growing a
   * program.
   */
  private double m_dynArityProb = 0.08d;

  /**
   * Percentage of the population that will be filled with new individuals
   * during evolution. Must be between 0.0d and 1.0d.
   */
  private double m_newChromsPercent = 0.3d;

  /**
   * In crossover: If random number (0..1) < this value, then choose a function
   * otherwise a terminal.
   */
  private double m_functionProb = 0.9d;

  /**
   * The maximum depth of an individual resulting from crossover.
   */
  private int m_maxCrossoverDepth = 17;

  /**
   * The maximum depth of an individual when the world is created.
   */
  private int m_maxInitDepth = 7;

  /**
   * The minimum depth of an individual when the world is created.
   */
  private int m_minInitDepth = 2;

  /**
   * The method of choosing an individual to perform an evolution operation on.
   */
  private INaturalGPSelector m_selectionMethod;

  /**
   * The method of crossing over two individuals during evolution.
   */
  private CrossMethod m_crossMethod;

  /**
   * True: Set of available functions must contain any "type of function" that
   * may be needed during construction of a new program. A "type of function"
   * is, for instance, a terminal with return type CommandGene.IntegerClass.
   */
  private boolean m_strictProgramCreation;

  /**
   * If m_strictProgramCreation is false: Maximum number of tries to construct
   * a valid program.
   */
  private int m_programCreationMaxTries = 5;

  /**
   * The fitness evaluator. See interface IGPFitnessEvaluator for details.
   */
  private IGPFitnessEvaluator m_fitnessEvaluator;

  private INodeValidator m_nodeValidator;

  /**
   * Internal flag to display a warning only once, in case a program could not
   * be evolved with the allowed maximum number of nodes.
   *
   * @since 3.2
   */
  private transient boolean m_warningPrinted;

  /**
   * Prototype of a valid program. May be cloned if needed (do not reference
   * it!).
   *
   * @since 3.2
   */
  private IGPProgram m_prototypeProgram;

  private boolean m_useProgramCache = false;

  private Map m_variables;

  private transient Map m_programCache;

  /**
   * Holds the central configurable factory for creating default objects.
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  private transient IJGAPFactory m_factory;

  /**
   * For initializing GP programs before random creation.
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  private IGPInitStrategy m_initStrategy;

  /**
   * TRUE: Activate methods checkErroneousPop and checkErroneousProg in class
   * GPGenotype.
   */
  private boolean m_verify;

  /**
   * TRUE: Do not clone command genes when creating a new GP program in
   * ProgramChromosome.
   *
   * @author Klaus Meffert
   * @since 3.4.3
   */
  private boolean m_noCommandGeneCloning;

  /**
   * Constructor utilizing the FitnessProportionateSelection.
   *
   * @throws InvalidConfigurationException
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public GPConfiguration()
      throws InvalidConfigurationException {
    this("", null);
  }

  public GPConfiguration(String a_id, String a_name)
      throws InvalidConfigurationException {
    super(a_id, a_name);
    init(true);
    m_selectionMethod = new TournamentSelector(3);
  }

  /**
   * Constructs a configuration with an informative name but without a unique
   * ID. This practically prevents more than one configurations to be
   * instantiated within the same thread.
   *
   * @param a_name informative name of the configuration, may be null
   * @throws InvalidConfigurationException
   *
   * @author Klaus Meffert
   */
  public GPConfiguration(final String a_name)
      throws InvalidConfigurationException {
    this();
    setName(a_name);
  }

  /**
   * Sets a GP fitness evaluator, such as
   * org.jgap.gp.impl.DefaultGPFitnessEvaluator.
   *
   * @param a_evaluator the fitness evaluator to set
   *
   * @author Klaus Meffert
   * @since 3.1
   */
  public void setGPFitnessEvaluator(IGPFitnessEvaluator a_evaluator) {
    m_fitnessEvaluator = a_evaluator;
  }

  /**
   * Helper for construction.
   *
   * @param a_fullInit true set event manager, random generator and fitness
   * evaluator to defauklt
   * @throws InvalidConfigurationException
   *
   * @author Klaus Meffert
   * @since 3.1
   */
  protected void init(boolean a_fullInit)
      throws InvalidConfigurationException {
    /**@todo make reusable in class Configuration and reuse from Configuration*/
    // Create factory for being able to configure the used default objects,
    // like random generators or fitness evaluators.
    // --------------------------------------------------------------------
    String clazz = System.getProperty(PROPERTY_JGAPFACTORY_CLASS);
    if (clazz != null && clazz.length() > 0) {
      try {
        m_factory = (IJGAPFactory) Class.forName(clazz).newInstance();
      } catch (Throwable ex) {
        throw new RuntimeException("Class " + clazz +
                                   " could not be instantiated" +
                                   " as type IJGAPFactory");
      }
    }
    else {
      m_factory = new JGAPFactory(false);
    }
    if (m_factory == null) {
      throw new IllegalStateException("JGAPFactory not registered!");
    }
    m_programCache = new HashMap(50);
    if (a_fullInit) {
      m_variables = new Hashtable();
      m_crossMethod = new BranchTypingCross(this);
      setEventManager(new EventManager());
      setRandomGenerator(new StockRandomGenerator());
      setGPFitnessEvaluator(new DefaultGPFitnessEvaluator());
    }
  }

  /**
   * Constructor utilizing the FitnessProportionateSelection.
   *
   * @param a_selectionMethod the selection method to use
   * @throws InvalidConfigurationException
   *
   * @author Klaus Meffert
   * @since 3.1
   */
  public GPConfiguration(INaturalGPSelector a_selectionMethod)
      throws InvalidConfigurationException {
    super();
    init(true);
    m_selectionMethod = a_selectionMethod;
  }

  /**
   * Sets the selection method to use.
   * @param a_method the selection method to use
   *
   * @author Klaus Meffert
   * @since 3.1
   */
  public void setSelectionMethod(INaturalGPSelector a_method) {
    if (a_method == null) {
      throw new IllegalArgumentException("Selection method must not be null");
    }
    m_selectionMethod = a_method;
  }

  /**
   * Sets the crossover method to use.
   * @param a_method the crossover method to use
   *
   * @author Klaus Meffert
   * @since 3.1
   */
  public void setCrossoverMethod(CrossMethod a_method) {
    if (a_method == null) {
      throw new IllegalArgumentException("Crossover method must not be null");
    }
    m_crossMethod = a_method;
  }

  public synchronized void verifyStateIsValid()
      throws InvalidConfigurationException {
    // Do nothing in here.
    // -------------------
  }

  public synchronized void addGeneticOperator(GeneticOperator a_operatorToAdd)
      throws InvalidConfigurationException {
    throw new UnsupportedOperationException(
        "Use addGeneticOperator(GPGeneticOperator) instead!");
  }

//  /**@todo implement something like that*/
//  public synchronized void addGeneticOperator(IGPGeneticOperator a_operatorToAdd)
//      throws InvalidConfigurationException {
//  }
  public double getCrossoverProb() {
    return m_crossoverProb;
  }

  public void setCrossoverProb(float a_crossoverProb) {
    m_crossoverProb = a_crossoverProb;
  }

  public double getReproductionProb() {
    return m_reproductionProb;
  }

  public void setReproductionProb(float a_reproductionProb) {
    m_reproductionProb = a_reproductionProb;
  }

  /**
   * @return probability for mutation of a node during growing a program
   *
   * @author Klaus Meffert
   * @since 3.3.1
   */
  public double getMutationProb() {
    return m_mutationProb;
  }

  /**
   * @param a_mutationProb probability for mutation of a node during growing a
   * program
   *
   * @author Klaus Meffert
   * @since 3.3.1
   */
  public void setMutationProb(float a_mutationProb) {
    m_mutationProb = a_mutationProb;
  }

  /**
   * @return probability for dynamizing the arity of a node during growing a
   * program
   *
   * @author Klaus Meffert
   * @since 3.4
   */
  public double getDynamizeArityProb() {
    return m_dynArityProb;
  }

  /**
   * @param a_dynArityProb probability for dynamizing the arity of a node during
   * growing a program

⌨️ 快捷键说明

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