nlopt_add_equality_constraint doesn't work

498 views Asked by At

I write a program,

to minimize: f = x^2 + y^2

constrain: c: x-1 < 0 ceq: x+y-5=0

I got answer: x = 0.12219 y = 5.678 which is not satisfy ceq. I don't know how to fix it.

My complete source code is here

main function is as follow:

int main()
{
    nlopt_opt opt;

    opt = nlopt_create(NLOPT_LD_MMA, 2); /* algorithm and dimensionality */

    nlopt_set_min_objective(opt, myfunc, NULL);

    nlopt_add_equality_constraint(opt, ceq1, NULL, 1e-8);
    nlopt_add_inequality_constraint(opt, c1, NULL, 1e-8);

    nlopt_set_xtol_rel(opt, 1e-4);

    double x[2] = { 1.234, 5.678 };  /* some initial guess */
    double minf; /* the minimum objective value, upon return */

    if (nlopt_optimize(opt, x, &minf) < 0) {
        printf("nlopt failed!\n");
    }
    else {
        printf("found minimum at f(%g,%g) = %0.10g\n", x[0], x[1], minf);
    }

    nlopt_destroy(opt);

    return 0;
}

update!!!!! After reading the document, I found the algorithm "MMA" doesn't support "equal constraint."

To replace "MMA" with "SLSQP" might solve this problem.

0

There are 0 answers