首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python中的约束优化

python中的约束优化
EN

Stack Overflow用户
提问于 2017-06-12 21:03:39
回答 0查看 2.3K关注 0票数 2

在Python中,使用SciPy,我需要找到给定限制Q*((1+y)*2-3*Q-0.1)-y**2的函数(1-z)*(Q*((1+y)*2-3*Q-0.1))-y**2=0的最大值。对于z,我想输入一些值,以找到在给定z值的情况下最大化函数的参数。

我尝试了很多方法来使用SciPy优化函数,但是我不知道该怎么做。我确实使用WolframAlpha成功地做到了这一点,但这并不能为后续问题提供答案。

尝试:

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

def equilibrium(z):
    #Objective function
    min_prof = lambda(Q,y): -1*(Q*((1+y)*2-3*Q-0.1)-y**2)

    #initial guess
    x0 = (0.6,0.9)

    #Restriction function
    cons = ({'type': 'eq', 'fun': lambda (Q,y): (1-z)*(Q*((1+y)*2-3*Q-0.1))-y**2})

    #y between 0 and 1, Q between 0 and 4
    bnds = ((0,4),(0,1))

    res = minimize(min_prof,x0, method='SLSQP', bounds=bnds ,constraints=cons)

    return res.x

from numpy import arange

range_z = arange(0,1,0.001)

print equilibrium(range_z)

错误:

代码语言:javascript
复制
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-527013574373> in <module>()
     21 range_z = arange(0,1,0.01)
     22 
---> 23 print equilibrium(range_z)

<ipython-input-20-527013574373> in equilibrium(z)
     14     bnds = ((0,4),(0,1))
     15 
---> 16     res = minimize(min_prof,x0, method='SLSQP', bounds=bnds ,constraints=cons)
     17 
     18     return res.x

/Users/Joost/anaconda/lib/python2.7/site-packages/scipy/optimize/_minimize.pyc in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    456     elif meth == 'slsqp':
    457         return _minimize_slsqp(fun, x0, args, jac, bounds,
--> 458                                constraints, callback=callback, **options)
    459     elif meth == 'dogleg':
    460         return _minimize_dogleg(fun, x0, args, jac, hess,

/Users/Joost/anaconda/lib/python2.7/site-packages/scipy/optimize/slsqp.pyc in _minimize_slsqp(func, x0, args, jac, bounds, constraints, maxiter, ftol, iprint, disp, eps, callback, **unknown_options)
    324             + 2*meq + n1 + ((n+1)*n)//2 + 2*m + 3*n + 3*n1 + 1
    325     len_jw = mineq
--> 326     w = zeros(len_w)
    327     jw = zeros(len_jw)
    328 

ValueError: negative dimensions are not allowed
EN

回答

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

https://stackoverflow.com/questions/44500206

复制
相关文章

相似问题

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