@Test
  public void testPoint() {
    ValuePointColored p1 = ValuePointColored.at(Point.at(10.0), 1.0, true);
    ValuePointColored p2 = ValuePointColored.at(Point.at(10.0), 1.0, true);

    ValuePointColored p3 = ValuePointColored.at(Point.at(13.0), 1.0, true);
    ValuePointColored p4 = ValuePointColored.at(Point.at(10.0), 2.0, false);

    assertTrue(p1.equals(p2));
    assertEquals(p1.hashCode(), p2.hashCode());

    assertFalse(p1.equals(p3));
    assertNotEquals(p3.hashCode(), p2.hashCode());

    assertFalse(p1.equals(p4));
    assertNotEquals(p4.hashCode(), p2.hashCode());

    assertTrue(p1.toString().contains("1.0@"));

    assertEquals(1, p1.getValue(), 0.000001);
    assertEquals(Point.at(10.0), p1.getPoint());

    assertTrue(p1.getBest());
    assertFalse(p4.getBest());

    assertTrue(p4.compareTo(ValuePoint.at(Point.at(13.0), 1.0)) > 0);
    assertTrue(p3.compareTo(ValuePoint.at(Point.at(10.0), 2.0)) < 0);
    assertTrue(
        ValuePointColored.at(Point.at(19.0), 2.0, true)
                .compareTo(ValuePoint.at(Point.at(10.0), 2.0))
            == 0);
  }
Ejemplo n.º 2
0
  public void init(ObjectiveFunction function) {

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

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

    xNew = new double[dimension];
    xDelta = new double[dimension];
    y = new double[dimension];
    H = new double[dimension][dimension];

    Hy = new double[dimension];
    xH = new double[dimension];

    direction = new double[dimension];

    switch (lineSearch) {
      case BRENT_WITHOUT:
        this.lineSearchMethod = new LineSearchBrentNoDerivatives(function);
        break;
      case BRENT_WITH:
        this.lineSearchMethod = new LineSearchBrentWithDerivatives(function);
        break;
      default:
        throw new IllegalStateException("Unknown line search method");
    }

    x = Point.random(dimension, min, max).toArray();
    solution = ValuePoint.at(Point.at(x), function);
    this.stopCondition.setInitialValue(solution.getValue());

    g = function.gradientAt(Point.at(x)).toArray();

    for (int i = 0; i < dimension; i++) {
      direction[i] = -g[i];
      H[i][i] = 1.0;
      y[i] = -1;
    }
  }
Ejemplo n.º 3
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());
  }
Ejemplo n.º 4
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;
    }
  }
Ejemplo n.º 5
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());
  }
Ejemplo n.º 6
0
 @Override
 public String toString() {
   return valuePoint.toString();
 }
Ejemplo n.º 7
0
 public ValuePointTelemetry() {
   valuePoint = ValuePoint.getDefault();
 }