[英]Penalty function method
I am trying to implement penalty function method for minimizing function. 我正在尝试实现罚函数方法以最小化函数。 I need to find the minimum of Rosenbrok's function .
我需要找到Rosenbrok的最小功能 。
I am using this penalty function: 我正在使用这个惩罚功能:
First of all, I have found the minimum using scipy.optimize.minimize
: 首先,我发现使用
scipy.optimize.minimize
的最小值:
from scipy.optimize import minimize, rosen
rz = lambda x: (1-x[0])**2 + 100*(x[1] - x[0]**2)**2;
h_1 = lambda x: (x[0] - 2 * x[1] + 2);
h_2 = lambda x: (-x[0] - 2 * x[1] + 6);
h_3 = lambda x: (-x[0] + 2 * x[1] + 2);
x0 = [2.3, 5];
cons = ({'type': 'ineq', 'fun': h_1},
{'type': 'ineq', 'fun': h_2},
{'type': 'ineq', 'fun': h_3})
minimize(rz, x0, constraints=cons)
The answer is x
: array([ 0.99971613, 0.99942073])
答案是
x
: array([ 0.99971613, 0.99942073])
Then I am trying to find the minimum using my implementation of penalty method: 然后我试图找到使用我的惩罚方法实现的最小值:
x_c = [2.3, 3];
i = 1;
while i < 1000:
curr_func = lambda x: rz(x) + i*(h_1(x)**2 + h_2(x)**2 + h_3(x)**2)
x_c = minimize(curr_func, x_c).x;
i *= 1.2;
print(answer.x);
Which gives me [ 2.27402022 1.4157964 ]
(if I increase the number of iterations, final values are even greater). 这给了我
[ 2.27402022 1.4157964 ]
(如果我增加迭代次数,最终值会更大)。
Where is the mistake in my implementation? 我的实施中的错误在哪里? Thanks.
谢谢。
PS Function curr_func
is specific for my constraints, of course, when they are all 'inequals' type. PS函数
curr_func
特定于我的约束,当然,它们都是'inequals'类型。
The problem you have is that the h_i
in your formula are for equality constraints, whereas the problem you are solving is for inequality constraints, which correspond to the g_i
in your formula. 您遇到的问题是公式中的
h_i
用于等式约束,而您要解决的问题是不等式约束,它们对应于公式中的g_i
。 Hence, your penalty function should be using terms like min(0, h_1(x))**2
instead of h_1(x)**2
. 因此,您的惩罚函数应使用
min(0, h_1(x))**2
等h_1(x)**2
而不是h_1(x)**2
。 To see why this is the case, just think about what happens if i = 1000
and x
is the desired solution (1, 1)
. 要了解为什么会出现这种情况,只需考虑如果
i = 1000
且x
是所需解决方案(1, 1)
会发生什么。 Then, the penalty will include a term i * h_1(x)**2 = 1000
, which is huge. 然后,惩罚将包括一个术语
i * h_1(x)**2 = 1000
,这是巨大的。
Note that I used min
instead of max
because it seems like the inequality you want to enforce is h_1(x) >= 0
. 请注意,我使用
min
而不是max
因为您想要强制执行的不等式似乎是h_1(x) >= 0
。 That means as long as h_1(x) >= 0
, the penalty should be zero, but as soon as h_1(x)
goes negative, you start penalizing. 这意味着只要
h_1(x) >= 0
,惩罚就应该为零,但只要h_1(x)
变为负数,就会开始惩罚。 If it's actually h_1(x) <= 0
you want, then you use max
(then you'll have to switch h_1
with -h_1
when you use scipy.optimize.minimize
). 如果它实际上
h_1(x) <= 0
你想,那么你用max
(那么你就必须改用h_1
与-h_1
当您使用scipy.optimize.minimize
)。
BTW, since i
is usually an index variable, it's probably better to name the penalty weight something else, like a
. 顺便说一句,因为
i
通常是一个索引变量,所以最好将惩罚权重命名为其他东西,比如a
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.