2

我写了一个程序,

最小化:f = x^2 + y^2

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

我得到了答案:x = 0.12219 y = 5.678 不满足 ceq。我不知道如何解决它。

我的完整源代码在这里

主要功能如下:

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;
}

更新!!!!!阅读文档后,我发现算法“MMA”不支持“等约束”。

将“MMA”替换为“SLSQP”可能会解决此问题。

4

0 回答 0