太原市網(wǎng)站網(wǎng)絡廣告有哪些形式
目錄
- 0. 官方說明
- 1. Parameters
- 2. Returns
- 3. 案例
- 1)無約束求極值
- 2)有約束求極值
- 參考資料
0. 官方說明
在 python 里用非線性規(guī)劃求極值,最常用的就是 scipy.optimize.minimize()。最小化一個或多個變量的標量函數(shù)。
(Minimization of scalar function of one or more variables.)
scipy.optimize.minimize(fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None)
在python腳本頁面中,點擊Ctrl+B或者Ctrl+左擊,即可查看函數(shù)的定義、函數(shù)的使用案例
。
1. Parameters
-
fun
:需要被最小化的目標函數(shù)(objective function) -
x0
:初始猜想值,形狀(n, )- 大小為 (n,) 的實數(shù)元素數(shù)組,其中 “n” 是自變量的數(shù)量。
-
args
:tuple元組,可選的 Optional- 傳遞給目標函數(shù)及其導數(shù)(fun、jac 和 hess 函數(shù))的額外參數(shù)。
-
method
: str or callable, 可選的
求解器的類型,應該從下面選取一種
(如果未給出,則選擇 BFGS、L-BFGS-B、SLSQP 之一,具體取決于問題是否有約束或界限。)- 'Nelder-Mead' :ref:`(see here) <optimize.minimize-neldermead>`- 'Powell' :ref:`(see here) <optimize.minimize-powell>`- 'CG' :ref:`(see here) <optimize.minimize-cg>`- 'BFGS' :ref:`(see here) <optimize.minimize-bfgs>`- 'Newton-CG' :ref:`(see here) <optimize.minimize-newtoncg>`- 'L-BFGS-B' :ref:`(see here) <optimize.minimize-lbfgsb>`- 'TNC' :ref:`(see here) <optimize.minimize-tnc>`- 'COBYLA' :ref:`(see here) <optimize.minimize-cobyla>`- 'SLSQP' :ref:`(see here) <optimize.minimize-slsqp>`- 'trust-constr':ref:`(see here) <optimize.minimize-trustconstr>`- 'dogleg' :ref:`(see here) <optimize.minimize-dogleg>`- 'trust-ncg' :ref:`(see here) <optimize.minimize-trustncg>`- 'trust-exact' :ref:`(see here) <optimize.minimize-trustexact>`- 'trust-krylov' :ref:`(see here) <optimize.minimize-trustkrylov>`- custom - a callable object (added in version 0.14.0),
-
jac
: {callable, ‘2-point’, ‘3-point’, ‘cs’, bool}, optional ,目標函數(shù)的雅可比矩陣。 -
bounds
:可選項,變量的邊界(僅適用于L-BFGS-B,TNC和SLSQP)。以(min,max)對的形式定義 x 中每個元素的邊界。如果某個參數(shù)在 min 或者 max 的一個方向上沒有邊界,則用 None 標識。如(None, max) -
constraints
:約束條件(只對 COBYLA 和 SLSQP)。 -
bounds
:可選項,變量的邊界(僅適用于L-BFGS-B,TNC和SLSQP)。以(min,max)對的形式定義 x 中每個元素的邊界。如果某個參數(shù)在 min 或者 max 的一個方向上沒有邊界,則用 None 標識。如(None, max) -
constraints
:約束條件(只對 COBYLA 和 SLSQP)。
2. Returns
res
:優(yōu)化結(jié)果- 優(yōu)化結(jié)果表示為“OptimizeResult”對象。
- 重要的屬性是:
x
解決方案數(shù)組,success
一個布爾標志,指示優(yōu)化器是否成功退出,message
描述終止原因。 有關其他屬性的描述,請參閱OptimizeResult
。
3. 案例
1)無約束求極值
計算 1/x+x 的最小值
# coding=utf-8
from scipy.optimize import minimize
import numpy as np#demo 1
#計算 1/x+x 的最小值def fun(args):a=argsv=lambda x:a/x[0] +x[0]return vif __name__ == "__main__":args = (1) #ax0 = np.asarray((2)) # 初始猜測值res = minimize(fun(args), x0, method='SLSQP')print(res.fun) # 函數(shù)的最小值print(res.success)print(res.x) # x 解決方案數(shù)組
執(zhí)行結(jié)果:
2.0000000815356342 (函數(shù)的最小值)
True
[1.00028559]
2)有約束求極值
例2-1
計算 (2+x1)/(1+x2) - 3x1+4x3 的最小值, x1, x2, x3 都處于[0.1, 0.9] 區(qū)間內(nèi)。
def fun(args):a,b,c,d = argsv = lambda x: (a+x[0])/(b+x[1]) -c*x[0]+d*x[2]return vdef con(args):# 約束條件 分為eq 和ineq# eq表示 函數(shù)結(jié)果等于0 ; ineq 表示 表達式大于等于0 x1min, x1max, x2min, x2max, x3min, x3max = argscons = ({'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# 定義常量值
args = (2,1,3,4) # a,b,c,d# 設置參數(shù)范圍/約束條件
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)
執(zhí)行結(jié)果:
- 0.773684210526435
- True
- [0.9 0.9 0.1]
例2-2
解決以下優(yōu)化問題
m i n i m i z e x [ 0 ] , x [ 1 ] l o g 2 ( 1 + x [ 0 ] × 2 3 + l o g 2 x [ 1 ] × 3 4 ) minimize_{x[0],x[1]}log_2(1+\frac{x[0]\times2}{3}+log_2\frac{x[1]\times3}{4}) minimizex[0],x[1]?log2?(1+3x[0]×2?+log2?4x[1]×3?)
s . t . s.t. s.t.
l o g 2 ( 1 + x [ 0 ] × 2 5 ) ≥ 5 log_2(1+\frac{x[0]\times2}{5})\geq5 log2?(1+5x[0]×2?)≥5
l o g 2 ( 1 + x [ 0 ] × 6 4 ) ) ≥ 5 log_2(1+\frac{x[0]\times6}{4}))\geq5 log2?(1+4x[0]×6?))≥5
# 目標函數(shù)
def fun(a,b,c,d):def v(x):return np.log2(1+x[0]*a/b)+np.log2(1+x[1]*c/d)return v#限制條件函數(shù)
def con(a,b,i):def v(x):return np.log2(1 + x[i] * a / b)-5return v# 定義常量值
args = [2, 1, 3, 4] # a,b,c,d
args1 = [2, 5, 6, 4] # 設置初始猜測值
x0 = np.asarray((0.5, 0.5))#設置限制條件
cons = ({'type': 'ineq', 'fun': con(args1[0],args1[1],0)},{'type': 'ineq', 'fun': con(args1[2],args1[3],1)},)res = minimize(fun(args[0], args[1], args[2], args[3]), x0, constraints=cons)
print(res.fun)
print(res.success)
print(res.x)
輸出結(jié)果:
- 11.329796332293162
- True
- [77.5 20.66666658]
參考資料
[1] 官網(wǎng)資料 2022.9.19
[2] 非線性規(guī)劃(scipy.optimize.minimize);