Title: Constraint Programming I
1Constraint Programming I
Martin Henz, School of Computing,
NUS www.comp.nus.edu.sg/henz
2Today
- Constraint programming in a nutshell
- Constraint propagation
- Branching
- Exploration
- Other search components
3Thursday
- Branching and Exploration in OPL
- Case study ACC 97/98 Basketball
- Constraint programming techniques
4Today
- Constraint programming in a nutshell
- Propagation
- Branching
- Exploration
- Other search components
5Constraint Programming in a Nutshell
6Constraint Programming in a Nutshell
7SEND MORE MONEY
- Assign distinct digits to the letters
- S, E, N, D, M, O, R, Y
- such that
- S E N D
- M O R E
-
- M O N E Y holds.
8SEND MORE MONEY
- Assign distinct digits to the letters
- S, E, N, D, M, O, R, Y
- such that S E N D
- M O R E
-
- M O N E Y
- holds.
Solution 9 5 6 7 1 0 8
5 1 0 6 5 2
9Modeling
- Formalize the problem as a constraint problem
- n variables
- m constraints c1,,cm ? ?n
- problem Find a (v1,,vn)? ?n such
- that a ? ci , for all 1 ? i ?
m
10A Model for MONEY
- 8 variables S,E,N,D,M,O,R,Y
- 5 constraints
- c1 (S,E,N,D,M,O,R,Y)? ?8 0 ? S,,Y ? 9
- c2 (S,E,N,D,M,O,R,Y)? ?8
- 1000S 100E 10N D
- 1000M 100O 10R E
- 10000M 1000O 100N 10E Y
11A Model for MONEY (continued)
- more constraints
-
- c3 (S,E,N,D,M,O,R,Y)? ?8 S ? 0
- c4 (S,E,N,D,M,O,R,Y)? ?8 M ? 0
- c5 (S,E,N,D,M,O,R,Y)? ?8 SY all different
12Solution for MONEY
- c1 (S,E,N,D,M,O,R,Y)? ?8 0?S,,Y?9
- c2 (S,E,N,D,M,O,R,Y)? ?8
- 1000S 100E 10N D
- 1000M 100O 10R E
- 10000M 1000O 100N 10E Y
- c3 (S,E,N,D,M,O,R,Y)? ?8 S ? 0
- c4 (S,E,N,D,M,O,R,Y)? ?8 M ? 0
- c5 (S,E,N,D,M,O,R,Y)? ?8 SY all
different - Solution (9,5,6,7,1,0,8,2)? ?8
13Constraint Programming
Exploiting constraints during tree search
- Choose propagation algorithms
- all different wait for fixing
- sum interval consistency
- Choose branching algorithms
- first-fail
- Choose exploration algorithms
- depth-first search
14S E N D M O R E M O N E Y
S ? ? E ? ? N ? ? D ? ? M ? ? O ? ? R ? ? Y ? ?
- 0?S,,Y?9
- S ? 0
- M ? 0
- SY all different
- 1000S 100E
10N D - 1000M 100O 10R E
- 10000M 1000O 100N 10E Y
15S E N D M O R E M O N E Y
Propagate
S ? 0..9 E ? 0..9 N ? 0..9 D ? 0..9 M ?
0..9 O ? 0..9 R ? 0..9 Y ? 0..9
- 0?S,,Y?9
- S ? 0
- M ? 0
- SY all different
- 1000S 100E
10N D - 1000M 100O 10R E
- 10000M 1000O 100N 10E Y
16S E N D M O R E M O N E Y
Propagate
S ? 1..9 E ? 0..9 N ? 0..9 D ? 0..9 M ?
1..9 O ? 0..9 R ? 0..9 Y ? 0..9
- 0?S,,Y?9
- S ? 0
- M ? 0
- SY all different
- 1000S 100E
10N D - 1000M 100O 10R E
- 10000M 1000O 100N 10E Y
17S E N D M O R E M O N E Y
Propagate
S ? 9 E ? 4..7 N ? 5..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
- 0?S,,Y?9
- S ? 0
- M ? 0
- SY all different
- 1000S 100E
10N D - 1000M 100O 10R E
- 10000M 1000O 100N 10E Y
18S E N D M O R E M O N E Y
S ? 9 E ? 4..7 N ? 5..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
Branching
E ? 4
E 4
S ? 9 E ? 4..7 N ? 5..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
S ? 9 E ? 4..7 N ? 5..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
- 0?S,,Y?9
- S ? 0
- M ? 0
- SY all different
- 1000S 100E
10N D - 1000M 100O 10R E
- 10000M 1000O 100N 10E Y
19S E N D M O R E M O N E Y
S ? 9 E ? 4..7 N ? 5..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
Propagate
E ? 4
E 4
S ? 9 E ? 5..7 N ? 6..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
- 0?S,,Y?9
- S ? 0
- M ? 0
- SY all different
- 1000S 100E
10N D - 1000M 100O 10R E
- 10000M 1000O 100N 10E Y
20S E N D M O R E M O N E Y
Branching
S ? 9 E ? 4..7 N ? 5..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
E ? 4
E 4
S ? 9 E ? 5..7 N ? 6..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
E 5
E ? 5
S ? 9 E ? 5..7 N ? 6..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
S ? 9 E ? 5..7 N ? 6..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
21S E N D M O R E M O N E Y
Propagate
S ? 9 E ? 4..7 N ? 5..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
E ? 4
E 4
S ? 9 E ? 5..7 N ? 6..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
E 5
E ? 5
S ? 9 E ? 5 N ? 6 D ? 7 M ? 1 O ? 0 R
? 8 Y ? 2
S ? 9 E ? 6..7 N ? 7..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
22S E N D M O R E M O N E Y
Complete Search Tree
S ? 9 E ? 4..7 N ? 5..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
E 4
E ? 4
S ? 9 E ? 5..7 N ? 6..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
E 5
E ? 5
S ? 9 E ? 5 N ? 6 D ? 7 M ? 1 O ? 0 R
? 8 Y ? 2
S ? 9 E ? 6..7 N ? 7..8 D ? 2..8 M ?
1 O ? 0 R ? 2..8 Y ? 2..8
E ? 6
E 6
23Using OPL Syntax
enum Letter S,E,N,D,M,O,R,Y var int lLetter
in 0..9 solve alldifferent(l) lS ltgt 0
lM ltgt 0 lS1000 lE100
lN10 lD lM1000
lO100 lR10 lE lM10000
lO1000 lN100 lE10 lY search
forall(i in Letter ordered by increasing
dsize(li)) tryall(v in 0..9)
li v ?All Solutions Execution ? Run
24The Art of Constraint Programming
- Choose model
- Choose propagation algorithms
- Choose branching algorithms
- Choose exploration algorithms
25Demo SEND MORE MONEY
Click here for MONEY
26Constraint Programming Systems
support constraint programming with high-level
constructs
- Constraint programming library
- ILOG Solver (Puget 1993)
- Constraint programming languages
- CONSTRAINTS (Steele, Sussman 1980)
- CHIP (Dincbas, Hentenryck, Simonis, Aggoun 1988)
- CLP(R) (Jaffar, Maher, Stuckey, Yap 1992)
- Oz (Smolka and others 1995)
- OPL (Hentenryck 1998)
27Today
- Constraint programming in a nutshell
- Propagation
- Branching
- Exploration
- Other search components
28Issues in Propagation
- Expressivity What kind of information can be
expressed as propagators? - Completeness What behavior can be expected from
propagation? - Efficiency How much computational resources does
propagation consume?
29Completeness
- Main purpose of propagation is exposure of
inconsistencies. - Inconsistencies allow to prune the search tree.
- An implementation is complete with respect to a
set of constraints, if inconsistencies between
constraints are guaranteed to be exhibited.
30Completeness
- General arithmetic constraints are undecidable
(Hilberts Tenth Problem). - Completeness is not always possible.
- Example
- c1 n gt 2
- c2 an bn cn
31Basic Constraints vs. Propagators
- Basic constraints
- are conjunctions of constraints of the form
x ? S, where S is a finite set of integers - enjoy complete constraint solving
- Propagators
- can be arbitrarily expressive (arithmetic,
symbolic) - implementation typically fast but incomplete
32Current Domain
- Let c be basic constraint. For a given variable
x, - the maximal set S such that c ? x ? S is
- consistent is called the current domain of x in
c, - denoted domc(x).
- Example
- C x?1..10, y?9..20,
- x?8..12
- domc(y) 9..20, domc(x)8..10
33Domain vs. Interval Consistency
- Domain consistency Check all elements of the
domains of all variables known to the propagator - Interval consistency Check only the boundaries
of the domains of the variables
34Domain Consistency
- Assume a constraint d over variables x and
y. - A basic constraint c is domain consistent in x
- with respect to d, if for every i ? domc(x)
- there is a j ? domc(y) such that (i,j) ? d.
- Example
- d x y 10
- c x ? 7,9, y ? 1..20
35Domain Consistency
- Assume a constraint d over variables x and
y. - A basic constraint c is domain consistent in x
- with respect to d, if for every i ? domc(x)
- there is a j ? domc(y) such that (i,j) ? d.
- Example
- d x y 10
- c x ? 7,9, y ? 1,3
36Interval Consistency
- Assume a constraint d over variables x and
y. - A basic constraint c is interval consistent in x
with - respect to d, if for imin(domc(x)) and for
- imax(domc(x)) there is a j ? domc(y)
- such that (i,j) ? d.
- Example
- d x y 10
- C x ? 7,9, y ? 1..20
37Interval Consistency
- Assume a constraint d over variables x and
y. - A basic constraint c is interval consistent in x
with - respect to d, if for imin(domc(x)) and for
- imax(domc(x)) there is a j ? domc(y)
- such that (i,j) ? d.
- Example
- d x y 10
- C x?7,9, y?1..3
38Domain and Interval Consistency
- OPL syntax for all-different
- interval consistency alldifferent(l)
- domain consistency alldifferent(l) domain
39Some Propagator Classes
- Symbolic propagators
- Arithmetic propagators
- Reification
40Symbolic PropagatorsExample The Element
Propagator
- OPL arrayi x
- Meaning x is the ith element of array
- Example int a1..4 5,6,7,8
- var int i,x in 0..9
- solve ai x
- domc(i)1,3 ? x ? 5,7
- domc(x)6,8 ? i ? 2,4
41Arithmetic Propagators
- General arithmetic equations
- i1x11x1m1 inxn1xnmn 0
- lt gt lt gt ltgt
42Reified Constraints
- Reflecting the validity of a constraint in a
- 0/1 variable
- Example Reified arithmetic equations
- x (i1x11x1m1 inxn1xnmn 0)
- lt gt lt gt ltgt
43Propagation vs Branching
- trade-off
- complex propagation algorithms incur
- fewer, but more expensive nodes in
tree - Example MONEY with
alldiff and sum only test fixed assignment
alldiff wait for fixed variables sum interval
cons.
alldiff and sum domain consistency
44Today
- Constraint programming in a nutshell
- Propagation
- Branching
- Exploration
- Other search components
45Branching Algorithms
- Constraint programming systems come with
- libraries of predefined branching algorithms
- programming support for user-defined branching
algorithms
46Branching for MONEY
enum Letter S,E,N,D,M,O,R,Y var int lLetter
in 0..9 solve alldifferent(l) lS ltgt 0
lM ltgt 0 lS1000 lE100
lN10 lD lM1000
lO100 lR10 lE lM10000
lO1000 lN100 lE10 lY search
forall(i in Letter ordered by
increasing dsize(li)) tryall(v in 0..9)
li v ?All Solutions Execution ?
Run
47Basic Choice Points
x lt y
x gt y
48Choice Point Sequences
- try x lt y x gt y endtry
- try z 1 z 2 endtry
x lt y
x gt y
z 1
z 2
z 1
z 2
49Abbreviation tryall
- tryall(i in 1..5) x i
- stands for
- try x1x2x3x4x5 endtry
50Abbreviation forall
- forall(i in 1..4)
- try ai0 ai1 endtry
- stands for
- try a1 0 a1 1 endtry
- try a2 0 a2 1 endtry
- try a3 0 a3 1 endtry
- try a4 0 a4 1 endtry
51Examples of Branching Algorithms
- Enumeration Choose variable, choose value
- naive enumeration
choose variables and values in
a fixed sequence - first-fail enumeration
choose a variable with minimal
domain size - Domain-splitting
- try x lt mid x gt mid endtry
- Task sequencing for scheduling
52Today
- Constraint programming in a nutshell
- Propagation
- Branching
- Exploration
- Other search components
53Exploration for MONEY
enum Letter S,E,N,D,M,O,R,Y var int lLetter
in 0..9 solve alldifferent(l) lS ltgt 0
lM ltgt 0 lS1000 lE100
lN10 lD lM1000
lO100 lR10 lE lM10000
lO1000 lN100 lE10 lY search
forall(i in Letter ordered by
increasing dsize(li)) tryall(v in 0..9)
li v ?All Solutions Execution ?
Run
54Exploration
- Depth-first search (default in OPL)
- Best-first search
- Limited discrepancy search Harvey/Ginsberg 95
- user-defined explorations
55Specifying Exploration in OPL
enum Letter S,E,N,D,M,O,T,Y var int lLetter
in 0..9 solve alldifferent(l) lS ltgt 0
lM ltgt 0 lS1000 lE100
lN10 lD lM1000
lO100 lS10 lT lM10000
lO1000 lN100 lE10 lY search
LDSearch(4) forall(i in Letter ordered by
increasing dsize(li)) tryall(v in 0..9)
li v
56Today
- Constraint programming in a nutshell
- Propagation
- Branching
- Exploration
- Other search components
57Other Search Components
- Optimization
- Interaction
- Visualization
- Search Limits
58Optimization
59Optimization in OPL
enum Letter S,E,N,D,M,O,T,Y var int lLetter
in 0..9 maximize money subject to money
lM10000lO1000lN100lE10lY
alldifferent(l) lS ltgt 0 lM ltgt 0
lS1000 lE100 lN10 lD
lM1000 lO100 lS10
lT lM10000 lO1000 lN100
lE10 lY search forall(i in Letter
ordered by increasing dsize(li)) tryall(v
in 0..9) li v ?All Solutions
Execution ? Run
60Interaction
- First-solution search
- All solution search
- Last solution search
- Search with user interaction
61Visualization
- Example Oz Explorer Schulte 1997.
- Oz Explorer combines
- visualization
- first/all solution / user interaction
- branch-and-bound optimization
- depth-first search
62Search Limits
- SEND MOST MONEY
- but dont spend more than 10 hours searching!!!
63Search Limits in OPL
enum Letter S,E,N,D,M,O,T,Y var int lLetter
in 0..9 maximize money subject to money
lM10000lO1000lN100lE10lY
alldifferent(l) lS ltgt 0 lM ltgt 0
lS1000 lE100 lN10 lD
lM1000 lO100 lS10
lT lM10000 lO1000 lN100
lE10 lY search timeLimit 36000
forall(i in Letter ordered by increasing
dsize(li)) tryall(v in 0..9)
li v ?All Solutions Execution ? Run
64Thursday
- Branching and Exploration in OPL
- Case study ACC 97/98 Basketball
- Constraint programming techniques