The Theory and Practice of Constraint Programming: An Overview Brahim Hnich Faculty of Computer Science Izmir University of Economics Izmir, Turkey
Dec 14, 2015
The Theory and Practice
of Constraint Programming:
An Overview
Brahim Hnich
Faculty of Computer Science
Izmir University of Economics
Izmir, Turkey
18/04/23 A CP Tutorial: Hnich 2
Quotation
“Constraint programming represents one of the closest approaches computer science has yet made to the Holy Grail of programming: the user states the problem, the computer solves it.”
Eugene C. Freuder, Constraints, April 1997
18/04/23 A CP Tutorial: Hnich 3
Caveat
•In this talk:
Constraint programming for combinatorial problems
“Programming” refers to its roots in computer science (programming languages)
18/04/23 A CP Tutorial: Hnich 4
Outline
•Modelling
•Constraint propagation
•Search
•Demo: Lot-sizing with stochastic non-stationary demand
18/04/23 A CP Tutorial: Hnich 5
A Puzzle
• Place the numbers 1 through 8 in the nodes such that: Each number appears exactly once No connected nodes have consecutive numbers
?
?
?
?
?
?
??
18/04/23 A CP Tutorial: Hnich 6
Modeling
• Each node a decision variable
• {1, …, 8} values in the domain of each variable
• No consecutive numbers a constraint (vi, vj) |vi – vj| > 1
• All values used a clique of not-equals constraints forall i<j. vi ≠ vj,
18/04/23 A CP Tutorial: Hnich 7
Modeling
• Each node a decision variable
• {1, …, 8} values in the domain of each variable
• No consecutive numbers a constraint (vi, vj) |vi – vj| > 1
• All values used forall i<j. vi ≠ vj,
Or more compactly, all-different[v1,…,v8]
18/04/23 A CP Tutorial: Hnich 8
Heuristic Search
?
?
?
?
?
?
?? 1 8
{1, 2, 3, 4, 5, 6, 7, 8}
18/04/23 A CP Tutorial: Hnich 9
Inference/Propagation
?
?
?
?
?
?
?? 1 8
{1, 2, 3, 4, 5, 6, 7, 8}
{1, 2, 3, 4, 5, 6, 7, 8}
18/04/23 A CP Tutorial: Hnich 10
Inference/Propagation
?
?
?
?
?
?
?? 1 8
{3, 4, 5, 6}
7 2
{3, 4, 5, 6}
{3, 4, 5, 6} {3, 4, 5, 6}
63
4 5
{3, 4, 5, 6, 7} {2, 3, 4, 5, 6}
18/04/23 A CP Tutorial: Hnich 11
An OPL Model
int N=8;struct edge{int x; int y;};{edge} Edges ={<1,2>, <1,3>,…,<7,8>};range Nodes 1..N;range Values 1..N;
var int Solution[Nodes] in Values;
solve{forall(e in Edges)
abs(Solution[e.x] - Solution[e.y]) >1;
alldifferent(Solution); };
18/04/23 A CP Tutorial: Hnich 12
The Core of Constraint Computation
Solving
HeuristicSearch
Propagation
Modelling
ANY QUESTIONS?
Modeling
18/04/23 A CP Tutorial: Hnich 15
Finite-domain Variables
•To each variable x is associated a set of values called its domain
e.g x Є {1,3,11}
•Each variable must take a value from its domain
•That domain is updated as decisions are made
•A domain may only shrink (no value is ever added)
18/04/23 A CP Tutorial: Hnich 16
Constraint Satisfaction Problems
CSP: (X, D, C)o X = {x1, x2,…, xn} variables
o D = {d1, d2,…,dn} domains (finite)
o C = {c1,c2,…,ce } constraints
c ЄC var(c) = {xi, xj,…, xk} scope
rel(c) ⊆ di x dj x .. x dk permitted
tuples
Solution: o assignment satisfying every constraint
o NP-complete task
18/04/23 A CP Tutorial: Hnich 17
CSP: Relevance
Artificial Intelligenceo temporal reasoning
Control Theoryo controllers for sensory based robots
Concurencyo process comm. and synchr.
Computer Graphicso geometric coherence
Database Systemso constraint databases
Bioinformaticso sequence alignment
Operations researcho optimization
Real-life applications
Production planningStaff schedulingResource allocationCircuit designOption tradingDNA sequencing...
Many problems can be represented as CSP:
18/04/23 A CP Tutorial: Hnich 18
Constraint Programming
CP: provides a platform for solving CSPs proven useful in many real applications
Platform: set of common structures to reuse best known algorithms for propagation & solving
Two stages: modelling solving
18/04/23 A CP Tutorial: Hnich 19
Language of Constraints
The usual relational operators (<, =, >, ≤, …)
… including ≠
Linear and nonlinear constraints
Logical connectives (→, ↔, ┐, …)
Set constraints (subset, union, intersection,…)
“Global constraints”: constraints capturing a common substructure (pattern) of combinatorial problems
18/04/23 A CP Tutorial: Hnich 20
Map Colouring
N
S F
Variables: F, N, S
Values: { }
Constraints: N ≠ S ≠ F ≠ N
A solution: F
N
S
18/04/23 A CP Tutorial: Hnich 21
Word Design Problem
This problem has its roots in Bioinformatics and Coding Theory.
Problem: find as large a set S of strings (words) of length 8 over the alphabet W = { A,C,G,T } with the following properties:
1. Each word in S has 4 symbols from { C,G };
18/04/23 A CP Tutorial: Hnich 22
Word Design Problem
Problem: find as large a set S of strings (words) of length 8 over the alphabet W = { A,C,G,T } with the following properties:
1. Each word in S has 4 symbols from { C,G };
2. Each pair of distinct words in S differ in at least 4 positions; and
18/04/23 A CP Tutorial: Hnich 23
Word Design Problem
Problem: find as large a set S of strings (words) of length 8 over
the alphabet W = { A,C,G,T } with the following properties:
1. Each word in S has 4 symbols from { C,G };
2. Each pair of distinct words in S differ in at least 4 positions; and
3. Each pair of words x and y in S (where x and y may be identical) are such that xR and yC differ in at least 4 positions. o (x1,…,x8)R = ( x8,…,x1 ) is the reverse of ( x1,…,x8 )
o (y1,…,y8)C is the Watson-Crick complement of ( y1,…,y8 ), i.e. the word where each A is replaced by a T and vice versa and each C is replaced by a G and vice versa.
18/04/23 A CP Tutorial: Hnich 24
A Solution
S= { AAGCCGTT, TACGCGAT}
Each word in S has 4 symbols from { C,G };
18/04/23 A CP Tutorial: Hnich 25
A Solution
S= { AAGCCGTT, TACGCGAT}
Each pair of distinct words in S differ
in at least 4 positions
18/04/23 A CP Tutorial: Hnich 26
A Solution
S= { AAGCCGTT, TACGCGAT}
SR= { TTGCCGAA, TAGCGCAT}
SC= { TTCGGCAA, ATGCGCTA}
Each pair of words x and y in S
(where x and y may be identical) are such that
xR and yC differ in at least 4 positions
18/04/23 A CP Tutorial: Hnich 27
A Matrix Model
M 1 2 3 4 5 6 7 8
1{ A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T }
…
m{ A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T }
Each row represents a word in S
M[i,j]: a decision variable with domain { A,C,G,T }
18/04/23 A CP Tutorial: Hnich 28
A Matrix Model
M 1 2 3 4 5 6 7 8
1{ A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T }
…
m{ A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T }
1. Expressing that each word in S has 4 symbols from { C,G }
18/04/23 A CP Tutorial: Hnich 29
A Matrix Model
M 1 2 3 4 5 6 7 8
1{ A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T }
…
m{ A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T }
For each row r
sum (p in 1..8) //channelling constraints
(M[r,p]=C or M[r,p]=G) = 4
18/04/23 A CP Tutorial: Hnich 30
A Matrix Model
M 1 2 3 4 5 6 7 8
1{ A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T }
…
m{ A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T }
1. …2. Each pair of distinct words in S differ in at least
4 positions
For each distinct rows r1 and r2
sum(p in 1..8) (M[r1,p] ≠ M[r2,p]) >= 4
18/04/23 A CP Tutorial: Hnich 31
A Matrix Model
MC 1 2 3 4 5 6 7 8
1{ A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T }
…
m{ A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T }
1. …2. …3. xR and yC differ in at least 4 positions.
Introduce a “compliment matrix”
Each pair //channelling constraints
<M[i,j], MC[i,j]> in {<C,G>, <G,C>, <A,T>, <T,A>}
18/04/23 A CP Tutorial: Hnich 32
A Matrix Model
MC 1 2 3 4 5 6 7 8
1{ A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T }
…
m{ A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T } { A,C,G,T }
1. …2. …3. xR and yC differ in at least 4 positions.
For each rows r1 and r2
sum(p in 1..8) (M[r1,9-p] ≠ MC[r2,p]) >= 4
ANY QUESTIONS?
Constraint Propagation
18/04/23 A CP Tutorial: Hnich 35
Constraint Propagation
•General principle
•Consistency
•Filtering on simple constraints
•Filtering on global constraints
•Conclusion
18/04/23 A CP Tutorial: Hnich 36
General Principle
•Each type of constraint relies on its own specific filtering algorithm to filter out (locally) inconsistent variable assignment
•The communication between the different filtering algorithms takes place through the domains of the variables
•It makes it easy to have different types of constraints work together
18/04/23 A CP Tutorial: Hnich 37
General Principle
?
?
?
?
?
?
?? 1 8
{1, 2, 3, 4, 5, 6, 7, 8}
{1, 2, 3, 4, 5, 6, 7, 8}
≠ ≠
≠
≠
≠
≠
≠
≠≠
≠ ≠
≠
≠≠
≠
≠
≠
Constraint network
18/04/23 A CP Tutorial: Hnich 38
General Principle
•This propagation necessarily terminates
•The results is the same regardless of the order in which we consider the constraints
18/04/23 A CP Tutorial: Hnich 39
Triggering the constraints
•A constraint is woken up by one of its variables.•domain event: anytime the domain changes(i.e. some value has been removed)
•range event: only when the minimum or maximum value in the domain has changed (e.g. for x ≤ y constraint)
•value event: only when the domain is reduced to a single value (e.g. for x≠y constraint)
18/04/23 A CP Tutorial: Hnich 40
Constraint Propagation
•General principle
•Consistency
•Filtering on simple constraints
•Filtering on global constraints
•Conclusion
18/04/23 A CP Tutorial: Hnich 41
Consistency
•We reason locally about a constraint, removing
inconsistent values from domains
•We can often characterize the level of consistency achieved by a filtering algorithm
18/04/23 A CP Tutorial: Hnich 42
Generalized Arc Consistency
•Given a constraint C on the variables X A support for Xi = vj on C is a partial assignment containing Xi = vj that satisfies C.
A variable Xi is generalized arc consistent (GAC) on C iff every value in D(Xi) has support on C.
C is GAC iff each constrained variable is GAC on C.
18/04/23 A CP Tutorial: Hnich 43
Bounds Consistency
•Given a constraint C on the variables X A bound support on C is a support where the interval [min(Xi); max(Xi)] is substituted for the domain of each constrained variable Xi.
A variable Xi is bound consistent (BC) on C if min(Xi) and max(Xi) have bound support on C.
C is BC iff all constrained variables are BC on C.
18/04/23 A CP Tutorial: Hnich 44
Consistency: BC example
•Consider 4x + 3y - 2z = 10
•with Dx = Dy = Dz = {0, 1, . . . , 9}
•Maintaining BC reduces domains toDx = {0, 1, . . . , 7},Dy = Dz = {0, 1, . . . , 9}
•BC is triggered on range event
18/04/23 A CP Tutorial: Hnich 45
Consistency: GAC example
•Consider 4x + 3y - 2z = 10
•with Dx = Dy = Dz = {0, 1, . . . , 9}
•Maintaining GAC reduces domains toDx = {0, 1, . . . , 7},Dy = {0, 2, 4, 6, 8},Dz = {0, 1, . . . , 9}
•GAC is triggered on domain event
18/04/23 A CP Tutorial: Hnich 46
Constraint Propagation
•General principle
•Consistency
•Filtering on simple constraints
•Filtering on global constraints
•Conclusion
18/04/23 A CP Tutorial: Hnich 47
Disequalities: Filtering Algorithm
•Consider x≠y with Dx = Dy = {0, 1, . . . , 9}
•Only once one of the variables is fixed may we remove any value from the domain of the other:Dx = {3} → Dy = {0, . . . , 2, 4, . . . , 9}
•achieves GAC
•triggered on value event
18/04/23 A CP Tutorial: Hnich 48
Inequalities: Filtering Algorithm
•Consider x<y with Dx = Dy = {0, 1, 2, 3}
•Only once one of the variables’ bound is changed may we remove any value from the domain of the other:
Dx = {0, 1, 2} Dy = {1, 2, 3}
•achieves BC (which is equivalent to GAC because of monotonicity)
•triggered on range event
18/04/23 A CP Tutorial: Hnich 49
Constraint Propagation
•General principle
•Consistency
•Filtering on simple constraints
•Filtering on global constraints
•Conclusion
18/04/23 A CP Tutorial: Hnich 50
Example: all-different
3 binary constraints,they are GAC,
no pruning
Var: F, N, S; Val: { }; Ctrs: N ≠ S ≠ F ≠ N
F { }
S { }
N { }
≠ ≠
≠
18/04/23 A CP Tutorial: Hnich 51
Example: all-different
•Using binary disequalities, this inconsistency goes undetected
3 binary constraints,they are GAC,
no pruning
Var: F, N, S; Val: { }; Ctrs: N ≠ S ≠ F ≠ N
F { }
S { }
N { }
≠ ≠
≠
18/04/23 A CP Tutorial: Hnich 52
Example: all-different
We can do something simple:
1. Count the number of variables, n
2. Count the number of values in the union of their domains, m
3. If n > m then no solution can possibly be found3 binary constraints,
they are GAC,no pruning
Var: F, N, S; Val: { }; Ctrs: N ≠ S ≠ F ≠ N
F { }
S { }
N { }
≠ ≠
≠
18/04/23 A CP Tutorial: Hnich 53
Example: all-different
We can do something simple:
1. Count the number of variables, n
2. Count the number of values in the union of their domains, m
3. If n > m then no solution can possibly be found
• ...still fooled by
Dx = Dy = Dz = {a, b}, Dw = {c, d}3 binary constraints,they are GAC,
no pruning
Var: F, N, S; Val: { }; Ctrs: N ≠ S ≠ F ≠ N
F { }
S { }
N { }
≠ ≠
≠
18/04/23 A CP Tutorial: Hnich 54
Example: all-different
3 binary constraints,they are GAC,
no pruning
1 ternary constraint, not GAC,GAC pruning empty domain
no solution!!
logically
equivalent
Var: F, N, S; Val: { }; Ctrs: N ≠ S ≠ F ≠ N
F { }
S { }
N { }
≠ ≠
≠
all-different
F { }
S { }
N { }
18/04/23 A CP Tutorial: Hnich 55
All-different: A Filtering Algorithm
•Build the corresponding bipartite graph
721 3 54 6
x2x1 x3 x5x4 x6
18/04/23 A CP Tutorial: Hnich 56
All-different: A Filtering Algorithm
•Build the corresponding bipartite graph
• ∃ solution iff ∃ matching covering all the variables
721 3 54 6
x2x1 x3 x5x4 x6
18/04/23 A CP Tutorial: Hnich 57
All-different: A Filtering Algorithm
•Build the corresponding bipartite graph
• ∃ solution iff ∃ matching covering all the variables
•filteringfind alternating cycles and pathsremove inconsistent values (useless edges) [X4=2]fix variables (vital edges) [X4=4]
721 3 54 6
x2x1 x3 x5x4 x6
18/04/23 A CP Tutorial: Hnich 58
All-different: A Filtering Algorithm
•Build the corresponding bipartite graph
• ∃ solution iff ∃ matching covering all the variables •starting from the current matching at each call makes the algorithm incremental•achieves GAC
721 3 54 6
x2x1 x3 x5x4 x6
18/04/23 A CP Tutorial: Hnich 59
Cardinality: A Filtering Algorithm
•distribute([c1, . . . , cm],[v1, . . . , vm],[x1, . . . ,xn]),ci [li, ui]∈
18/04/23 A CP Tutorial: Hnich 60
Cardinality: A Filtering Algorithm
•distribute([c1, . . . , cm],[v1, . . . , vm],[x1, . . . ,xn]),ci [li, ui]∈
•transformed into a network flow problem
• ∃ solution iff feasible flow∃
S T
x1
xn
xi
v2
v1
vm
vj
[1,1]
[0,1] [l1,u1]
[lm,um]
18/04/23 A CP Tutorial: Hnich 61
Cardinality: A Filtering Algorithm
•Compute a maximum flow
•Build the residual graph
•Find its strongly connected components
•Remove zero-flow arcs between components
•achieves GAC
18/04/23 A CP Tutorial: Hnich 62
Lexicographic Ordering: A Filtering Algorithm
• A new family of global constraints
• Linear time complexity
• Ensures that a pair of vectors of variables are lexicographically ordered.
0 1 4 2
2 9 8 7
lex
18/04/23 A CP Tutorial: Hnich 63
Motivation: Symmetry
Symmetry: transformation of an entity that preserves the properties of the entity
Example:
180º
18/04/23 A CP Tutorial: Hnich 64
Motivation: Symmetry
• Frequently occurs Combinatorial problems like
covering arrayso Rows and columns can be permuted
Messy real world problems like nurse rostering
o Nurses with same skills can be swapped
• Tough for IP Very active research area
within CP Some effective techniques
have been developed
18/04/23 A CP Tutorial: Hnich 65
Motivation
• An important class of symmetries in CP matrices of decision variables rows/columns represent indistinguishable
objects, hence symmetric
• Rows and columns can be permuted without affecting satisfiability
• Encountered frequently
18/04/23 A CP Tutorial: Hnich 66
• Schedule games between n teams over n-1 weeks• Each week is divided into n/2 periods• Each period has 2 slots: home and away• Find a schedule such that
every team plays exactly once a week
every team plays against every other team
every team plays at most twice in the same period over the tournament
Example: Sports Scheduling
18/04/23 A CP Tutorial: Hnich 67
Example: Sport Scheduling
Period3
Period4
Period2
Period1
0 vs 72 vs 72 vs 60 vs 41 vs 63 vs 54 vs 5
0 vs 5
1 vs 4
3 vs 7
Week 5
3 vs 4
0 vs 6
1 vs 5
Week 6
1 vs 31 vs 22 vs 54 vs 66 vs 7
5 vs 65 vs 70 vs 31 vs 72 vs 3
2 vs 4 3 vs 64 vs 70 vs 20 vs 1
Week 7Week 4Week 3Week 2Week1
• We need a table of meetings!
18/04/23 A CP Tutorial: Hnich 68
Example: Sport Scheduling
Period3
Period4
Period2
Period1
0 vs 72 vs 72 vs 60 vs 41 vs 63 vs 54 vs 5
0 vs 5
1 vs 4
3 vs 7
Week 5
3 vs 4
0 vs 6
1 vs 5
Week 6
1 vs 31 vs 22 vs 54 vs 66 vs 7
5 vs 65 vs 70 vs 31 vs 72 vs 3
2 vs 4 3 vs 64 vs 70 vs 20 vs 1
Week 7Week 4Week 3Week 2Week1
• Weeks are indistinguishable• Periods are indistinguishable
• We need a table of meetings!
18/04/23 A CP Tutorial: Hnich 69
0 vs 72 vs 72 vs 60 vs 41 vs 63 vs 54 vs 5Period 3
0 vs 5
1 vs 4
3 vs 7
Week 5
3 vs 4
0 vs 6
1 vs 5
Week 6
1 vs 31 vs 22 vs 54 vs 66 vs 7Period 4
5 vs 65 vs 70 vs 31 vs 72 vs 3Period 2
2 vs 4 3 vs 64 vs 70 vs 20 vs 1Period 1
Week 7Week 4Week 3Week 2Week 1
Example: Sport Scheduling
• Weeks are indistinguishable• Periods are indistinguishable
18/04/23 A CP Tutorial: Hnich 70
0 vs 72 vs 72 vs 6 0 vs 41 vs 6 3 vs 54 vs 5Period 3
0 vs 5
1 vs 4
3 vs 7
Week 5
3 vs 4
0 vs 6
1 vs 5
Week 6
1 vs 31 vs 22 vs 5 4 vs 66 vs 7Period 4
5 vs 65 vs 70 vs 3 1 vs 72 vs 3Period 2
2 vs 4 3 vs 64 vs 7 0 vs 20 vs 1Period 1
Week 7Week 4Week 3Week 2Week 1
Example: Sport Scheduling
• Weeks are indistinguishable• Periods are indistinguishable
18/04/23 A CP Tutorial: Hnich 71
Example: Bin Packing
• Consider 2 identical bins:
BA
18/04/23 A CP Tutorial: Hnich 72
Example
• Consider 2 identical bins:
• We must pack 6 items: 3 41 652
BA
18/04/23 A CP Tutorial: Hnich 73
Example
• Here is one solution:
1 2
3
65
4
BA
18/04/23 A CP Tutorial: Hnich 74
Example
• Here is another:
BA
1
3
5
2
6
4
18/04/23 A CP Tutorial: Hnich 75
Example
• Is there any fundamental difference?
135
2
64a)
b)
A B
BA135
2
64
18/04/23 A CP Tutorial: Hnich 76
Example
• Consider a matrix model:
1 2 3 4 5 6
A 1 0 1 0 1 0
B 0 1 0 1 0 1
1 2 3 4 5 6
A 0 1 0 1 0 1
B 1 0 1 0 1 0
135
2
64a)
b)
A B
BA135
2
64
18/04/23 A CP Tutorial: Hnich 77
Example
• Consider a matrix model:
NB: ‘1’ means place this item in this bin:
1 2 3 4 5 6
A 1 0 1 0 1 0
B 0 1 0 1 0 1
1 2 3 4 5 6
A 0 1 0 1 0 1
B 1 0 1 0 1 0
135
2
64a)
b)
A B
BA135
2
64
18/04/23 A CP Tutorial: Hnich 78
Example
• Consider a matrix model:
If we insist that row A lex row B,
we remove a) from the solution set.
1 2 3 4 5 6
A 1 0 1 0 1 0
B 0 1 0 1 0 1
1 2 3 4 5 6
A 0 1 0 1 0 1
B 1 0 1 0 1 0
b)
BA135
2
64
18/04/23 A CP Tutorial: Hnich 79
Example
• Notice that items 3 and 4 are identical.
1 2 3 4 5 6
A 0 1 0 1 0 1
B 1 0 1 0 1 0
b)
BA135
2
64
1 2 3 4 5 6
A 0 1 1 0 0 1
B 1 0 0 1 1 0
c)145
2
63
18/04/23 A CP Tutorial: Hnich 80
Example
• Notice that items 3 and 4 are identical.
1 2 3 4 5 6
A 0 1 0 1 0 1
B 1 0 1 0 1 0
b)
BA135
2
64
1 2 3 4 5 6
A 0 1 1 0 0 1
B 1 0 0 1 1 0
If we insist that col 3 lex col 4,
we remove c) from the solution set.
18/04/23 A CP Tutorial: Hnich 81
Aims
• Main Goal Eliminate row and column symmetries effectively and efficiently.
• Aims: Investigate types of ordering constraints to break row and column
symmetries.
Devise global constraints to easily pose and efficiently solve the ordering constraints.
Examine the effectiveness of the ordering constraint
we focus on lexicographic ordering constraints
18/04/23 A CP Tutorial: Hnich 82
How GACLex Works
• Consider the following example.
• We have two vectors of decision variables:
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
18/04/23 A CP Tutorial: Hnich 83
How GACLex Works
• Consider the following example.
• We have two vectors of decision variables:
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
• We want to enforce GAC on: x lex y.
18/04/23 A CP Tutorial: Hnich 84
A Tale of Two Pointers
• We use two pointers, α and β, to avoid repeatedly traversing the vectors.
18/04/23 A CP Tutorial: Hnich 85
A Tale of Two Pointers
• We use two pointers, α and β, to avoid repeatedly traversing the vectors.
• We index the vectors as follows:
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
Most Significant Index
18/04/23 A CP Tutorial: Hnich 86
A Tale of Two Pointers
• We use two pointers, α and β, to avoid repeatedly traversing the vectors.
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
• α: index such that all variables at more significant indices are ground and equal.
18/04/23 A CP Tutorial: Hnich 87
A Tale of Two Pointers
• We use two pointers, α and β, to avoid repeatedly traversing the vectors.
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 88
A Tale of Two Pointers
• We use two pointers, α and β, to avoid repeatedly traversing the vectors.
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
• α: index such that all variables at more significant indices are ground and equal.
• β: If tails never violate the constraint:
18/04/23 A CP Tutorial: Hnich 89
Pointer Initialisation
• Needs one traversal of the vectors (linear).
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
α
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 90
Pointer Initialisation
• Needs one traversal of the vectors (linear).
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
α β
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 91
Failure
• Inconsistent if β α.
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 92
How GACLex Works
• We maintain α and β as assignments made.
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 93
How GACLex Works
• We maintain α and β as assignments made.
• When β = α + 1 we enforce bounds consistency on: xα < yα
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 94
How GACLex Works
• We maintain α and β as assignments made.
• When β = α + 1 we enforce bounds consistency on: xα < yα
The variable at the αth element of each vector.
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 95
How GACLex Works
• We maintain α and β as assignments made.
• When β = α + 1 we enforce bounds consistency on: xα < yα
• When β > α + 1 we enforce bounds consistency
on: xα yα
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 96
How GACLex Works
• We maintain α and β as assignments made.
• Key: we reduce GAC on vectors to BC on binary constraints.
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 97
How GACLex Works
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
α β
• 0, 1 removed from yα.
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 98
How GACLex Works
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
α β
• 0, 1 removed from yα.
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 99
How GACLex Works
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
α β
• Update α.
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 100
How GACLex Works
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
α β
• Update α.
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 101
How GACLex Works
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
α β
• 3, 4 removed from xα.
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 102
How GACLex Works
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
α β
• 3, 4 removed from xα.
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 103
How GACLex Works
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
α β
• Update α.
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 104
How GACLex Works
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
α β
• Update α.
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 105
How GACLex Works
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
α β
• 4, 5 removed from xα, 0, 1 removed from yα.
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 106
How GACLex Works
0 1 2 3 4
x {2} {1,3,4} {1,2,3,4,5} {1,2} {3,4,5}
y {0,1,2} {1} {0,1,2,3,4} {0,1} {0,1,2}
α β
• 4, 5 removed from xα, 0, 1 removed from yα.
• α: index such that all variables at more significant indices are ground and equal.
• β: most significant index from which the two vectors’ tails necessarily violate the constraint.
18/04/23 A CP Tutorial: Hnich 107
Complexity
• Initialisation: O(n)
• Propagation:• We enforce bounds consistency between at most n pairs of
variables: xα < yα or xα yα.
• Cost: b.
• Overall cost: O(nb).
• Amortised cost: O(b)
18/04/23 A CP Tutorial: Hnich 108
Results: BIBD
Time(s)
1
10
100
1000
10000
1 10 100 1000 10000
GACLex
De
co
mp
os
itio
n
Decomposition takesAbout 9 times longer on each of these instances.
18/04/23 A CP Tutorial: Hnich 109
Constraint Propagation
•General principle
•Consistency
•Filtering on simple constraints
•Filtering on global constraints
•Conclusion
18/04/23 A CP Tutorial: Hnich 110
Conclusion
•Constraint propagation is the glue to combine efficient
filtering algorithms for common substructuresMatching theory, network flow theory, automata theory, computational geometry, …, encapsulated in constraints
•Characterization of level of consistency
•Aim for incrementality
•Amount/frequency of filtering vs processing time
ANY QUESTIONS?
Search
18/04/23 A CP Tutorial: Hnich 113
Search
•General principle
•Variable selection heuristics
•Value selection heuristics
•Conclusion
18/04/23 A CP Tutorial: Hnich 114
General principle
…
var int Solution[Nodes] in Values;
solve{forall(e in Edges)
abs(Solution[e.x] - Solution[e.y]) >1;
alldifferent(Solution); };
18/04/23 A CP Tutorial: Hnich 115
General principle
…
var int Solution[Nodes] in Values;
solve{… }; search {…}
18/04/23 A CP Tutorial: Hnich 116
General principle
…
var int Solution[Nodes] in Values;
solve{forall(e in Edges)
abs(Solution[e.x] - Solution[e.y]) >1;
alldifferent(Solution); }; search {…}
With a good search strategy
We can quickly find good solution
18/04/23 A CP Tutorial: Hnich 117
Searching for a good solution
•For any interesting problem, propagation alone is not enough
•We typically proceed by tree search
18/04/23 A CP Tutorial: Hnich 118
Solving CSP by Search
Search tree: root: empty node one variable per level sucessors of a node: every value of the next level var
F
N
S
18/04/23 A CP Tutorial: Hnich 119
Searching for a good solution
•Two main decisions to control searchchoose a variablechoose a value from its domain
18/04/23 A CP Tutorial: Hnich 120
Search
•General principle
•Variable selection heuristics
•Value selection heuristics
•Conclusion
18/04/23 A CP Tutorial: Hnich 121
Variable Selection Heuristics
•Has an impact on tree topology
•static vs dynamic ordering
•smallest-domain-firstfirst-fail principle: “To succeed, try first where you are most likely to fail.”
•regret: favour variable with greatest difference in cost between two best values in domain
18/04/23 A CP Tutorial: Hnich 122
Search
•General principle
•Variable selection heuristics
•Value selection heuristics
•Conclusion
18/04/23 A CP Tutorial: Hnich 123
Value Selection Heuristics
•Not as critical
•Very problem-dependent
•Alternative for large domains: domain splitting
18/04/23 A CP Tutorial: Hnich 124
Search
•General principle
•Variable selection heuristics
•Value selection heuristics
•Conclusion
18/04/23 A CP Tutorial: Hnich 125
Conclusion
•A lot of control over the search strategy
•Many heuristics developed, some of them generic
ANY QUESTIONS?
Lot-sizing under demand uncertainty
18/04/23 A CP Tutorial: Hnich 128
Demand Uncertainty in Supply Chain Networks
ProductionInventories
Sales
18/04/23 A CP Tutorial: Hnich 129
Demand Uncertainty in Supply Chain Networks
ProductionInventories
Sales
?
When to order?
How much to order?
18/04/23 A CP Tutorial: Hnich 130
Demand Uncertainty in Supply Chain Networks
ProductionInventories
Sales
Demand
Uncertainty
?
When to order?
How much to order?
18/04/23 A CP Tutorial: Hnich 131
Demand Uncertainty in Supply Chain Networks
ProductionInventories
Sales
Demand
Uncertainty
?
When to order?
How much to order?
work in collaboration
with Bell Labs Ireland
18/04/23 A CP Tutorial: Hnich 132
Demand Uncertainty in Supply Chain Networks
Determining the optimal inventory control policy parameters is key to profitability for any company involved in distribution and/or production of goods
We developed a CP model to find the optimal dynamic (R,S) inventory policy parameters such that
• the expected cost is minimized;
• demand is stochastic, non-stationary; and
• a minimum service level is required
18/04/23 A CP Tutorial: Hnich 133
Results
• State-of-the-art improvement for the stochastic non-stationary formulation of the lot-sizing problem
• Real-world instances can be solved in few seconds
• The strategy could be extended to deal with Capacity constraints Lead time uncertainty
18/04/23 A CP Tutorial: Hnich 134
(Demo)
18/04/23 A CP Tutorial: Hnich 135
Experimental results
Seasonal Demands
0.001
0.01
0.1
1
10
100
1000
10000
100000
24 26 28 30 32 34 36 38 40 42 44 46 48 50
periods
seco
nd
s ILOG - CP model
CPLEX - MIP model
Choco - CP model dyn red
ANY QUESTIONS?
18/04/23 A CP Tutorial: Hnich 137
Successful CP
•(Machine) Scheduling
(whole book on constraint-based scheduling)
•Sports Scheduling (e.g. NFL)
•Rostering
•Allocation (e.g. terminal gates to aircrafts)
•Transportation (e.g. VRP, airline crew rotation)
•Even pure problems like Maximum Clique
•Production planning (Lot-sizing)
18/04/23 A CP Tutorial: Hnich 138
Finding out more
•Talks: CP, CPAIOR, IJCAI, AAAI, ECAI,
INFORMS, CORS
•Papers: Lecture Notes in Computer Science (Springer), Constraints (Kluwer), AI journals, OR journals
•Software: CHIP; ECLiPSe; ECLAIR; FaCiLe;
ILOG OPL, Solver; SISCtus Prolog, Choco,. . .
18/04/23 A CP Tutorial: Hnich 139
Finding out more
Books:
•Apt, K., Principles of Constraint Programming, Cambridge University Press, 2003.•Baptiste, P., Le Pape, C., Nuijten, W.. Constraint-Based Scheduling, Kluwer Academic Publishers,2001.•Hooker, J., Logic-Based Methods for Optimization: Combining Optimization and Constraint Satisfaction, John Wiley & Sons, 2000.•Marriott, K., Stuckey, P.J., Programming with Constraints: An Introduction, MIT Press, 1998.•Constraint and Integer Programming: Toward a Unified Methodology, edited by M. Milano, Kluwer Academic Publishers, 2003.•Tsang, E., Foundations of Constraint Satisfaction, Academic Press, 1993.•Van Hentenryck, P., The OPL Optimization Programming Language, MIT Press, 1999.
18/04/23 A CP Tutorial: Hnich 140
Acknowledgements
•Some parts of this tutorial are adapted material from tutorials given by:
Gilles PesantPedro MesseguerChris Beck
•Most parts of the tutorial is work done in collaboration with:
Alan Frisch, Ian miguel, Zeynep Kiziltan, Toby Walsh, Armagan Tarim, Roberto Rossi, Steven Prestwich