本文介绍了如何在nlopt中设置grad []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下NLopt,如下所示。 (NLopt网站引用 [])



问题1:如果数字约束的数量大于变量的数量,我们如何在myconstraint中设置grad []?有没有(自动)方法解决问题而不引入拉格朗日乘数?



使用拉格朗日多路复用器,我知道我们可以解决这个问题。然而,使用拉格朗日多路复用器我们必须手动获得my_constraint_data,这使得解决大规模问题变得困难。



示例:最小化f = - ( (x1)^ 3) - (2 *(x2)^ 2)+(10 *(x1)) - 6-(2 *(x2)^ 3)经受10 - ((x1)(x2))> =(0,(x1)(x2)^ 2)-5> = 0且(x2) - (x1)*(x2)^ 3> = 0



基于上述问题,我们有三个不等式约束,如下所示。



约束条件1:c1 = 10-(x1)*(x2)> = 0

约束2:c2 =((x1)(x2)^ 2)-5> = 0

约束3:c3 =(x2) - (x1)(x2 )^ 3> = 0



在NLopt教程中,我们知道grad [0] = d(c1)/ d(x1)和grad1 = d(c2 )/ d(x2)作为约束的梯度。然后,我们将grad []设置为如下。

I would like to ask about NLopt as follows below. (The NLopt website refers to Tutorial - NLopt Documentation[^])

Question1: If the number of constraints is bigger than the number of variables, how can we set "grad[ ]" in the "myconstraint"? Is there any (automatic) method to solve the problem without introducing Lagrangian multiplier?

Using Lagrangian multiplexer, I know we can solve the problem. However the use of Lagrangian multiplexer we have to obtain "my_constraint_data’’ manually, which make it difficult to solve large-scale problem.

Example : Minimize f= -((x1)^3)-(2*(x2)^2)+(10*(x1))-6-(2*(x2)^3) subject to 10-((x1)(x2))>=0, ((x1)(x2)^2)-5>=0 and (x2)-(x1)*(x2)^3 >= 0

Based on the problem above, we have three inequality constraints like below.

Constraint 1: c1 = 10-(x1)*(x2) >= 0
Constraint 2: c2 = ((x1)(x2)^2)-5>=0
Constraint 3: c3 = (x2)-(x1)(x2)^3 >= 0

In NLopt tutorial, we know that grad[0] = d(c1)/d(x1) and grad1 = d(c2)/d(x2) as the gradient of constraints. Then, we set grad[ ] as same as follows.

double myconstraint(unsigned n, const double *x, double *grad, void *data) {
	my_constraint_data *d = (my_constraint_data *)data;

	if (grad) {
		grad[0] = -x[1];       //grad[0] = d(c1)/dx[1]
		grad[1] = 2*x[0]+x[1]; //grad[1] = d(c2)/dx[2]
        grad[2] = ???;         //grad[2] = d(c3)/dx[3] but we only have 2 variable (x1)&(x2)
	}
	return (10-x[0]*x[1], x[0]*x[1]*x[1]-5, x[1]-x[0]*x[1]*x[1]*x[1];
}

问题是如果约束的数量大于变量的数量,我们不知道如何设置grad [](特别是对于c3)。



我尝试了什么:



当然我们可以使用非自动方法解决问题,如下所示拉格朗日多路复用器(l1,l2,l3),其中grad [0] = -l1 *(d(c1)/ d(x1)) - l2 *(d(c2)/ d(x1)) - l3 *(d(c) )/ d(x1))和grad [1] = -l1 *(d(c1)/ d(x2)) - l2 *(d(c2)/ d(x2)) - l3 *(d(c)/ d(x3))。

The problem is we do not know how to set "grad[ ]" (especially for c3) if the number of constraints are larger than the number of variables.

What I have tried:

Of course we can solve the problem with non-automatic method like below by using Lagrangian multiplexer (l1, l2, l3) where grad[0] = -l1*(d(c1)/d(x1))-l2*(d(c2)/d(x1))-l3*(d(c)/d(x1)) and grad[1] = -l1*(d(c1)/d(x2))-l2*(d(c2)/d(x2))-l3*(d(c)/d(x3)).

double myconstraint(unsigned n, const double *x, double *grad, void *data) {
	my_constraint_data *d = (my_constraint_data *)data;
        //set l1, l2, and l3 as parameter of lagrangian multiplier
	double l1=d->l1,l2=d->l2,l3=d->l3;
	++count;
	if (grad) {
		grad[0] = l1*x[1]-l2*x[1]*x[1]-l3*x[1]*x[1]*x[1];
		grad[1] = l1*x[0]-2*l2*x[0]*x[1]-l3+3*l3*x[0]*x[1]*x[1];
	}
	return (10-x[0]*x[1], x[0]*x[1]*x[1]-5, x[1]-x[0]*x[1]*x[1]*x[1]);
}

同时,将非自动方法应用于大规模问题并不容易,因为编程效率低且复杂。



问题2:有没有任何方法可以使用NLopt求解非线性联立方程? (当在约束数大于变量数的情况下应用拉格朗日多路复用器时,应解决非线性联立方程)。



您的回答对我们非常有帮助。谢谢。

Meanwhile, it is not easy to apply non-automatic method into large-scale problem because it will be inefficient and complicated in programming.

Question2: Is there any method to solve nonlinear simultaneous equations using NLopt? (When Lagrangian multiplexer is applied in case of the number of constraints are larger than the number of variables, nonlinear simultaneous equations should be solved).

Your answer will be really helpful to us. Thank you.

推荐答案


这篇关于如何在nlopt中设置grad []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 04:05