线性规化 - 运输问题

线性规化 - 运输问题(transportation problem)

运输问题:有两个城市北京和上海,分别拥有300件衣服和500件衣服,另外有三个城市分别是1,2,3分别需要200,300,250件衣服。现在需要把衣服从北京和上海运送到城市1,2,3。 假定每运输一件衣服产生的代价为:

  • 北京 -> 1: 5
  • 北京 -> 2: 6
  • 北京 -> 3: 4
  • 上海 -> 1: 6
  • 上海 -> 2: 3
  • 上海 -> 3: 7

问题:最小化成本。

(a)线性规化方程(linear programming formulation)的标准形式(stanford form)

x = ( x 1 , x 2 , , x 6 ) \mathbf{x} = (x_{1}, x_{2}, \dots, x_{6}) , w = ( 5 , 6 , 4 , 6 , 3 , 7 ) \mathbf{w} = (5, 6, 4, 6, 3, 7)

min x w T x s . t . { x 1 + x 2 + x 3 300 x 4 + x 5 + x 6 500 x 1 + x 3 = 200 x 2 + x 4 = 300 x 3 + x 6 = 250 x i 0 \begin{aligned} & \min_{\mathbf{x}} \mathbf{w}^{\text{T}} \mathbf{x} \\ & s.t. \begin{cases} x_{1} + x_{2} + x_{3} \leq 300 \\ x_{4} + x_{5} + x_{6} \leq 500 \\ x_{1} + x{3} = 200 \\ x_{2} + x{4} = 300 \\ x_{3} + x{6} = 250 \\ x_{i} \geq 0 \end{cases} \end{aligned}

min x w T x s . t . { A ub x b ub 0 A eq x b eq = 0 x 0 \begin{aligned} & \min_{\mathbf{x}} \mathbf{w}^{\text{T}} \mathbf{x} \\ & s.t. \begin{cases} \mathbf{A}_{\text{ub}} \mathbf{x} - \mathbf{b}_{\text{ub}} \leq 0 \\ \mathbf{A}_{\text{eq}} \mathbf{x} - \mathbf{b}_{\text{eq}} = 0 \\ - \mathbf{x} \leq 0 \end{cases} \end{aligned}

其中

A ub = [ 1 1 1 0 0 0 0 0 0 1 1 1 ] \mathbf{A}_{\text{ub}} = \begin{bmatrix} 1 & 1 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 1 & 1 \end{bmatrix}

(b)lp solver求解

from scipy.optimize import linprog
import numpy as np

w = np.array([5, 6, 4, 6, 3, 7])

A_ub = np.array([[1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1]])
b_ub = np.array([300, 500])

A_eq = np.array([[1, 0, 0, 1, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 0, 1]])
b_eq = np.array([200, 300, 250])

ret = linprog(c=w, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq)

if ret.success:
    x_opt = ret.x
    cost = ret.fun
else:
    x_opt = None
    cost = None
    
print("cost: {:f} with x = {}".format(cost, x_opt))
cost: 3049.999996 with x = [5.00000000e+01 3.80939619e-08 2.49999999e+02 1.50000000e+02
 2.99999999e+02 1.15679510e-07]
# rounding to integer
x_opt = [int(round(num)) for num in x_opt]
cost = np.sum(c * x)
print("cost: {:d} with x = {}".format(cost, x_opt))
cost: 3050 with x = [50, 0, 250, 150, 300, 0]

(c): 线性规化问题的对偶形式(dual form)

  • 主问题:

min x w T x s . t . { A ub x b ub 0 A eq x b eq = 0 x 0 \begin{aligned} & \min_{\mathbf{x}} \mathbf{w}^{\text{T}} \mathbf{x} \\ & s.t. \begin{cases} \mathbf{A}_{\text{ub}} \mathbf{x} - \mathbf{b}_{\text{ub}} \leq 0 \\ \mathbf{A}_{\text{eq}} \mathbf{x} - \mathbf{b}_{\text{eq}} = 0 \\ - \mathbf{x} \leq 0 \end{cases} \end{aligned}

拉格朗日方程(lagrangian formula)

L ( x , α , β , γ ) = w T x + α T ( A ub x b ub ) + β T ( A eq x b eq ) γ T x \begin{aligned} \mathcal{L}(\mathbf{x}, \mathbf{\alpha}, \mathbf{\beta}, \mathbf{\gamma}) & = \mathbf{w}^{\text{T}} \mathbf{x} + \mathbf{\alpha}^{\text{T}} (\mathbf{A}_{\text{ub}} \mathbf{x} - \mathbf{b}_{\text{ub}}) + \mathbf{\beta}^{\text{T}} (\mathbf{A}_{\text{eq}} \mathbf{x} - \mathbf{b}_{\text{eq}}) - \mathbf{\gamma}^{\text{T}} \mathbf{x} \end{aligned}

其中

α 0 , γ 0 \mathbf{\alpha} \geq 0, \mathbf{\gamma} \geq 0

定义

P ( x ) = sup α 0 , β , γ 0 L ( x , α , β , γ ) = { w T x , if  x  is feasible + , otherwise \begin{aligned} \mathcal{P}(\mathbf{x}) & = \sup_{\mathbf{\alpha} \geq 0, \mathbf{\beta}, \mathbf{\gamma} \geq 0} \mathcal{L}(\mathbf{x}, \mathbf{\alpha}, \mathbf{\beta}, \mathbf{\gamma}) \\ & = \begin{cases} \mathbf{w}^{\text{T}} \mathbf{x}, & \text{if } \mathbf{x} {\text{ is feasible}} \\ + \infty, & \text{otherwise} \\ \end{cases} \end{aligned}

因此

min x w T x inf x P ( x ) = inf x sup α 0 , β , γ 0 L ( x , α , β , γ ) \begin{aligned} \min_{\mathbf{x}} \mathbf{w}^{\text{T}} \mathbf{x} & \Leftrightarrow \inf_{\mathbf{x}} \mathcal{P}(\mathbf{x}) = \inf_{\mathbf{x}} \sup_{\mathbf{\alpha} \geq 0, \mathbf{\beta}, \mathbf{\gamma} \geq 0} \mathcal{L}(\mathbf{x}, \mathbf{\alpha}, \mathbf{\beta}, \mathbf{\gamma}) \end{aligned}

  • 对偶问题:

定义

D ( α , β , γ ) = inf x L ( x , α , β , γ ) = w T x + α T ( A ub x b ub ) + β T ( A eq x b eq ) γ T x = ( w T + α T A ub + β T A eq γ T ) x α T b ub β T b eq = { α T b ub β T b eq , if  w T + α T A ub + β T A eq γ T = 0 , otherwise \begin{aligned} \mathcal{D}(\mathbf{\alpha}, \mathbf{\beta}, \mathbf{\gamma}) & = \inf_{\mathbf{x}} \mathcal{L}(\mathbf{x}, \mathbf{\alpha}, \mathbf{\beta}, \mathbf{\gamma}) \\ & = \mathbf{w}^{\text{T}} \mathbf{x} + \mathbf{\alpha}^{\text{T}} (\mathbf{A}_{\text{ub}} \mathbf{x} - \mathbf{b}_{\text{ub}}) + \mathbf{\beta}^{\text{T}} (\mathbf{A}_{\text{eq}} \mathbf{x} - \mathbf{b}_{\text{eq}}) - \mathbf{\gamma}^{\text{T}} \mathbf{x} \\ & = (\mathbf{w}^{\text{T}} + \mathbf{\alpha}^{\text{T}} \mathbf{A}_{\text{ub}} + \mathbf{\beta}^{\text{T}} \mathbf{A}_{\text{eq}} - \mathbf{\gamma}^{\text{T}}) \mathbf{x} - \mathbf{\alpha}^{\text{T}} \mathbf{b}_{\text{ub}} - \mathbf{\beta}^{\text{T}} \mathbf{b}_{\text{eq}} \\ & = \begin{cases} - \mathbf{\alpha}^{\text{T}} \mathbf{b}_{\text{ub}} - \mathbf{\beta}^{\text{T}} \mathbf{b}_{\text{eq}}, & \text{if } \mathbf{w}^{\text{T}} + \mathbf{\alpha}^{\text{T}} \mathbf{A}_{\text{ub}} + \mathbf{\beta}^{\text{T}} \mathbf{A}_{\text{eq}} - \mathbf{\gamma}^{\text{T}} = 0 \\ - \infty, & \text{otherwise} \\ \end{cases} \end{aligned}

因此,对偶问题

sup α 0 , β , γ 0 D ( α , β , γ ) = sup α 0 , β , γ 0 inf x L ( x , α , β , γ ) \begin{aligned} \sup_{\mathbf{\alpha} \geq 0, \mathbf{\beta}, \mathbf{\gamma} \geq 0} \mathcal{D}(\mathbf{\alpha}, \mathbf{\beta}, \mathbf{\gamma}) & = \sup_{\mathbf{\alpha} \geq 0, \mathbf{\beta}, \mathbf{\gamma} \geq 0} \inf_{\mathbf{x}} \mathcal{L}(\mathbf{x}, \mathbf{\alpha}, \mathbf{\beta}, \mathbf{\gamma}) \end{aligned}

可以写为:

max α , β ( α T b ub β T b eq ) s . t . { w T + α T A ub + β T A eq 0 α 0 \begin{aligned} & \max_{\mathbf{\alpha}, \mathbf{\beta}}(- \mathbf{\alpha}^{\text{T}} \mathbf{b}_{\text{ub}} - \mathbf{\beta}^{\text{T}} \mathbf{b}_{\text{eq}}) \\ & s.t. \begin{cases} \mathbf{w}^{\text{T}} + \mathbf{\alpha}^{\text{T}} \mathbf{A}_{\text{ub}} + \mathbf{\beta}^{\text{T}} \mathbf{A}_{\text{eq}} \geq 0 \\ \mathbf{\alpha} \geq 0 \\ \end{cases} \end{aligned}

即:

min α , β ( b ub T α + b eq T β ) s . t . { w T + α T A ub + β T A eq 0 α 0 \begin{aligned} & \min_{\mathbf{\alpha}, \mathbf{\beta}}( \mathbf{b}_{\text{ub}}^{\text{T}} \mathbf{\alpha} + \mathbf{b}_{\text{eq}}^{\text{T}} \mathbf{\beta} ) \\ & s.t. \begin{cases} \mathbf{w}^{\text{T}} + \mathbf{\alpha}^{\text{T}} \mathbf{A}_{\text{ub}} + \mathbf{\beta}^{\text{T}} \mathbf{A}_{\text{eq}} \geq 0 \\ \mathbf{\alpha} \geq 0 \\ \end{cases} \end{aligned}

发布了103 篇原创文章 · 获赞 162 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/zhaoyin214/article/details/105466426