Exemplo n.º 1
0
  public double valueAt(Point point) {
    double[] ax = point.toArray();

    double sum1 = 0.0;
    double sum2 = 0.0;

    for (int i = 0; i < n; i++) {
      sum1 += ax[i] * ax[i];
      sum2 += Math.cos(2 * Math.PI * ax[i]);
    }

    return 20 + Math.exp(1) - 20 * Math.exp(-0.2 * Math.sqrt(sum1 / n)) - Math.exp(sum2 / n);
  }
  @Override
  protected void reproduceInternal(Individual[] firstChildren, Individual[] secondChildren) {
    // make crossover
    for (int i = 0; i < firstChildren.length; i++) {
      double[] firstValues = firstChildren[i].getRepresentation().getDoubleValue();
      double[] secondValues = secondChildren[i].getRepresentation().getDoubleValue();

      double parentFitness =
          Math.max(firstChildren[i].getFitness(), secondChildren[i].getFitness());
      firstChildren[i].setParentFitness(parentFitness);
      secondChildren[i].setParentFitness(parentFitness);

      double tmp;
      boolean[] cutPointFlags =
          this.makeCutPointFlags(this.crossPointCount, this.function.getDimension());
      for (int j = 0; j < cutPointFlags.length; j++) {
        if (!cutPointFlags[j]) {
          tmp = firstValues[j];
          firstValues[j] = secondValues[j];
          secondValues[j] = tmp;
        }
      }
      // finally set the new values to the representation
      firstChildren[i].getRepresentation().setDoubleValue(firstValues);
      secondChildren[i].getRepresentation().setDoubleValue(secondValues);
    }
  }
Exemplo n.º 3
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;
    }
  }
Exemplo n.º 4
0
  public Gradient gradientAt(Point point) {
    double[] ax = point.toArray();
    double[] gradient = new double[n];

    double sqrtSum = 0.0;
    double cosSum = 0.0;
    for (int i = 0; i < n; i++) {
      sqrtSum += ax[i] * ax[i];
      cosSum += Math.cos(2 * Math.PI * ax[i]);
    }

    sqrtSum = Math.sqrt(sqrtSum);
    cosSum /= n;

    for (int i = 0; i < n; i++)
      gradient[i] =
          (2 * ax[i] * Math.exp(-0.1 * sqrtSum)) / sqrtSum
              + 0.5 * Math.exp(cosSum) * Math.PI * Math.sin(2 * Math.PI * ax[i]);

    return Gradient.valueOf(gradient);
  }
Exemplo n.º 5
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;
    }
  }
Exemplo n.º 6
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());
  }
Exemplo n.º 7
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());
  }
Exemplo n.º 8
0
  public void init(final Function function) {
    xAxis.clear();
    yAxis.clear();

    if (function != lastFunction) { // need to reset
      Slot.CIRCLE.clearList();
      Slot.CROSS.clearList();
      Slot.SQUARE.clearList();
      Slot.TRIANGLE.clearList();
    }

    data = new GridPointData(resolution, resolution, 1);
    plot = new InterpolatedPlot(data);

    useSlot.clearList();
    final List<List<ValuePointColored>> points = useSlot.getPoints();
    for (Slot slot : Slot.values()) {
      if (slot.isShow()) {
        for (List<ValuePointColored> valuePoints : slot.getPoints()) {
          for (ValuePointColored valuePoint : valuePoints) {
            xAxis.add(valuePoint);
            yAxis.add(valuePoint);
          }
        }
      }
    }

    // clean drawing panel
    drawablePanel.removeDrawables(InterpolatedPlot.class);
    drawablePanel.addDrawable(plot);

    datasetRecreation =
        new Closure() {

          public void execute(Object input) {
            List<ValuePointColored> vps = (List<ValuePointColored>) input;
            points.add(vps);
            for (ValuePointColored vp : vps) {
              xAxis.add(vp);
              yAxis.add(vp);
            }

            if (points.size() > maxPoints) {
              List<ValuePointColored> removed = points.remove(0);
              for (ValuePointColored valuePoint : removed) {
                xAxis.remove(valuePoint);
                yAxis.remove(valuePoint);
              }
            }

            // for ilustration
            if (xAxis.size() > 0 && yAxis.size() > 0) {
              double lowerXBound =
                  Math.min(
                      data.getLeft(), Math.floor(xAxis.first().getPoint().toArray()[0] - buffer));
              double upperXBound =
                  Math.max(
                      data.getRight(), Math.ceil(xAxis.last().getPoint().toArray()[0] + buffer));
              double lowerYBound =
                  Math.min(
                      data.getBottom(), Math.floor(yAxis.first().getPoint().toArray()[1] - buffer));
              double upperYBound =
                  Math.max(data.getTop(), Math.ceil(yAxis.last().getPoint().toArray()[1] + buffer));

              data.setScale(lowerXBound, upperXBound, lowerYBound, upperYBound);
              double[][][] xyz = data.getData();

              for (int i = 0; i < resolution; i++) {
                for (int j = 0; j < resolution; j++) {
                  Point p = Point.at(xyz[i][j]);
                  // check the cached value of the point
                  xyz[i][j][2] = /*cache.containsKey(p) ? cache.get(p) :*/ function.valueAt(p);
                }
              }
            }

            plot.setGridData(data);

            drawablePanel.repaint();
          }
        };

    lastFunction = function;

    // initialize to center point
    double lowerXBound = Math.floor(centerX - buffer);
    double upperXBound = Math.ceil(centerX + buffer);
    double lowerYBound = Math.floor(centerY - buffer);
    double upperYBound = Math.ceil(centerY + buffer);

    data.setScale(lowerXBound, upperXBound, lowerYBound, upperYBound);
    double[][][] xyz = data.getData();

    for (int i = 0; i < resolution; i++) {
      for (int j = 0; j < resolution; j++) {
        Point p = Point.at(xyz[i][j]);
        // check the cached value of the point
        xyz[i][j][2] = /*cache.containsKey(p) ? cache.get(p) :*/ function.valueAt(p);
      }
    }

    plot.setGridData(data);
    drawablePanel.repaint();
  }