stevengj / nlopt

library for nonlinear optimization, wrapping many algorithms for global and local, constrained or unconstrained, optimization
Other
1.86k stars 574 forks source link

STOGO doesnt register progress #529

Open jschueller opened 1 year ago

jschueller commented 1 year ago

STOGO fails to optimize a simple quadratic problem with bound constraints:

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <iomanip>
#include <nlopt.hpp>

double myvfunc(const std::vector<double> &x, std::vector<double> &grad, void *data)
{
  (void)data;
  if (!grad.empty()) {
    grad[0] = -2.0 *x[0];
    grad[1] = -2.0 *x[1];
  }
  const double f= -(x[0]*x[0] + x[1]*x[1]);
  std::cout << "-- progress x="<<x[0]<<", "<<x[1]<<" f="<<f<<std::endl;
  return f;
}

int main(int argc, char *argv[]) {
  nlopt::algorithm algo = nlopt::GD_STOGO;
  std::cout << "algo=" <<nlopt_algorithm_to_string((nlopt_algorithm)algo)<<std::endl;

  nlopt::opt opt(algo, 2);
  std::vector<double> lb = {-10, -10};
  std::vector<double> ub = {10, 10};
  opt.set_lower_bounds(lb);
  opt.set_upper_bounds(ub);
  opt.set_min_objective(myvfunc, NULL);
  opt.set_ftol_rel(1e-4);
  opt.set_xtol_rel(1e-4);
  opt.set_maxeval(100000);

  std::vector<double> x = {0.5, 0.5};
  double minf;
  try{
    opt.optimize(x, minf);
    std::cout << "found minimum at f(" << x[0] << "," << x[1] << ") = "
              << std::setprecision(10) << minf <<std::endl;
    return std::fabs(minf - 0.5443310474) < 1e-3 ? EXIT_SUCCESS : EXIT_FAILURE;
  }
  catch(std::exception &e) {
    std::cerr << "nlopt failed: " << e.what() << std::endl;
    return EXIT_FAILURE;
  }
}

we can see it "moves" to better points but doesnt record the objective enhancements and returns x=(0,0):

...
-- progress x=-0.588479, -1.71051 f=-3.27216
-- progress x=-0.593562, -1.72529 f=-3.32893
-- progress x=-0.585938, -1.65625 f=-3.08649
-- progress x=-0.588543, -1.66362 f=-3.114
-- progress x=-0.593754, -1.67835 f=-3.16939
-- progress x=-0.604177, -1.70781 f=-3.28163
-- progress x=-0.625022, -1.76673 f=-3.51198
-- progress x=-0.609375, -1.67969 f=-3.19269
-- progress x=-0.612039, -1.68703 f=-3.22067
-- progress x=-0.617368, -1.70172 f=-3.27699
-- progress x=-0.628026, -1.7311 f=-3.39111
-- progress x=-0.5625, -1.67969 f=-3.13776
-- progress x=-0.564981, -1.6871 f=-3.1655
-- progress x=-0.569943, -1.70191 f=-3.22134
-- progress x=-0.579866, -1.73154 f=-3.33449
-- progress x=-0.507812, -1.75781 f=-3.34778
-- progress x=-0.513233, -1.77658 f=-3.41963
-- progress x=-0.524075, -1.8141 f=-3.56563
-- progress x=-0.507812, -1.78125 f=-3.43073
-- progress x=-0.509954, -1.78876 f=-3.45973
-- progress x=-0.514238, -1.80379 f=-3.5181
The program has run out of time or function evaluations

*** Inner loop completed ***
Number of outer iterations : 9
Number of unexplored boxes : 3420
Number of boxes in garbage : 932
Number of elements in SolSet : 1
Number of function evaluations : 99999
Number of gradient evaluations : 99999
found minimum at f(0,0) = -0

here compiled with int stogo_verbose = 1;