第 12 章
线性规划入门:建模、标准形与对偶
用饮食与烘焙两个问题写出线性规划,说明标准形与规范形之间如何互相转换,再从「把约束加权相加」推出对偶规划,并证明弱对偶。
Introduction to Linear Programming: Modelling, Standard Form and Duality
We build linear programs for a diet problem and a bake sale, convert freely between standard and canonical form, then derive the dual by weighting constraints and prove weak duality.
篇内目录
- What linear programming does
- Timeline
- Example 1: the diet problem
- Example 2: the bake sale
- Two standard forms
- Basic feasible solutions and the simplex method
- Duality: upper bounds from combining constraints
- Weak and strong duality
- What this lecture establishes
- Comprehension check
- Relation to neighbouring lectures
线性规划在做什么
线性规划(LP)就是:在线性约束下,最大化或最小化一个线性函数。它是整个优化的发动机——课程提到,1987 年 CPLEX 问世后成为最赚钱的求解器之一,背后的商业价值说明了它的地位。
本讲做四件事:写两个模型、定义标准形与规范形、说明两者如何互转、推出对偶。
时间轴
| 时间 | 内容 |
|---|---|
| 0:00 | 线性规划的地位与用途 |
| 1:54 | 饮食问题:营养需求与两种谷物 |
| 10:04 | 烘焙问题:曲奇与蛋白饼 |
| 14:55 | 可行解、最优解;在平面上画可行域 |
| 19:39 | 标准形与规范形 |
| 22:16 | 任意 LP 化归为两种形式的四类变换 |
| 28:44 | 为什么需要两种形式:单纯形法用标准形,对偶用规范形 |
| 31:17 | 基本可行解与单纯形法的思路 |
| 41:18 | 对偶:从约束的加权组合得到上界 |
| 57:26 | 规范形的对偶公式 |
| 59:53 | 弱对偶定理及其证明 |
| 1:02:26 | 强对偶定理(陈述) |
| 1:06:48 | 对偶的对偶是原问题 |
例一:饮食问题
给小鼠配餐。需要 单位淀粉、 单位蛋白质、 单位维生素。谷物 1 每单位提供 ,价格 ;谷物 2 每单位提供 ,价格 。
设 分别是两种谷物的用量。模型:
(价格按美分计。)最后一行的非负约束容易被忘掉,但它不是装饰:没有它,模型可以把 当作「省钱」的解,而负的谷物用量没有物理意义。线性规划软件通常把非负约束单独处理。
建模的固定套路:先选变量,再写目标函数,再写约束,最后交给求解器。本课不教怎么手算求解——实际工作中你也不会手算。
例二:烘焙问题
卖曲奇和蛋白饼。一批曲奇需要 个蛋、 杯糖、 杯面粉;一批蛋白饼需要 个蛋、 杯糖、 杯面粉。现有 个蛋、 杯糖、 杯面粉。两批都卖 美元。
设 为曲奇批数, 为蛋白饼批数:
可行域。 两条变量时可以直接画。第一条约束的边界过 与 ;第二条过 与 。面粉约束 完全多余:满足前两条的点一定满足它。可行域是这两条直线与坐标轴围成的四边形。
可行解是满足全部约束的点(不必最优);最优解是使目标函数最优的可行解。例如 可行,目标值 ; 也可行,目标值 。后面会证明 就是最优值。
两种标准形式
标准形(standard form):
规范形(canonical form):
定理。 任何线性规划都可以化成标准形或规范形。
四类变换:
1. 最小化改最大化。 等价于 ,最优解相同,最优值取相反数。
2. 自由变量拆成两个非负变量。 若 可正可负,令
任意实数都能写成两个非负数之差(例如 ),于是所有变量都非负。
3. 不等式加松弛/剩余变量变等式。
其中 叫松弛变量。
其中 叫剩余变量。
4. 等式拆成两个不等式, 用取负号变 。
而 再写成 ,就落进规范形。
两个形式的分工:单纯形法用标准形,因为要解线性方程组找基本可行解;对偶理论用规范形,因为约束全是 ,加权相加时不等号方向一致。
基本可行解与单纯形法
定理。 有最优解的线性规划,一定存在基本可行解是最优解。
几何上,基本可行解就是可行域的顶点。回忆烘焙问题的可行域是个四边形,四个顶点中必有一个使 最大。
为什么叫「基本」?把 LP 写成标准形,每个松弛变量对应一列。选一组线性无关的列构成基,令其余变量为 ,解出基变量,就得到一个基本解;若它非负,就是基本可行解。
单纯形法:从一个基本可行解出发,沿可行域的棱走到相邻顶点,每步都让目标函数变大,直到没有更好的邻点。Dantzig 提出后实践效果极好,但很长一段时间里,对任何选边策略都能构造出需要指数步的例子。直到 Spielman 与 Teng 提出平滑分析,才在理论上说明了「几乎总是很快」。
对偶:从约束组合得到上界
回到烘焙问题。可行解给出下界: 可行,目标值 ,所以最优值至少 。
上界从哪来? 把约束加权相加。取权重 、:
左边两个括号分别不超过 与 ,而系数非负,所以
上下界相等,最优值确定为 。这组权重就是对偶最优解,它同时是一个「最优性证书」。
一般地,对规范形
取 作权重,把第 行乘以 再相加。要让左边能够覆盖目标函数的系数,需要
在这些条件下,
于是 是一个上界。要让上界尽可能紧,就求
这就是对偶规划。原问题与对偶的对应关系:
| 原问题(规范形) | 对偶问题 |
|---|---|
| 最大化 | 最小化 |
| 约束矩阵 | 转置 |
| 约束右端 | 变成目标函数系数 |
| 目标系数 | 变成约束右端 |
为什么 ?因为如果某个 ,乘上 约束会翻转不等号,相加就没有意义了。
弱对偶与强对偶
弱对偶定理。 若 是原问题可行解、 是对偶可行解,则
证明。 由对偶可行性 ,两边左乘 :
由原问题可行性 及 ,左乘 :
串联起来即得。整个证明只用了「非负权重乘不等号不翻向」这一件事。
强对偶定理(陈述,下一讲证明)。 若原问题的最优值 有限,则对偶最优值 也有限,且 。
对偶的对偶是原问题。 把对偶 、、 先化成规范形(取负号变最大化、变 ),再取对偶,得到 、、;再取负号就回到原问题。名字「对偶」由此而来。
本节结论
- 线性规划 = 线性目标 + 线性约束 + 非负约束;建模顺序是变量、目标、约束。
- 标准形是 ;规范形是 ;两者可以互相转换。
- 转换四招:取负、拆自由变量、加松弛/剩余变量、等式拆成两个不等式。
- 基本可行解是可行域的顶点,单纯形法沿棱走到最优顶点。
- 对偶 = 用非负权重把约束相加,得到最优值的上界;权重就是灵敏度。
- 弱对偶:任何原可行值不超过任何对偶可行值。
理解检验
1. 写出饮食问题的完整模型,并说明为什么非负约束不能省。
参考答案:
省掉 后,模型允许负的用量,而负用量可以减少「成本」、又能无限制地满足 约束(取足够负的值也不行——实际上会让营养约束更难过,但目标会趋向 ,问题变成无界或荒谬)。更本质的理由是:用量在物理上不能为负,非负性属于问题本身,而不是求解技巧。
2. 把下面的问题化成规范形:,约束 ,,, 自由。
参考答案:先令 ,;再把最小化取负变最大化;最后把 约束两边取负:
3. 用松弛变量把 写成等式;如果是 ,又该怎么写?
参考答案: 引入松弛变量 ,写成 ; 引入剩余变量 ,写成 。两者都保持「原不等式与等式解集相同」。
4. 对烘焙问题,给出一个可行解、一个上界证书,并说明最优值。
参考答案:可行解 :,,满足全部约束,目标值 。上界证书取对偶可行解 :;对偶约束为 、,非负。上下界相等,故最优值为 。
5. 证明弱对偶。
参考答案:由 与 得 ;由 、 与 得 。串联:,即原问题任意可行值不超过对偶任意可行值。
6. 为什么把等式拆成两个不等式,而不是别的方式?
参考答案:等式 与「 且 」完全等价,这是集合层面的等价,不引入新变量。而规范形只允许 ,所以再把 写成 。这样写的好处是每条约束的权重符号规则统一,取对偶时不必特殊处理。
与前后课的关系
本讲建立了模型与对偶的框架,但把强对偶留作结论。下一讲给出强对偶的三种证明思路(单纯形表、Farkas 引理、力学类比),并用互补松弛从原问题最优解反推对偶最优解,最后用它证明二分图的 König 定理。再往后,对偶会直接变成零和博弈的极小极大定理与最大流最小割定理。
What linear programming does
A linear program (LP) is: maximise or minimise a linear function subject to linear constraints. It is the engine behind a great deal of optimisation — the lecture notes that when CPLEX appeared in 1987 it became the dominant solver, and the commercial sums involved show how much it matters.
This lecture does four things: writes two models, defines standard and canonical form, shows how to convert between them, and derives the dual.
Timeline
| Time | Topic |
|---|---|
| 0:00 | Why linear programming matters |
| 1:54 | The diet problem: nutritional requirements and two grains |
| 10:04 | The bake sale: cookies and meringues |
| 14:55 | Feasible and optimal solutions; drawing the feasible region |
| 19:39 | Standard form and canonical form |
| 22:16 | Four transformations that put any LP into either form |
| 28:44 | Why two forms: simplex needs standard form, duality needs canonical form |
| 31:17 | Basic feasible solutions and the idea of the simplex method |
| 41:18 | Duality: upper bounds from weighted combinations of constraints |
| 57:26 | The dual formula for canonical form |
| 59:53 | Weak duality and its proof |
| 1:02:26 | Strong duality (statement) |
| 1:06:48 | The dual of the dual is the primal |
Example 1: the diet problem
Feed some mice. They need units of starch, of protein and of vitamins. Grain 1 supplies per unit at ; grain 2 supplies at .
Let be the amounts of the two grains:
(Prices in cents.) The final line, the non-negativity constraints, is easy to forget and is not decoration: without it the model can regard a negative amount of grain as a saving, while negative grain has no physical meaning. Solvers usually treat non-negativity separately from the other constraints.
The modelling recipe is fixed: choose variables, write the objective, write the constraints, hand it to a solver. This course does not teach hand-solving — in practice you would not either.
Example 2: the bake sale
You sell cookies and meringues. A batch of cookies needs egg, cups of sugar and cups of flour; a batch of meringues needs eggs, cup of sugar and no flour. You have eggs, cups of sugar and cups of flour. Each batch sells for \20$.
Let be batches of cookies and batches of meringues:
The feasible region. With two variables it can be drawn. The first constraint's boundary passes through and ; the second through and . The flour constraint is redundant: every point satisfying the first two satisfies it. The feasible region is the quadrilateral cut out by the two lines and the axes.
A feasible solution satisfies all constraints (not necessarily optimally); an optimal solution optimises the objective among feasible points. For instance is feasible with value , and is feasible with value . We will see that is optimal.
Two standard forms
Standard form:
Canonical form:
Theorem. Any linear program can be put into standard or canonical form.
Four transformations:
1. Minimisation to maximisation. is equivalent to : same optimiser, objective negated.
2. Split a free variable. If may have either sign, set
Every real number is a difference of two non-negative numbers (for example ), so all variables become non-negative.
3. Inequalities to equations with slack or surplus variables.
where is a slack variable, and
where is a surplus variable.
4. Equations become two inequalities; becomes after negation.
and is rewritten as , which is canonical.
The division of labour: the simplex method uses standard form, since it solves linear systems to find basic feasible solutions; duality uses canonical form, since all constraints point the same way and can be added with non-negative weights.
Basic feasible solutions and the simplex method
Theorem. If a linear program has an optimal solution, then some basic feasible solution is optimal.
Geometrically, basic feasible solutions are the vertices of the feasible region. The bake-sale region is a quadrilateral, and one of its four vertices maximises .
Why "basic"? In standard form each slack variable contributes a column. Pick a linearly independent set of columns — a basis — set the remaining variables to zero, and solve for the basic variables. The result is a basic solution; if it is non-negative it is a basic feasible solution.
The simplex method starts at a basic feasible solution and walks along an edge of the feasible region to a neighbouring vertex, each step improving the objective, until no neighbour is better. Dantzig's method worked extremely well in practice, yet for a long time every pivot rule admitted examples requiring exponentially many steps. Spielman and Teng's smoothed analysis finally explained why it is fast almost always.
Duality: upper bounds from combining constraints
Back to the bake sale. A feasible solution gives a lower bound: is feasible with value , so the optimum is at least .
Where does an upper bound come from? Weight and add the constraints. Take , :
The two brackets are at most and , and the weights are non-negative, so
The bounds meet, so the optimum is exactly . Those weights form a dual optimal solution and act as a certificate of optimality.
In general, for canonical form
take weights , multiply row by and add. To cover the objective coefficients on the left we need
Under that condition,
so is an upper bound. The tightest such bound solves
the dual program. The correspondence:
| Primal (canonical) | Dual |
|---|---|
| Maximise | Minimise |
| Matrix | Transpose |
| Right-hand side | Becomes objective coefficients |
| Objective coefficients | Become right-hand side |
Why ? If some , multiplying a constraint flips its inequality, and adding the results would prove nothing.
Weak and strong duality
Weak duality. If is primal feasible and is dual feasible, then
Proof. From dual feasibility , left-multiply by :
From primal feasibility and , left-multiply by :
Chaining the two gives the claim. The whole proof rests on one fact: a non-negative weight times an inequality does not flip it.
Strong duality (stated; proved next lecture). If the primal optimum is finite, so is the dual optimum , and .
The dual of the dual is the primal. Put the dual , , into canonical form (negate to get a maximisation with ), take the dual to get , , , then negate again to recover the primal. This is where the name comes from.
What this lecture establishes
- An LP is a linear objective, linear constraints and non-negativity; model by choosing variables, objective, constraints.
- Standard form is ; canonical form is ; either converts to the other.
- Four conversions: negate, split free variables, add slack/surplus, split equations into two inequalities.
- Basic feasible solutions are vertices; simplex walks along edges to the best vertex.
- The dual weights the constraints to bound the optimum; the weights are the certificate.
- Weak duality: no primal feasible value exceeds any dual feasible value.
Comprehension check
1. Write the full diet model and explain why non-negativity cannot be dropped.
Reference answer:
Without the model allows negative amounts of grain, which are physically meaningless; the feasible set no longer describes the problem. Non-negativity is part of the problem, not a solution technique.
2. Convert to canonical form: subject to , , , free.
Reference answer: Set with ; negate the objective to maximise; negate the constraint:
3. Turn into an equation; what if it were ?
Reference answer: For , add a slack : . For , subtract a surplus : . Both preserve the solution set of the original inequality.
4. For the bake sale, give a feasible solution, an upper-bound certificate, and the optimum.
Reference answer: is feasible: and , objective . The certificate is the dual feasible : , with , and . The bounds agree, so the optimum is .
5. Prove weak duality.
Reference answer: From and we get . Using , and gives . Hence : every primal feasible value is at most every dual feasible value.
6. Why split an equation into two inequalities rather than something else?
Reference answer: is exactly equivalent, as a set of solutions, to " and ", and this introduces no new variables. Since canonical form allows only , the second is rewritten as . The advantage is that every constraint now has the same orientation, so the sign rules for dual weights are uniform.
Relation to neighbouring lectures
The lecture sets up modelling and duality but leaves strong duality as a statement. The next lecture gives three proof routes (simplex tableau, Farkas' lemma, a mechanical analogy), uses complementary slackness to recover a dual optimum from a primal one, and closes with König's theorem for bipartite graphs. Further on, duality becomes the minimax theorem for zero-sum games and the max-flow min-cut theorem.