Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ exceptions are ignored when eliminating dimensions #317

Closed
roelmatthysen opened this issue Mar 5, 2020 · 0 comments · Fixed by #323
Closed

C++ exceptions are ignored when eliminating dimensions #317

roelmatthysen opened this issue Mar 5, 2020 · 0 comments · Fixed by #323

Comments

@roelmatthysen
Copy link

Hi,

When the objective function throws an exception such as nlopt::forced_stop, optimization should be gracefully halted and an exception is thrown to the caller. However when bounds coincide for one or more variables in combination with a gradient-free algorithm, this exception is never rethrown.

When stepping through the code, it seems the force_stop flag is set in the original nlopt::opt, while the check is done for the elimdim wrapper, could this be the issue?

MWE:

double myvfunc(const std::vector<double> &x, std::vector<double> &grad,
               void *my_func_data) {
  throw nlopt::forced_stop();
  return x[0];
}

int main() {
  nlopt::opt opt("GN_CRS2_LM", 2);
  std::vector<double> lb{-10.0, 0};
  opt.set_lower_bounds(lb);
  std::vector<double> ub{10, 1};
  opt.set_upper_bounds(ub);
  opt.set_min_objective(myvfunc, NULL);
  opt.set_maxeval(1000);
  opt.set_xtol_rel(1e-4);
  std::vector<double> x{1.234, 0};
  double minf;
// This works as it should, catching and rethrowing the forced_stop exception
  try {
    opt.optimize(x, minf);
    std::cout << "found minimum at f(" << x[0] << "," << x[1]
              << ") = " << std::setprecision(10) << minf << std::endl;
  } catch (std::exception &e) {
    std::cout << "nlopt failed: " << e.what() << std::endl;
  }
  opt.set_upper_bounds({10, 0});
// This catches but never rethrows the exceptions and continues until maxeval is reached.
  try {
    opt.optimize(x, minf);
    std::cout << "found minimum at f(" << x[0] << "," << x[1]
              << ") = " << std::setprecision(10) << minf << std::endl;
  } catch (std::exception &e) {
    std::cout << "nlopt failed: " << e.what() << std::endl;
  }
}

This returns

nlopt failed: nlopt forced stop
found minimum at f(1.234,0) = inf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant