astamm / nloptr

nloptr provides an R interface to NLopt, a free/open-source library for nonlinear optimization providing a common interface to a number of different optimization routines which can handle nonlinear constraints and lower and upper bounds for the controls.
https://astamm.github.io/nloptr/
Other
106 stars 34 forks source link

Access more detailed error message via `nlopt_get_errmsg`? #172

Open helske opened 2 weeks ago

helske commented 2 weeks ago

I'm using nloptrAPI in my pacakge and trying to figure out why my optimization fails with generic error code -1. I noticed that there is a function nlopt_get_errmsg in NLopt but when trying to use it in my R package I get an error undefined reference tonlopt_get_errmsg'` during the compilation. Is this function missing from the API, and if so, could it be added?

eddelbuettel commented 2 weeks ago

The nlopt.h header has it defined as

NLOPT_EXTERN(const char *) nlopt_get_errmsg(nlopt_opt opt);

and the problem may be that we do not hold one (long enough?) to the nlopt_opt instance describing it. So it would likely require some minor rejigging of things.

A cheapish way may be put a fixed-length char buffer somewher (easy and ugly: as a global var), say char buf[256], and in the case of error have that function called to fill it. You can then easily add an R accessor to buf to return its content as a character variable to R.