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

📄 crossoveroperator.java

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

import java.util.*;
import org.jgap.*;

/**
 * The crossover operator randomly selects two Chromosomes from the
 * population and "mates" them by randomly picking a gene and then
 * swapping that gene and all subsequent genes between the two
 * Chromosomes. The two modified Chromosomes are then added to the
 * list of candidate Chromosomes.
 *
 * If you work with CompositeGene's, this operator expects them to contain
 * genes of the same type (e.g. IntegerGene). If you have mixed types, please
 * provide your own crossover operator.
 *
 * This CrossoverOperator supports both fixed and dynamic crossover rates.
 * A fixed rate is one specified at construction time by the user. This
 * operation is performed 1/m_crossoverRate as many times as there are
 * Chromosomes in the population. Another possibility is giving the crossover
 * rate as a percentage. A dynamic rate is one determined by this class on the
 * fly if no fixed rate is provided.
 *
 * @author Neil Rotstan
 * @author Klaus Meffert
 * @author Chris Knowles
 * @since 1.0
 */
public class CrossoverOperator
    extends BaseGeneticOperator implements Comparable {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.44 $";

  /**
   * The current crossover rate used by this crossover operator (mutual
   * exclusive to m_crossoverRatePercent and m_crossoverRateCalc).
   */
  private int m_crossoverRate;

  /**
   * Crossover rate in percentage of population size (mutual exclusive to
   * m_crossoverRate and m_crossoverRateCalc).
   */
  private double m_crossoverRatePercent;

  /**
   * Calculator for dynamically determining the crossover rate (mutual exclusive
   * to m_crossoverRate and m_crossoverRatePercent)
   */
  private IUniversalRateCalculator m_crossoverRateCalc;

  /**
   * true: x-over before and after a randomly chosen x-over point
   * false: only x-over after the chosen point.
   */
  private boolean m_allowFullCrossOver;

  /**
   * true: also x-over chromosomes with age of zero (newly created chromosomes)
   */
  private boolean m_xoverNewAge;

  /**
   * Constructs a new instance of this CrossoverOperator without a specified
   * crossover rate, this results in dynamic crossover rate being turned off.
   * This means that the crossover rate will be fixed at populationsize/2.<p>
   * Attention: The configuration used is the one set with the static method
   * Genotype.setConfiguration.
   *
   * @throws InvalidConfigurationException
   *
   * @author Chris Knowles
   * @since 2.0
   */
  public CrossoverOperator()
      throws InvalidConfigurationException {
    super(Genotype.getStaticConfiguration());
    init();
  }

  /**
   * Constructs a new instance of this CrossoverOperator without a specified
   * crossover rate, this results in dynamic crossover rate being turned off.
   * This means that the crossover rate will be fixed at populationsize/2.
   *
   * @param a_configuration the configuration to use
   * @throws InvalidConfigurationException
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public CrossoverOperator(final Configuration a_configuration)
      throws InvalidConfigurationException {
    super(a_configuration);
    init();
  }

  /**
   * Initializes certain parameters.
   *
   * @author Klaus Meffert
   * @since 3.3.2
   */
  protected void init() {
    // Set the default crossoverRate.
    // ------------------------------
    m_crossoverRate = 6;
    m_crossoverRatePercent = -1;
    setCrossoverRateCalc(null);
    setAllowFullCrossOver(true);
    setXoverNewAge(true);
  }

  /**
   * Constructs a new instance of this CrossoverOperator with a specified
   * crossover rate calculator, which results in dynamic crossover being turned
   * on.
   *
   * @param a_configuration the configuration to use
   * @param a_crossoverRateCalculator calculator for dynamic crossover rate
   * computation
   * @throws InvalidConfigurationException
   *
   * @author Chris Knowles
   * @author Klaus Meffert
   * @since 3.0 (since 2.0 without a_configuration)
   */
  public CrossoverOperator(final Configuration a_configuration,
                           final IUniversalRateCalculator
                           a_crossoverRateCalculator)
      throws InvalidConfigurationException {
    this(a_configuration, a_crossoverRateCalculator, true);
  }

  /**
   * Constructs a new instance of this CrossoverOperator with a specified
   * crossover rate calculator, which results in dynamic crossover being turned
   * on.
   *
   * @param a_configuration the configuration to use
   * @param a_crossoverRateCalculator calculator for dynamic crossover rate
   * computation
   * @param a_allowFullCrossOver true: x-over before AND after x-over point,
   * false: only x-over after x-over point
   * @throws InvalidConfigurationException
   *
   * @author Klaus Meffert
   * @since 3.3.2
   */
  public CrossoverOperator(final Configuration a_configuration,
                           final IUniversalRateCalculator
                           a_crossoverRateCalculator,
                           boolean a_allowFullCrossOver)
      throws InvalidConfigurationException {
    super(a_configuration);
    setCrossoverRateCalc(a_crossoverRateCalculator);
    setAllowFullCrossOver(a_allowFullCrossOver);
  }

  /**
   * Constructs a new instance of this CrossoverOperator with the given
   * crossover rate.
   *
   * @param a_configuration the configuration to use
   * @param a_desiredCrossoverRate the desired rate of crossover
   * @throws InvalidConfigurationException
   *
   * @author Chris Knowles
   * @author Klaus Meffert
   * @since 3.0 (since 2.0 without a_configuration)
   */
  public CrossoverOperator(final Configuration a_configuration,
                           final int a_desiredCrossoverRate)
      throws InvalidConfigurationException {
    this(a_configuration, a_desiredCrossoverRate, true);
  }

  /**
   * Constructs a new instance of this CrossoverOperator with the given
   * crossover rate. No new chromosomes are x-overed.
   *
   * @param a_configuration the configuration to use
   * @param a_desiredCrossoverRate the desired rate of crossover
   * @param a_allowFullCrossOver true: x-over before AND after x-over point,
   * false: only x-over after x-over point
   * @throws InvalidConfigurationException
   *
   * @author Klaus Meffert
   * @since 3.3.2
   */
  public CrossoverOperator(final Configuration a_configuration,
                           final int a_desiredCrossoverRate,
                           boolean a_allowFullCrossOver)
      throws InvalidConfigurationException {
    this(a_configuration, a_desiredCrossoverRate, a_allowFullCrossOver, false);
  }

  /**
   * Constructs a new instance of this CrossoverOperator with the given
   * crossover rate.
   *
   * @param a_configuration the configuration to use
   * @param a_desiredCrossoverRate the desired rate of crossover
   * @param a_allowFullCrossOver true: x-over before AND after x-over point,
   * false: only x-over after x-over point
   * @throws InvalidConfigurationException
   * @param a_xoverNewAge true: also x-over chromosomes with age of zero (newly
   * created chromosomes)
   *
   * @author Klaus Meffert
   * @since 3.3.2
   */
  public CrossoverOperator(final Configuration a_configuration,
                           final int a_desiredCrossoverRate,
                           final boolean a_allowFullCrossOver,
                           final boolean a_xoverNewAge)
      throws InvalidConfigurationException {
    super(a_configuration);
    if (a_desiredCrossoverRate < 1) {
      throw new IllegalArgumentException("Crossover rate must be greater zero");
    }
    m_crossoverRate = a_desiredCrossoverRate;
    m_crossoverRatePercent = -1;
    setCrossoverRateCalc(null);
    setAllowFullCrossOver(a_allowFullCrossOver);
    setXoverNewAge(a_xoverNewAge);
  }

  /**
   * Constructs a new instance of this CrossoverOperator with the given
   * crossover rate. No new chromosomes are x-overed.
   *
   * @param a_configuration the configuration to use
   * @param a_crossoverRatePercentage the desired rate of crossover in
   * percentage of the population
   * @throws InvalidConfigurationException
   *
   * @author Chris Knowles
   * @author Klaus Meffert
   * @since 3.0 (since 2.0 without a_configuration)
   */
  public CrossoverOperator(final Configuration a_configuration,
                           final double a_crossoverRatePercentage)
      throws InvalidConfigurationException {
    this(a_configuration, a_crossoverRatePercentage, true);
  }

  /**
   * Constructs a new instance of this CrossoverOperator with the given
   * crossover rate. No new chromosomes are x-overed.
   *
   * @param a_configuration the configuration to use
   * @param a_crossoverRatePercentage the desired rate of crossover in
   * percentage of the population
   * @param a_allowFullCrossOver true: x-over before AND after x-over point,
   * false: only x-over after x-over point
   * @throws InvalidConfigurationException
   *
   * @author Klaus Meffert
   * @since 3.3.2.
   */
  public CrossoverOperator(final Configuration a_configuration,
                           final double a_crossoverRatePercentage,
                           boolean a_allowFullCrossOver)
      throws InvalidConfigurationException {
    this(a_configuration, a_crossoverRatePercentage, a_allowFullCrossOver, false);
  }

  /**
   * Constructs a new instance of this CrossoverOperator with the given
   * crossover rate.
   *

⌨️ 快捷键说明

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