Python Scipy Optimization.minimize使用SLSQP显示最大化结果 [英] Python Scipy Optimization.minimize using SLSQP showing maximized results

查看:1780
本文介绍了Python Scipy Optimization.minimize使用SLSQP显示最大化结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习用scipy.optimize.minimize优化一个多变量约束非线性问题,但是收到了奇怪的结果.

I am learning to optimize a multivariate constrained nonlinear problem with scipy.optimize.minimize,but received strange results.

我的问题:

minimize objfun

objfun   x*y

constraints 0<=x<=5,  0<=y<=5,  x+y==5

我的代码:

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)

收到的结果:

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

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

I am expecting the fun to be 0 or significantly close to 0 and x or y to be 0

推荐答案

我认为您遇到了麻烦.如果您尝试使用非对称猜测,那么您将收敛到正确的解决方案.

I think you are hitting a edge case. If you try with a guess that is not symmetric, you converge to the right solution.

只需将x0=[0,0]更改为其他内容,例如x0=[.2,.9].

Just change x0=[0,0] to something else, like x0=[.2,.9].

在@pv评论后展开.

[x,y]=[2.5,2.5]是受约束函数的局部最大值.在跳到该局部最大值之后,算法会再次计算将目标最小化所应采取的方向.

[x,y]=[2.5,2.5] is a local maximum of the constrained function. After jumping to this local maximum, the algorithm calculates again the direction it should take to minimize the target.

它是通过计算[ 2.50000001 2.5 ][ 2.5 2.50000001]处的值来实现的.发现该方向为(-1,-1).但是,该方向与约束正交,然后停止.

It does so by calculating the value at [ 2.50000001 2.5 ] and at [ 2.5 2.50000001]. It finds that this direction is (-1,-1). This direction is however orthogonal to the constraint, and it then stops.

之所以出现问题,是因为目标和约束相对于x=y是对称的,并且我们是从x=y的确切猜测开始的.

The problem arises because the target and the constraint are symmetric with respect to x=y, and that we are starting with the guess exactly on x=y.

这篇关于Python Scipy Optimization.minimize使用SLSQP显示最大化结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆