线性规划
Linear Programming
线性规划问题在高中数学中就已经接触过,确实是一个具有极大应用价值的问题。在介绍 LP 问题的解法之前,我们先引入一个可供把玩的例子:brewer's problem.
- 仓库中有 480 单位的玉米,160 单位的啤酒花 (hops) 与 1190 单位的麦芽 (malt)。
- 酿造一桶爱尔啤酒 (ale) 需要 3 单位玉米,4 单位啤酒花与 35 单位麦芽。
- 酿造一同啤酒 (beer) 需要 15 单位玉米,4 单位啤酒花与 20 单位麦芽。
- 一桶爱尔啤酒的利润为 \$13;一桶啤酒的利润为 \$23。
试问如何最大化酿酒厂的利润?根据高中数学的思路,我们设酿酒厂决定酿造
- corn:
. - hops:
. - malt:
. - variable constraints:
.
在以 ale 与 beer 为轴的二维平面中,每个 constraint (inequalities) 划分出一个 halfplane,这些 halfplanes 的交集是一个 convex polygon (凸多边形),我们称其为 feasible region。使得 objective function 最大的点一定位于 feasible region 的某个 extreme point (极点) 上。
Greedy property. Extreme point optimal iff no better adjacent extreme point.
Linear program 的 standard form (though no widely agreed) 如下:
- Maximize linear objective function of
nonnegative variables, subject to linear equations. - Input. real numbers
. - Output. real numbers
.
可以发现之前我们对 brewer's problem 的描述是不标准的;所有的 constraints 都是不等式而非等式,因此无法构造对应的矩阵。如何用 standard form 来描述 LP 问题,[stay tuned]。
Simplex Algorithm
如果我在高中,解决 LP 问题的方法就是构造出 feasible region 并尝试所有的 extreme points 并取最大值作为 objective function 的答案。
然而,如果采用这个朴素的做法,对应的程序将十分复杂;我们将需要分别求出凸多边形与其所有的极点。Simplex algorithm 则从线性代数的角度对该问题进行了进一步抽象,大大简化了程序实现。
首先,我们通过向 inequality constraints 中加入 slack variable 的形式把一般描述的 LP 转化为 standard form。以 brewer's problem 为例:
- Add variable
and equation corresponding to objective function. - Add slack variable to convert each inequality to equality.
- Now a 6-dimensional problem (
).
将 LP 问题写成标准形式,再提取变量和系数,所形成的矩阵称为 simplex tableaux (单纯形表)。如何理解单纯形表与 slack variables
二维 feasible region 的每个极点都受到两个 constraints 的束缚:
Simplex algorithm 利用了这一性质,结合线性代数对每个极值点进行遍历。其基本思想是:
- Start at some extreme point (usually origin).
- Pivot from one extreme point to an adjacent one (never decreasing objective function).
- Repeat until optimal.
首先,根据线性代数中的相关知识,我们知道 A basis is a subset of
- Set
nonbasic variables to , solve for remaining variables. - Solve
equations in unknowns. - If unique and feasible (lies in feasible region)
BFS. - BFS
extreme point.
既然在单纯形表中选择 basis 能够计算出 BFS,而 BFS 又对应极点,那么遍历每一个极点就相当于不断地在单纯形表中选择不同的 basis。在线性代数中,这种操作称为基变换。在建立单纯形表之后,持续对其进行基变换,就能实现遍历所有极点的效果。下面我们来具体走几个流程:
初始化:Initial basic feasible solution - 从原点
- Start with slack variables
as the basis. - Set non-basic variables
and to . - 3 equations in 3 unknowns yields
.
接下来我们开始遍历相邻的极点:具体的操作是选择一个变量作为 pivot,在进行对应的矩阵变换 (消去其他方程,包括 objective function 中的该变量) 后将该变量替换 basis 中原有的某个变量。如何选择作为 pivot 的变量?在这个例子中,下一个作为 pivot 的变量是
- Why pivot on column 2 (corresponding to variable
)? - [Bland's rule] Its objective function coefficient is positive. (因此能给
带来正收益: each unit increase from increases objective value by . - Therefore pivoting on column 1 (corresponding to
) also OK.
- [Bland's rule] Its objective function coefficient is positive. (因此能给
- Why pivot on row 2?
- Preserves feasibility by ensuring RHS
. - [Minimum ratio rule]
. 我们选取 ratio 最小的行作为基准进行消元。如果选择 row 3 或者 4,得到的解将是 basic infeasible solution (见上图)。
- Preserves feasibility by ensuring RHS
当 objective function 中所有变量对应的系数 (coefficient) 均小于
- In particular,
. - Thus, optimal objective value
since . - Current BFS has value 800 (since
) optimal.
Remarkable property. In typical practical applications, simplex algorithm terminates after at most
一个 best practice 建议: Do NOT implement simplex algorithm yourself!
LP Reductions
LP 问题的应用范围远比想象中大;我们目前接触过的很多问题都能抽象为 LP 问题的形式。将某个问题抽象 (或者说,归约到) 为 LP 问题模型的标准步骤如下:
- Identify variables.
- Define constraints (inequality and equations).
- Define objective function.
- Convert to standard form.
将问题转化为 LP standard form 这一步有许多小 tricks,我们之前看到的通过 slack variables 将 inequality 转化为 equations 是其中的一个。
- Minimization problem. Replace
with . constraints. Replace with . - Unrestricted variables (未规定
). Replace with .
再探最大流问题:实际上,maxflow problem can be modeled as a linear program.
- Variables.
flow on edge . - Constraints. Capacity and flow conservation.
- Objective function. Net flow into
.
这给我们解决问题提供了一个新的 insight: 对于任意的 optimization problem 例如最大流,二分图最大匹配,最短路问题,最小生成树问题等等,我们有两种解决问题的思路:
- Approach 1: Use a specialized algorithm to solve it. Fast but vast literature on algorithms.
- Approach 2: Use linear programming.
- Many programs are easily modeled as LPs.
- Commercial solvers can solve those LPs.
- Might be slower than specialized solution (but we might not care).






