首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用SciPy求解约束最大化问题

问用SciPy求解约束最大化问题
EN

Stack Overflow用户
提问于 2014-12-05 20:11:02
回答 1查看 1.2K关注 0票数 2

最大限度地发挥以下作用:

代码语言:javascript
复制
x[0] + x[1] + x[2]

制约因素:

代码语言:javascript
复制
0.2 * x[0] + 0.4 * x[1] - 0.33 * x[2] <= 25
5 * x[0] + 8.33 * x[2] <= 130
...
x[0] >= 0
x[1] >= 0
x[2] >= 0

我的代码看起来是:

代码语言:javascript
复制
from numpy import *
from scipy.optimize import minimize


cons = ({'type': 'ineq', 'fun': lambda x:  array([25 - 0.2 * x[0] - 0.4 * x[1] - 0.33 * x[2]])},
        {'type': 'ineq', 'fun': lambda x:  array([130 - 5 * x[0] - 8.33 * x[2]])},
        {'type': 'ineq', 'fun': lambda x:  array([16 - 0.6 * x[1] - 0.33 * x[2]])},
        {'type': 'ineq', 'fun': lambda x:  array([7 - 0.2 * x[0] - 0.1 * x[1] - 0.33 * x[2]])},
        {'type': 'ineq', 'fun': lambda x:  array([14 - 0.5 * x[1]])},
        {'type': 'ineq', 'fun': lambda x:  array([x[0]])},
        {'type': 'ineq', 'fun': lambda x:  array([x[1]])},
        {'type': 'ineq', 'fun': lambda x:  array([x[2]])})


f = lambda x: -1 * (x[0] + x[1] + x[2])

res = minimize(f, [0, 0, 0], method='SLSQP', constraints=cons, options={'disp': True})

print(res)

不幸的是,结果是:

代码语言:javascript
复制
Positive directional derivative for linesearch    (Exit mode 8)
            Current function value: -18240083542.4
            Iterations: 20
            Function evaluations: 180
            Gradient evaluations: 16
       x: array([  6.05846118e+09,   6.05846118e+09,   6.12316118e+09])
     jac: array([ 0.,  0.,  0.,  0.])
 message: 'Positive directional derivative for linesearch'
     fun: -18240083542.377449
  status: 8
    njev: 16
    nfev: 180
     nit: 20
 success: False

我可以在Excel中成功地解决这个问题,所以我想我在Python中做了一些错误的事情。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-07 11:10:50

在SciPy中,代码在Python2下工作,但拒绝在Python3下工作

GitHub:https://github.com/scipy/scipy/issues/4240杂志

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27323910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档