Ejemplo n.º 1
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());
  }