99LinearProgramming 2x2
99LinearProgramming 2x2
Linear programming
L INEAR P ROGRAMMING
maximize 13A + 23B
‣ brewer’s problem 5A + 15B ≤ 480
subject
‣ simplex algorithm to the 4A + 4B ≤ 160
‣ implementations constraints
Algorithms 35A + 20B ≤ 1190
‣ reductions
F O U R T H E D I T I O N
A , B ≥ 0
Applications
Small brewery produces ale and beer. Brewer’s problem: choose product mix to maximize profits.
・Production limited by scarce resources: corn, hops, barley malt. 34 barrels × 35 lbs malt = 1190 lbs
[ amount of available malt ]
Linear programming formulation. Inequalities define halfplanes; feasible region is a convex polygon.
・Let A be the number of barrels of ale.
・Let B be the number of barrels of beer. hops
4A + 4B ≤ 160
malt
35A + 20B ≤ 1190
ale beer
A , B ≥ 0
corn
beer 5A + 15B ≤ 480
(26, 14)
7 8
Brewer’s problem: objective function Brewer’s problem: geometry
intersection of 2 constraints in 2d
hi
gh
er
pr
of
it? extreme point
(0, 32) (0, 32)
beer beer
(26, 14) (26, 14)
Standard form linear program Converting the brewer’s problem to the standard form
A , B ≥ 0
13A + 23B − Z = 0
x1 , x2 , … , xn ≥ 0
subject 5A + 15B + SC = 480
to the
constraints 4A + 4B + SH = 160
Inequalities define halfspaces; feasible region is a convex polyhedron. Extreme point property. If there exists an optimal solution to (P),
then there exists one that is an extreme point.
A set is convex if for any two points a and b in the set, so is ½ (a + b). ・Good news: number of extreme points to consider is finite.
・Bad news : number of extreme points can be exponential!
An extreme point of a set is a point in the set that can't be written as
½ (a + b), where a and b are two distinct points in the set.
extreme
point
local optima are global optima
(follows because objective function is linear
and feasible region is convex)
Warning. Don't always trust intuition in higher dimensions. Greedy property. Extreme point optimal iff no better adjacent extreme point.
13 14
http://algs4.cs.princeton.edu http://algs4.cs.princeton.edu
Simplex algorithm Simplex algorithm: basis
・Pivot from one extreme point to an adjacent one. {B, SH, SM } basic infeasible
maximize Z {A, B, SH }
How to implement? Linear algebra. 13A + 23B − Z = 0 (19.41, 25.53)
A , B , SC , SH , SM ≥ 0
{SH, SM, SC } ale {A, SH, SC }
(0, 0) (34, 0)
17 18
substitute B = (1/15) (480 – 5A – SC) and add B into the basis which basic variable
does B replace?
(rewrite 2nd equation, eliminate B in 1st, 3rd, and 4th equations)
one basic variable per row
A , B , SC , SH , SM ≥ 0
19 20
Simplex algorithm: pivot 1 Simplex algorithm: pivot 2
positive coefficient
maximize Z maximize Z
pivot basis = { SC, SH, SM } pivot basis = { B, SH, SM }
13A + 23B − Z = 0 A=B=0 (16/3) A − (23/15) SC − Z = -736 A = SC = 0
subject Z=0 subject (1/3) A + B + (1/15) SC = 32 Z = 736
5A + 15B + SC = 480
to the SC = 480 to the B = 32
4A + 4B + SH = 160 constraints (8/3) A − (4/15) SC + SH = 32
constraints SH = 160 SH = 32
35A + 20B + SM = 1190 SM = 1190 (85/3) A − (4/3) SC + SM = 550 SM = 550
A , B , SC , SH , SM ≥ 0 A , B , SC , SH , SM ≥ 0
substitute A = (3/8) (32 + (4/15) SC – SH ) and add A into the basis which basic variable
does A replace?
(rewrite 3rd equation, eliminate A in 1st, 2nd, and 4th equations)
Q. Why pivot on column 2 (corresponding to variable B)?
・Its objective function coefficient is positive.
(each unit increase in B from 0 increases objective value by $23) maximize Z
basis = { A, B, SM }
21 22
A , B , SC , SH , SM ≥ 0
23
Simplex tableau
maximize Z
13A + 23B − Z = 0
subject
L INEAR P ROGRAMMING to the
5A + 15B + SC = 480
constraints 4A + 4B + SH = 160
A , B , SC , SH , SM ≥ 0
‣ simplex algorithm
‣ implementations
Algorithms 5 15 1 0 0 480
‣ reductions
4 4 0 1 0 160 m A I b
R OBERT S EDGEWICK | K EVIN W AYNE
35 20 0 0 1 1190
http://algs4.cs.princeton.edu 1 c 0 0
13 23 0 0 0 0
n m 1
initial simplex tableaux
26
Simplex algorithm transforms initial 2D array into solution. Construct the initial simplex tableau.
m A I b
maximize Z
− SC − 2 SH − Z = -800 1 c 0 0
subject B + (1/10) SC + (1/8) SH = 28 n m 1
to the
constraints A − (1/10) SC + (3/8) SH = 12
public class Simplex
− (25/6) SC − (85/8) SH + SM = 110 {
constructor
private double[][] a; // simplex tableaux
A , B , SC , SH , SM ≥ 0 private int m, n; // M constraints, N variables
27 28
Simplex algorithm: Bland's rule Simplex algorithm: min-ratio rule
0 q m+n 0 q m+n
Find entering column q using Bland's rule: 0 Find leaving row p using min ratio rule. 0
index of first column whose objective function (Bland's rule: if a tie, choose first such row)
p + p +
coefficient is positive.
m + m +
29 30
p + p +
m + m +
31 32
Simplex algorithm: running time Simplex algorithm: running time
Remarkable property. In typical practical applications, simplex algorithm Remarkable property. In typical practical applications, simplex algorithm
terminates after at most 2 (m + n) pivots. terminates after at most 2 (m + n) pivots.
ABSTRACT
33 34
1. INTRODUCTION
Degeneracy. New basis, same extreme point. To improve the bare-bones implementation.
Permission to make digital or hard copies of all or part of this work for
personal or classroom use is granted without fee provided that copies are
not made or distributed for profit or commercial advantage and that copies
Best practice. Don't implement it yourself!
bear this notice and the full citation on the first page. To copy otherwise, to
republish, to post on servers or to redistribute to lists, requires prior specific
permission and/or a fee.
STOC’01, July 6-8, 2001, Hersonissos, Crete, Greece.
Copyright 2001 ACM 1-58113-349-9/01/0007 ... 5.00.
35 36
LP solvers: industrial strength Brief history
http://algs4.cs.princeton.edu http://algs4.cs.princeton.edu
Reductions to standard form Modeling
Minimization problem. Replace min 13A + 15B with max – 13A – 15B. Linear “programming” (1950s term) = reduction to LP (modern term).
≥ constraints. Replace 4A + 4B ≥ 160 with 4A + 4B – SH = 160, SH ≥ 0. ・Process of formulating an LP model for a problem.
Unrestricted variables. Replace B with B = B0 – B1, B0 ≥ 0 , B1 ≥ 0. ・Solution to LP for a specific problem gives solution to the problem.
nonstandard form
1. Identify variables.
KBMBKBx2 13A + 15B 2. Define constraints (inequalities and equations).
bm#D2+i iQ, 5A + 15B ≤ 480 3. Define objective function.
4A + 4B ≥ 160 software usually performs
4. Convert to standard form. this step automatically
35A + 20B = 1190
A ≥ 0
Examples.
B Bb mM`2bi`B+i2/
standard form
・Maxflow.
KtBKBx2 −13A − 15B0 + 15B1
・Shortest paths.
bm#D2+i iQ, 5A + 15B0 − 15B1 + SC = 480
・Bipartite matching.
4A + 4B0 − 4B1 − SH = 160
・Assignment problem.
35A + 20B0 − 20B1 = 1190
・2-person zero-sum games.
...
A B0 B1 SC SH ≥ 0
41 42
・NP-complete problems.
… see next lecture
http://algs4.cs.princeton.edu http://algs4.cs.princeton.edu