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

PRAXIS yields nan inputs on #528

Closed
jschueller opened this issue Sep 15, 2023 · 0 comments
Closed

PRAXIS yields nan inputs on #528

jschueller opened this issue Sep 15, 2023 · 0 comments

Comments

@jschueller
Copy link
Collaborator

PRAXIS in this example leads to x=[nan,nan] eventhough this is a bounded problem

#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*x[0];
    grad[1] = 2*x[1];
  }
  std::cout << "x=["<<x[0]<<", "<<x[1]<<"]"<<std::endl;
  if (std::isnan(x[0]))
    throw std::invalid_argument("nan");
  return x[0]*x[0]+x[1]*x[1];
}


int main() {
  nlopt::srand(10);
  for (int repeat = 0; repeat < 1000000; ++repeat)
  {
    std::cout << "repeat="<<repeat<<std::endl;
    nlopt::opt opt(nlopt::LN_PRAXIS, 2);
    std::vector<double> lb = {-1.0, -10.0};
    std::vector<double> ub = {10.0, 1.0};
    opt.set_lower_bounds(lb);
    opt.set_upper_bounds(ub);
    opt.set_max_objective(myvfunc, NULL);
    std::vector<double> x = {0.5, 0.5};
    double minf;
    try{
      opt.optimize(x, minf);
      std::cout << "found optimum at f(" << x[0] << "," << x[1] << ") = "
                << std::setprecision(10) << minf <<std::endl;
    }
    catch(std::exception &e) {
      std::cerr << "nlopt failed: " << e.what() << std::endl;
      return EXIT_FAILURE;
    }
  }
  return EXIT_SUCCESS;
}
jschueller added a commit to jschueller/nlopt that referenced this issue Nov 11, 2024
jschueller added a commit to jschueller/nlopt that referenced this issue Nov 11, 2024
jschueller added a commit to jschueller/nlopt that referenced this issue Nov 11, 2024
jschueller added a commit to jschueller/nlopt that referenced this issue Nov 11, 2024
jschueller added a commit to jschueller/nlopt that referenced this issue Nov 11, 2024
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

No branches or pull requests

1 participant