Hi,
I'm trying to solve the following problem (example 1) using NLopt,
http://www.i2c2.aut.ac.nz/Wiki/OPTI/index.php/Probs/NLP
but it doesn't work and return NLOPT_INVALID_ARGS error
in "add_equality_constraint" function.
I don't understand the reason why.
//--------------------------------------------------------
#include <iostream>
#include <math.h>
#include <nlopt.hpp>
using namespace std;
// ------ cost function ------
// f = min(log(1+x[0]^2-x[1]))
// ----------------------
double cost_function(unsigned n, const double *x, double *grad, void *func_data)
{
if (grad) {
grad[0] = 1.0 / (1.0 + x[0] * x[0]) * 2.0 * x[0];
grad[1] = -1.0;
}
return log(1.0 + x[0] * x[0]) - x[1];
}
// ------ equality constraint ------
// (1 + x[0]^2)^2 + x[1]^2 - 4 = 0
// ---------------------------------
double equality_constraint(unsigned n, const double *x, double *grad, void
*data)
{
if (grad) {
grad[0] = 2.0 * (1 + x[0] * x[0]) * 2.0 * x[0];
grad[1] = 2.0 * x[1];
}
return (1.0 + x[0] * x[0]) * (1.0 + x[0] * x[0]) + x[1] * x[1] - 4.0;
}
int main(void)
{
nlopt::opt opt(nlopt::LD_MMA, 2);
opt.set_min_objective(cost_function, NULL);
opt.add_equality_constraint(equality_constraint, NULL, 1e-8);
opt.set_xtol_rel(1e-4);
vector<double> x(2);
x[0] = 0.0;
x[1] = 0.0;
double minf;
nlopt::result result = opt.optimize(x, minf);
if (result < 0) {
cout << "failure" << endl;
}
else {
cout << "found minimum at f(" << x[0] << "," << x[1] << ") = " << minf << endl;
}
return 0;
}
//--------------------------------------------------------
Thanks,
Tomo
_______________________________________________
NLopt-discuss mailing list
[email protected]
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss