On Jul 21, 2010, at 7:55 AM, Yann COLLETTE wrote:
I have some difficulties to use the function
nlopt_add_equality_mconstraint.
I create a solver + some initialization (I use the COBYLA algorithm):
I can't replicate your problem. Here is a version of the tutorial
from the manual that uses an mconstraint with COBYLA, and works for me.
#include <math.h>
#include <nlopt.h>
#include <stdio.h>
int count = 0;
double myfunc(unsigned n, const double *x, double *grad, void
*my_func_data)
{
printf("myfunc_%d(%g,%g) = %g\n", ++count, x[0],x[1], sqrt(x[1]));
if (grad) {
grad[0] = 0.0;
grad[1] = 0.5 / sqrt(x[1]);
}
return sqrt(x[1]);
}
typedef struct {
double a, b;
} my_constraint_data;
void myconstraintm(unsigned m, double *result,
unsigned n, const double *x, double *grad, void
*data)
{
my_constraint_data *d = (my_constraint_data *) data;
unsigned i;
for (i = 0; i < m; ++i) {
double a = d[i].a, b = d[i].b;
if (grad) {
grad[i*n + 0] = 3 * a * (a*x[0] + b) * (a*x[0] + b);
grad[i*n + 1] = -1.0;
}
result[i] = (a*x[0] + b) * (a*x[0] + b) * (a*x[0] + b) -
x[1];
}
}
int main(void) {
double lb[2] = { -10, 1e-6 }; /* lower bounds */
double ub[2] = { 10, 10 }; /* upper bounds */
nlopt_opt opt, local_opt;
double ctol[2] = {1e-6, 1e-6};
nlopt_srand(314159);
opt = nlopt_create(NLOPT_LN_COBYLA, 2); /* algorithm and
dimensionality */
nlopt_set_lower_bounds(opt, lb);
nlopt_set_upper_bounds(opt, ub);
nlopt_set_min_objective(opt, myfunc, NULL);
my_constraint_data data[2] = { {2,0}, {-1,1} };
nlopt_add_inequality_mconstraint(opt, 2, myconstraintm, data,
ctol);
nlopt_set_stopval(opt, sqrt(8./27.)+1e-6);
nlopt_set_maxeval(opt, 10000);
double dx[2] = { 1.0, 10.0 };
nlopt_set_initial_step(opt, dx);
double x[2] = {000.234, 7.654};
double minf; /* ''the minimum objective value, upon return'' */
if (nlopt_optimize(opt, x, &minf) < 0) {
printf("nlopt failed!\n");
}
else {
printf("found minimum after %d evaluations\n", count);
printf("found minimum at f(%g,%g) = %0.10g\n", x[0], x[1],
minf);
}
nlopt_destroy(opt);
return 0;
}
_______________________________________________
NLopt-discuss mailing list
[email protected]
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss