线性规化 - 运输问题(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=(x1,x2,…,x6),
w=(5,6,4,6,3,7)
xminwTxs.t.⎩⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎧x1+x2+x3≤300x4+x5+x6≤500x1+x3=200x2+x4=300x3+x6=250xi≥0
即
xminwTxs.t.⎩⎪⎨⎪⎧Aubx−bub≤0Aeqx−beq=0−x≤0
其中
Aub=[101010010101]
(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]
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)
xminwTxs.t.⎩⎪⎨⎪⎧Aubx−bub≤0Aeqx−beq=0−x≤0
拉格朗日方程(lagrangian formula)
L(x,α,β,γ)=wTx+αT(Aubx−bub)+βT(Aeqx−beq)−γTx
其中
α≥0,γ≥0
定义
P(x)=α≥0,β,γ≥0supL(x,α,β,γ)={wTx,+∞,if x is feasibleotherwise
因此
xminwTx⇔xinfP(x)=xinfα≥0,β,γ≥0supL(x,α,β,γ)
定义
D(α,β,γ)=xinfL(x,α,β,γ)=wTx+αT(Aubx−bub)+βT(Aeqx−beq)−γTx=(wT+αTAub+βTAeq−γT)x−αTbub−βTbeq={−αTbub−βTbeq,−∞,if wT+αTAub+βTAeq−γT=0otherwise
因此,对偶问题
α≥0,β,γ≥0supD(α,β,γ)=α≥0,β,γ≥0supxinfL(x,α,β,γ)
可以写为:
α,βmax(−αTbub−βTbeq)s.t.{wT+αTAub+βTAeq≥0α≥0
即:
α,βmin(bubTα+beqTβ)s.t.{wT+αTAub+βTAeq≥0α≥0