scipy.optimize.minimize() 是 Python 计算库 Scipy 的一个功能,用于求解函数在某一初始值附近的极值,获取 一个或多个变量的标量函数的最小化结果 ( Minimization of scalar function of one or more variables. )。
参数
类型
含义
fun
callable
要最小化的目标函数。
fun(x, *args) -> float
其中 $x$ 是一个带有形状($n$)的一维数组,$args$ 是完全指定函数所需的固定参数的元组。
x0
ndarray, shape (n,)
初始猜测: 大小为($n$)的实元素的数组,其中 $n$ 是变量的数目。
args
tuple, optional
额外的参数传递给目标函数及其导数(fun、 jac 和 hess 函数)。
method
str or callable, optional
求解器的类型,如果没有给出,则根据问题是否有约束或边界,选择 BFGS、 L-BFGS-B、 SLSQP 中的一个。
jac
{callable, ‘2-point’, ‘3-point’, ‘cs’, bool}, optional
梯度向量的计算方法。只适用于 CG,BFGS,Newton-CG,L-BFGS-B,TNC,SLSQP,dogleg,trust-ncg,trust-krylov,trust-fine 和 trust-Constr。如果它是可调用的,那么它应该是一个返回梯度向量的函数
hess
{callable, ‘2-point’, ‘3-point’, ‘cs’, HessianUpdateStrategy}, optional
计算 Hessian 矩阵的一种方法。只适用于 Newton-CG,dogleg,trust-ncg,trust-krylov,trust-fine 和 trust-constr. 。如果它是可调用的,它应该返回黑森矩阵
hessp
callable, optional
目标函数的 Hessian 乘以任意向量 p。只适用于 Newton-CG,trust-ncg,trust-krylov,trust-Constr。只需要一个 Hessp 或者 Hess 就够了。如果提供 hess,那么 hessp 将被忽略。Hessp 必须计算 Hessian 乘以任意向量。
bounds
sequence or Bounds, optional
Nelder-Mead,L-BFGS-B,TNC,SLSQP,Powell 和 trust-conr 方法的变量界。
constraints
{Constraint, dict} or List of {Constraint, dict}, optional
约束条件。仅适用于 COBYLA、 SLSQP 和 trust-Constr。
tol
float, optional
终止公差。 指定 tol 后,所选的最小化算法会将一些相关的特定于求解器的公差设置为 tol。 要进行详细控制,请使用特定于求解器的选项。
options
dict, optional
求解器选项字典。除 TNC 外的所有方法都接受以下通用选项:
maxiter **int:**要执行的最大迭代次数。 根据方法,每次迭代可能使用多个函数评估。
disp bool: 设置为 True 可打印消息。
callback
callable, optional
在每次迭代之后调用。对于“ trust-conr”,它是一个带有签名的可调用函数
res
Optimize Result
优化结果表示为 OptimizeResult 对象。 重要的属性有:
x 解决方案数组
success 一个布尔标志,指示优化器是否成功退出,以及描述终止原因的消息。
有关其他属性的说明,请参阅 OptimizeResult。
注:
jac可选,代表jac有五种选项{callable, 2-point, 3-point, cs, bool},可任选其一。默认为None,即采用有限差分近似计算;2/3-point 或者 cs 采用2点、3点、中心差分近似计算;若为True,则目标函数需返回目标函数值和jac向量;若为callable,则提供jac计算函数。
hess 也有五种选项{callable, 2-point, 3-point, cs, HessianUpdateStrategy},但要注意,只有jac提供计算函数,hess才可以使用差分近似,我想这也是避免因差分二次近似导致数值耗散的缘故。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| # coding=utf-8 from scipy.optimize import minimize import numpy as np # demo 2 #计算 (2+x1)/(1+x2) - 3*x1+4*x3 的最小值 x1,x2,x3的范围都在0.1到0.9 之间 def fun(args): a,b,c,d=args v=lambda x: (a+x[0])/(b+x[1]) -c*x[0]+d*x[2] return v def con(args): # 约束条件 分为eq 和ineq #eq表示 函数结果等于0 ; ineq 表示 表达式大于等于0 x1min, x1max, x2min, x2max,x3min,x3max = args cons = ({'type': 'ineq', 'fun': lambda x: x[0] - x1min},\ {'type': 'ineq', 'fun': lambda x: -x[0] + x1max},\ {'type': 'ineq', 'fun': lambda x: x[1] - x2min},\ {'type': 'ineq', 'fun': lambda x: -x[1] + x2max},\ {'type': 'ineq', 'fun': lambda x: x[2] - x3min},\ {'type': 'ineq', 'fun': lambda x: -x[2] + x3max}) return cons if __name__ == "__main__": #定义常量值 args = (2,1,3,4) #a,b,c,d #设置参数范围/约束条件 args1 = (0.1,0.9,0.1, 0.9,0.1,0.9) #x1min, x1max, x2min, x2max cons = con(args1) #设置初始猜测值 x0 = np.asarray((0.5,0.5,0.5)) res = minimize(fun(args), x0, method='SLSQP',constraints=cons) print(res.fun) print(res.success) print(res.x)
|