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

📄 permutingconfiguration.java

📁 java实现的遗传算法
💻 JAVA
字号:
/*
 * This file is part of JGAP.
 *
 * JGAP offers a dual license model containing the LGPL as well as the MPL.
 *
 * For licencing 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.audit;

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

/**
 * Configuration that allows for permutating several components of it for
 * evaluation/auditing purposes.
 *
 * @author Klaus Meffert
 * @since 2.2
 */
public class PermutingConfiguration
    extends Configuration {

  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.2 $";

  private List m_randomGeneratorSlots;

  private int m_randomGeneratorIndex;

  private List m_naturalSelectorSlots;

  private int m_naturalSelectorIndex;

  private List m_geneticOperatorSlots;

  private int m_geneticOperatorIndex;

  private List m_fitnessFunctionSlots;

  private int m_fitnessFunctionIndex;

  private int m_componentIndex;

  /**
   * The resulting configuration as determined by permutation.
   */
  private Configuration m_configuration;

  public PermutingConfiguration() {
    super();
    init();
  }

  public void init() {
    m_randomGeneratorSlots = new Vector();
    m_naturalSelectorSlots = new Vector();
    m_geneticOperatorSlots = new Vector();
    m_fitnessFunctionSlots = new Vector();
    m_randomGeneratorIndex = 0;
    m_naturalSelectorIndex = 0;
    m_geneticOperatorIndex = 0;
    m_fitnessFunctionIndex = 0;
    m_componentIndex = 0;
  }

  /**
   * Initializes the configuration by preselecting important parameters from
   * the input configuration object
   * @param a_conf Configuration
   * @throws InvalidConfigurationException
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public PermutingConfiguration(Configuration a_conf)
      throws InvalidConfigurationException {
    this();
    setEventManager(a_conf.getEventManager());
    setFitnessEvaluator(a_conf.getFitnessEvaluator());
    if (a_conf.getFitnessFunction() != null) {
      setFitnessFunction(a_conf.getFitnessFunction());
    }
    setMinimumPopSizePercent(a_conf.getMinimumPopSizePercent());
    if (a_conf.getPopulationSize() > 0) {
      setPopulationSize(a_conf.getPopulationSize());
    }
    if (a_conf.getSampleChromosome() != null) {
      setSampleChromosome(a_conf.getSampleChromosome());
    }
    if(a_conf.getRandomGenerator() != null) {
      setRandomGenerator(a_conf.getRandomGenerator());
    }
    if (a_conf.getChromosomePool() != null) {
      setChromosomePool(a_conf.getChromosomePool());
    }
  }

  public void addRandomGeneratorSlot(RandomGenerator a_randomGenerator) {
    m_randomGeneratorSlots.add(a_randomGenerator);
  }

  public void addNaturalSelector(NaturalSelector a_naturalSelector, boolean egal) {
    throw new UnsupportedOperationException("Use addNaturalSelectorSlot instead!");
  }

  public void addNaturalSelectorSlot(NaturalSelector a_naturalSelector) {
    m_naturalSelectorSlots.add(a_naturalSelector);
  }

  public void addGeneticOperator(GeneticOperator a_geneticOperator) {
    throw new UnsupportedOperationException("Use addGeneticOperatorSlot instead!");

  }
  public void addGeneticOperatorSlot(GeneticOperator a_geneticOperator) {
    m_geneticOperatorSlots.add(a_geneticOperator);
  }

  public void addFitnessFunctionSlot(FitnessFunction a_fitnessFunction) {
    m_fitnessFunctionSlots.add(a_fitnessFunction);
  }

  public Configuration next()
      throws InvalidConfigurationException {
    m_configuration = new Configuration();
    m_configuration.setEventManager(getEventManager());
    m_configuration.setFitnessEvaluator(getFitnessEvaluator());
    if (getFitnessFunction() != null) {
      setFitnessFunction(getFitnessFunction());
    }
    m_configuration.setMinimumPopSizePercent(getMinimumPopSizePercent());

    if (getPopulationSize() > 0) {
      m_configuration.setPopulationSize(getPopulationSize());
    }
    if (getSampleChromosome() != null) {
      m_configuration.setSampleChromosome(getSampleChromosome());
    }
    if(getRandomGenerator() != null) {
      m_configuration.setRandomGenerator(getRandomGenerator());
    }
    if (getChromosomePool() != null) {
      m_configuration.setChromosomePool(getChromosomePool());
    }

    List list;
    Iterator it;
    /**@todo make permutation below computed dynamically and not statically*/
    /**@todo introduce new parameters: populationSize, setPreservFittestIndividual,
     * MAX_ALLOWED_EVOLUTIONS
     */

    // Permute GeneticOperator's.
    // --------------------------
    if (m_geneticOperatorIndex >= Math.pow(2, m_geneticOperatorSlots.size())-1) {
//      m_componentIndex++;
//    }
//    if (bitSet(m_componentIndex, 0)) {
      m_geneticOperatorIndex = 0;
      m_naturalSelectorIndex++;
//      m_randomGeneratorIndex = 0;
//      m_fitnessFunctionIndex = 0;
    }
//    System.err.print(m_geneticOperatorIndex+" ");
    list = nextList(m_geneticOperatorIndex++, m_geneticOperatorSlots);
    it = list.iterator();
    GeneticOperator op;
    while (it.hasNext()) {
      op = (GeneticOperator) it.next();
      m_configuration.addGeneticOperator(op);
    }

    // Permute NaturalSelector's.
    // --------------------------
    if (m_naturalSelectorIndex >= Math.pow(2, m_naturalSelectorSlots.size())-1) {
//      m_componentIndex++;
//    }
//    if (bitSet(m_componentIndex, 1)) {
      m_naturalSelectorIndex = 0;
      m_randomGeneratorIndex++;
//      m_fitnessFunctionIndex = 0;
    }
//    System.err.print(m_naturalSelectorIndex+" ");
    list = nextList(m_naturalSelectorIndex, m_naturalSelectorSlots);
    it = list.iterator();
    NaturalSelector ns;
    while (it.hasNext()) {
      ns = (NaturalSelector) it.next();
      m_configuration.addNaturalSelector(ns, true);
          /**@todo allow for "false"*/
    }

    // Permute RandomGenerator's.
    // --------------------------
    if (true || bitSet(m_componentIndex, 2)) {
      m_randomGeneratorIndex++;
      if (m_randomGeneratorIndex >= m_randomGeneratorSlots.size()) {
        m_randomGeneratorIndex = 0;
        m_fitnessFunctionIndex++;
      }
    }
//    System.err.print(m_randomGeneratorIndex+" ");
    RandomGenerator rg = (RandomGenerator) m_randomGeneratorSlots.get(
        m_randomGeneratorIndex);
    m_configuration.setRandomGenerator(rg);

    // Permute FitnessFunction's.
    // --------------------------
    if (true || bitSet(m_componentIndex, 3)) {
      m_fitnessFunctionIndex++;
      if (m_fitnessFunctionIndex >= m_fitnessFunctionSlots.size()) {
        m_fitnessFunctionIndex = 0;
      }
    }

    /**@todo BulkFitnessOffsetRemover vs. FitnessFunction*/

//    System.err.println(m_fitnessFunctionIndex+" / "+index++);
    FitnessFunction ff = (FitnessFunction) m_fitnessFunctionSlots.get(
        m_fitnessFunctionIndex);
    m_configuration.setFitnessFunction(ff);

    m_componentIndex++;

    return m_configuration;
  }

  /**
   * Returns a subset of a given list acording to the index given.
   * If a bit in the binary number represented by the index is set then the
   * element at this index in the list will be included in the result list
   * @param index int
   * @param list List
   * @return List
   */
  private List nextList(int index, List list) {
    if (index <= 0) {
      index = 1;
    }
    else {
      index++;
    }
    List newList = new Vector();
    for (int i = 0; i < list.size(); i++) {
      if ( (index & (int) Math.pow(2, i)) > 0) {
        newList.add(list.get(i));
      }
    }

    return newList;
  }

  private boolean bitSet(int number, int bitIndex) {
    return ( (number & (int) Math.pow(2, (bitIndex))) > 0);
  }

  public boolean hasNext() {
    double r = (m_randomGeneratorSlots.size())
        *(m_fitnessFunctionSlots.size())
        *(Math.pow(2,m_naturalSelectorSlots.size())-1)
        *(Math.pow(2,m_geneticOperatorSlots.size())-1);
    return m_componentIndex < r;
  }
}

⌨️ 快捷键说明

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