示例#1
0
文件: OneMax.java 项目: DieGreco/GA
  /**
   * Evaluates a solution
   *
   * @param solution The solution to evaluate
   */
  public void evaluate(Solution solution) {
    Binary variable;
    int counter;

    variable = ((Binary) solution.getDecisionVariables()[0]);

    counter = 0;

    for (int i = 0; i < variable.getNumberOfBits(); i++)
      if (variable.bits_.get(i) == true) counter++;

    // OneMax is a maximization problem: multiply by -1 to minimize
    solution.setObjective(0, -1.0 * counter);
  } // evaluate
示例#2
0
  /**
   * Evaluates a solution.
   *
   * @param solution The solution to evaluate.
   * @throws JMException
   */
  public void evaluate(Solution solution) throws JMException {
    Variable[] decisionVariables = solution.getDecisionVariables();

    double[] x = new double[numberOfVariables_];
    for (int i = 0; i < numberOfVariables_; i++) x[i] = decisionVariables[i].getValue();

    int count1, count2, count3;
    double sum1, sum2, sum3, yj;
    sum1 = sum2 = sum3 = 0.0;
    count1 = count2 = count3 = 0;

    for (int j = 3; j <= numberOfVariables_; j++) {
      yj =
          x[j - 1] - 2.0 * x[1] * Math.sin(2.0 * Math.PI * x[0] + j * Math.PI / numberOfVariables_);
      if (j % 3 == 1) {
        sum1 += yj * yj;
        count1++;
      } else if (j % 3 == 2) {
        sum2 += yj * yj;
        count2++;
      } else {
        sum3 += yj * yj;
        count3++;
      }
    }

    solution.setObjective(
        0,
        Math.cos(0.5 * Math.PI * x[0]) * Math.cos(0.5 * Math.PI * x[1])
            + 2.0 * sum1 / (double) count1);
    solution.setObjective(
        1,
        Math.cos(0.5 * Math.PI * x[0]) * Math.sin(0.5 * Math.PI * x[1])
            + 2.0 * sum2 / (double) count2);
    solution.setObjective(2, Math.sin(0.5 * Math.PI * x[0]) + 2.0 * sum3 / (double) count3);
  } // evaluate