线性规划

Linear Programming

线性规划问题在高中数学中就已经接触过,确实是一个具有极大应用价值的问题。在介绍 LP 问题的解法之前,我们先引入一个可供把玩的例子:brewer's problem.

  • 仓库中有 480 单位的玉米,160 单位的啤酒花 (hops) 与 1190 单位的麦芽 (malt)。
  • 酿造一桶爱尔啤酒 (ale) 需要 3 单位玉米,4 单位啤酒花与 35 单位麦芽。
  • 酿造一同啤酒 (beer) 需要 15 单位玉米,4 单位啤酒花与 20 单位麦芽。
  • 一桶爱尔啤酒的利润为 \$13;一桶啤酒的利润为 \$23。

试问如何最大化酿酒厂的利润?根据高中数学的思路,我们设酿酒厂决定酿造 xx 桶爱尔啤酒,yy 桶啤酒。我们要求 objective function (目标函数) 13x+23y13x+23y 的值最大。并遵循以下 constraints:

  • corn: 5A+15B≤4805A+15B\leq 480.
  • hops: 4A+4B≤1604A+4B\leq 160.
  • malt: 35A+20B≤119035A+20B\leq 1190.
  • variable constraints: A,B≥0A,B\geq 0.

在以 ale 与 beer 为轴的二维平面中,每个 constraint (inequalities) 划分出一个 halfplane,这些 halfplanes 的交集是一个 convex polygon (凸多边形),我们称其为 feasible region。使得 objective function 最大的点一定位于 feasible region 的某个 extreme point (极点) 上。

Optimal solution occurs at an 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 nn nonnegative variables, subject to mm linear equations.
  • Input. real numbers aij,cj,bia_{ij}, c_j, b_i.
  • Output. real numbers xjx_j.

note that every constraint is in equality form

可以发现之前我们对 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 ZZ and equation corresponding to objective function.
  • Add slack variable to convert each inequality to equality.
  • Now a 6-dimensional problem (A,B,SC,SH,SM,ZA,B,S_C,S_H,S_M,Z).

add variable Z and slack variable

将 LP 问题写成标准形式,再提取变量和系数,所形成的矩阵称为 simplex tableaux (单纯形表)。如何理解单纯形表与 slack variables SC,SH,SMS_C,S_H,S_M 的含义?我们还是结合图像来说明。

每个极点对应一组 basis (基向量)

二维 feasible region 的每个极点都受到两个 constraints 的束缚:SC,SH,SMS_C,S_H,S_M 这三个 slack variables 就是 constraints 的一种具象表现。例,当 SH=SM=0S_H=S_M=0 时 (受 hops 与 malt 束缚),对应的极点为 (26,14)(26,14)。

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 mm of the nn variables. 通过在单纯形表中选择特定的 basis 可以得到其对应的 Basic feasible solution (BFS).

  • Set n−mn-m nonbasic variables to 00, solve for remaining mm variables.
  • Solve mm equations in mm unknowns.
  • If unique and feasible (lies in feasible region) →\to BFS.
  • BFS   ⟺  \iff extreme point.

既然在单纯形表中选择 basis 能够计算出 BFS,而 BFS 又对应极点,那么遍历每一个极点就相当于不断地在单纯形表中选择不同的 basis。在线性代数中,这种操作称为基变换。在建立单纯形表之后,持续对其进行基变换,就能实现遍历所有极点的效果。下面我们来具体走几个流程:

初始化:Initial basic feasible solution - 从原点 (0,0)(0,0) 开始遍历。

  • Start with slack variables {SC,SH,SM}\{S_C, S_H,S_M\} as the basis.
  • Set non-basic variables AA and BB to 00.
  • 3 equations in 3 unknowns yields SC=480,SH=160,SM=1190S_C=480,S_H=160,S_M=1190.

接下来我们开始遍历相邻的极点:具体的操作是选择一个变量作为 pivot,在进行对应的矩阵变换 (消去其他方程,包括 objective function 中的该变量) 后将该变量替换 basis 中原有的某个变量。如何选择作为 pivot 的变量?在这个例子中,下一个作为 pivot 的变量是 BB。

 in column 2 row 2

  • Why pivot on column 2 (corresponding to variable BB)?
    • [Bland's rule] Its objective function coefficient is positive. (因此能给 ZZ 带来正收益: each unit increase BB from 00 increases objective value by $23\$23.
    • Therefore pivoting on column 1 (corresponding to AA) also OK.
  • Why pivot on row 2?
    • Preserves feasibility by ensuring RHS ≥0\geq 0.
    • [Minimum ratio rule] min⁡{480/15‾,160/4,1190/20}\min \{ \underline{480/15},160/4,1190/20\}. 我们选取 ratio 最小的行作为基准进行消元。如果选择 row 3 或者 4,得到的解将是 basic infeasible solution (见上图)。

当 objective function 中所有变量对应的系数 (coefficient) 均小于 00 时,我们停止 pivoting (也因此停止了对极点的遍历)。能够证明此时的 basis feasible solution 是全局最优解。同样的例子,此时我们有:

simplex algorithm stops

  • In particular, Z=800−SC−2SHZ=800-S_C-2S_H.
  • Thus, optimal objective value Z∗≤800Z^*\leq 800 since SC,SH≥0S_C,S_H\geq 0.
  • Current BFS has value 800 (since SC=SH=0S_C=S_H=0) →\to optimal.

Remarkable property. In typical practical applications, simplex algorithm terminates after at most 2(m+n)2(m+n) pivots.

一个 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 min⁡(13A+15B)\min (13A+15B) with max⁡−13A−15B\max-13A-15B.
  • ≥\geq constraints. Replace 4A+4B≥1604A+4B\geq 160 with 4A+4B−SH=160,SH≥04A+4B-S_H=160, S_H\geq 0.
  • Unrestricted variables (未规定 B≥0B\geq 0). Replace BB with B=B0−B1,B0≥0,B1≥0B=B_0-B_1, B_0\geq 0, B_1\geq 0.

再探最大流问题:实际上,maxflow problem can be modeled as a linear program.

  • Variables. xvw=x_{vw}= flow on edge v→wv\to w.
  • Constraints. Capacity and flow conservation.
  • Objective function. Net flow into tt.

a little bit detour but works well

这给我们解决问题提供了一个新的 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).
CC BY-NC-SA 4.0
正在加载评论…