Lecture 22: CP Models for Sports Scheduling © J. Christopher Beck 2008 1 Outline Arc consistency and generalized arc consistency Recall the problem (again) Step 1: Two CP Models Step 2: A better CP Model © J. Christopher Beck 2008 2 Readings P Ch 10.3 © J. Christopher Beck 2008 3 Arc Consistency Fundamental notion in CP! Given: c1(v1,v2) a binary constraint e.g., v1 < v2 Given: D1 = D2 = {0, 1, …, 5} © J. Christopher Beck 2008 4 Arc Consistency c1 is arc consistent iff for all values d1 є D1 there exists a value d2 є D2 such that c1(v1=d1,v2=d2) T And similarly for all values d2 є D2 V1 < {0,1,2,3,4,5} © J. Christopher Beck 2008 V2 {0,1,2,3,4,5} 5 Generalized Arc Consistency (GAC) Given: c1(v1,..., vm) C1 is GAC iff for all variables di, for all values di є Di there exists a tuple of values [dj є Dj], j≠i such that C1(vi=di,[vj=dj]) T E.g., c1(v1,v2,v3,v4) for every value in d1 є D1 there must be a triple [d2 є D2, d3 є D3, d4 є D4] s.t. c1(v1=d1, v2=d2, v3=d3, v4=d4) T © J. Christopher Beck 2008 6 All-Diff vs. Clique of ≠ Logically, all-diff(v1, v2, …, vn) =def vi ≠ vj for 1 ≤ i < j ≤ n D1=D2=D3={1,3} Establish AC (or GAC) for v1 ≠ v2, v1 ≠ v3, v2 ≠ v3 all-diff(v1,v2,v3) © J. Christopher Beck 2008 7 Generalizing All-Diff All-Diff say that each value can appear at most once #1: each value must appear a given (specific) number of times #2: Global Cardinality Constraint (GCC): each value must appear a given (variable) number of times e.g., between 2 and 5 times © J. Christopher Beck 2008 8 The Global Cardinality Constraint gcc(card, value, base) card – array of variables value – array of values base – array of variables value[i] is taken by card[i] elements of base gcc([{0,1}, {1,3}], [0, 1], [w1, …, wn]) card © J. Christopher Beck 2008 value base 9 Question from a Previous Quiz gcc([C0, C1, C2, C3, C4], [0, 1, 2, 3, 4] [W0, W1, W2, W3, W4, W5, W6, W7, W8, W9]) The initial domain of each W variable is {0,1,2,3,4} The initial domains of the cardinality variables are: Which of the following assignments of the W variables satisfies the gcc-constraint? C0 0,2,3,4 C1 2,3 C2 1,3 C3 1 C4 0,1 W0 W1 W2 W3 W4 W5 W6 W7 W8 W9 1 4 0 1 1 0 2 2 0 1 3 2 0 0 2 2 1 3 1 2 1 1 3 0 2 1 0 3 2 0 2 0 1 4 0 0 0 2 1 1 1 4 2 2 5 3 1 1 0 0 4 1 2 2 2 Question from a Quiz gcc([C0, C1, C2, C3, C4], [0, 1, 2, 3, 4] [W0, W1, W2, W3, W4, W5, W6, W7, W8, W9]) The initial domain of each W variable is {0,1,2,3,4} The initial domains of the cardinality variables are: Which of the following assignments of the W variables satisfies the gcc-constraint? C0 0,2,3,4 C1 2,3 C2 1,3 C3 1 C4 0,1 W0 W1 W2 W3 W4 W5 W6 W7 W8 W9 1 4 0 1 1 0 2 2 0 1 3 2 0 0 2 2 1 3 1 2 1 1 3 0 2 1 0 3 2 0 2 0 1 4 0 0 0 2 1 1 1 4 2 2 5 3 1 1 0 0 4 1 2 2 2 GAC on All-Diff & GCC There exists a polynomial time algorithm to enforce GAC on all-diff and GCC otherwise if wouldn’t be very useful © J. Christopher Beck 2008 12 3-Step Approach Step 1: HAPs Find at least n HAPs HAP – string of H, A, B Find a set of n consistent HAPs Step 2: Assign games to HAPs Step 3: Assign teams to HAPs © J. Christopher Beck 2008 14 Constraint Programming Models Double RR with odd n Break is now 3 consecutive Hs or As No breaks are allowed H or B in at least 2 of first 4 rounds No team can be away in both of the final 2 games © J. Christopher Beck 2008 15 Step 1: CP Model 1 ht, at, bt – 0,1 variables denoting home, away, bye for a given team in slot t ht at bt 1 t 1,...,2n at at 1 at 2 2 t 1,...,2n 2 ht ht 1 ht 2 2 t 1,...,2n 2 a2 n 1 a2 n 1 4 a i 2 i 1 © J. Christopher Beck 2008 No breaks No team away for both of final 2 games H or B in at least 2 of first 4 slots 16 Step 1: CP Model 2 gt є {H,A,B} – variable denoting home, away, bye for a given team in slot t gcc([{0,1,2}, {0,1,2}], [H, A], [gt, gt+1, gt+2]) t = 1, …, 2n-2 (g2n ≠ A) OR (g2n-1 ≠ A) No breaks No team away for both of final 2 games gcc([{0,1,2}], [A], [g1, g2, g3 , g4]) H or B in at least 2 of first 4 slots © J. Christopher Beck 2008 17 Step 1: CP Model 2 Some additional constraints gcc([{n-1}, {n-1}, {2}], [H, A, B], [g1, …, g2n]) 2 Bs, Equal # As & Hs Let t1, … tn, be the HAP for one slot (across all teams) gcc([{(n-1)/2}, {(n-1)/2}, {1}], [H, A, B], [t1, …, tn]) 1 Bs, Equal # As & Hs © J. Christopher Beck 2008 18 Step 1: CP Model With either of these models, you can then easily generate all possible HAPs You could also add a cost function to generate only “good” HAPs © J. Christopher Beck 2008 19 Step 2: Assign Games Given a set of consistent HAPs Devise a CP model to assign games The one in the book is overly complicated! slot 1 2 3 4 5 6 7 8 9 10 Team a H A H A B H A H A B Team b H B A H A H B A H A Team c A A B H H A A B H H Team d B H H A A B H H A A Team e A H A B H A H A B H © J. Christopher Beck 2008 20 Step 2: CP Model Let P be a n * 2n matrix of patterns P(i,j) = {H,A,B} – The team assigned to pattern i plays P(i,j) in time slot j This is given as input from Step 1! Let G be a n * 2n matrix Each element of G has initial domain [0..n] G(i,j) is the “team” that team i will play in time slot j Really the pattern that © J. Christopher Beck 2008 pattern i will play in slot j 22 P(i,j) & G(i,j) P G H A H A B A H A H B H B A H A A B H A H A A B H H H H B A A B H H A A B A A H H A H A B H H A H B A 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 © J. Christopher Beck 2008 23 Preprocess P(i,j) & G(i,j) P G H A H A B A H A H B H B A H A A B H A H A A B H H H H B A A B H H A A B A A H H A H A B H H A H B A 3,5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 3,5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 1,2 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 1,2 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 © J. Christopher Beck 2008 24 Preprocess P(i,j) & G(i,j) P G H A H A B A H A H B H B A H A A B H A H A A B H H H H B A A B H H A A B A A H H A H A B H H A H B A 3,5 4,5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 3,5 0 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 1,2 4,5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0 1,3 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 1,2 1,3 0..5 0..5 0..5 0..5 0..5 0..5 0..5 0..5 © J. Christopher Beck 2008 25 Preprocess P(i,j) & G(i,j) P G H A H A B A H A H B H B A H A A B H A H A A B H H H H B A A B H H A A B A A H H A H A B H H A H B A 3,5 4,5 2,5 2,3 0 3,5 4,5 2,5 2,3 0 3,5 0 1,4 1,4 3,5 3,5 0 1,4 1,4 3,5 1,2 4,5 0 1,4 2,4 1,2 4,5 0 1,4 2,4 0 1,3 2,5 2,3 3,5 0 1,3 2,5 2,3 3,5 1,2 1,3 1,4 0 2,4 1,2 1,3 1,4 0 2,4 © J. Christopher Beck 2008 26 Constraints (G(i,j) = k) ↔ (G(k,j) = i) G for all i,k s.t. P(i,j) ≠ B, P(k,j) ≠ B for all j 3,5 4,5 2,5 2,3 0 3,5 4,5 2,5 2,3 0 3,5 0 1,4 1,4 3,5 3,5 0 1,4 1,4 3,5 1,2 4,5 0 1,4 2,4 1,2 4,5 0 1,4 2,4 0 1,3 2,5 2,3 3,5 0 1,3 2,5 2,3 3,5 1,2 1,3 1,4 0 2,4 1,2 1,3 1,4 0 2,4 © J. Christopher Beck 2008 27 Constraints P all-diff(G(1,1), G(1,3), G(1,7), G(1,9)) all-diff(G(1,2), G(1,4), G(1,6), G(1,8)) H A H A B A H A H B H B A H A A B H A H A A B H H H H B A A B H H A A B A A H H A H A B H H A H B A © J. Christopher Beck 2008 28 All-Diff for Row 2? P H A H A B A H A H B H B A H A A B H A H A A B H H H H B A A B H H A A B A A H H A H A B H H A H B A © J. Christopher Beck 2008 29