简体   繁体   English

罚函数法

[英]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]) 答案是xarray([ 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))**2h_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 = 1000x是所需解决方案(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.

相关问题 如何使用惩罚方法最小化约束违反的二次目标函数 - How to minimize an quadratic objective function with constraint violation using penalty method 是否有 numpy/scipy 函数来计算出站惩罚? - Is there a numpy/scipy function to calculate outbound penalty? Pyomo:如何在目标 function 中包含惩罚 - Pyomo: How to include a penalty in the objective function 在 Scipy 中使用 Penalty function 优化风电场 - Optimization of wind farm using Penalty function in Scipy Mystic 中非线性不等式约束的惩罚函数在边界外进行评估 - Penalty function for nonlinear inequality constraints in Mystic are evaluated outside of bounds 在 Python 中使用惩罚 function 时如何编码“优化”问题 - How to code "optimization" problem while using penalty function in Python 需要帮助设计解决方案来计算自动换行功能的“惩罚性” - Need help devising solution to calculating the “penalty” of a word wrap function 有没有办法向整体损失函数添加基于keras“自定义层”的/特定惩罚? - Is there a way to add keras 'custom layer' based/specific penalty to the overall loss function? 为什么不能在惩罚函数中将元组解压缩到scipy quad中? - Why can't I tuple unpack into scipy quad inside a penalty function? LinearSVC中`penalty`和`loss`的含义 - Meaning of `penalty` and `loss` in LinearSVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM