570 likes | 815 Views
An Overview of Constraint Programming. February 15/16, 2000. Today. Constraint programming in a nutshell Constraint propagation Branching Exploration. Thursday/Friday. Applying constraint programming to job-shop and other scheduling problems. Today. Constraint programming in a nutshell
E N D
An Overview of Constraint Programming February 15/16, 2000
Today • Constraint programming in a nutshell • Constraint propagation • Branching • Exploration SMA HPC (c) NUS 15.094 Constraint Programming
Thursday/Friday • Applying constraint programming to job-shop and other scheduling problems SMA HPC (c) NUS 15.094 Constraint Programming
Today • Constraint programming in a nutshell • Propagation • Branching • Exploration SMA HPC (c) NUS 15.094 Constraint Programming
Constraint Programming in a Nutshell SEND MORE MONEY SMA HPC (c) NUS 15.094 Constraint Programming
Constraint Programming in a Nutshell SEND + MORE = MONEY SMA HPC (c) NUS 15.094 Constraint Programming
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 holds. SMA HPC (c) NUS 15.094 Constraint Programming
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 holds. Solution 9 5 6 7 + 1 0 8 5 = 1 0 6 5 2 SMA HPC (c) NUS 15.094 Constraint Programming
Modeling Formalize the problem as a constraint problem: • number of variables: n • constraints: c1,…,cm n • problem: Find a =(v1,…,vn) nsuch that a ci , for all 1 i m SMA HPC (c) NUS 15.094 Constraint Programming
A Model for MONEY • number of variables: 8 • 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 | 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
A 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 | S…Y all different} SMA HPC (c) NUS 15.094 Constraint Programming
Solution for MONEY c1={(S,E,N,D,M,O,R,Y) 8 | 0S,…,Y9 } c2={(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} 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 | S…Y all different} Solution: (9,5,6,7,1,0,8,2)8 SMA HPC (c) NUS 15.094 Constraint Programming
Constraint Programming Exploiting constraints during tree search • Choose propagation algorithms • all different: wait for fixing • sum: interval consistency • Choose branching algorithm • first-fail • Choose exploration algorithm • depth-first search SMA HPC (c) NUS 15.094 Constraint Programming
S E N D+ M O R E= M O N E Y S E N D M O R Y 0S,…,Y9 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
S 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} 0S,…,Y9 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
S 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} 0S,…,Y9 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
S 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} 0S,…,Y9 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
S 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} 0S,…,Y9 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 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} SMA HPC (c) NUS 15.094 Constraint Programming
S 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} 0S,…,Y9 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 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} SMA HPC (c) NUS 15.094 Constraint Programming
S 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} SMA HPC (c) NUS 15.094 Constraint Programming
S 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} SMA HPC (c) NUS 15.094 Constraint Programming
S 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 SMA HPC (c) NUS 15.094 Constraint Programming
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
The Art of Constraint Programming • Choose model • Choose propagation algorithms • Choose branching algorithm • Choose exploration algorithm SMA HPC (c) NUS 15.094 Constraint Programming
Demo: SEND + MORE = MONEY Click here for MONEY SMA HPC (c) NUS 15.094 Constraint Programming
Constraint 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) SMA HPC (c) NUS 15.094 Constraint Programming
Today • Constraint programming in a nutshell • Propagation • Branching • Exploration SMA HPC (c) NUS 15.094 Constraint Programming
Issues 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? SMA HPC (c) NUS 15.094 Constraint Programming
Completeness • General arithmetic constraints are undecidable (Hilbert’s Tenth Problem). • Propagation may not exhibit all inconsistencies. • Example: c1: n > 2 c2: an + bn = cn SMA HPC (c) NUS 15.094 Constraint Programming
Basic 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 SMA HPC (c) NUS 15.094 Constraint Programming
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 ofXin 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
Domain 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 SMA HPC (c) NUS 15.094 Constraint Programming
Domain Consistency Assume a constraint d over variables X and Y. A basic constraint C is domain consistent inX with respect tod, 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
Domain Consistency Assume a constraint d over variables X and Y. A basic constraint C is domain consistent inX with respect tod, if for every idomc(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
Interval Consistency Assume a constraint d over variables X and Y. A basic constraint C is interval consistent inXwith respect tod, 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
Interval Consistency Assume a constraint d over variables X and Y. A basic constraint C is interval consistent inXwith respect tod, 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
Some Propagator Classes • Symbolic propagators • Arithmetic propagators • Scheduling propagators • Reification SMA HPC (c) NUS 15.094 Constraint Programming
Symbolic PropagatorsExample: 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
Arithmetic Propagators General arithmetic equations: I1*X11*…*X1m1 + … + In*Xn1*…*Xnmn = 0 < > <= >= <> SMA HPC (c) NUS 15.094 Constraint Programming
Scheduling Propagators Details on Thursday/Friday Issues: • resource constraints • precedence constraints • edge finding • task intervals SMA HPC (c) NUS 15.094 Constraint Programming
Reified Constraints Reflecting the validity of a constraint in a 0/1 variable Example: Reified arithmetic equations X = (I1*X11*…*X1m1 + … + In*Xn1*…*Xnmn = 0) < > <= >= <> SMA HPC (c) NUS 15.094 Constraint Programming
Propagation vs Branching Obvious trade-off complex propagation algorithms: fewer, but more expensive nodes Example: MONEY with alldiff and sum: only test fixed assignment alldiff: wait for fixed variables sum: interval cons. alldiff and sum: domain consistency SMA HPC (c) NUS 15.094 Constraint Programming
Today • Constraint programming in a nutshell • Propagation • Branching • Exploration SMA HPC (c) NUS 15.094 Constraint Programming
Branching Algorithms Constraint programming systems come with • libraries of predefined branching algorithms • programming support for user-defined branching algorithms SMA HPC (c) NUS 15.094 Constraint Programming
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
Basic Choice Points try x < y | x >= y endtry; x < y x >= y SMA HPC (c) NUS 15.094 Constraint Programming
Choice Point Sequences try x < y | x >= y endtry; try z = 1 | z = 2 endtry; x < y x >= y z = 1 z = 2 z = 1 z = 2 SMA HPC (c) NUS 15.094 Constraint Programming
Abbreviation: tryall 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
Abbreviation: forall forall(i in 1..4) try a[i]=0 | a[i]=1 endtry; stands for try a[1] = 0 | a[1] = 1 endtry; try a[2] = 0 | a[2] = 1 endtry; try a[3] = 0 | a[3] = 1 endtry; try a[4] = 0 | a[4] = 1 endtry; SMA HPC (c) NUS 15.094 Constraint Programming
Examples 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 < mid | x >= mid endtry; • Task sequencing for scheduling (Thursday/Friday) SMA HPC (c) NUS 15.094 Constraint Programming