OR1 20101 Basic Graphs (1)
OR1 20101 Basic Graphs (1)
special cases
I n = 0 linear program (LP)
I q = 0 (pure) integer linear program (ILP, IP)
I x ∈ {0, 1}n binary program (BP)
I integer programs will prove to be extremely more powerful modeling tools than
linear programs
1 Introduction
3 Exact Algorithms
4 Heuristics
I persons
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-2
Minimum Cost (Bipartite) Matching Problem
I persons, machines
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-2
Minimum Cost (Bipartite) Matching Problem
I persons, machines
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-2
Minimum Cost (Bipartite) Matching Problem
I persons, machines
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-2
Minimum Cost (Bipartite) Matching Problem
I persons, machines
I persons have different skills
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-2
Minimum Cost (Bipartite) Matching Problem
I persons, machines
I persons have different skills
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-2
Minimum Cost (Bipartite) Matching Problem
n persons, m machines find a cost minimum assignment such that each machine is
cij assignment cost i → j operated, and no person operates more than one machine
m
n
U V
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-3
Minimum Cost (Bipartite) Matching Problem
n persons, m machines find a cost minimum assignment such that each machine is
cij assignment cost i → j operated, and no person operates more than one machine
n persons, m machines find a cost minimum assignment such that each machine is
cij assignment cost i → j operated, and no person operates more than one machine
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-4
Minimum Cost (Bipartite) Matching Problem
n persons, m machines find a cost minimum assignment such that each machine is
cij assignment cost i → j operated, and no person operates more than one machine
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-4
Minimum Cost (Bipartite) Matching Problem
n persons, m machines find a cost minimum assignment such that each machine is
cij assignment cost i → j operated, and no person operates more than one machine
n X
X m
min cij xij // minimize cost of edges in matching
i=1 j=1
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-4
Minimum Cost (Bipartite) Matching Problem
n persons, m machines find a cost minimum assignment such that each machine is
cij assignment cost i → j operated, and no person operates more than one machine
n X
X m
min cij xij // minimize cost of edges in matching
i=1 j=1
n
X
s. t. xij = 1 j = 1, . . . , m // every machine must be operated
i=1
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-4
Minimum Cost (Bipartite) Matching Problem
n persons, m machines find a cost minimum assignment such that each machine is
cij assignment cost i → j operated, and no person operates more than one machine
n X
X m
min cij xij // minimize cost of edges in matching
i=1 j=1
n
X
s. t. xij = 1 j = 1, . . . , m // every machine must be operated
i=1
Xm
xij ≤ 1 i = 1, . . . , n // noone can operate more than one machine
j=1
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-4
Minimum Cost (Bipartite) Matching Problem
1
1
2 n
X
2 I xij = 1, j = 1, . . . , m
i=1
3
m
n
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-5
Minimum Cost (Bipartite) Matching Problem
1
1
2 n
X
2 j=2 I xi2 = 1, j = 2
i=1
3
m
n
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-5
Minimum Cost (Bipartite) Matching Problem
1
1
2 n
X
2 j=2 I xi2 = 1, j = 2
i=1
3
m
n
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-5
Minimum Cost (Bipartite) Matching Problem
1
1
2 n
X
2 I xij = 1, j = 1, . . . , m
i=1
3 m
X
I xij ≤ 1, i = 1, . . . , n
j=1
m
n
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-5
Minimum Cost (Bipartite) Matching Problem
1
1
2 n
X
2 I xij = 1, j = 1, . . . , m
i=1
i=3 3 m
X
I x3j ≤ 1, i = 3
j=1
m
n
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-5
Minimum Cost (Bipartite) Matching Problem
1
1
2 n
X
2 I xij = 1, j = 1, . . . , m
i=1
i=3 3 m
X
I x3j ≤ 1, i = 3
j=1
m
n
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-5
Variables Encode Solutions
I example n = 4, m = 3
I 4 · 3 variables: x1,1 , x1,2 , x1,3 , x1,4 , x2,1 , x2,2 , . . . , x4,2 , x4,3
I theoretically, this yields 212 different 0-1 combinations
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-6
Variables Encode Solutions
I example n = 4, m = 3
I 4 · 3 variables: x1,1 , x1,2 , x1,3 , x1,4 , x2,1 , x2,2 , . . . , x4,2 , x4,3
I theoretically, this yields 212 different 0-1 combinations
I only a subset is feasible (defined by the constraints)
0 1 0 0 1 ··· 0 1 0
0 0 0 1 1 ··· 1 0 0
0 1 0 0 1 ··· 1 0 0
. .. ..
.. . .
0 0 1 0 1 ··· 1 0 0
.. ..
. .
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-6
Variables Encode Solutions
I example n = 4, m = 3
I 4 · 3 variables: x1,1 , x1,2 , x1,3 , x1,4 , x2,1 , x2,2 , . . . , x4,2 , x4,3
I theoretically, this yields 212 different 0-1 combinations
I only a subset is feasible (defined by the constraints)
I an algorithm later computes an optimal solution
0 1 0 0 1 ··· 0 1 0
0 0 0 1 1 ··· 1 0 0
0 1 0 0 1 ··· 1 0 0
.. .. ..
. . .
0 0 1 0 1 ··· 1 0 0
.. ..
. .
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-6
A Note on Notation
I the graph needs not be complete // not all edges need to be present
Xn Xm
I however, the notation cij xij implicitly assumes this; better use ij ∈ E:
i=1 j=1
X
min cij xij // minimize cost of edges in matching
ij∈E
X
s. t. xij = 1 j = 1, . . . , m // operate every machine
ij∈E
X
xij ≤ 1 i = 1, . . . , n // no more than one machine
ij∈E
xij ∈ {0, 1} ij ∈ E // assign i → j
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-7
Assignment Problem in Python/Gurobi
# variables
x = {}
for i in range(n):
for j in range(m):
x[i,j] = model.addVar(obj=c[i][j], vtype=GRB.BINARY)
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-8
. Modeling Basics: Alternatives
I analogously:
choose at most one alternative “≤ 1”
choose at least one alternative “≥ 1”
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-9
Example: Course Timetabling: More Alternatives
I binary variables: xc,r,t ∈ {0, 1} // should course c take place in room r at time t?
I e.g., xOR1,Mo 8.30,TEMP2 ∈ {0, 1}
I constraint: choose exactly two weekly time slots for lecture OR1
I denote by ROR1 the set of rooms and by TOR1 the time slots eligible for OR1
X X
xOR1,r,t = 2
r∈ROR1 t∈TOR1
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-10
Example: Course Timetabling: Modeling of Conflicts
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-11
Example: Vertex Coloring
Data
G = (V, E) undirected graph
Goal
color all vertices (=choose exactly one color per vertex from a set C) such that
adjacent vertices receive different colors, minimizing the number of used colors
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-12
Example: Vertex Coloring
Data
G = (V, E) undirected graph
Goal
color all vertices (=choose exactly one color per vertex from a set C) such that
adjacent vertices receive different colors, minimizing the number of used colors
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-12
Vertex Coloring: An Integer Program
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-13
Vertex Coloring: An Integer Program
X
s.t. xic = 1 i∈V // color each vertex
c∈C
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-13
Vertex Coloring: An Integer Program
X
s.t. xic = 1 i∈V // color each vertex
c∈C
xic + xjc ≤ 1 ij ∈ E, c ∈ C // avoid conflicts
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-13
Vertex Coloring: An Integer Program
X
s.t. xic = 1 i∈V // color each vertex
c∈C
xic + xjc ≤ 1 ij ∈ E, c ∈ C // avoid conflicts
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-13
Vertex Coloring: An Integer Program
X
χ(G) = min yc // minimize number of used colors
c∈C
X
s.t. xic = 1 i∈V // color each vertex
c∈C
xic + xjc ≤ 1 ij ∈ E, c ∈ C // avoid conflicts
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-13
Vertex Coloring: An Integer Program
X
χ(G) = min yc // minimize number of used colors
c∈C
X
s.t. xic = 1 i∈V // color each vertex
c∈C
xic + xjc ≤ 1 ij ∈ E, c ∈ C // avoid conflicts
xic ≤ yc i ∈ V, c ∈ C // if xic = 1 then yc = 1
xic ∈ {0, 1} i ∈ V, c ∈ C // color vertex i with color c?
yc ∈ {0, 1} c ∈ C // do we use color c?
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.1 Assignment Problem · p. 2.1-13
Transportation Problem (Hitchcock, 1941)
Transportation Problem
given G = (U ∪ V, E)
with E ⊆ U × V (bipartite)
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.2 Transportation Problem · p. 2.1-14
Transportation Problem (Hitchcock, 1941)
Transportation Problem
given G = (U ∪ V, E)
with E ⊆ U × V (bipartite)
I supplies ai ∈ Z+ , i ∈ U
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.2 Transportation Problem · p. 2.1-14
Transportation Problem (Hitchcock, 1941)
Transportation Problem
given G = (U ∪ V, E)
with E ⊆ U × V (bipartite)
I supplies ai ∈ Z+ , i ∈ U
I demands bj ∈ Z+ , j ∈ V
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.2 Transportation Problem · p. 2.1-14
Transportation Problem (Hitchcock, 1941)
Transportation Problem
given G = (U ∪ V, E)
with E ⊆ U × V (bipartite)
I supplies ai ∈ Z+ , i ∈ U
I demands bj ∈ Z+ , j ∈ V
I cost c(e) ∈ Z+ , e ∈ E
// per unit transportation cost
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.2 Transportation Problem · p. 2.1-14
Transportation Problem
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.2 Transportation Problem · p. 2.1-15
Transportation Problem
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.2 Transportation Problem · p. 2.1-15
Transportation Problem
X
min cij xij // minimize total transportation cost
ij∈E
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.2 Transportation Problem · p. 2.1-15
Transportation Problem
X
min cij xij // minimize total transportation cost
ij∈E
X
xij ≥ bj j = 1, . . . , m // satisfy demands
ij∈E
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.2 Transportation Problem · p. 2.1-15
Transportation Problem
X
min cij xij // minimize total transportation cost
ij∈E
X
s. t. xij ≤ ai i = 1, . . . , n // do not exceed supplies
ij∈E
X
xij ≥ bj j = 1, . . . , m // satisfy demands
ij∈E
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.2 Transportation Problem · p. 2.1-15
Example/Variant: Courses with limited Enrollment
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.2 Transportation Problem · p. 2.1-16
Example/Variant: Courses with limited Enrollment
1
1
X
2 min cij xij // minimize total preference
2 ij∈E
X
3 s. t. xij = ai i = 1, . . . , n // student wishes
m ij∈E
X
xij ≤ bj j = 1, . . . , m // course limits
ij∈E
n xij ∈ {0, 1} ij ∈ E // student i takes course j
students courses
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.2 Transportation Problem · p. 2.1-17
Example/Variant: Courses with limited Enrollment
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.2 Transportation Problem · p. 2.1-17
Example/Variant: Courses with limited Enrollment
−2 0 1
r (3, 1/2) t (2, 1/1) z
(10, 0/5)
(6, 3/5)
(5, 2/4)
(6
)
/2
,0
,0
/3
i (c(a), f (a)/u(a)) j
(2, 2/2) b(i) b(j)
s (4, 0/1) v w
(1, 0/3)
3 0 −2
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.3 Minimum Cost Flows · p. 2.1-18
More Generally: Minimum Cost Network Flows
X
min cij xij
ij∈A
X X
s. t. xij − xji = bi , i∈V // flow conservation
ij∈δ + (i) ji∈δ − (i)
xij ≤ uij , ij ∈ A // capacities
xij ∈ Z+ , ij ∈ A
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.3 Minimum Cost Flows · p. 2.1-19
More Generally: Minimum Cost Network Flows
δ+ (i)
directed graph G = (V, A); cost c : A → Z+ i
capacities u : A → Z+ ; demands b : V → Z
X
min cij xij
ij∈A
X X
s. t. xij − xji = bi , i∈V // flow conservation
ij∈δ + (i) ji∈δ − (i)
xij ≤ uij , ij ∈ A // capacities
xij ∈ Z+ , ij ∈ A
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.3 Minimum Cost Flows · p. 2.1-19
More Generally: Minimum Cost Network Flows
δ+ (i)
directed graph G = (V, A); cost c : A → Z+ i
capacities u : A → Z+ ; demands b : V → Z δ− (i)
X
min cij xij
ij∈A
X X
s. t. xij − xji = bi , i∈V // flow conservation
ij∈δ + (i) ji∈δ − (i)
xij ≤ uij , ij ∈ A // capacities
xij ∈ Z+ , ij ∈ A
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.3 Minimum Cost Flows · p. 2.1-19
Special Case of a Min Cost Flow: Shortest s-t-Paths
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.3 Minimum Cost Flows · p. 2.1-20
Variants of Shortest Paths
xak + xak ≤ 1 k = 1, . . . , K
1 2
2 Modeling with Integer Variables · 2.1 Integer Linear Programs · 2.1.3 Minimum Cost Flows · p. 2.1-21
What we have Seen