目标函数和约束条件均为线性函数时的问题,成为线性规划问题
m a x z = ∑ j = 1 n c j x j s . t . { ∑ j = 1 n a i j x j ≤ b x ≥ 0 , j = 1 , 2 , . . . , n max\ z = \sum_{j=1}^n c_jx_j \\ s.t. \begin{cases} \sum_{j=1}^n a_{ij}x_j\leq b \\ x\ge 0\ ,j=1,2,...,n \end{cases} max z=j=1∑ncjxjs.t.{∑j=1naijxj≤bx≥0 ,j=1,2,...,n
为规范,将线性规划的标准形式定为:
min c T x , s . t . { A u p ⋅ x = b U p A e q ⋅ x = b e q l b ≤ x ≤ u b \min c^Tx, \\ s.t. \begin{cases} A_{up}\cdot x = b_{Up} \\ A_{eq}\cdot x = b_{eq} \\ lb \leq x \leq ub \end{cases} mincTx,s.t.⎩⎪⎨⎪⎧Aup⋅x=bUpAeq⋅x=beqlb≤x≤ub
python代码如下,其中 r r r为解:
import numpy as np
from scipy.optimize import linprog
c = np.array([-2, -3, +5])
A_up = np.array([[-2, 5, -1], [1, 3, 1]])
b_up = np.array([-10, 12])
A_eq = np.array([[1, 1, 1]])
b_eq = np.array([7])
r = linprog(c, A_up, b_up, A_eq, b_eq, bounds=((0, None), (0, None), (0, None)))
print(r)
运行结果实例:
con: array([1.80711979e-09])
fun: -14.571428565645085
message: 'Optimization terminated successfully.'
nit: 5
slack: array([-2.24563479e-10, 3.85714286e+00])
status: 0
success: True
x: array([6.42857143e+00, 5.71428571e-01, 2.35900788e-10])
求解下列线性规划问题:
min z = 2 x 1 + 3 x 2 + x s . t . { x 1 + 4 x 2 + 2 x 3 ≥ 8 3 x 1 + 2 x 2 ≥ 6 x 1 , x 2 , x 3 ≥ 0 \min z = 2x_1+3x_2+x \\ s.t. \begin{cases} x_1+4x_2+2x_3 \ge 8 \\ 3x_1+2x_2 \ge 6 \\ x_1, x_2, x_3 \ge 0 \end{cases} minz=2x1+3x2+xs.t.⎩⎪⎨⎪⎧x1+4x2+2x3≥83x1+2x2≥6x1,x2,x3≥0
将线性规划转化成以下形式:
min z = 2 x 1 + 3 x 2 + x s . t . { [ − 1 − 4 − 2 − 3 − 2 0 ] [ x 1 x 2 x 3 ] ≤ [ − 8 − 6 ] x 1 , x 2 , x 3 ≥ 0 \min z = 2x_1+3x_2+x \\ s.t. \begin{cases} \begin{bmatrix} -1 & -4 & -2 \\ -3 & -2 & 0 \end{bmatrix} \begin{bmatrix} x_1\\x_2\\x_3 \end{bmatrix} \leq \begin{bmatrix} -8\\-6 \end{bmatrix} \\ x_1, x_2, x_3 \ge 0 \end{cases} minz=2x1+3x2+xs.t.⎩⎪⎪⎨⎪⎪⎧[−1−3−4−2−20]⎣⎡x1x2x3⎦⎤≤[−8−6]x1,x2,x3≥0
python代码如下:
import numpy as np
from scipy.optimize import linprog
c = np.array([2, 3, 1])
A_up = np.array([[-1, -4, -2], [-3, -2, 0]])
b_up = np.array([-8, -6])
r = linprog(c, A_ub=A_up, b_ub=b_up, bounds=((0, None), (0, None), (0, None)))
print(r)
结果:
con: array([], dtype=float64)
fun: 6.999999994872991
message: 'Optimization terminated successfully.'
nit: 3
slack: array([ 3.85261067e-09, -1.41066305e-08])
status: 0
success: True
x: array([1.17949641, 1.23075538, 0.94874104])
注:
scipy
的优化函数有限,在精度和优化程度上可能比不上专业优化软件MATLAB
,Lingo
等。
数学规划问题:
min ∣ x 1 ∣ + ∣ x 2 ∣ + . . . + ∣ x n ∣ s . t . A x ≤ b \min |x_1|+|x_2|+...+|x_n| \\ s.t. \ Ax\leq b min∣x1∣+∣x2∣+...+∣xn∣s.t. Ax≤b
求解下列数学规划问题:
min z = ∣ x 1 ∣ + 2 ∣ x 2 ∣ + 3 ∣ x 3 ∣ + 4 ∣ x 4 ∣ s . t . { x 1 − x 2 − x 3 + x 4 ≤ − 2 , x 1 − x 2 + x 3 − 3 x 4 ≤ − 1 , x 1 − x 2 − 2 x 3 + 3 x 4 ≤ − 1 2 \min z = |x_1|+2|x_2|+3|x_3|+4|x_4| \\ s.t. \begin{cases} x_1-x_2-x_3+x_4\leq -2, \\ x_1-x_2+x_3-3x_4 \leq -1, \\ x_1-x_2-2x_3+3x_4 \leq -\frac{1}{2} \end{cases} minz=∣x1∣+2∣x2∣+3∣x3∣+4∣x4∣s.t.⎩⎪⎨⎪⎧x1−x2−x3+x4≤−2,x1−x2+x3−3x4≤−1,x1−x2−2x3+3x4≤−21
将变量变换 u i = x i + ∣ x i ∣ 2 , v i = ∣ x i ∣ − x i 2 u_i=\frac{x_i+|x_i|}{2},v_i=\frac{|x_i|-x_i}{2} ui=2xi+∣xi∣,vi=2∣xi∣−xi,则可把模型变换为:
min c T y , s . t . { [ A , − A ] [ u v ] ≤ b u , v ≥ 0 \min c^Ty, \\s.t.\begin{cases}[A, -A]\begin{bmatrix}u\\v\end{bmatrix}\leq b \\u, v \ge 0\end{cases} mincTy,s.t.⎩⎨⎧[A,−A][uv]≤bu,v≥0