350 likes | 366 Views
Modelling for Constraint Programming. Barbara Smith b.m.smith@leeds.ac.uk. Context. I will assume A well-defined problem that can be represented as a finite domain constraint satisfaction or optimization problem no uncertainty, preferences, etc. A constraint solver providing:
E N D
Modelling for Constraint Programming Barbara Smith b.m.smith@leeds.ac.uk CP 2010 Doctoral Programme
Context CP 2010 Doctoral Programme • I will assume • A well-defined problem that can be represented as a finite domain constraint satisfaction or optimization problem • no uncertainty, preferences, etc. • A constraint solver providing: • a systematic search algorithm • combined with constraint propagation • a set of pre-defined constraints • I am not going to discuss • Automated modelling • Choice of search strategy • Global constraints • etc. • There won’t be enough examples!
Solving CSPs CP 2010 Doctoral Programme • Systematic search: given a CSP M = <X,D,C> • choose a variable xi that is not yet assigned • create a choice point, i.e. a set of mutually exclusive & exhaustive choices, e.g. xi= av.xi≠ a • try the first & backtrack to try the other if this fails • Constraint propagation: • add xi= a or xi≠ a to the set of constraints • re-establish local consistency on each constraint • remove values from the domains of future variables that can no longer be used because of this choice • fail if any future variable has no values left
Representing a Problem CP 2010 Doctoral Programme • If a CSP M = <X,D,C> represents a problem P, thenevery solution of M corresponds to a solution of P and every solution of P can be derived from at least one solution of M • More than one solution of M can represent the same solution of P, if modelling introduces symmetry • The variables and values of M represent entities in P • The constraints of M ensure the correspondence between solutions of M and solutions of P • The aim is (usually) to find a model M that can be solved as quickly as possible • NB shortest run-time might not mean least search
Interactions with Search Strategy CP 2010 Doctoral Programme • Whether M1 is better than M2 can depend on the search algorithm and search strategy • I will assume the search algorithm is fixed and choice points are always xi= a v. xi≠ a • But the search strategy is still important • choice of search variables • variable and value ordering heuristics • E.g. Stable marriage, Graceful labelling of a graph • Is designing the search strategy part of modelling? • I think it is, in practice • but I will not discuss it in this talk
Viewpoints CP 2010 Doctoral Programme • A viewpoint is a pair <X,D>, i.e. a set of variables and their domains • Given a viewpoint, the constraints have to restrict the solutions of M to solutions of P • So the constraints are (largely) decided by the viewpoint • Different viewpoints give very different models • We can combine viewpoints - more later • Good rule of thumb: choose a viewpoint that allows the constraints to be expressed easily and concisely • will propagate well, so problem can be solved efficiently
Example: Magic Square • Arrange the numbers 1 to 9 in a 3 x 3 square so that each row, column and diagonal has the same sum • V1 : a variable for each cell, domain is the numbers that can go in the cell • V2 : a variable for each number, domain is the cells where that number can go • Constraints on row, column & diagonal sums are easy to express in V1: • x1+x2+x3 = x4+x5+x6 = x1+x4+x7 = … • but not in V2 CP 2010 Doctoral Programme
Constraints CP 2010 Doctoral Programme • Given a viewpoint, the role of the constraints is: • To ensure that the solutions of the CSP match the solutions of the problem • To guide the search, i.e. to ensure that as far as possible, partial solutions that will not lead to a solution fail immediately
Expressing the Constraints CP 2010 Doctoral Programme • For efficient solving, we need to know: • the constraints provided by the constraint solver • the level of consistency enforced on each • the complexity of the constraint propagation algorithms • Not very declarative! • There is often a trade-off between time spent on propagation and time saved on search • which choice is best often depends on the problem • E.g. sometimes it is not worthwhile to enforce GAC on an allDifferent constraint • If the constraint is loose, i.e. there are many more values than variables
Auxiliary Variables CP 2010 Doctoral Programme • Often, the constraints can be expressed more easily/propagate better if more variables are introduced • E.g. in the magic square problem, introduce a variable s representing the sum of the rows & columns • x1+x2+x3 = x4+x5+x6 becomes x1+x2+x3 = s, x4+x5+x6 = s • Better still, add s = 15 (in the 3 × 3 case)
Auxiliary Variables (II) • 10 cars to be made on a production line, each requires some options • Stations installing options have lower capacity than rest of line e.g. at most 1 car out of 2 for option 1 • Find a feasible production sequence CP 2010 Doctoral Programme Example: car sequencing (Dincbas, Simonis and van Hentenryck, ECAI 1988)
Car Sequencing - Model CP 2010 Doctoral Programme • Variables : s1 , s2 , …, s10 • Value of siis the class of car in position i in the sequence • Constraints: • Each class occurs the correct number of times • Option capacities are respected - ? • Introduce variables oij : • oij= 1 iff the car in the ith slot in the sequence requires option j • Option 1 capacity is one car in every two: • oi,1 + oi+1,1≤ 1 for 1 ≤i < 10 • Relate the auxiliary variables to the sivariables: • λjk = 1 if car class k requires option j • oij= λjsi, 1 ≤i ≤ 10, 1 ≤ j≤ 5
One Constraint is Better than Several (maybe) CP 2010 Doctoral Programme • If there are several constraints all with the same scope, rewriting them as a single constraint will lead to more propagation… • if the same level of consistency is maintained on the new constraint • … more propagation means shorter run-time • if enforcing consistency on the new constraint can be done efficiently • NB conjoining all the constraints in the problem into a single constraint is not usually a good idea!
Example: n-queens • A variable for each row, x1 , x2 , …, xn • Values represent the columns, 1 to n • The assignment (xi,c) means that the queen in row i is in column c • Constraints for each pair of rows i, j: • xi ≠xi • xi − xi≠ i − j • xi − xi≠ j − i CP 2010 Doctoral Programme
Propagating the Constraints • A queen in row 5, column 3 conflicts with both remaining values for x3 • But the constraints are consistent • xi ≠xi thinks that (x3 ,1) can support (x5 ,3) • xi − xi≠ i − j thinks that (x3 ,3) can support (x5 ,3) × • Enforcing AC on the conjunction (xi ≠xi ) ٨ (xi − xi≠ i − j ) ٨ (xi − xi≠ j − i ) would remove 3 from the domain of x5 • If you can enforce GAC on the constraint CP 2010 Doctoral Programme
Implied Constraints • Implied constraints are logical consequences of the set of existing constraints • So are logically redundant (sometimes called redundant constraints) • They do not change the set of solutions • Adding implied constraints can reduce the search effort and run-time CP 2010 Doctoral Programme
Example: Car Sequencing • Existing constraints only say that the option capacities cannot be exceeded • Suppose there are 30 cars and 12 require option 1 (capacity 1/2) • At least one car in slots 1 to 8 of the production sequence must require option 1; otherwise 12 of cars 9 to 30 will require option 1, i.e. too many • Cars 1 to 10 must include at least two option 1 cars, ... , and cars 1 to 28 must include at least 11 option 1 cars • These are implied constraints CP 2010 Doctoral Programme
Useful Implied Constraints • An implied constraint reduces search if: • at some point during search, a partial assignment will fail because of the implied constraint • without the implied constraint, the search would continue • the partial assignment cannot lead to a solution • the implied constraint forbids it, but does not change the set of solutions • In car sequencing, partial assignments with option 1 under-used could be explored during search, without the implied constraints CP 2010 Doctoral Programme
Useless Implied Constraints • The assignments forbidden by an implied constraint may never actually arise • depends on the search order • e.g. in car sequencing, • at least one of cars 1 to 8 must require option 1 • any 8 consecutive cars must have one option 1 car • but if the sequence is built up from slot 1, only the implied constraints on slots 1 to k can cause the search to backtrack • If we find a class of implied constraints, maybe only some are useful • adding a lot of constraints that don’t reduce search will increase the run-time CP 2010 Doctoral Programme
Implied Constraints v. Global Constraints • Régin and Puget (CP97) developed a global constraint for sequence problems, including the car sequencing problem • “our filtering algorithm subsumes all the implied constraints” used by Dincbas et al. • Implied constraints may only be useful because a suitable global constraint does not (yet) exist • But many implied constraints are simple and quick to propagate • Use a global constraint if there is one available and it is cost-effective • but look for useful implied constraints as well CP 2010 Doctoral Programme
Symmetry in CSPs • A symmetry transforms any solution into another • Sometimes symmetry is inherent in the problem (e.g. chessboard symmetry in n-queens) • Sometimes it’s introduced in modelling • Symmetry causes wasted search effort: after exploring choices that don’t lead to a solution, symmetrically equivalent choices may be explored CP 2010 Doctoral Programme
Symmetry between Values: Car sequencing • A natural model has individual cars as the values • introduces symmetry between cars requiring the same option • The model instead has classes of car • needs constraints to ensure the right number of cars in each class • Using set variables is a similar and common technique to remove symmetry CP 2010 Doctoral Programme
Symmetry Breaking • Often, not all the symmetry can be eliminated by remodelling • Remaining symmetry should be reduced or eliminated: • dynamic symmetry breaking methods (SBDS, SBDD, etc.) • symmetry-breaking constraints • unlike implied constraints, they change the set of solutions • can lead to further implied constraints CP 2010 Doctoral Programme
Changing Viewpoint • We can improve a CSP model of a problem • express the constraints better • break the symmetry • add implied constraints • But sometimes it’s better just to use a different model • i.e. a different viewpoint CP 2010 Doctoral Programme
Different Viewpoints • Reformulate in a standard way, e.g. • non-binary to binary translations • dual viewpoint for permutation problems • Boolean to integer or set viewpoints • Reformulations are being developed for automated modelling • Or find a new viewpoint by viewing the problem from a different angle • the constraints may express different insights into the problem CP 2010 Doctoral Programme
Permutation Problems • A CSP is a permutation problem if: • it has the same number of values as variables • all variables have the same domain • each variable must be assigned a different value • Any solution assigns a permutation of the values to the variables • Other constraints determine which permutations are solutions • There is a dual viewpoint in which the variables and values are swapped CP 2010 Doctoral Programme
Example: n-queens • Standard model • a variable for each row, x1 , x2 , …, xn • values represent the columns, 1 to n • xi =j means that the queen in row i is in column j • n variables, n values,allDifferent(x1 , x2 , …, xn) • Dual viewpoint • a variable for each column, d1 , d2 , …, dn ; values represent the rows • In this problem, both viewpoints give the same CSP CP 2010 Doctoral Programme
Example: Magic Square • First viewpoint: • variables x1 , x2 , …, x9 • values represent the numbers 1 to 9 • The assignment (xi ,j) means that the number in square i is j • Dual viewpoint • a variable for each number, d1 , d2 , …, d9 • values represent the squares • Constraints are much easier to express in the first viewpoint (& there are far fewer of them) CP 2010 Doctoral Programme
Different Perspectives: Example • Constraint Modelling Challenge, IJCAI 05 • “Minimizing the maximum number of open stacks” • There were almost as many models of the problem as participants in the Challenge CP 2010 Doctoral Programme
Which Viewpoint to Choose? • Sometimes one viewpoint is clearly better, e.g. if we can’t express the constraints easily in one • But different perspectives often allow different expression of the constraints and different implied constraints • can be hard to decide which is better • We don’t need to choose one viewpoint – we can use two (or more) at once • We need channelling constraints to link the variables CP 2010 Doctoral Programme
Combining Viewpoints: Permutation Problems • Dual viewpoints of a permutation problem with variables x1, x2, …, xnand d1, d2, …, dn • Combine them using the channelling constraints (xi =j)≡ (dj = i ) • Also allows both sets of variables to be search variables • e.g. use a dynamic variable order e.g. variable with smallest domain in either viewpoint • combines variable and value ordering: dual variable with smallest domain corresponds to the value occurring in fewest domains (in the other viewpoint) CP 2010 Doctoral Programme
Combining Viewpoints: Integer & Set Variables • In a nurse rostering problem, we can allocate shifts to nurses or nurses to shifts • First viewpoint: • an integer variable nijfor each nurse i and day j • its value is the shift that nurse i works on day j • Second viewpoint: • a set variable Skj for each shift k and day j • its value is the set of nurses that work shift k on day j • Channelling constraints: (nij = k) ≡ (i Skj) • Constraints on nurse availability are stated in the first viewpoint; constraints on work requirements in the 2nd (e.g. no. of nurses required for each shift) CP 2010 Doctoral Programme
Search Variables – Permutation Problems • We can use both sets of variables as search variables • e.g. use a dynamic variable order e.g. variable with smallest domain in either viewpoint • combines variable and value ordering: dual variable with smallest domain corresponds to the value occurring in fewest domains (in the other viewpoint) CP 2010 Doctoral Programme
Summary CP 2010 Doctoral Programme • Choose a viewpoint • that allows the constraints to be expressed easily and concisely • Use auxiliary variables if they help to express the constraints • Implied constraints can detect infeasible subproblems earlier • Make sure they are useful • Look out for symmetry in the CSP • avoid it if possible by changing the model • eliminate it e.g. by adding constraints • Consider alternative viewpoints • think of standard reformulations • think about the problem in different ways • consider combining viewpoints
Conclusion • Aim for a rich model • multiple viewpoints • auxiliary variables • add constraints to improve propagation (implied constraints, global constraints) & symmetry breaking constraints • But check that your additions do reduce run-time with your chosen search strategy • Understand the problem as well as you can • build that insight into the model • the better you can understand a problem, the better you can solve it THE END CP 2010 Doctoral Programme