在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成功地做到了这一点,但这并不能为后续问题提供答案。
尝试:
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)错误:
---------------------------------------------------------------------------
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 allowedhttps://stackoverflow.com/questions/44500206
复制相似问题