The Theory and Practice of Constraint Programming: An Overview Brahim Hnich Faculty of Computer Science Izmir University of Economics Izmir, Turkey 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 21/03/2016 A CP Tutorial: Hnich 2 Caveat •In this talk: Constraint programming for combinatorial problems “Programming” refers to its roots in computer science (programming languages) 21/03/2016 A CP Tutorial: Hnich 3 Outline •Modelling •Constraint propagation •Search •Demo: Lot-sizing with stochastic non-stationary demand 21/03/2016 A CP Tutorial: Hnich 4 A Puzzle • Place the numbers 1 through 8 in the nodes such that: Each number appears exactly once No connected nodes have consecutive numbers ? 21/03/2016 ? ? ? ? ? ? A CP Tutorial: Hnich ? 5 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, 21/03/2016 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 forall i<j. vi ≠ vj, Or more compactly, all-different[v1,…,v8] 21/03/2016 A CP Tutorial: Hnich 7 Heuristic Search {1, 2, 3, 4, 5, 6, 7, 8} ? 21/03/2016 ? ? 1? 8? ? ? A CP Tutorial: Hnich ? 8 Inference/Propagation {1, 2, 3, 4, 5, 6, 7, 8} ? ? ? 1? 8? ? ? ? {1, 2, 3, 4, 5, 6, 7, 8} 21/03/2016 A CP Tutorial: Hnich 9 Inference/Propagation {3, 4, 5, 6} {3, 4, 5, 6} 3? 6? {3, 4, 5, 6, 7} 7? {2, 3, 4, 5, 6} 1? 8? 4? 5? {3, 4, 5, 6} 21/03/2016 2? {3, 4, 5, 6} A CP Tutorial: Hnich 10 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); }; 21/03/2016 A CP Tutorial: Hnich 11 The Core of Constraint Computation Modelling Solving Heuristic Search 21/03/2016 Propagation A CP Tutorial: Hnich 12 ANY QUESTIONS? Modeling 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) 21/03/2016 A CP Tutorial: Hnich 15 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 21/03/2016 A CP Tutorial: Hnich 16 CSP: Relevance Many problems can be represented as CSP: Artificial Intelligence Real-life applications o temporal reasoning Control Theory o controllers for sensory based robots Concurency Production planning Staff scheduling o process comm. and synchr. Resource allocation Computer Graphics o geometric coherence Circuit design Database Systems Option trading o constraint databases Bioinformatics DNA sequencing o sequence alignment ... Operations research o optimization 21/03/2016 A CP Tutorial: Hnich 17 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 21/03/2016 A CP Tutorial: Hnich 18 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 21/03/2016 A CP Tutorial: Hnich 19 Map Colouring Variables: F, N, S Values: { } Constraints: N ≠ S ≠ F ≠ N S F N A solution: F N S 21/03/2016 A CP Tutorial: Hnich 20 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 }; 21/03/2016 A CP Tutorial: Hnich 21 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 21/03/2016 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 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. 21/03/2016 A CP Tutorial: Hnich 23 A Solution S= { AAGCCGTT, TACGCGAT} Each word in S has 4 symbols from { C,G }; 21/03/2016 A CP Tutorial: Hnich 24 A Solution S= { AAGCCGTT, TACGCGAT} Each pair of distinct words in S differ in at least 4 positions 21/03/2016 A CP Tutorial: Hnich 25 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 A4CP positions 21/03/2016 Tutorial: Hnich 26 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 } { 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 Each row represents a word in S M[i,j]: a decision variable with domain { A,C,G,T } 21/03/2016 A CP Tutorial: Hnich 27 A Matrix Model 1. Expressing that each word in S has 4 symbols from { C,G } 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 } { 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 21/03/2016 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 } { 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 For each row r sum (p in 1..8) //channelling constraints (M[r,p]=C or M[r,p]=G) = 4 21/03/2016 A CP Tutorial: Hnich 29 A Matrix Model 1. … 2. Each pair of distinct words in S differ in at least 4 positions 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 } { 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 For each distinct rows r1 and r2 sum(p in 1..8) (M[r1,p] ≠ M[r2,p]) >= 4 21/03/2016 A CP Tutorial: Hnich 30 A Matrix Model 1. … 2. … 3. xR and yC differ in at least 4 positions. 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 } { 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 Introduce a “compliment matrix” Each pair //channelling constraints <M[i,j], MC[i,j]> in {<C,G>, <G,C>, <A,T>, <T,A>} 21/03/2016 A CP Tutorial: Hnich 31 A Matrix Model 1. … 2. … 3. xR and yC differ in at least 4 positions. 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 } { 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 For each rows r1 and r2 sum(p in 1..8) (M[r1,9-p] ≠ MC[r2,p]) >= 4 21/03/2016 A CP Tutorial: Hnich 32 ANY QUESTIONS? Constraint Propagation Constraint Propagation •General principle •Consistency •Filtering on simple constraints •Filtering on global constraints •Conclusion 21/03/2016 A CP Tutorial: Hnich 35 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 21/03/2016 A CP Tutorial: Hnich 36 General Principle {1, 2, 3, 4, 5, 6, 7, 8} Constraint network ≠ ? ≠ ? {1, 2, 3, 4, 5, 6, 7, 8} 21/03/2016 ≠ ≠ ≠ ≠ ≠ ≠ 1? ≠ ? A CP Tutorial: Hnich ? ≠ ≠ ≠ ≠ 8? ≠ ≠ ≠ ? ≠ ? 37 General Principle •This propagation necessarily terminates •The results is the same regardless of the order in which we consider the constraints 21/03/2016 A CP Tutorial: Hnich 38 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) 21/03/2016 A CP Tutorial: Hnich 39 Constraint Propagation •General principle •Consistency •Filtering on simple constraints •Filtering on global constraints •Conclusion 21/03/2016 A CP Tutorial: Hnich 40 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 21/03/2016 A CP Tutorial: Hnich 41 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. 21/03/2016 A CP Tutorial: Hnich 42 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. 21/03/2016 A CP Tutorial: Hnich 43 Consistency: BC example •Consider 4x + 3y - 2z = 10 •with Dx = Dy = Dz = {0, 1, . . . , 9} •Maintaining BC reduces domains to Dx = {0, 1, . . . , 7}, Dy = Dz = {0, 1, . . . , 9} •BC is triggered on range event 21/03/2016 A CP Tutorial: Hnich 44 Consistency: GAC example •Consider 4x + 3y - 2z = 10 •with Dx = Dy = Dz = {0, 1, . . . , 9} •Maintaining GAC reduces domains to Dx = {0, 1, . . . , 7}, Dy = {0, 2, 4, 6, 8}, Dz = {0, 1, . . . , 9} •GAC is triggered on domain event 21/03/2016 A CP Tutorial: Hnich 45 Constraint Propagation •General principle •Consistency •Filtering on simple constraints •Filtering on global constraints •Conclusion 21/03/2016 A CP Tutorial: Hnich 46 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 21/03/2016 A CP Tutorial: Hnich 47 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 21/03/2016 A CP Tutorial: Hnich 48 Constraint Propagation •General principle •Consistency •Filtering on simple constraints •Filtering on global constraints •Conclusion 21/03/2016 A CP Tutorial: Hnich 49 Example: all-different Var: F, N, S; Val: { F { ≠ Ctrs: N ≠ S ≠ F ≠ N } ≠ N { }; } ≠ S { } 3 binary constraints, they are GAC, no pruning 21/03/2016 A CP Tutorial: Hnich 50 Example: all-different Var: F, N, S; Val: { F { ≠ } } ≠ Ctrs: N ≠ S ≠ F ≠ N •Using binary disequalities, this inconsistency goes undetected ≠ N { }; S { } 3 binary constraints, they are GAC, no pruning 21/03/2016 A CP Tutorial: Hnich 51 Example: all-different Var: F, N, S; Val: { F { ≠ N { } ≠ } Ctrs: N ≠ S ≠ F ≠ N 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 S 3 binary constraints, they are GAC, no pruning 21/03/2016 }; } 3. If n > m then no solution can possibly be found A CP Tutorial: Hnich 52 Example: all-different Var: F, N, S; Val: { F { ≠ } } ≠ S { 3 binary constraints, they are GAC, no pruning 21/03/2016 Ctrs: N ≠ S ≠ F ≠ N 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 ≠ N { }; } • ...still fooled by Dx = Dy = Dz = {a, b}, Dw = {c, d} A CP Tutorial: Hnich 53 Example: all-different Var: F, N, S; Val: { F { ≠ }; Ctrs: N ≠ S ≠ F ≠ N } F { } logically equivalent ≠ all-different N { } ≠ S { 3 binary constraints, they are GAC, no pruning 21/03/2016 S N } { } { } 1 ternary constraint, not GAC, GAC pruning empty domain no solution!! A CP Tutorial: Hnich 54 All-different: A Filtering Algorithm •Build the corresponding bipartite graph x1 1 21/03/2016 x2 2 x3 3 x4 4 x5 5 A CP Tutorial: Hnich x6 6 7 55 All-different: A Filtering Algorithm •Build the corresponding bipartite graph x1 1 x2 2 x3 3 x4 4 x5 5 x6 6 7 •∃ solution iff ∃ matching covering all the variables 21/03/2016 A CP Tutorial: Hnich 56 All-different: A Filtering Algorithm •Build the corresponding bipartite graph x1 1 x2 2 x3 3 x4 4 x5 5 x6 6 7 •∃ solution iff ∃ matching covering all the variables •filtering find alternating cycles and paths remove inconsistent values (useless edges) [X4=2] fix variables (vital edges) [X4=4] 21/03/2016 A CP Tutorial: Hnich 57 All-different: A Filtering Algorithm •Build the corresponding bipartite graph x1 1 x2 2 x3 3 x4 4 x5 5 x6 6 7 •∃ solution iff ∃ matching covering all the variables •starting from the current matching at each call makes the algorithm incremental •achieves GAC 21/03/2016 A CP Tutorial: Hnich 58 Cardinality: A Filtering Algorithm •distribute([c1, . . . , cm],[v1, . . . , vm],[x1, . . . ,xn]), ci ∈ [li, ui] 21/03/2016 A CP Tutorial: Hnich 59 Cardinality: A Filtering Algorithm •distribute([c1, . . . , cm],[v1, . . . , vm],[x1, . . . ,xn]), ci ∈ [li, ui] •transformed into a network flow problem •∃ solution iff ∃ feasible flow [0,1] v1 [l1,u1] x1 [1,1] S v2 T xi vj [lm,um] xn vm 21/03/2016 A CP Tutorial: Hnich 60 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 21/03/2016 A CP Tutorial: Hnich 61 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 8 7 2 21/03/2016 lex 9 A CP Tutorial: Hnich 62 Motivation: Symmetry Symmetry: transformation of an entity that preserves the properties of the entity Example: 180º 21/03/2016 A CP Tutorial: Hnich 63 Motivation: Symmetry • Frequently occurs Combinatorial problems like covering arrays o 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 21/03/2016 A CP Tutorial: Hnich 64 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 21/03/2016 A CP Tutorial: Hnich 65 Example: Sports Scheduling • • • • 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 21/03/2016 A CP Tutorial: Hnich 66 Example: Sport Scheduling • We need a table of meetings! Week1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Period1 0 vs 1 0 vs 2 4 vs 7 3 vs 6 3 vs 7 1 vs 5 2 vs 4 Period2 2 vs 3 1 vs 7 0 vs 3 5 vs 7 1 vs 4 0 vs 6 5 vs 6 Period3 4 vs 5 3 vs 5 1 vs 6 0 vs 4 2 vs 6 2 vs 7 0 vs 7 Period4 6 vs 7 4 vs 6 2 vs 5 1 vs 2 0 vs 5 3 vs 4 1 vs 3 21/03/2016 A CP Tutorial: Hnich 67 Example: Sport Scheduling • We need a table of meetings! Week1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Period1 0 vs 1 0 vs 2 4 vs 7 3 vs 6 3 vs 7 1 vs 5 2 vs 4 Period2 2 vs 3 1 vs 7 0 vs 3 5 vs 7 1 vs 4 0 vs 6 5 vs 6 Period3 4 vs 5 3 vs 5 1 vs 6 0 vs 4 2 vs 6 2 vs 7 0 vs 7 Period4 6 vs 7 4 vs 6 2 vs 5 1 vs 2 0 vs 5 3 vs 4 1 vs 3 • Weeks are indistinguishable • Periods are indistinguishable 21/03/2016 A CP Tutorial: Hnich 68 Example: Sport Scheduling • Weeks are indistinguishable • Periods are indistinguishable Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Period 1 0 vs 1 0 vs 2 4 vs 7 3 vs 6 3 vs 7 1 vs 5 2 vs 4 Period 2 2 vs 3 1 vs 7 0 vs 3 5 vs 7 1 vs 4 0 vs 6 5 vs 6 Period 3 4 vs 5 3 vs 5 1 vs 6 0 vs 4 2 vs 6 2 vs 7 0 vs 7 Period 4 6 vs 7 4 vs 6 2 vs 5 1 vs 2 0 vs 5 3 vs 4 1 vs 3 21/03/2016 A CP Tutorial: Hnich 69 Example: Sport Scheduling • Weeks are indistinguishable • Periods are indistinguishable Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Period 1 0 vs 1 3 vs 7 4 vs 7 3 vs 6 0 vs 2 1 vs 5 2 vs 4 Period 2 2 vs 3 1 vs 4 0 vs 3 5 vs 7 1 vs 7 0 vs 6 5 vs 6 Period 3 4 vs 5 2 vs 6 1 vs 6 0 vs 4 3 vs 5 2 vs 7 0 vs 7 Period 4 6 vs 7 0 vs 5 2 vs 5 1 vs 2 4 vs 6 3 vs 4 1 vs 3 21/03/2016 A CP Tutorial: Hnich 70 Example: Bin Packing • Consider 2 identical bins: A 21/03/2016 B A CP Tutorial: Hnich 71 Example • Consider 2 identical bins: A B • We must pack 6 items: 21/03/2016 A CP Tutorial: Hnich 1 2 3 4 5 6 72 Example • Here is one solution: 21/03/2016 5 6 3 4 1 2 A B A CP Tutorial: Hnich 73 Example • Here is another: 21/03/2016 6 5 4 3 2 1 A B A CP Tutorial: Hnich 74 Example • Is there any fundamental difference? a) b) 21/03/2016 5 3 1 A 6 4 2 B 6 4 2 A 5 3 1 B A CP Tutorial: Hnich 75 Example • Consider a matrix model: a) b) 21/03/2016 5 3 1 A 6 4 2 A 1 2 3 4 5 6 6 4 2 B 5 3 1 B 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 A CP Tutorial: Hnich B 1 0 1 0 1 0 76 Example • Consider a matrix model: a) b) 21/03/2016 5 3 1 A 6 4 2 A 6 4 2 B 5 3 1 B 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 A CP Tutorial: Hnich 77 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 b) 21/03/2016 6 4 2 A 5 3 1 B 1 2 3 4 5 6 A 0 1 0 1 0 1 A CP Tutorial: Hnich B 1 0 1 0 1 0 78 Example • Notice that items 3 and 4 are identical. b) c) 21/03/2016 6 4 2 A 6 3 2 1 2 3 4 5 6 5 3 1 B A 0 1 0 1 0 1 B 1 0 1 0 1 0 1 2 3 4 5 6 5 4 1 A 0 1 1 0 0 1 A CP Tutorial: Hnich B 1 0 0 1 1 0 79 Example • Notice that items 3 and 4 are identical. b) 6 4 2 A 1 2 3 4 5 6 5 3 1 B A 0 1 0 1 0 1 B 1 0 1 0 1 0 If we insist that col 3 lex col 4, we remove c) from the solution set. 21/03/2016 A CP Tutorial: Hnich 1 2 3 4 5 6 A 0 1 1 0 0 1 B 1 0 0 1 1 0 80 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 21/03/2016 A CP Tutorial: Hnich 81 How GACLex Works • Consider the following example. • We have two vectors of decision variables: x {2} {1,3,4} {1,2,3,4,5} y {0,1,2} {1} 21/03/2016 {0,1,2,3,4} A CP Tutorial: Hnich {1,2} {3,4,5} {0,1} {0,1,2} 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} y {0,1,2} {1} {0,1,2,3,4} {1,2} {3,4,5} {0,1} {0,1,2} • We want to enforce GAC on: x lex y. 21/03/2016 A CP Tutorial: Hnich 83 A Tale of Two Pointers • We use two pointers, α and β, to avoid repeatedly traversing the vectors. 21/03/2016 A CP Tutorial: Hnich 84 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 21/03/2016 A CP Tutorial: Hnich 85 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. 21/03/2016 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. β: most significant index from which the two vectors’ tails necessarily violate the constraint. 21/03/2016 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. • β: If tails never violate the constraint: 21/03/2016 A CP Tutorial: Hnich 88 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. 21/03/2016 A CP Tutorial: Hnich 89 Pointer Initialisation • Needs one traversal of the vectors (linear). 0 1 2 • 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 β α: 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. 21/03/2016 A CP Tutorial: Hnich 90 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. 21/03/2016 A CP Tutorial: Hnich 91 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. 21/03/2016 A CP Tutorial: Hnich 92 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. 21/03/2016 A CP Tutorial: Hnich 93 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. 21/03/2016 A CP Tutorial: Hnich 94 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. 21/03/2016 A CP Tutorial: Hnich 95 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. 21/03/2016 A CP Tutorial: Hnich 96 How GACLex Works • 0, 1 removed from yα. 0 1 2 • 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 β α: 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. 21/03/2016 A CP Tutorial: Hnich 97 How GACLex Works • 0, 1 removed from yα. 0 1 2 • 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 β α: 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. 21/03/2016 A CP Tutorial: Hnich 98 How GACLex Works • Update α. 0 1 2 • 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 β α: 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. 21/03/2016 A CP Tutorial: Hnich 99 How GACLex Works • Update α. 0 1 2 • 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 β α: 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. 21/03/2016 A CP Tutorial: Hnich 100 How GACLex Works • 3, 4 removed from xα. 0 x {2} 1 {1,3,4} y {0,1,2} {1} 2 • 4 {1,2,3,4,5} {1,2} {3,4,5} {0,1,2,3,4} {0,1} {0,1,2} α • 3 β α: 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. 21/03/2016 A CP Tutorial: Hnich 101 How GACLex Works • 3, 4 removed from xα. 0 x {2} 1 {1,3,4} y {0,1,2} {1} 2 • 4 {1,2,3,4,5} {1,2} {3,4,5} {0,1,2,3,4} {0,1} {0,1,2} α • 3 β α: 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. 21/03/2016 A CP Tutorial: Hnich 102 How GACLex Works • Update α. 0 x {2} 1 {1,3,4} y {0,1,2} {1} 2 • 4 {1,2,3,4,5} {1,2} {3,4,5} {0,1,2,3,4} {0,1} {0,1,2} α • 3 β α: 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. 21/03/2016 A CP Tutorial: Hnich 103 How GACLex Works • Update α. 0 x {2} 1 {1,3,4} y {0,1,2} {1} 2 • 4 {1,2,3,4,5} {1,2} {3,4,5} {0,1,2,3,4} {0,1} {0,1,2} α • 3 β α: 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. 21/03/2016 A CP Tutorial: Hnich 104 How GACLex Works • 4, 5 removed from xα, 0, 1 removed from yα. 0 x {2} 1 {1,3,4} y {0,1,2} {1} 2 • 4 {1,2,3,4,5} {1,2} {3,4,5} {0,1,2,3,4} {0,1} {0,1,2} α • 3 β α: 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. 21/03/2016 A CP Tutorial: Hnich 105 How GACLex Works • 4, 5 removed from xα, 0, 1 removed from yα. 0 x {2} 1 {1,3,4} y {0,1,2} {1} 2 • 4 {1,2,3,4,5} {1,2} {3,4,5} {0,1,2,3,4} {0,1} {0,1,2} α • 3 β α: 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. 21/03/2016 A CP Tutorial: Hnich 106 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) 21/03/2016 A CP Tutorial: Hnich 107 Results: BIBD Time(s) 10000 Decomposition takes About 9 times longer on each of these instances. Decomposition 1000 100 10 1 1 21/03/2016 10 100 GACLex 1000 A CP Tutorial: Hnich 10000 108 Constraint Propagation •General principle •Consistency •Filtering on simple constraints •Filtering on global constraints •Conclusion 21/03/2016 A CP Tutorial: Hnich 109 Conclusion •Constraint propagation is the glue to combine efficient filtering algorithms for common substructures Matching 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 21/03/2016 A CP Tutorial: Hnich 110 ANY QUESTIONS? Search Search •General principle •Variable selection heuristics •Value selection heuristics •Conclusion 21/03/2016 A CP Tutorial: Hnich 113 General principle … var int Solution[Nodes] in Values; solve{ forall(e in Edges) abs(Solution[e.x] - Solution[e.y]) >1; alldifferent(Solution); }; 21/03/2016 A CP Tutorial: Hnich 114 General principle … var int Solution[Nodes] in Values; solve{ … }; search { … } 21/03/2016 A CP Tutorial: Hnich 115 General principle … var int Solution[Nodes] in Values; solve{ forall(e in Edges) abs(Solution[e.x] - Solution[e.y]) >1; alldifferent(Solution); }; search { … } 21/03/2016 With a good search strategy We can quickly find good solution A CP Tutorial: Hnich 116 Searching for a good solution •For any interesting problem, propagation alone is not enough •We typically proceed by tree search 21/03/2016 A CP Tutorial: Hnich 117 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 21/03/2016 A CP Tutorial: Hnich 118 Searching for a good solution •Two main decisions to control search choose a variable choose a value from its domain 21/03/2016 A CP Tutorial: Hnich 119 Search •General principle •Variable selection heuristics •Value selection heuristics •Conclusion 21/03/2016 A CP Tutorial: Hnich 120 Variable Selection Heuristics •Has an impact on tree topology •static vs dynamic ordering •smallest-domain-first first-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 21/03/2016 A CP Tutorial: Hnich 121 Search •General principle •Variable selection heuristics •Value selection heuristics •Conclusion 21/03/2016 A CP Tutorial: Hnich 122 Value Selection Heuristics •Not as critical •Very problem-dependent •Alternative for large domains: domain splitting 21/03/2016 A CP Tutorial: Hnich 123 Search •General principle •Variable selection heuristics •Value selection heuristics •Conclusion 21/03/2016 A CP Tutorial: Hnich 124 Conclusion •A lot of control over the search strategy •Many heuristics developed, some of them generic 21/03/2016 A CP Tutorial: Hnich 125 ANY QUESTIONS? Lot-sizing under demand uncertainty Demand Uncertainty in Supply Chain Networks Inventories Production 21/03/2016 A CP Tutorial: Hnich Sales 128 Demand Uncertainty in Supply Chain Networks ? When to order? How much to order? Inventories Production 21/03/2016 A CP Tutorial: Hnich Sales 129 Demand Uncertainty in Supply Chain Networks ? When to order? How much to order? Inventories Production Demand Uncertainty 21/03/2016 A CP Tutorial: Hnich Sales 130 Demand Uncertainty in Supply Chain Networks ? When to order? How much to order? Inventories Production Demand work in collaboration Uncertainty with Bell Labs Ireland Sales 21/03/2016 A CP Tutorial: Hnich 131 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 21/03/2016 A CP Tutorial: Hnich 132 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 21/03/2016 A CP Tutorial: Hnich 133 (Demo) 21/03/2016 A CP Tutorial: Hnich 134 Experimental results Seasonal Demands 100000 10000 seconds 1000 100 ILOG - CP model 10 CPLEX - MIP model Choco - CP model dyn red 1 0.1 24 26 28 30 32 34 36 38 40 42 44 46 48 50 0.01 0.001 periods 21/03/2016 A CP Tutorial: Hnich 135 ANY QUESTIONS? 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) 21/03/2016 A CP Tutorial: Hnich 137 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,. . . 21/03/2016 A CP Tutorial: Hnich 138 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. 21/03/2016 A CP Tutorial: Hnich 139 Acknowledgements •Some parts of this tutorial are adapted material from tutorials given by: Gilles Pesant Pedro Messeguer Chris 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 21/03/2016 A CP Tutorial: Hnich 140