Esempio n. 1
0
  public void init(ObjectiveFunction function) {

    generator = new Random();

    this.function = function;
    dimension = function.getDimension();

    min = Math.max(min, function.getMinimum()[0]);
    max = Math.min(max, function.getMaximum()[0]);

    means = new double[dimension];
    deviations = new double[dimension];
    paths = new ValuePoint[populationSize];

    best = ValuePoint.at(Point.getDefault(), Double.POSITIVE_INFINITY);
    stopCondition.setInitialValue(Double.POSITIVE_INFINITY);

    for (int i = 0; i < dimension; i++) {
      means[i] = (generator.nextDouble() * (max - min)) + min;
      deviations[i] = (max - min) / 2;
    }
  }
Esempio n. 2
0
  public void optimize() {

    for (int ant = 0; ant < populationSize; ant++) {

      // generate solution
      double[] newPoint = new double[dimension];
      for (int d = 0; d < dimension; d++)
        newPoint[d] = (generator.nextGaussian() * deviations[d]) + means[d];

      // improve solution using gradient
      if (gradientWeight != 0.0) {
        double[] gradient = function.gradientAt(Point.at(newPoint)).toArray();
        for (int d = 0; d < dimension; d++) newPoint[d] -= gradientWeight * gradient[d];
      }

      // get solution error (& update best solution
      double error = function.valueAt(Point.at(newPoint));
      paths[ant] = ValuePoint.at(Point.at(newPoint), error);

      if (error < best.getValue()) best = paths[ant]; // ValuePoint.at(Point.at(newPoint), error);
    }

    // update pheromone
    double[] bestVect = best.getPoint().toArray();
    for (int i = 0; i < dimension; i++) {
      deviations[i] =
          (1 - evaporationFactor) * deviations[i]
              + evaporationFactor * Math.abs(bestVect[i] - means[i]);
      means[i] = (1 - evaporationFactor) * means[i] + evaporationFactor * bestVect[i];
    }

    telemetry = new ValuePointListTelemetry(Arrays.asList(paths));
    if (consumer != null) consumer.notifyOf(this);

    stopCondition.setValue(best.getValue());
  }
Esempio n. 3
0
  public void optimize() {

    System.arraycopy(x, 0, xNew, 0, dimension);
    lineSearchMethod.minimize(xNew, direction);

    for (int i = 0; i < dimension; i++) xDelta[i] = xNew[i] - x[i];

    System.arraycopy(xNew, 0, x, 0, dimension);
    System.arraycopy(g, 0, y, 0, dimension);
    g = function.gradientAt(Point.at(x)).toArray();

    for (int i = 0; i < dimension; i++) y[i] = g[i] - y[i];

    for (int i = 0; i < dimension; i++) {
      Hy[i] = 0.0;
      for (int j = 0; j < dimension; j++) Hy[i] += H[i][j] * y[j];
    }

    fac = 0.0;
    fae = 0.0;
    sumY = 0.0;
    sumXDelta = 0.0;

    for (int i = 0; i < dimension; i++) {
      fac += y[i] * xDelta[i];
      fae += y[i] * Hy[i];
      sumY += y[i] * y[i];
      sumXDelta += xDelta[i] * xDelta[i];
    }

    if (fac > Math.sqrt(MachineAccuracy.EPSILON * sumY * sumXDelta)) {
      fac = 1.0 / fac;
      fad = 1.0 / fae;

      switch (updateMethod) {
        case DFP:
          for (int i = 0; i < dimension; i++)
            for (int j = i; j < dimension; j++) {
              H[i][j] += fac * xDelta[i] * xDelta[j] - fad * Hy[i] * Hy[j];
              H[j][i] = H[i][j];
            }
          break;

        case BFGS:
          for (int i = 0; i < dimension; i++) y[i] = fac * xDelta[i] - fad * Hy[i];

          for (int i = 0; i < dimension; i++)
            for (int j = i; j < dimension; j++) {
              H[i][j] += fac * xDelta[i] * xDelta[j] - fad * Hy[i] * Hy[j] + fae * y[i] * y[j];
              H[j][i] = H[i][j];
            }
          break;

        case BROYDEN_FAMILY:
          for (int i = 0; i < dimension; i++) y[i] = fac * xDelta[i] - fad * Hy[i];

          for (int i = 0; i < dimension; i++)
            for (int j = i; j < dimension; j++) {
              H[i][j] +=
                  fac * xDelta[i] * xDelta[j] - fad * Hy[i] * Hy[j] + phi * (fae * y[i] * y[j]);
              H[j][i] = H[i][j];
            }
          break;

        case BROYDEN:
          fae = 0.0;

          for (int i = 0; i < dimension; i++) {
            xH[i] = 0.0;

            for (int j = 0; j < dimension; j++) xH[i] += xDelta[j] * H[j][i];

            fae += xDelta[i] * Hy[i];
          }

          fae = 1.0 / fae;

          for (int i = 0; i < dimension; i++) {
            y[i] = xDelta[i] - Hy[i];
            for (int j = i; j < dimension; j++) {
              H[i][j] += fae * y[i] * xH[j];
              H[j][i] = H[i][j];
            }
          }
          break;
      }
    }

    for (int i = 0; i < dimension; i++) {
      direction[i] = 0.0;
      for (int j = 0; j < dimension; j++) direction[i] -= H[i][j] * g[j];
    }

    solution = ValuePoint.at(Point.at(x), function);

    telemetry = new ValuePointTelemetry(solution);
    if (consumer != null) consumer.notifyOf(this);

    stopCondition.setValue(solution.getValue());
  }