简体   繁体   English

Scipy约束最小化不遵守约束

[英]Scipy constrained minimization does not respect constraint

I apologize if the question seems straightforward and easy. 如果这个问题看起来简单明了,我深表歉意。 I tried to look for an answer, but did not find one that could solve my problem. 我试图寻找一个答案,但是没有找到可以解决我的问题的答案。 I have a very simple minimization problem: I need to maximize an expected value (in a second phase the objective function will become more complicated): 我有一个非常简单的最小化问题:我需要最大化期望值(在第二阶段,目标函数将变得更加复杂):

    def EV(q, P):
       return (-1)*np.sum(100 * q * (2*P - 1))

q is a 12 dimensional vector whose elements need to be between 0 and 1 and, clearly, the sum of the elements of q is 1. So I proceed to set the bounds and constraints: q是一个12维向量,其元素需要在0到1之间,并且显然q的元素之和为1。因此,我继续设置边界和约束:

     cons = {'type': 'eq', 'fun': lambda q: np.sum(q) - 1}
     bds = [(0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1)]
     P = array([ 0.32510069,  0.96284943,  0.33966465,  0.61696874,  0.77368336,
    0.10127222,  0.47836665,  0.87537657,  0.2086234 ,  0.52468426,
    0.31931169,  0.86424427]).

Then I call scipy.optimize.minimize: 然后我调用scipy.optimize.minimize:

    X0 = np.array([0.5,0,0,0,0,0,0,0,0,0,0.4,0])
    qstar = scipy.optimize.minimize(fun = EV, x0 = X0, args = (P), method = 'L-BFGS-B', bounds = bds, constraints = cons).

However, when I print the solution qstar I get the following: 但是,当我打印解决方案qstar时,我得到以下信息:

    fun: -323.56132559388169
    hess_inv: <12x12 LbfgsInvHessProduct with dtype=float64>
    jac: array([ 34.97985972, -92.56988847,  32.06706651, -23.39374987,
   -54.7366767 ,  79.74555274,   4.32666525, -75.0753145 ,
    58.27532163,  -4.93685093,  36.13766353, -72.84884873])
    message: 'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL'
    nfev: 26
    nit: 1
    status: 0
    success: True
    x: array([ 0.,  1.,  0.,  1.,  1.,  0.,  0.,  1.,  0.,  1.,  0.,  1.])

Why isn't the solution satisfying the equality constraint? 为什么解决方案不满足等式约束? Is it, perhaps, because of the message? 可能是因为该消息吗? Any help is very much appreciated. 很感谢任何形式的帮助。

Change the solver method to SLSQP, as mentioned in the comment, constraints are only supported in SLSQP and COBYLA. 如注释中所述,将求解器方法更改为SLSQP,仅SLSQP和COBYLA支持约束。 SLSQP solves the problem by sequential least squares quadratic programming. SLSQP通过顺序最小二乘二次编程解决了该问题。

Note that COBYLA only supports inequality constraints. 请注意,COBYLA仅支持不平等约束。

import numpy as np
import scipy.optimize

def EV(q, P):
    return (-1)*np.sum(100 * q * (2*P - 1))

cons = {'type': 'eq', 'fun': lambda q: np.sum(q) - 1}
bds = [(0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1)]
P = np.array([ 0.32510069,  0.96284943,  0.33966465,  0.61696874,  0.77368336,
0.10127222,  0.47836665,  0.87537657,  0.2086234 ,  0.52468426,
0.31931169,  0.86424427])

X0 = np.array([0.5,0,0,0,0,0,0,0,0,0,0.4,0])
qstar = scipy.optimize.minimize(fun = EV, x0 = X0, args = (P), method ='SLSQP', bounds = bds, constraints = cons)
print(qstar)

gives me the following output. 给我以下输出。

fun: -92.56988588438836
jac: array([ 34.97986126, -92.56988621,  32.06707001, -23.39374828,
   -54.7366724 ,  79.74555588,   4.32666969, -75.07531452,
    58.27532005,  -4.93685246,  36.13766193, -72.84885406])
message: 'Optimization terminated successfully.'
nfev: 28
nit: 2
njev: 2
status: 0
success: True
x: array([  2.07808604e-10,   1.00000000e+00,   1.95365391e-10,
     0.00000000e+00,   0.00000000e+00,   4.37596612e-10,
     5.51522994e-11,   0.00000000e+00,   3.28030922e-10,
     8.07265366e-12,   2.14253171e-10,   0.00000000e+00])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM