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

📄 multiobjectivefitnessfunction.java

📁 jgap3.2 遗传算法工具包,嘿嘿,笨鸟先飞哦
💻 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 examples.multiobjective;

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

/**
 * Sample fitness function for the multiobjective problem.
 *
 * @author Klaus Meffert
 * @since 2.6
 */
public class MultiObjectiveFitnessFunction
    extends BulkFitnessFunction {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.2 $";

  public static final int MAX_BOUND = 4000;

  public static final double MIN_X = -10;

  public static final double MAX_X = 10;

  /**
   * Determine the fitness of the given Chromosome instance. The higher the
   * returned value, the fitter the instance. This method should always
   * return the same fitness value for two equivalent Chromosome instances.
   *
   * @param a_subject the population of chromosomes to evaluate
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void evaluate(Population a_subject) {
    Iterator it = a_subject.getChromosomes().iterator();
    while (it.hasNext()) {
      IChromosome a_chrom1 = (IChromosome) it.next();
      // evaluate values to fill vector of multiobjectives with
      DoubleGene g1 = (DoubleGene) a_chrom1.getGene(0);
      double d = g1.doubleValue();
      double y1 = formula(1, d);
      List l = new Vector();
      l.add(new Double(y1));
      double y2 = formula(2, d);
      l.add(new Double(y2));
      ( (Chromosome) a_chrom1).setMultiObjectives(l);
    }
  }

  public static Vector getVector(IChromosome a_chrom) {
    Vector result = new Vector();
//    Gene g1 = a_chrom.getGene(0);
//    result.add(g1);
//    Gene g2 = a_chrom.getGene(1);
//    result.add(g2);
    List mo = ( (Chromosome) a_chrom).getMultiObjectives();
    Double d = (Double) mo.get(0);
    result.add(d);
    d = (Double) mo.get(1);
    result.add(d);
    return result;
  }

  private double formula(int a_index, double a_x) {
    if (a_index == 1) {
      return a_x * a_x;
    }
    else {
      return (a_x - 2) * (a_x - 2);
    }
  }

  /**
   * @return deep clone of the current instance
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public Object clone() {
    return new MultiObjectiveFitnessFunction();
  }
}

⌨️ 快捷键说明

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