功能约束优化Python错误答案

时间:2017-10-23 20:29:58

标签: python optimization scipy

我试图最小化函数(函数的积分),给定约束

def s(y,a,b,c,d):
    v = [1, y, y**2, y**3]
    alpha = [a, b, c, d]
    q = np.inner(v,alpha)
    return -q*np.exp(-q)
def p(y,a,b,c,d):
    v = [1, y, y**2, y**3]
    alpha = [a, b, c, d]
    q = np.inner(v,alpha)
    return np.exp(-q)

def Q(u):
    a, b, c, d = u
    d = integrate.quad(lambda y: s(y,a,b,c,d), 0, 1)
    return d[0]
cons = ({'type': 'eq', 'fun' : integrate.quad(lambda y: p(y,a,b,c,d), 0,     1)[0]-1},
    {'type': 'eq', 'fun' : integrate.quad(lambda y: (p(y,a,b,c,d)*y), 0, 1)[0]-0.483523521402009},
    {'type': 'eq', 'fun' : integrate.quad(lambda y: (p(y,a,b,c,d)*y**2), 0, 1)[0]-0.300458990347083},
    {'type': 'eq', 'fun' : integrate.quad(lambda y: (p(y,a,b,c,d)*y**3), 0, 1)[0]-0.209996591802522})
res = minimize(Q, x0 = (0, 0, 0, 0), method='BFGS', constraints=cons)
print(res)

我得到了一个输出

fun: -0.36787942624169967
 hess_inv: array([[  17.98311921,  -49.74794121,    2.50822967,   36.21942131],
   [ -49.74794121,  191.70720321,  -23.14586623, -158.65310285],
   [   2.50822967,  -23.14586623,    8.1640543 ,   25.72129091],
   [  36.21942131, -158.65310285,   25.72129091,  142.59127393]])
  jac: array([ -3.54647636e-06,  -1.94460154e-06,  -1.75461173e-06,
     3.24100256e-07])
  message: 'Optimization terminated successfully.'
 nfev: 126*
  *nit: 19
 njev: 21
   status: 0
  success: True
    x: array([ 0.99920744,  0.0092224 , -0.02276881,  0.0150456 ])**

然而,使用这个X数组,不满足约束

x = (0.99920744,  0.0092224 , -0.02276881,  0.0150456)
integrate.quad(lambda y: p(y,x[0],x[1],x[2],x[3]), 0, 1)[0]

0.3678829742546207

不是1,如约束中具体说明的那样。当它显然没有收敛时,它如何声称收敛?

0 个答案:

没有答案