Global Constraints JeanCharles REGIN ILOG Sophia Antipolis reginilog
Global Constraints Jean-Charles REGIN ILOG, Sophia Antipolis regin@ilog. fr 1
Plan • General Principles of Constraint Programming • Categorization of Global Constraints • What is a good filtering algorithm? • Global constraints and over constrained problems • Conclusion 2
Constraint Programming • 3 notions: - constraint network: variables, domains constraints + filtering (domain reduction) - propagation - search procedure (assignments + backtrack) 3
Problem = conjunction of sub-problems • In CP a problem can be viewed as a conjunction of sub-problems that we are able to solve • A sub-problem can be trivial: x < y or complex: search for a feasible flow • A sub-problem = a constraint 4
Filtering • We are able to solve a sub-problem: a method is available • CP uses this method to remove values from domain that do not belong to a solution of this sub-problem: filtering • E. g: x < y and D(x)=[10, 20], D(y)=[5, 15] => D(x)=[10, 14], D(y)=[11, 15] 5
Filtering • A filtering algorithm is associated with each constraint (sub-problem). • Can be simple (x < y) or complex (alldiff) 6
Arc consistency • All the values which do not belong to any solution of the constraint are deleted. • Example: Alldiff({x, y, z}) with D(x)=D(y)={0, 1}, D(z)={0, 1, 2} the two variables x and y take the values 0 and 1, thus z cannot take these values. FA by AC => 0 and 1 are removed from D(z) 7
Propagation • Domain Reduction due to one constraint can lead to new domain reduction of other variables • When a domain is modified all the constraints involving this variable are studied and so on. . . 8
Why Propagation? • A problem = conjunction of easy sub-problems. • Sub-problems: local point of view. Propagation tries to obtain a global point of view from independent local point of view • The conjunction is stronger that the union of independent resolutions 9
Why Propagation? • A problem = conjunction of easy sub-problems. • Sub-problems: local point of view. Propagation tries to obtain a global point of view from independent local point of view • The conjunction is stronger that the union of independent resolution • To help the propagation to have a global point of view: use global constraints ! 10
Constraints • Primitives • Global constraints 11
Primitives • No decomposable constraints: x<y x y x+y=z x =3, x =y atmost(xarray, #time, val), atleast etc. . . 12
Global Cardinality Constraint • GCC(X, {li}, {ui}) • Defined on a set X of variables, the number of times each value vi can be taken must be in a given interval [li, ui] • Example: D(x 1)={a, b, c, d}, D(x 2)={a, b, c, d}, D(x 3)={b, c}, D(x 4)={c, d}. Values b and c must be taken at most 2, values a and d must be taken at least 1. 13
Global constraints • 3 categories: - pseudo-primitives - grouping of constraints based on variables - specific nonbinary constraints (complex primitive ? ) 14
Pseudoprimitives • Can be represented by a conjunction of primitives • The definition is “closed” • x + y = z *t: x + y = a, b=z*t, a=b Sum. Eq. Prod(x, y, z, t): X(C) cannot change • Currently, only a few are predefined 15
Grouping of constraints based on variables • The semantics of the constraint is defined from primitive constraints which are ensured on a set of variables. X(C) can be changed • Ex: alldiff(X) : primitive , vars: X global cardinality constraint: primitives: atmost, atleast sum: primitive exp 1 + exp 2 = exp 3 • Currently a lot are predefined 16
Specific constraints • The meaning is global, and cannot be represented by primitives constraints. X(C) can be changed • Ex: path constraint: defined from a graph, a source and a sink. The notion of path is global (connectivity) 17
Global constraints • x+y=z*t • alldiff(X) • path(G) 18
Global constraints • 2 interests: - definition of the problem - strength of CP thanks to Filtering Algorithm 19
Filtering Algorithm • based on constraints addition (from the simultaneous presence of constraints, new constraints are added) • general (GAC-Schema) • ad-hoc (integration of OR algorithm) 20
Constraints addition • 5 variables: X={x 1, x 2, x 3, x 4, x 5} • domains: [0. . 4] • constraints: atleast(X, 1, 1) , atleast(X, 1, 2) atleast(X, 1, 3), atleast(X, 1, 4) • What will happen ? 21
Constraints addition • 5 variables: X={x 1, x 2, x 3, x 4, x 5} • domains: [0. . 4] • constraints: atleast(X, 1, 1) , atleast(X, 1, 2) atleast(X, 1, 3), atleast(X, 1, 4) • atleast(X, 1, val): local view: while val belongs to 1+1=2 domains of variable, nothing can be deduced 22
Constraints addition • 5 variables: X={x 1, x 2, x 3, x 4, x 5} • domains: [0. . 4] • constraints: atleast(X, 1, 1) , atleast(X, 1, 2) atleast(X, 1, 3), atleast(X, 1, 4) • atleast(X, 1, val): local view: while val belongs to 1+1=2 domains of variable, nothing can be deduced • x 1=0, x 2=0, x 3=0 is ok, any combination of 3 variables containing 2 times val 0 is ok. This 23 is stupid
Constraints addition • 5 variables: X={x 1, x 2, x 3, x 4, x 5} • domains: [0. . 4] • constraints: atleast(X, 1, 1) , atleast(X, 1, 2) atleast(X, 1, 3), atleast(X, 1, 4) • From the simultaneous presence of constraints we can deduce other constraints: if (4 values must be taken atleast 1) then the other values can be taken at most n-4=5 -4=1 New constraint: atmost(X, 1, 0) 24
Constraints addition • Done for the global cardinality constraint (the unconstrained values becomes constrained) • Therefore, done for the alldiff constraint and permutation (each value has to be taken exactly once) 25
Constraints addition • • There is no new filtering algorithm Only implicit constraints are added The previous problem is solved! Easy and really interesting: a kind of presolve. 26
Filtering Algorithm • based on constraints addition (from the simultaneous presence of constraints, new constraints are added) • general (GAC-Schema) • ad-hoc (integration of OR algorithm) 27
GAC-Schema • A generic framework for achieving AC for any kind of constraint (can be non binary). Bessiere and Regin, IJCAI’ 97 • You just have to say how to compute a solution. • Manages the incrementality for you (notion of support). 28
GAC-Schema: instantiation • • • List of allowed tuples List of forbidden tuples Predicates Any OR algorithm Solver reentrace 29
GAC-Schema • Idea: tuple = solution of the constraint support = valid tuple - while the tuple is valid: do nothing - if the tuple is no longer valid, then search for a new support for the values it contains • a solution (support) can be computed by any OR algorithm 30
Example • X(C)={x 1, x 2, x 3} D(xi)={a, b} • T(C)={(a, a, a), (a, b, b), (b, b, a), (b, b, b)} 31
Example • X(C)={x 1, x 2, x 3} D(xi)={a, b} • T(C)={(a, a, a), (a, b, b), (b, b, a), (b, b, b)} • Support for (x 1, a): (a, a, a) is computed and (a, a, a) is added to S(x 2, a) and S(x 3, a), (x 1, a) in (a, a, a) is marked as supported. 32
Example • X(C)={x 1, x 2, x 3} D(xi)={a, b} • T(C)={(a, a, a), (a, b, b), (b, b, a), (b, b, b)} • Support for (x 1, a): (a, a, a) is computed and (a, a, a) is added to S(x 2, a) and S(x 3, a), (x 1, a) in (a, a, a) is marked as supported. • Support for (x 2, a): (a, a, a) is in S(x 2, a) it is valid, therefore it is a support. (Multidirectionnality). No need to compute a solution 33
Example • X(C)={x 1, x 2, x 3} D(xi)={a, b} • T(C)={(a, a, a), (a, b, b), (b, b, a), (b, b, b)} • Support for (x 1, a): (a, a, a) is computed and (a, a, a) is added to S(x 2, a) and S(x 3, a), (x 1, a) in (a, a, a) is marked as supported. • Value a is removed from x 2, then all the tuple in S(x 2, a) are no longer valid: (a, a, a) for instance. The validity of the values supported by this tuple must be reconsidered. 34
Example • X(C)={x 1, x 2, x 3} D(xi)={a, b} • T(C)={(a, a, a), (a, b, b), (b, b, a), (b, b, b)} • Support for (x 1, a): (a, a, a) is computed and (a, a, a) is added to S(x 2, a) and S(x 3, a), (x 1, a) in (a, a, a) is marked as supported. • Support for (x 1, b): (b, b, a) is computed, and update. . . 35
GAC-Schema: complexity • CC complexity to check consistency (seek in table, call to OR algorithm): seek for a Support costs CC • n variables, d values: for each value: CC for all values: O(nd. CC) • For any OR algorithm which is able to compute a solution, Arc consistency can be achieved in O(nd. CC). 36
GAC-Schema: complexity • After 1 modification: consistency in O(CC)) arc consistency in O(nd. CC) • After k modifications consistency in O(CC) arc consistency in O(nd. CC) 37
Table Constraint: An example • Configuration problem: 5 types of components: {glass, plastic, steel, wood, copper} 3 types of bins: {red, blue, green} whose capacity is red 5, blue 5, green 6 Constraints: - red can contain glass, cooper, wood - blue can contain glass, steel, cooper - green can contain plastic, copper, wood - wood require plastic; glass exclusive copper - red contains at most 1 of wood - green contains at most 2 of wood For all the bins there is either no plastic or at least 2 plastic Given an initial supply of 12 of glass, 10 of plastic, 8 of steel, 12 of wood and 8 of copper; what is the minimum total number of bins? 38
Table Constraint: results #bk time standard model 1, 361, 709 430 Table Constraint 12, 659 9. 7 39
Filtering Algorithm • based on constraints addition (from the simultaneous presence of constraints, new constraints are added) • general (GAC-Schema) • ad-hoc (integration of OR algorithm) 40
Semantics of a constraint • Speed-up the search for a support (solution which contain a value (x, a)) 41
Semantics of a constraint • Speed-up the search for a support (solution which contain a value (x, a)): x < y, D(x)=[0. . 10000], D(y)=[0. . 10000] support for (x, 9000): immediate any value greater than 9000 in D(y) 42
Semantics of a constraint • Design of ad-hoc filtering algorithm: x<y: (a) max(x) = max(y) -1 (b) min(y) = min(x) +1 43
Semantics of a constraint • Design of ad-hoc filtering algorithm: x<y: (a) max(x) = max(y) -1 (b) min(y) = min(x) +1 • Triggering of the filtering algorithm: no possible pruning of D(x) while max(y) is not modified no possible pruning of D(y) while min(x) is not modified 44
Semantics of a constraint • Speed-up the search for a support (solution which contain a value (x, a)) • Design of specific algorithm • Incrementality 45
Alldiff constraint The value graph: 1 x 1 D(x 1)={1, 2} D(x 2)={2, 3} x 2 D(x 3)={1, 3} D(x 4)={3, 4} x 3 D(x 5)={2, 4, 5, 6} D(x 6)={5, 6, 7} x 4 2 3 4 5 x 5 6 x 6 7 46
Alldiff constraint Default orientation 1 x 1 2 x 2 3 x 3 4 s x 4 5 x 5 6 x 6 7 47
Alldiff constraint Default orientation 1 x 1 2 x 2 3 t x 3 4 s x 4 5 x 5 6 x 6 7 48
Alldiff constraint Default orientation 1 x 1 2 x 2 3 t x 3 4 s x 4 5 x 5 6 x 6 7 49
Value network Default orientation (1, 1) (0, 1) 1 (0, 1) x 1 2 x 2 3 x 3 t 4 s x 4 5 x 5 6 x 6 (6, 6) 7 50
A feasible flow Default orientation (1, 1) (0, 1) 1 (0, 1) x 1 2 x 2 3 x 3 t 4 s x 4 5 x 5 6 x 6 (6, 6) 7 51
Residual graph orientation 1 x 1 2 x 2 3 x 3 4 s x 4 5 x 5 6 x 6 7 52
Residual graph orientation 1 x 1 2 x 2 3 x 3 4 s x 4 5 x 5 6 x 6 7 53
Arc consistency The value graph: D(x 1)={1, 2} D(x 2)={2, 3} D(x 3)={1, 3} D(x 4)={4} D(x 5)={5, 6} D(x 6)={5, 6, 7} 1 x 1 2 x 2 3 x 3 4 x 4 5 x 5 6 x 6 7 54
Alldiff results • Color the graph with cliques: • c 0 = {0, 1, 2, 3, 4} c 1 = {0, 5, 6, 7, 8} c 2 = {1, 5, 9, 10, 11} c 3 = {2, 6, 9, 12, 13} c 4 = {3, 7, 10, 12, 14} c 5 = {4, 8, 11, 13, 14} clique size: 27 Global: #fails: 0 cpu time: 1. 212 s Local: #fails: 1 cpu time: 0. 171 s clique size: 31 Global: #fails: 4 cpu time: 2. 263 s Local: #fails: 65 cpu time: 0. 37 s clique size: 51 Global: #fails: 501 cpu time: 25. 947 s Local: #fails: 24512 cpu time: 66. 485 s clique size: 61 Global: #fails: 5 cpu time: 58. 223 s Local: ? ? ? ? 55
Alldiff constraint • Compute a feasible flow • Compute the strongly connected components • Remove every arc of flow value 0 for which the ends belong to two different components • Linear algorithm achieving arc consistency • Idem for global cardinality constraints • work well due to (0, 1) arcs 56
Alldiff constraint: complexity • After 1 modification: - consistency computed in O(nd) - arc consistency computed in O(nd) • After k modifications: - consistency in O(nd k) - arc consistency in O(nd k + nd) 57
Filtering algorithm • x + y = z*t: conjunction of primitive constraints ad-hoc constraints addition 58
Filtering algorithm • Grouping of constraints based on variables: - constraint addition recommended - ad-hoc algorithm 59
Filtering algorithm • Specific non-binary constraint: -ad-hoc algorithm 60
Good Filtering Algorithm • What is a good filtering algorithm ? Or what is a poor one ? (The FA is considered and not the idea of using an OR algorithm) • GAC-Schema: - After 1 modification: consistency in O(CC)) arc consistency in O(nd. CC) - After k modifications consistency in O(CC) arc consistency in O(nd. CC) 61
Good or Poor ? • If O(CC) for consistency then arc consistency: - O(nd. CC) poor (reached by GAC-Schema) 62
Good or Poor ? • If O(CC) for consistency then arc consistency: - O(nd. CC) poor (reached by GAC-Schema) - O(CC) good! 63
Good or Poor ? • If O(CC) for consistency then arc consistency: - O(nd. CC) poor (reached by GAC-Schema) - O(CC) good! - and between ? Not so bad. . . 64
Good FA • Alldiff, • global cardinality constraint 65
Medium FA • Algorithm such that arc consistency is in: O(n. CC) (a factor of d is gained): - Global cardinality with cost - Symmetric alldiff (alldiff U {xi =j <=> xj =i}) - sum with binary inequalities - edge-finder 66
Poor FA ? • I hope there is none : -) • Be careful with algorithms that successively try all values of variables (or ranges) 67
Perfect FA ? • Idea: FA has no cost. • Complexity of the FA is always the same as the complexity of the consistency checking algorithm • All the possible cases are considered and not only the worst case • Another possibility O(1) per deletion. • Alldiff: 1 modification: if the deleted arc does not belong to the current maximum matching, then consistency in O(1), AC in O(nd): not perfect • No perfect FA is known 68
Incremental algorithms • An incremental approach is not always the best. (cf IJCAI-2001 paper on AC-2001) • The consequence of the deletions is a good approach if the number of modifications is less than the number of remaining things. Otherwise it is not good. • The incremental aspect is quite important for a FA 69
AC-2001 vs AC-6 Last=a Last=b Last=c Last=e i j a b c d e Sja={a, b} Sjb={c} Sjc={d} Sjd={} Sje={e} If Δ(j)={a, b, c} and Δ(i)={a, b, c} then: recomputation from scratch: 4 operations study of the consequences of the deletions: 6 operations 70
Adaptive Algorithm: Adaptive AC • • A=Σ v in Δ(j) (|Svj| + 1) B=|D(i)| If A < B then run AC-6 If B < A then run AC-2001 If 2|Δ(j)| < |D(i)| then run AC-6 else run AC-2001 71
Closure or not ? • Is the FA closed w. r. t a property? • Consider the values deleted by the FA. The consequence of these new deletions can be: 1) taken into account by the same pass of the FA (alldiff) 2) ignored by the same pass of the FA (Table) • 1) no need to call again the FA 2) need to call again the FA • It is a choice. 72
Amortized Complexity • It is possible to define the complexity in regards to the number of deletions (ex: O(CC) per deletion) • Symmetric alldiff: AC: from every var, run algorithm A. Algorithm A can remove some values. AC Complexity: n. O(A). Pb systematic. • Other FA: pick one variable, run A, and set k=#deletions. You gain k runs! Complexity O(A) per deletion 73
Incomplete algorithms • The constraint is an NP-Hard problem • Try to characterize what is done • useful in practice but sometimes difficult to handle: - no fixpoint (largest clique depends on the way the graph is defined): - less constraints can lead to more pruning - debug is difficult • global sequencing constraint (fixpoint) 74
Global Constraint and overconstrained problems • Real world over-constrained problems: - General purpose method gives poor results after more than 10 years of research RLFAP are not solved, best results obtained from RDS which is a B&B variation. Unable to detect an inconsistency in: x < y, y < z, z < x - WHY ? 75
Global Constraint and overconstrained problems • Filtering algorithms associated with constraints are needed to solve some problems • Relaxation of global constraints is needed • Specific filtering algorithms are needed 76
Global Constraint and overconstrained problems • Filtering algorithms associated with constraints are needed to solve some problems • Relaxation of global constraints is needed • Specific filtering algorithms are needed • That is: successful ideas for satisfaction problems are still valid for overconstrained problems (cf MAC vs FC CP’ 96) 77
Over-constrained: Cost of violations • The enumeration of all elements of the Cartesian Product with an associated cost is not realistic. In practice, the cost has always a meaning: x < y: if violated cost=x-y • Use this semantic and exploit it: define (x < y AND cost=0) OR (cost = x -y) • Define the cost as a variable • Domain reduction from cost to var 78
Soft Global Constraints • Two different general costs: - Variables based violation cost: the cost is equal to the number of variables that should change their value in order to satisfy the constraint - Primal Graph Based Violation Cost: the cost is equal to the number of binary inequalities violated in the CSP corresponding to the primal graph of C. 79
Variable based violation cost • Alldiff({x 1, x 2, x 3, x 4, x 5}) (a, a, a, b, b) cost = 3 (a, a, b) cost = 3 • General definition can be applied to all constraints that are not pseudoprimitive 80
Primal graph based partition cost • Alldiff({x 1, x 2, x 3, x 4, x 5}) (a, a, a, b, b) cost = triangle(a, a, a) + pair (b, b) = 3 + 2 =5 (a, a, b) cost = quadrangle (a, a, a, a) =6 • Can be used for the constraint defined by grouping primitive constraints 81
Soft global constraints • For the alldiff constraint specific algorithm have been designed. These FA are able to take into account a cost variable w. r. t. the defined cost (see paper in CP’ 01) 82
Over-constrained problems • MAX-CSP is a global constraint! One satisfiability variable (bi) per constraint A sum constraint on this bi defined a MAXCSP problem. 83
Some results • Car sequencing • Sports scheduling • All-interval series 84
Car sequencing • opt 0 1 2 3 4 cap 1/2 2/3 1/3 2/5 1/5 #cars 0 X X X 1 configurations 1 2 3 X X 4 X 5 X X 1 X X 2 2 85
Car sequencing • opt 0 1 2 3 4 cap 1/2 2/3 1/3 2/5 1/5 #cars • Sequences 0 X X X configurations 1 2 3 X X 4 X 5 X X X 1 1 2 2 2 4, 4 or 4, 5 or 0, 4 or 0, 5 are forbbiden 2 86
Car sequencing • opt 0 1 2 3 4 cap 1/2 2/3 1/3 2/5 1/5 #cars • Sequences 0 X X X configurations 1 2 3 X X 4 X 5 X X X 1 1 2 2 2, 2, 1 or 2, 3, 0 are allowed 2, 2, 3 or 5, 3, 2 are fobbiden 2 2 87
Car sequencing • Instances provided by Barbara Smith: 100 cars, 25 configurations, 5 options • We proved (in 1997) that: one instance has no solution in 3. 5 s one instance has no solution in 422 s. As far as we know, this is currently the only one method which is able to obtain this result • We solve some other open problems 88
Sports scheduling • n teams and n-1 weeks and n/2 periods • every two teams play each other exactly once • every team plays one game in each week • no team plays more than twice in the same period 89
Sports scheduling 90
Conclusion • Filtering algorithm are one of the main strength of CP. Define your model by using them • If you write an FA: - try to write a good or a medium one. Do not forget that GAC-Schema exists - take care of the semantics of the constraint and especially the triggering of the FA 91
Conclusion • Incremental algorithms are not always the best. • General filtering algorithms are efficient in lack of other algorithms, when some predefined FA exist, use them. • Over-constrained problems: use the semantics of constraints 92
References • • • Alldiff constraint: J-C. Regin, AAAI-94 Global Cardinality constraint: J-C. Regin, AAAI-96 GAC-Schema: C. Bessiere and J-C. Regin, IJCAI-97 GAC-Schema with computation on the fly: C. Bessiere and J-C Regin, CP’ 99 Sequence: J-C. Regin and J-F. Puget, CP’ 97 Sum with binary inequalities: J-C Regin and M. Rueher, CP’ 00 Gcc with costs: J-C Regin, CP’ 99 Symmetric alldiff: J-C Regin, IJCAI-99 Soft global constraints: T. Petit, J-C Regin, C. Bessiere, CP’ 01 93
- Slides: 93