我正在学习优化多变量约束非线性问题scipy.optimize.minimize,但收到了奇怪的结果.
我的问题:
minimize objfun
objfun x*y
constraints 0<=x<=5, 0<=y<=5, x+y==5
Run Code Online (Sandbox Code Playgroud)
我的代码:
from scipy import optimize
def func(x):
return x[0]*x[1]
bnds=((0,100),(0,5))
cons=({'type':'eq','fun':lambda x:x[0]+x[1]-5})
x0=[0,0]
res= optimize.minimize(func,x0,method='SLSQP',bounds=bnds,constraints=cons)
Run Code Online (Sandbox Code Playgroud)
收到的结果:
status: 0
success: True
njev: 2
nfev: 8
fun: 6.2499999999999991
x: array([ 2.5, 2.5])
message: 'Optimization terminated successfully.'
jac: array([ 2.5, 2.5, 0. ])
nit: 2
我期待乐趣为0或显着接近0,x或y为0