An Overview of Constraint Programming February 1516 2000
An Overview of Constraint Programming February 15/16, 2000 SMA HPC (c) NUS 15. 094 Constraint Programming
Today h Constraint programming in a nutshell h Constraint propagation h Branching h Exploration SMA HPC (c) NUS 15. 094 Constraint Programming 2
Thursday/Friday h Applying constraint programming to jobshop and other scheduling problems SMA HPC (c) NUS 15. 094 Constraint Programming 3
Today h Constraint programming in a nutshell h Propagation h Branching h Exploration SMA HPC (c) NUS 15. 094 Constraint Programming 4
Constraint Programming in a Nutshell SEND SMA HPC (c) NUS MORE MONEY 15. 094 Constraint Programming 5
Constraint Programming in a Nutshell SEND + MORE SMA HPC (c) NUS = MONEY 15. 094 Constraint Programming 6
SEND + 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 SMA HPC (c) NUS holds. 15. 094 Constraint Programming 7
SEND + MORE = MONEY Assign distinct digits to the letters S, E, N, D, M, O, R, Y Solution such that S E N D 9 5 6 7 + M O R E + 1 0 8 5 = M O N E Y = 1 0 6 5 2 holds. SMA HPC (c) NUS 15. 094 Constraint Programming 8
Modeling Formalize the problem as a constraint problem: h number of variables: n h constraints: c 1, …, cm n h problem: Find a = (v 1, …, vn) n such that a ci , for all 1 i m SMA HPC (c) NUS 15. 094 Constraint Programming 9
A Model for MONEY h number of variables: 8 h constraints: c 1 = {(S, E, N, D, M, O, R, Y) c 2 = {(S, E, N, D, M, O, R, Y) 1000*S + 1000*M = 10000*M + 1000*O SMA HPC (c) NUS 8 8 + + + | 0 | 100*E 100*O 100*N S, …, Y 9 } + 10*N + D + 10*R + E + 10*E + Y} 15. 094 Constraint Programming 10
A Model for MONEY (continued) h more constraints c 3 = {(S, E, N, D, M, O, R, Y) 8 | S 0 } c 4 = {(S, E, N, D, M, O, R, Y) 8 | M 0 } c 5 = {(S, E, N, D, M, O, R, Y) 8 | S…Y all different} SMA HPC (c) NUS 15. 094 Constraint Programming 11
Solution for MONEY c 1 = {(S, E, N, D, M, O, R, Y) 8 | 0 S, …, Y 9 } c 2 = {(S, E, N, D, M, O, R, Y) 8 | 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y} c 3 = {(S, E, N, D, M, O, R, Y) 8 | S 0 } c 4 = {(S, E, N, D, M, O, R, Y) 8 | M 0 } c 5 = {(S, E, N, D, M, O, R, Y) 8 | S…Y all different} Solution: (9, 5, 6, 7, 1, 0, 8, 2) 8 SMA HPC (c) NUS 15. 094 Constraint Programming 12
Constraint Programming Exploiting constraints during tree search h Choose propagation algorithms hall different: wait for fixing hsum: interval consistency h Choose branching algorithm hfirst-fail h Choose exploration algorithm hdepth-first search SMA HPC (c) NUS 15. 094 Constraint Programming 13
S E N D M O R Y S E N D + M O R E = M O N E Y 0 S, …, Y 9 S 0 M 0 S…Y all different 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y SMA HPC (c) NUS 15. 094 Constraint Programming 14
Propagate S E N D M O R Y {0. . 9} {0. . 9} S E N D + M O R E = M O N E Y 0 S, …, Y 9 S 0 M 0 S…Y all different 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y SMA HPC (c) NUS 15. 094 Constraint Programming 15
Propagate S E N D M O R Y {1. . 9} {0. . 9} S E N D + M O R E = M O N E Y 0 S, …, Y 9 S 0 M 0 S…Y all different 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y SMA HPC (c) NUS 15. 094 Constraint Programming 16
Propagate S E N D M O R Y SMA HPC (c) NUS {9} {4. . 7} {5. . 8} {2. . 8} {1} {0} {2. . 8} S E N D + M O R E = M O N E Y 0 S, …, Y 9 S 0 M 0 S…Y all different 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y 15. 094 Constraint Programming 17
S E N D M O R Y Branching E=4 S E N D M O R Y {9} {4. . 7} {5. . 8} {2. . 8} {1} {0} {2. . 8} S E N D + M O R E E 4 S E N D M O R Y = M O N E Y {9} {4. . 7} {5. . 8} {2. . 8} {1} {0} {2. . 8} 0 S, …, Y 9 S 0 M 0 S…Y all different 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y SMA HPC (c) NUS 15. 094 Constraint Programming 18
S E N D M O R Y Propagate E=4 {9} {4. . 7} {5. . 8} {2. . 8} {1} {0} {2. . 8} S E N D + M O R E E 4 S E N D M O R Y = M O N E Y {9} {5. . 7} {6. . 8} {2. . 8} {1} {0} {2. . 8} 0 S, …, Y 9 S 0 M 0 S…Y all different 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y SMA HPC (c) NUS 15. 094 Constraint Programming 19
Branching S E N D M O R Y {9} {4. . 7} {5. . 8} {2. . 8} {1} {0} {2. . 8} S E N D + M O R E E 4 E=4 S E N D M O R Y E=5 S E N D M O R Y SMA HPC (c) NUS {9} {5. . 7} {6. . 8} {2. . 8} {1} {0} {2. . 8} = M O N E Y E 5 S E N D M O R Y {9} {5. . 7} {6. . 8} {2. . 8} {1} {0} {2. . 8} 15. 094 Constraint Programming 20
Propagate S E N D M O R Y {9} {4. . 7} {5. . 8} {2. . 8} {1} {0} {2. . 8} S E N D + M O R E E 4 E=4 S E N D M O R Y E=5 S E N D M O R Y SMA HPC (c) NUS {9} {5} {6} {7} {1} {0} {8} {2} {9} {5. . 7} {6. . 8} {2. . 8} {1} {0} {2. . 8} = M O N E Y E 5 S E N D M O R Y {9} {6. . 7} {7. . 8} {2. . 8} {1} {0} {2. . 8} 15. 094 Constraint Programming 21
Complete Search E=4 Tree S E N D M O R Y {9} {4. . 7} {5. . 8} {2. . 8} {1} {0} {2. . 8} E 4 E=5 S E N D M O R Y {9} {5. . 7} {6. . 8} {2. . 8} {1} {0} {2. . 8} {9} {5} {6} {7} {1} {0} {8} {2} E=6 SMA HPC (c) NUS S E N D + M O R E = M O N E Y E 5 S E N D M O R Y {9} {6. . 7} {7. . 8} {2. . 8} {1} {0} {2. . 8} E 6 15. 094 Constraint Programming 22
Using OPL Syntax enum Letter {S, E, N, D, M, O, R, Y}; var int l[Letter] in 0. . 9; solve { alldifferent(l); l[S] <> 0; l[M] <> 0; l[S]*1000 + l[E]*100 + l[N]*10 + l[D] + l[M]*1000 + l[O]*100 + l[R]*10 + l[E] = l[M]*10000 + l[O]*1000 + l[N]*100 + l[E]*10 + l[Y] }; search { forall(i in Letter ordered by increasing dsize(l[i])) tryall(v in 0. . 9) l[i] = v; }; All Solutions; Execution Run SMA HPC (c) NUS 15. 094 Constraint Programming 23
The Art of Constraint Programming h Choose model h Choose propagation algorithms h Choose branching algorithm h Choose exploration algorithm SMA HPC (c) NUS 15. 094 Constraint Programming 24
Demo: SEND + MORE = MONEY Click here for MONEY SMA HPC (c) NUS 15. 094 Constraint Programming 25
Constraint Programming Systems support constraint programming with high-level constructs h Constraint programming library h. ILOG Solver (Puget 1993) h Constraint programming languages h. CONSTRAINTS (Steele, Sussman 1980) h. CHIP (Dincbas, Hentenryck, Simonis, Aggoun 1988) h. CLP(R) (Jaffar, Maher, Stuckey, Yap 1992) h. Oz (Smolka and others 1995) h. OPL (Hentenryck 1998) SMA HPC (c) NUS 15. 094 Constraint Programming 26
Today h Constraint programming in a nutshell h Propagation h Branching h Exploration SMA HPC (c) NUS 15. 094 Constraint Programming 27
Issues in Propagation h Expressivity: What kind of information can be expressed as propagators? h Completeness: What behavior can be expected from propagation? h Efficiency: How much computational resources does propagation consume? SMA HPC (c) NUS 15. 094 Constraint Programming 28
Completeness h General arithmetic constraints are undecidable (Hilbert’s Tenth Problem). h Propagation may not exhibit all inconsistencies. h Example: c 1: c 2: SMA HPC (c) NUS n > 2 an + b n = cn 15. 094 Constraint Programming 29
Basic Constraints vs. Propagators h Basic constraints hare conjunctions of constraints of the form X S, where S is a finite set of integers henjoy complete constraint solving h Propagators hcan be arbitrarily expressive (arithmetic, symbolic) himplementation typically fast but incomplete SMA HPC (c) NUS 15. 094 Constraint Programming 30
Current 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} SMA HPC (c) NUS 15. 094 Constraint Programming 31
Domain vs. Interval Consistency h Domain consistency: Check all elements of the domains of all variables known to the propagator h Interval consistency: Check only the boundaries of the domains of the variables SMA HPC (c) NUS 15. 094 Constraint Programming 32
Domain 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} SMA HPC (c) NUS 15. 094 Constraint Programming 33
Domain 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} SMA HPC (c) NUS 15. 094 Constraint Programming 34
Interval 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 i=min(domc(X)) and for i=max(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} SMA HPC (c) NUS 15. 094 Constraint Programming 35
Interval 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 i=min(domc(X)) and for i=max(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} SMA HPC (c) NUS 15. 094 Constraint Programming 36
Some Propagator Classes h Symbolic propagators h Arithmetic propagators h Scheduling propagators h Reification SMA HPC (c) NUS 15. 094 Constraint Programming 37
Symbolic Propagators Example: The “Element” Propagator OPL: array[i] = x Meaning: x is the ith element of array Example: int a[1. . 4] = [5, 6, 7, 8] var int i, x in 0. . 9; solve { a[i] = x; } domc(I)={1, 3} X {5, 7} domc(X)={6, 8} I {2, 4} SMA HPC (c) NUS 15. 094 Constraint Programming 38
Arithmetic Propagators General arithmetic equations: I 1*X 11*…*X 1 m 1 + … + In*Xn 1*…*Xnmn = 0 < > <= >= SMA HPC (c) NUS <> 15. 094 Constraint Programming 39
Scheduling Propagators Details on Thursday/Friday Issues: h resource constraints h precedence constraints h edge finding h task intervals SMA HPC (c) NUS 15. 094 Constraint Programming 40
Reified Constraints Reflecting the validity of a constraint in a 0/1 variable Example: Reified arithmetic equations X = (I 1*X 11*…*X 1 m 1 + … + In*Xn 1*…*Xnmn = 0) < > <= >= SMA HPC (c) NUS <> 15. 094 Constraint Programming 41
Propagation vs Branching Obvious trade-off complex propagation algorithms: fewer, but more expensive nodes Example: MONEY with alldiff and sum: only test fixed assignment SMA HPC (c) NUS alldiff: wait for fixed variables sum: interval cons. alldiff and sum: domain consistency 15. 094 Constraint Programming 42
Today h Constraint programming in a nutshell h Propagation h Branching h Exploration SMA HPC (c) NUS 15. 094 Constraint Programming 43
Branching Algorithms Constraint programming systems come with hlibraries of predefined branching algorithms hprogramming support for user-defined branching algorithms SMA HPC (c) NUS 15. 094 Constraint Programming 44
Branching for MONEY enum Letter {S, E, N, D, M, O, R, Y}; var int l[Letter] in 0. . 9; solve { alldifferent(l); l[S] <> 0; l[M] <> 0; l[S]*1000 + l[E]*100 + l[N]*10 + l[D] + l[M]*1000 + l[O]*100 + l[R]*10 + l[E] = l[M]*10000 + l[O]*1000 + l[N]*100 + l[E]*10 + l[Y] }; search { forall(i in Letter ordered by increasing dsize(l[i])) tryall(v in 0. . 9) l[i] = v; }; All Solutions; Execution Run SMA HPC (c) NUS 15. 094 Constraint Programming 45
Basic Choice Points try x < y SMA HPC (c) NUS | x >= y endtry; x >= y 15. 094 Constraint Programming 46
Choice Point Sequences try x < y try z = 1 | | x < y z = 1 SMA HPC (c) NUS z = 2 x >= y endtry; z = 2 endtry; x >= y z = 1 z = 2 15. 094 Constraint Programming 47
Abbreviation: tryall(i in 1. . 5) x = i; stands for try x=1|x=2|x=3|x=4|x=5 endtry; SMA HPC (c) NUS 15. 094 Constraint Programming 48
Abbreviation: forall(i in 1. . 4) try a[i]=0 | a[i]=1 endtry; stands for try try SMA HPC (c) NUS a[1] a[2] a[3] a[4] = = 0 0 | | a[1] a[2] a[3] a[4] = = 1 1 endtry; 15. 094 Constraint Programming 49
Examples of Branching Algorithms h Enumeration: Choose variable, choose value hnaive enumeration: choose variables and values in a fixed sequence hfirst-fail enumeration: choose a variable with minimal domain size h Domain-splitting: try x < mid | x >= mid endtry; h Task sequencing for scheduling (Thursday/Friday) SMA HPC (c) NUS 15. 094 Constraint Programming 50
Today h Constraint programming in a nutshell h Propagation h Branching h Exploration SMA HPC (c) NUS 15. 094 Constraint Programming 51
Exploration Algorithms Combinations of aspects such as: h Order of exploration h Interaction h Optimization h Visualization SMA HPC (c) NUS 15. 094 Constraint Programming 52
Exploration for MONEY enum Letter {S, E, N, D, M, O, R, Y}; var int l[Letter] in 0. . 9; solve { alldifferent(l); l[S] <> 0; l[M] <> 0; l[S]*1000 + l[E]*100 + l[N]*10 + l[D] + l[M]*1000 + l[O]*100 + l[R]*10 + l[E] = l[M]*10000 + l[O]*1000 + l[N]*100 + l[E]*10 + l[Y] }; search { forall(i in Letter ordered by increasing dsize(l[i])) tryall(v in 0. . 9) l[i] = v; }; All Solutions; Execution Run SMA HPC (c) NUS 15. 094 Constraint Programming 53
Order of Exploration h Depth-first search h Limited discrepancy search [Harvey/Ginsberg 95] SMA HPC (c) NUS 15. 094 Constraint Programming 54
Interaction h First-solution search h All solution search h Last solution search h Search with user interaction SMA HPC (c) NUS 15. 094 Constraint Programming 55
Optimization h Branch-and-bound h Restart optimization (details on Friday) SMA HPC (c) NUS 15. 094 Constraint Programming 56
Visualization Example: Oz Explorer [Schulte 1997]. Oz Explorer combines hvisualization hfirst/all solution / user interaction hbranch-and-bound optimization hdepth-first search SMA HPC (c) NUS 15. 094 Constraint Programming 57
- Slides: 57