Hello everybody.
I'm using NLopt as an C++ alternative to the "fmincon" function of Matlab or
the "sqp" function of Octave.
I have a nonlinear equality constraint and a nonlinear function that I want to
minimize without using its the derivatives.
It seems the best algorithm to use, with NLopt, is the COBYLA one.
I was able to run the tutorial example without any problem, but when I try my
own function it's not working.
For example (a problem I created just to test):
min f(x1,x2) = 100*(x1² - 1) + x2² - 39*x1;
subject to x1² + x2² = 22;
I tried a C++ and a C version of the code, the first one returns me a
roundoff_limited error, and the second one an "inf".
Here is the C version (all in a single cpp file):
#include <iostream>
#include <math.h>
#include <nlopt.h>
double myfunc(unsigned n, const double *x, double *grad, void *my_func_data);
double myconstraint(unsigned n, const double *x, double *grad, void *data);
double myfunc(unsigned n, const double *x, double *grad, void *my_func_data)
{
return 100.0 * (x[0]*x[0] - 1.0) + x[1]*x[1] - 39.0 * x[0];
}
double myconstraint(unsigned n, const double *x, double *grad, void *data)
{
return (x[0]*x[0] + x[1]*x[1] - 22.0);
}
int main()
{
nlopt_opt opt;
opt = nlopt_create(NLOPT_LN_COBYLA, 2); /* algorithm and dimensionality */
nlopt_set_min_objective(opt, myfunc, NULL);
nlopt_add_inequality_constraint(opt, myconstraint, NULL, 1e-8);
nlopt_add_inequality_constraint(opt, myconstraint, NULL, 1e-8);
nlopt_set_xtol_rel (opt, 1.0e-5);
double x[2] = { -1.0, 1.0 }; /* some initial guess */
double minf; /* the minimum objective value, upon return */
nlopt_optimize(opt, x, &minf);
std::cout << "found minimum at f(" << x[0] << ", " << x[1] << ") = " <<
minf << "\n" << std::endl;
return 0;
}
Since Octave was able to give me suiting results, I don't think the problem
comes from my function, so maybe I forgot something in the design of the solver.
Can somebody please tell me where I'm wrong?
Thank you.
Mangaf
_______________________________________________
NLopt-discuss mailing list
[email protected]
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss