Beruflich Dokumente
Kultur Dokumente
Linear Programming
‣ brewer’s problem
‣ simplex algorithm
‣ implementations
‣ duality
‣ modeling
Algorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · April 20, 2011 10:36:00 AM
Overview: introduction to advanced topics
Shifting gears.
• From individual problems to problem-solving models.
• From linear/quadratic to polynomial/exponential scale.
• From details of implementation to conceptual framework.
Goals
• Place algorithms we've studied in a larger context.
• Introduce you to important and essential ideas.
• Inspire you to learn more about algorithms!
2
Linear programming
to the 4A + 4B ≤ 160
constraints 35A + 20B ≤ 1190
A , B ≥ 0
Why significant?
• Fast commercial solvers available. Ex: Delta claims that LP
•
saves $100 million per year.
Widely applicable problem-solving model.
• Key subroutine for integer programming solvers.
3
Applications
5
Toy LP example: brewer’s problem
6
Toy LP example: brewer’s problem
? ? > $800 ?
corn (480 lbs) hops (160 oz) malt (1190 lbs) $13 profit per barrel $23 profit per barrel
7
Brewer’s problem: linear programming formulation
ale beer
constraints
35A + 20B ≤ 1190 malt
A , B ≥ 0
8
Brewer’s problem: feasible region
hops malt
4A + 4B ≤ 160 35A + 20B ≤ 1190
(0, 32)
(12, 28)
corn
beer 5A + 15B ≤ 480
(26, 14)
9
Brewer’s problem: objective function
pr
of
it
(0, 32)
(12, 28)
beer
(26, 14)
intersection of 2 constraints in 2d
extreme point
(0, 32)
(12, 28)
beer
(26, 14)
11
7
Standard form linear program
maximize c1 x 1 + c2 x 2 + … + cn x n maximize cT x
a11 x1 + a12 x2 + … + a1n xn = b1
subject Ax = b
subject
a21 x1 + a22 x2 + … + a2n xn = b2 to the
to the
⋮ ⋮ ⋮ ⋮ ⋮ constraints x ≥ 0
constraints
am1 x1 + am2 x2 + … + amn xn = bm
x1 , x2 , … , xn ≥ 0
12
Converting the brewer’s problem to the standard form
Original formulation.
maximize 13A + 23B
5A + 15B ≤ 480
subject
to the 4A + 4B ≤ 160
constraints
35A + 20B ≤ 1190
A , B ≥ 0
Standard form.
• Add variable Z and equation corresponding to objective function.
• Add slack variable to convert each inequality to an equality.
• Now a 6-dimensional problem.
maximize Z
13A + 23B − Z = 0
subject 5A + 15B + SC = 480
to the
constraints 4A + 4B + SH = 160
A , B , SC , SC , SM ≥ 0
13
Geometry
A set is convex if for any two points a and b in the set, so is ½ (a + b).
Greedy property. Extreme point optimal iff no better adjacent extreme point.
15
‣ brewer’s problem
‣ simplex algorithm
‣ implementations
‣ duality
‣ modeling
16
Simplex algorithm
17
Simplex algorithm: basis
maximize Z
{A, B, SH }
13A + 23B − Z = 0 (19.41, 25.53)
subject
5A + 15B + SC = 480 beer {A, B, SC }
to the
4A + 4B + SH = 160 (26, 14)
constraints
35A + 20B + SM = 1190
A , B , SC , SH , SM ≥ 0
{SH, SM, SC } ale {A, SH, SC }
(0, 0) (34, 0)
18
Simplex algorithm: initialization
maximize Z
basis = { SC, SH, SM }
13A + 23B − Z = 0 A=B=0
subject Z=0
5A + 15B + SC = 480
to the SC = 480
4A + 4B + SH = 160
constraints SH = 160
35A + 20B + SM = 1190
SM = 1190
A , B , SC , SH , SM ≥ 0
19
Simplex algorithm: pivot 1
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)
maximize Z
basis = { B, SH, SM }
(16/3) A − (23/15) SC − Z = -736 A = SC = 0
subject
(1/3) A + B + (1/15) SC = 32 Z = 736
to the B = 32
(8/3) A − (4/15) SC + SH = 32
constraints SH = 32
(85/3) A − (4/3) SC + SM = 550 SM = 550
A , B , SC , SH , SM ≥ 0
20
Simplex algorithm: pivot 1
positive coefficient
maximize Z
pivot basis = { B, SH, SM }
(16/3) A − (23/15) SC − Z = -736
A = SC = 0
subject
(1/3) A + B + (1/15) SC = 32 Z = 736
to the
(8/3) A − (4/15) SC + SH = 32 B = 32
constraints SH = 32
(85/3) A − (4/3) SC + SM = 550
SM = 550
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)
maximize Z
basis = { A, B, SM }
− SC − 2 SH − Z = -800 SC = SH = 0
subject
B + (1/10) SC + (1/8) SH = 28 Z = 800
to the
B = 28
A − (1/10) SC + (3/8) SH = 12
constraints A = 12
− (25/6) SC − (85/8) SH + SM = 110 SM = 110
A , B , SC , SH , SM ≥ 0
22
Simplex algorithm: optimality
maximize Z
basis = { A, B, SM }
− SC − 2 SH − Z = -800 SC = SH = 0
subject
B + (1/10) SC + (1/8) SH = 28 Z = 800
to the
B = 28
A − (1/10) SC + (3/8) SH = 12
constraints A = 12
− (25/6) SC − (85/8) SH + SM = 110 SM = 110
A , B , SC , SH , SM ≥ 0
23
‣ brewer’s problem
‣ simplex algorithm
‣ implementations
‣ duality
‣ modeling
24
Simplex tableau
maximize Z
13A + 23B − Z = 0
subject
5A + 15B + SC = 480
to the
4A + 4B + SH = 160
constraints
35A + 20B + SM = 1190
A , B , SC , SH , SM ≥ 0
5 15 1 0 0 480
4 4 0 1 0 160 m A I b
35 20 0 0 1 1190
1 c 0 0
13 23 0 0 0 0
n m 1
initial simplex tableaux
25
Simplex tableau
maximize Z
− SC − 2 SH − Z = -800
subject
B + (1/10) SC + (1/8) SH = 28
to the
A − (1/10) SC + (3/8) SH = 12
constraints
− (25/6) SC − (85/8) SH + SM = 110
A , B , SC , SH , SM ≥ 0
0 1 1/10 1/8 0 28
1 0 -1/10 3/8 0 12 m
26
Simplex algorithm: initial simplex tableaux
1 c 0 0
n m 1
27
Simplex algorithm: Bland's rule
0 q m+n
Find entering column q using Bland's rule: 0
28
Simplex algorithm: min-ratio rule
0 q m+n
Find leaving row p using min ratio rule. 0
m +
29
Simplex algorithm: pivot
0 q m+n
Pivot on element row p, column q. 0
p +
m +
30
Simplex algorithm: bare-bones implementation
0 q m+n
Execute the simplex algorithm. 0
p +
m +
31
Simplex algorithm: running time
Pivoting rules. Carefully balance the cost of finding an entering variable with
the number of pivots needed.
ABSTRACT
32
Simplex algorithm: degeneracy
Cycling. Get stuck by cycling through different bases that all correspond
to same extreme point.
• Doesn't occur in the wild.
• Bland's rule guarantees finite # of pivots.
choose lowest valid index for
entering and leaving columns
33
Simplex algorithm: implementation issues
34
LP solvers: basic implementations
import drasys.or.mp.Problem;
import drasys.or.mp.lp.DenseSimplex;
36
LP solvers: basic implementations
sorry, no longer
support on Mac
37
LP solvers: basic implementations
38
LP solvers: industrial strength
39
‣ brewer’s problem
‣ simplex algorithm
‣ implementations
‣ duality
‣ modeling
40
LP duality: economic interpretation
Brewer's problem. Find optimal mix of beer and ale to maximize profits.
A , B ≥ 0
coincidence?
Entrepreneur's problem. Buy resources from brewer to minimize cost.
• C, H, M = unit prices for corn, hops, malt.
• Brewer won't agree to sell resources if 5C + 4H + 35M < 13
or if 15C + 4H + 20M < 23
Q. How much should brewer be willing to pay (marginal price) for additional
supplies of scarce resources?
A. Corn $1, hops $2, malt $0.
maximize Z
− SC − 2SH − Z = -800
subject
B + (1/10) SC + (1/8) SH = 28
to the
A − (1/10) SC + (3/8) SH = 12
constraints
− (25/6) SC − (85/8) SH + SM = 110
A, B, SC, SH, SM ≥ 0
42
LP duality theorem
Goal. Given real numbers aij, cj, bi, find real numbers xj and yi that solve:
max c1 x 1 + c2 x 2 + … + cn x n min b1 y1 + b2 y2 + … + bm ym
a11 x1 + a12 x2 + … + a1n xn = b1
a11 y1 + a21 y2 + … + an1 ym = c1
a21 x1 + a22 x2 + … + a2n xn = b2
subject subject a12 y1 + a22 y2 + … + an2 ym = c2
⋮ ⋮ ⋮ ⋮ ⋮
to to
⋮ ⋮ ⋮ ⋮ ⋮
am1 x1 + am2 x2 + … + amn xn = bm
a1n y1 + a2n y2 + … + anm ym = cn
x1 , x2 , … , xn ≥ 0 y1 , y2 , … , ym ≥ 0
Proposition. If (P) and (D) have feasible solutions, then max = min.
43
LP duality theorem
Goal. Given a matrix A and vectors b and c, find vectors x and y that solve:
maximize cT x minimize bT y
subject Ax = b subject AT y ≥ c
to the to the
constraints x ≥ 0 constraints y ≥ 0
Proposition. If (P) and (D) have feasible solutions, then max = min.
44
Brief history
46
Modeling
Linear “programming.”
• Process of formulating an LP model for a problem.
• Solution to LP for a specific problem gives solution to the problem.
1. Identify variables.
2. Define constraints (inequalities and equations).
3. Define objective function.
4. Convert to standard form. software usually performs
this step automatically
Examples.
• Shortest paths.
• Maxflow.
• Bipartite matching.
• Assignment problem.
• 2-person zero-sum games.
...
47
maxflow problem maxflo
V
Maxflow problem (revisited)
6 E
8
0 1 2.0 LP formulation LP solution
0 2 3.0
Input. Weighted digraph G, single source
1 3 3.0
s and single sinkx 35t.+ x 45
Maximize
subject to the constraints
Goal. Find maximum flow from s to t.
1 4 1.0
2 3 1.0 0 ! x 01 ! 2 x 01 = 2
2 4 1.0 0 ! x 02 ! 3 x 02 = 2
3 5 2.0 0 ! x 13 ! 3 x 13 = 1
4 5 3.0
0 ! x 14 ! 1 x 14 = 1
0 ! x 23 ! 1 x 23 = 1
capacities
0 ! x 24 ! 1 x 24 = 1
0 ! x 35 ! 2 x 35 = 2
maxflow problem 0 ! x 45maxflow
! 3 solution x 45 = 2
V x 01 = x 13 + x 14Max flow from 0 to
6 E 5
8 x 02 = x 23 + x 24 0->2 3.0 2.0
0 1 2.0 LP formulation LP solution
x 13 + x 23 = x 35 0->1 2.0 2.0
0 2 3.0 Maximize x 35 + x 45
1 3 3.0 x 14 + x 24 = x 45 1->4 1.0 1.0
subject to the constraints
1 4 1.0 1->3 3.0 1.0
2 3 1.0 0 ! x 01 ! 2 x 01 = 2 2->3 1.0 1.0
2 4 1.0 0 ! x 02 ! 3 x 02 = 2 2->4 1.0 1.0
3 5 2.0 0 ! x 13 ! 3 x 13 = 1 3->5 2.0 2.0
4 5 3.0
0 ! x 14 ! 1 x 14 = 1 4->5 3.0 2.0
0 ! x 23 ! 1 x 23 = 1 Max flow value: 4.0
capacities
0 ! x 24 ! 1 x 24 = 1
0 ! x 35 ! 2 Example ofxreducing
35 = 2 network flow to linear programming
0 ! x 45 ! 3 x 45 = 2
x 01 = x 13 + x 14 48
x 02 = x 23 + x 24
maxflow problem maxflo
V
Modeling the maxflow problem
6 as
E a linear program
8
0 1 2.0 LP formulation LP solution
0 2 3.0
Variables. xvw = flow on edge v→w.
1 3 3.0
Maximize x 35 + x 45
subject to the constraints
Constraints. Capacity and1flow conservation.
4 1.0
2 3 1.0 0 ! x 01 ! 2 x 01 = 2
Objective function. Net flow4 into
1.0 t.
2 problem
maxflow 0 ! x 02 ! 3 x 02 = 2 maxflow so
3 5 2.0 0 ! x 13 ! 3 x 13 = 1
V
4 65 3.0E Max
0 ! x 14 ! 1 x 14 = 1
8 0
0 1capacities
2.0 0 ! x 23 ! 1
LP formulation LP solution
x 23 = 1
0
0 2 3.0 0 ! x ! 1
Maximize x2435 + x 45 x 24 = 1
1 3 3.0 1
subject to the
0 ! x 35 ! 2constraints x 35 = 2
1 4 1.0 1
maxflow problem maxflow
00!!xx0145!!2 3 solution x 01x=452= 2
2 3 1.0 2
V 2 4 1.0 x001!=xx0213!+3x 14Max flow fromx 02 =02 to 5
6 2
E 3 5 2.0
8 x002!=xx1323!+3x 24 0->2 3.0 x2.0 13 = 1 3
0 1 2.0 LP4 formulation
5 3.0 LP solution
x013!+xx1423!=1x 35 0->1 2.0 x2.0 14 = 1 4
0 2 3.0 Maximize x 35 + x 45 capacity constraints
1 3 3.0 capacities x014!+xx2324!=1x 45 1->4 1.0 x1.0 23 = 1 Max
subject to the constraints
1 4 1.0 0 ! x 24 ! 1 1->3 3.0 x1.0
24 = 1
2 3 1.0 0 ! x 01 ! 2 x 01 =02! x ! 2 2->3 1.0 x1.0
35 35 = 2
2 4 1.0 0 ! x 02 ! 3 x 02 =02! x ! 3 2->4 1.0 x1.0
3 5 2.0 45 45 = 2
0 ! x 13 ! 3 x 13x= 1= x + x 3->5 2.0 2.0
4 5 3.0 01 13 14
0 ! x 14 ! 1 x 14 = 1 4->5 3.0 2.0
x 02 = x 23 + x 24 flow conservation
0 ! x 23 ! 1 x 23 = 1 Max flow value: 4.0
capacities x 13 + x 23 = x 35 constraints
0 ! x 24 ! 1 x 24 = 1
x 14 + x 24 =network
Example of reducing x 45 flow to linear programming
0 ! x 35 ! 2 x 35 = 2
0 ! x 45 ! 3 x 45 = 2
x 01 = x 13 + x 14 49
x 02 = x 23 + x 24
Linear programming dual of maxflow problem
Dual variables. One variable zvw for each edge and one variable yv for each vertex.
Dual constraints. One inequality for each edge.
Objective function. Capacity of edges in cut.
z01 ≥ y0 – y1 z23 ≥ y2 – y3
subject
to the z02 ≥ y0 – y2 z24 ≥ y2 – y4 if yv = 1 and yw = 0,
then zvw = 1
constraints
z13 ≥ y1 – y3 z35 ≥ y3 – y5
z14 ≥ y1 – y4 z45 ≥ y4 – y5
y0 = 1 y5 = 0
source sink
yv unrestricted zvw ≥ 0
50
Linear programming perspective
51
Universal problem-solving model (in theory)
• Factoring intractable ?
• NP-complete problems.
… see next lecture