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

📄 chromosomefitnesscomparator.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.util;

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

/**
 * Simple comparator to allow the sorting of Chromosome lists with the highest
 * fitness value in first place of the list.
 * Usage example:
 *   Collections.sort(
 *     population.getChromosomes(), new ChromosomeFitnessComparator() );
 *
 * @author Charles Kevin Hill, Klaus Meffert
 * @since 2.4
 */
public class ChromosomeFitnessComparator
    implements Comparator {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.8 $";

  private FitnessEvaluator m_fitnessEvaluator;

  /**
   * Constructs the comparator using the DefaultFitnessEvaluator
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public ChromosomeFitnessComparator() {
    this(new DefaultFitnessEvaluator());
  }

  /**
   * @param a_evaluator the fitness evaluator to use
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public ChromosomeFitnessComparator(FitnessEvaluator a_evaluator) {
    if (a_evaluator == null) {
      throw new IllegalArgumentException("Evaluator must not be null");
    }
    m_fitnessEvaluator = a_evaluator;
  }

  /**
   * Compares two chromosomes by using a FitnessEvaluator.
   *
   * @param a_chromosome1 the first chromosome to compare
   * @param a_chromosome2 the second chromosome to compare
   * @return -1 if a_chromosome1 is fitter than a_chromosome2, 1 if it is the
   * other way round and 0 if both are equal
   * @author Charles Kevin Hill, Klaus Meffert
   * @since 2.6
   */
  public int compare(final Object a_chromosome1, final Object a_chromosome2) {
    IChromosome chromosomeOne = (IChromosome) a_chromosome1;
    IChromosome chromosomeTwo = (IChromosome) a_chromosome2;
    if (m_fitnessEvaluator.isFitter(chromosomeOne, chromosomeTwo)) {
      return -1;
    }
    else if (m_fitnessEvaluator.isFitter(chromosomeTwo, chromosomeOne)) {
      return 1;
    }
    else {
      return 0;
    }
  }
}

⌨️ 快捷键说明

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