标准形式
m i n i m i z e c T x s u b j e c t t o A x = b s u b j e c t t o x ⩾ 0 w h e r e c ∈ R n , b ∈ R m , A ∈ R m × n \mathrm{minimize} \ \ \mathbf{c}^\mathsf{T} \mathbf{x} \\ \mathrm{subject \ to} \ \ \mathbf{Ax} = \mathbf{b} \\ \ \ \phantom{\mathrm{subject \ to} \ \ } \mathbf{x} \geqslant \mathbf{0} \\ where \ \mathbf{c} \in \mathbb{R}^n \ , \ \mathbf{b} \in \mathbb{R}^m \ , \ \mathbf{A} \in \mathbb{R}^{m \times n} minimize cTxsubject to Ax=b subject to x⩾0where c∈Rn , b∈Rm , A∈Rm×n
图解法
考虑问题:
max c T x s . t . A x ⩽ b s . t . x ⩾ 0 w h e r e x = [ x 1 , x 2 ] T a n d c T = [ 1 , 5 ] , A = [ 5 6 3 2 ] , b = [ 30 12 ] \max \ \mathbf{c}^\mathsf{T} \mathbf{x} \\ \mathrm{s.t.} \ \ \mathbf{Ax} \leqslant \mathbf{b} \\ \ \ \phantom{\mathrm{s.t.} \ \ } \mathbf{x} \geqslant \mathbf{0} \\[8pt] where \ \mathbf{x} = [x_1 , \ x_2]^{\mathsf{T}} \ and \\[6pt] \mathbf{c}^{\mathsf{T}} = [1, \ 5] , \\[6pt] \mathbf{A} = \begin{bmatrix} 5 \ \ 6 \\ 3 \ \ 2 \end{bmatrix} , \ \ \mathbf{b} = \begin{bmatrix} 30 \\ 12 \end{bmatrix} max cTxs.t. Ax⩽b s.t. x⩾0where x=[x1, x2]T andcT=[1, 5],A=[5 63 2], b=[3012]
可行域:
绘图代码:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.0, 8.0, 50)
y1 = (-5 / 6) * x + 5
y2 = (-3 / 2) * x + 6
y = np.minimum(y1, y2)
upper = (-1 / 5) * x + 5
lower = (-1 / 5) * x
plt.fill_between(x, 0, y, where=y >= 0, facecolor='blue', alpha=0.5)
plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--', label=r'$5x_1 + 6x_2 = 30$')
plt.plot(x, y2, color='orange', linewidth=1.0, linestyle='--', label=r'$3x_1 + 2x_2 = 12$')
plt.plot(x, upper, color='black', linewidth=1.5, label=r'$upper = x_1 + 5x_2 = 25$')
plt.plot(x, lower, color='black', linewidth=1.5, label=r'$lower = x_1 + 5x_2 = 0$')
ax = plt.gca() # get the current Axes
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
plt.xlim((-1.0, 6.0))
plt.ylim((-1.0, 8.0))
plt.xlabel(r'$x_1$')
plt.ylabel(r'$x_2$')
plt.legend(fontsize=14)
plt.show()
软件求解(scipy)
标准形式:
min c T x s u c h t h a t A u b ⩽ b u b , s u c h t h a t A e q x = b e q , s u c h t h a t l ⩽ x ⩽ u \min \ \mathbf{c}^{\mathsf{T}} \mathbf{x} \\ \mathrm{such \ that} \ \ \mathbf{A}_{ub} \leqslant \mathbf{b}_{ub} , \\ \phantom{\mathrm{such \ that}} \mathbf{A}_{eq} \mathbf{x} = \mathbf{b}_{eq} , \\ \phantom{\mathrm{such \ that}} \mathbf{l} \leqslant \mathbf{x} \leqslant \mathbf{u} min cTxsuch that Aub⩽bub,such thatAeqx=beq,such thatl⩽x⩽u
x \mathbf{x} x 是决策变量的向量形式
c , b u b , b e q , l , u \mathbf{c}, \ \mathbf{b}_{ub}, \ \mathbf{b}_{eq}, \ \mathbf{l}, \ \mathbf{u} c, bub, beq, l, u 是向量
A u b , A e q \mathbf{A}_{ub}, \ \mathbf{A}_{eq} Aub, Aeq 是矩阵
例题:
max z = 24 x 1 + 16 x 2 + 44 x 3 + 32 x 4 − 3 x