nlopt
nlopt copied to clipboard
C++ API: force_stop bug for maximization vs. minimization
This appears to be generic (i.e. applies to all optimization algos) bug for C++ api when a user stoppage occurs by throwing an exception on the objective function.
In src/api/nlopt.hpp at line 234:
static double myvfunc(unsigned n, const double *x, double *grad, void *d_){
...
return HUGE_VAL;
}
But in src/api/optimize.c, f_max will flip the sign of HUGE_VAL for maximization into -HUGE_VAL which happens to be consistent with nlopt_opt::stopval set in src/api/options.c. However for minimization (where user provided objective function is not wrapped in f_max), HUGE_VAL is inconsistent with nlopt_opt::stopval resulting in failure to force stop.
It appears that the fix should be for the C++ wrapper myvfunc to return different -/+HUGE_VAL depending on whether the objective function is supposed to be minimized/maximized. But it's not clear how to detect this at that point in the program as d->o->o->maximize and d->o->o->stopvol for maximization have been set back to the same for minimization when f_max is set in nlopt_optimize (in src/api/nlopt.hpp).