Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modelling for Constraint Programming Barbara Smith CP 2010 Doctoral Programme.

Similar presentations


Presentation on theme: "Modelling for Constraint Programming Barbara Smith CP 2010 Doctoral Programme."— Presentation transcript:

1 Modelling for Constraint Programming Barbara Smith b.m.smith@leeds.ac.uk CP 2010 Doctoral Programme

2 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: 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! CP 2010 Doctoral Programme

3 Solving CSPs Systematic search: given a CSP M = – choose a variable x i that is not yet assigned – create a choice point, i.e. a set of mutually exclusive & exhaustive choices, e.g. x i = a v. x i ≠ a – try the first & backtrack to try the other if this fails Constraint propagation: – add x i = a or x i ≠ 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 CP 2010 Doctoral Programme

4 Representing a Problem If a CSP M = represents a problem P, then every 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 CP 2010 Doctoral Programme

5 Interactions with Search Strategy 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 x i = a v. x i ≠ 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 CP 2010 Doctoral Programme

6 Viewpoints A viewpoint is a pair, 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 CP 2010 Doctoral Programme

7 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: – x 1 + x 2 + x 3 = x 4 + x 5 + x 6 = x 1 + x 4 + x 7 = … but not in V2 438 951 276 x1x1 x2x2 x3x3 x4x4 x5x5 x6x6 x7x7 x8x8 x9x9 CP 2010 Doctoral Programme

8 Constraints 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 CP 2010 Doctoral Programme

9 Expressing the Constraints 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 CP 2010 Doctoral Programme

10 Auxiliary Variables 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 – x 1 + x 2 + x 3 = x 4 + x 5 + x 6 becomes x 1 + x 2 + x 3 = s, x 4 + x 5 + x 6 = s – Better still, add s = 15 (in the 3 × 3 case) CP 2010 Doctoral Programme

11 Auxiliary Variables (II) Example: car sequencing (Dincbas, Simonis and van Hentenryck, ECAI 1988) CP 2010 Doctoral Programme 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 classes123456Capacity Option 11000111/2 Option 20011012/3 Option 31000101/3 Option 41101002/5 Option 50010001/5 No. of cars112222

12 Car Sequencing - Model Variables : s 1, s 2, …, s 10 Value of s i is 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 o ij : – o ij = 1 iff the car in the i th slot in the sequence requires option j Option 1 capacity is one car in every two: – o i,1 + o i+1,1 ≤ 1 for 1 ≤ i < 10 Relate the auxiliary variables to the s i variables : – λ jk = 1 if car class k requires option j – o ij = λ jsi, 1 ≤ i ≤ 10, 1 ≤ j ≤ 5 CP 2010 Doctoral Programme

13 One Constraint is Better than Several (maybe) 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! CP 2010 Doctoral Programme

14 Example: n-queens A variable for each row, x 1, x 2, …, x n Values represent the columns, 1 to n The assignment (x i,c) means that the queen in row i is in column c Constraints for each pair of rows i, j: – x i ≠x i – x i − x i ≠ i − j – x i − x i ≠ j − i CP 2010 Doctoral Programme

15 Propagating the Constraints A queen in row 5, column 3 conflicts with both remaining values for x 3 But the constraints are consistent – x i ≠x i thinks that (x 3,1) can support (x 5,3) – x i − x i ≠ i − j thinks that (x 3,3) can support (x 5,3) CP 2010 Doctoral Programme × Enforcing AC on the conjunction (x i ≠x i ) ٨ (x i − x i ≠ i − j ) ٨ (x i − x i ≠ j − i ) would remove 3 from the domain of x 5 If you can enforce GAC on the constraint

16 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

17 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

18 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

19 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

20 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

21 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

22 CP 2010 Doctoral Programme Symmetry between Values: Car sequencing cars12345678910 option 11000001111 option 20011110011 option 31000001100 option 41100110000 option 50011000000 classes123456 option 1100011 option 2001101 option 3100010 option 4110100 option 5001000 no. of cars112222 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

23 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

24 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

25 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

26 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

27 CP 2010 Doctoral Programme Example: n-queens Standard model – a variable for each row, x 1, x 2, …, x n – values represent the columns, 1 to n – x i = j means that the queen in row i is in column j – n variables, n values, allDifferent(x 1, x 2, …, x n ) Dual viewpoint – a variable for each column, d 1, d 2, …, d n ; values represent the rows In this problem, both viewpoints give the same CSP

28 CP 2010 Doctoral Programme Example: Magic Square First viewpoint: – variables x 1, x 2, …, x 9 – values represent the numbers 1 to 9 – The assignment (x i,j) means that the number in square i is j Dual viewpoint – a variable for each number, d 1, d 2, …, d 9 – values represent the squares Constraints are much easier to express in the first viewpoint (& there are far fewer of them) x1x1 x2x2 x3x3 x4x4 x5x5 x6x6 x7x7 x8x8 x9x9

29 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

30 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

31 CP 2010 Doctoral Programme Combining Viewpoints: Permutation Problems Dual viewpoints of a permutation problem with variables x 1, x 2, …, x n and d 1, d 2, …, d n Combine them using the channelling constraints (x i = j) ≡ (d j = 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)

32 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 n ij for each nurse i and day j – its value is the shift that nurse i works on day j Second viewpoint: – a set variable S kj for each shift k and day j – its value is the set of nurses that work shift k on day j Channelling constraints: (n ij = k) ≡ (i  S kj ) Constraints on nurse availability are stated in the first viewpoint; constraints on work requirements in the 2 nd (e.g. no. of nurses required for each shift)

33 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)

34 Summary 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 CP 2010 Doctoral Programme

35 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


Download ppt "Modelling for Constraint Programming Barbara Smith CP 2010 Doctoral Programme."

Similar presentations


Ads by Google