使用scipy求解带约束最优化问题

在看李航老师的《统计学习方法》时,想把例7.1通过代码实现,有了这篇博客。

1、求解思路

使用scipy中的minimize函数求解最优化问题的形式如下:
在这里插入图片描述
其中 x x x是一个向量, g i ( x ) g_{i}(x) gi(x)是非等式约束, h j ( x ) h_{j}(x) hj(x)是等式约束。

例7.1中的优化问题如下:
在这里插入图片描述
刚好能够带到式子里面,下面是代码:

from scipy.optimize import minimize
import numpy as np 

#目标函数
fun=lambda x: 0.5*(x[0]*x[0]+x[1]*x[1])
#约束条件
cons=(
    {'type':'ineq','fun':lambda x: 3*x[0]+3*x[1]+x[2]-1},
    {'type':'ineq','fun':lambda x: 4*x[0]+3*x[1]+x[2]-1},
    {'type':'ineq','fun':lambda x: -x[0]-x[1]-x[2]-1}
)
#w1,w2,b初始值
x0=np.array([0]*3)
#结果
res=minimize(fun,x0,method='SLSQP',constraints=cons)
print(res)

程序运行结果:

     fun: 0.25000000000000056
     jac: array([0.50000001, 0.50000001, 0.        ])
 message: 'Optimization terminated successfully.'
    nfev: 10
     nit: 2
    njev: 2
  status: 0
 success: True
       x: array([ 0.5,  0.5, -2. ])

2、minimize函数讲解

scipy.optimize.minimize(fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None)

函数官网链接:https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html#scipy.optimize.minimize

我这里只列几个常用的参数:

method常用的有BFGS, L-BFGS-B, SLSQP,分别的含义如下:

  • SLSQP:使用顺序最小二乘编程(SLSQP)最小化一个或多个变量的标量函数;
  • BFGS:使用BFGS算法最小化一个或多个变量的标量函数;
  • L-BFGS-B:使用L-BFGS-B算法最小化一个或多个变量的标量函数。

constraints定义了约束条件,每个约束定义为一个词典,键值对如下:

  • fun:约束函数;
  • type:约束类型,‘eq’是等式约束,‘ineq’是不等式约束;

函数的返回值是一个OptimizeResult对象,各属性如下:

  • x: ndarray
    The solution of the optimization.

  • success: bool
    Whether or not the optimizer exited successfully.

  • status: int
    Termination status of the optimizer. Its value depends on the underlying solver. Refer to message for details.

  • message: str
    Description of the cause of the termination.

  • fun, jac, hess: ndarray
    Values of objective function, its Jacobian and its Hessian (if available). The Hessians may be approximations, see the documentation of the function in question.

  • hess_inv: object
    Inverse of the objective function’s Hessian; may be an approximation. Not available for all solvers. The type of this attribute may be either np.ndarray or scipy.sparse.linalg.LinearOperator.

  • nfev, njev, nhev: int
    Number of evaluations of the objective functions and of its Jacobian and Hessian.

  • nit: int
    Number of iterations performed by the optimizer.

  • maxcv: float
    The maximum constraint violation.

3、参考资料

Python——使用scipy求解带约束的最优化问题
机器学习核心:优化问题基于Scipy

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值