Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE461: Constraint Programming & Modelling --Constraint Satisfaction Problems-- u Constraint satisfaction problems (CSPs) u A backtracking solver u Node.

Similar presentations


Presentation on theme: "CSE461: Constraint Programming & Modelling --Constraint Satisfaction Problems-- u Constraint satisfaction problems (CSPs) u A backtracking solver u Node."— Presentation transcript:

1 CSE461: Constraint Programming & Modelling --Constraint Satisfaction Problems-- u Constraint satisfaction problems (CSPs) u A backtracking solver u Node and arc consistency u Bounds consistency u Generalized consistency u Combining consistency methods with backtracking u Optimization for arithmetic CSPs Based on Chapter 3 of Marriott & Stuckey

2 Constraint Satisfaction Problems u A constraint satisfaction problem (CSP) consists of: u A conjunction of primitive constraints C over variables X 1,..., X n u A domain D which maps each variable X i to a set of possible values D(X i ) u It is understood as the constraint C  (X 1  D(X 1 )  …  X n  D(X n ) ) u CSPs arose in Artificial Intelligence (AI) and now are also studied in Constraint Programming

3 Map Colouring A classic CSP is the problem of coloring a map so that no adjacent regions have the same color Can the map of Australia be colored with 3 colors ?

4 4-Queens Place 4 queens on a 4 x 4 chessboard so that none can take another. Q1Q2Q3Q4 1 2 3 4 Four variables Q1, Q2, Q3, Q4 representing the row of the queen in each column. Domain of each variable is {1,2,3,4} One solution! -->

5 4-Queens (Cont.) The constraints: Not on the same row Not diagonally up Not diagonally down

6 Smuggler’s Knapsack Smuggler with knapsack with capacity 9, who needs to choose items to smuggle to make profit at least 30 What should be the domains of the variables?

7 Simple Backtracking Solver u The simplest way to solve CSPs is to enumerate all possible solutions and try each in turn u The backtracking solver: u Iterates through the values for each variable in turn u Checks that no primitive constraint is false at each stage u Assume satisfiable(c) returns false when primitive constraint c with no variables is unsatisfiable u We let vars(C) return the variables in a constraint C.

8 Partial Satisfiable partial_satisfiable The function partial_satisfiable(C) checks whether constraint C is unsatisfiable due to it containing a primitive constraint with no variables which is unsatisfiable partial_satisfiable partial_satisfiable(C) for each primitive constraint c in C if vars(c) is empty if satisfiable(c) = false return false return true

9 Simple Backtracking Solver back_solve back_solve(C,D) partial_satisfiable if vars(C) is empty return partial_satisfiable(C) choose x in vars(C) for each value d in D(x) let C1 be C with x replaced by d partial_satisfiable( if partial_satisfiable(C1) then back_solve if back_solve(C1,D) then return true return false

10 Backtracking Solve partial_satisfiable partial_satisfiable false Choose X Choose Y Choose Z partial_satisfiable partial_satisfiable false unsatisfiable

11 Consistency Methods u Unfortunately the worst-case time complexity of Simple Backtracking is exponential u Instead we can use fast (polynomial time) but incomplete constraint solving methods: u Can return true, false or unknown u One class are the consistency methods: u Find an equivalent CSP to the original one with smaller variable domains u If any domain is empty the original problem is unsatisfiable u Consistency methods are local-- they examine each primitive constraint in isolation u We shall look at: Node consistencyArc consistency Bounds consistencyGeneralized consistency

12 Node Consistency u Primitive constraint c is node consistent with domain D u if |vars(c)| ≠ 1 or u if vars(c) = {x} then for each d in D(x), {x  d }is a solution of c u A CSP is node consistent if each primitive constraint in it is node consistent

13 Node Consistency Examples This CSP is not node consistent This CSP is node consistent The map coloring and 4-queens CSPs are node consistent. Why?

14 Achieving Node Consistency node_consistent node_consistent(C,D) for each primitive constraint c in C node_consistent_primitive D := node_consistent_primitive(c, D) return D node_consistent_primitive node_consistent_primitive(c, D) if |vars(c)| =1 then let {x} = vars(c) D(x) := { d  D(x) | {x  d} is a solution of c} return D

15 Achieving Node Consistency- Example Let C and D be node_consistent Then node_consistent(C,D) returns domain D where

16 Arc Consistency u A primitive constraint c is arc consistent with domain D u if |vars{c}| ≠ 2 or u vars(c) = {x,y} and for each d in D(x) there exists e in D(y) such that { x  d, y  e} is a solution of c and for each e in D(y) there exists d in D(x) such that { x  d, y  e} is a solution of c A CSP is arc consistent if each primitive constraint in it is arc consistent

17 Arc Consistency Examples This CSP is node consistent but not arc consistent For example the value 4 for X and X < Y. The following equivalent CSP is arc consistent The map coloring and 4-queens CSPs are also arc consistent.

18 Achieving Arc Consistency u arc_consistent_primitive u arc_consistent_primitive(c, D) if |vars(c)| = 2 then let {x,y} = vars(c) return D u Removes values which are not arc consistent with c D(x) := { d  D(x) | exists e  D(y) {x  d, y  e} is a solution of c} D(y) := { e  D(y) | exists d  D(x) {x  d, y  e} is a solution of c}

19 Achieving Arc Consistency (Cont.) u arc_consistent u arc_consistent(C,D) repeat W := D for each primitive constraint c in C arc_consistent_primitive D := arc_consistent_primitive(c,D) until W = D return D u Note that iteration is required u The above is a very naive algorithm Faster algorithms are described in E. Tsang “Foundations of Constraint Satisfaction”, Academic Press, 1983.

20 Achieving Arc Consistency-- Example u Let C and D be given by arc_consistent u Consider arc_consistent(C,D) arc_consistent_primitive -- calls arc_consistent_primitive(X<Y,D) which updates D to arc_consistent_primitive -- calls arc_consistent_primitive(Z≤2,D) which doesn’t change D arc_consistent_primitive -- calls arc_consistent_primitive(Y<Z,D) which updates D to arc_consistent_primitive -- calls arc_consistent_primitive 5 more times until it recognises that D remains unchanged arc_consistent_primitive -- calls arc_consistent_primitive(X<Y,D) which updates D to

21 Using Node and Arc Consistency u We can build constraint solvers using these consistency methods u Two important kinds of domain u False domain: some variable has an empty domain u Valuation domain: every variable has a domain with a single value

22 Node and Arc Constraint Solver node_consistent D := node_consistent(C,D) arc_consistent D := arc_consistent(C,D) if D is a false domain then return false if D is a valuation domain then return satisfiable(C,D) return unknown

23 Node and Arc Solver Example Colouring Australia: with constraints WA NT SA Q NSW V T Node consistency

24 Node and Arc Solver Example Colouring Australia: with constraints WA NT SA Q NSW V T Arc consistency

25 Node and Arc Solver Example Colouring Australia: with constraints WA NT SA Q NSW V T Arc consistency

26 Node and Arc Solver Example Colouring Australia: with constraints WA NT SA Q NSW V T Arc consistency Answer: unknown

27 B&ANC Combo Constraint Solver u We can combine consistency methods with the backtracking solver u Apply node and arc consistency before starting the backtracking solver and after each variable is given a value u This reduces the number of values that need to be tried for each variable. I.e. it reduces the search space

28 B&ANC Combo Constraint Solver - - Example There is no possible value for variable Q3! Q1Q2Q3Q4 1 2 3 4 No value can be assigned to Q3 in this case! Therefore, we need to choose another value for Q2.

29 B&ANC Combo Constraint Solver - - Example Q1Q2Q3Q4 1 2 3 4 We cannot find any possible value for Q4 in this case! Backtracking… Find another value for Q3? No! backtracking, Find another value of Q2? No! backtracking, Find another value of Q1? Yes, Q1 = 2

30 B&ANC Combo Constraint Solver - - Example Q1Q2Q3Q4 1 2 3 4

31 B&ANC Combo Constraint Solver - - Example Q1Q2Q3Q4 1 2 3 4

32 Node and Arc Solver Example Colouring Australia: with constraints WA NT SA Q NSW V T Backtracking enumeration Select a variable with domain of more than 1, T Add constraintApply consistency Answer: true

33 Bounds Consistency u What about primitive constraints with more than 2 variables? E.g. X=3Y+5Z. u One possibility: hyper-arc consistency -- extend arc consistency to arbitrary number of variables u Unfortunately determining hyper-arc consistency is NP- hard (so it’s probably exponential) u What can we do?

34 Bounds Consistency u Use bounds consistency! u This only works for arithmetic CSPs: I.e. CSPs whose constraints are over the integers. u Two key ideas u Use consistency over the real numbers u map to integer interval arithmetic u Only consider the endpoints (upper and lower bounds) of the interval u narrow intervals whenever possible u Let the range [l..u] represent the set of integers {l, l+1,..., u} and the empty set if l>u. u Define min(D,x) to be the smallest element in the domain of x u Define max(D,x) to be the largest

35 Bounds Consistency u A primitive constraint c is bounds consistent with domain D if for each variable x in vars(c) u There exist real numbers d 1,..., d k for remaining vars x 1,..., x k such that for each x i, min(D,x i ) ≤ d i ≤ max(D,x i ) and { x  min(D,x), x 1  d 1,..., x k  d k } is a solution of c u and there exist real numbers e 1,..., e k for x 1,..., x k such that for each x i, min(D,x i ) ≤ e i ≤ max(D,x i ) and { x  max(D,x), x 1  e 1,..., x k  e k } is a solution of c u An arithmetic CSP is bounds consistent if all its primitive constraints are.

36 Bounds Consistency Examples Not bounds consistent, consider Z=2, then X-3Y=10 But the domain below is bounds consistent Compare with the hyper-arc consistent domain

37 Achieving Bounds Consistency u Given a current domain we wish to modify the endpoints of the domains so the result is bounds consistent u Propagation rules do this u The constraint solver generates propagation rules for each primitive constraint. u These are repeatedly applied until there is no change in the domain u We do not need to apply a propagation rule until the domains of the variables involved are modified We now look at how propagation rules are generated for common kinds of primitive constraint.

38 Propagation Rules for X = Y + Z Consider the primitive constraint X = Y + Z which is equivalent to the three forms Propagation Rules ??

39 Propagation Rules for X = Y + Z Consider the primitive constraint X = Y + Z which is equivalent to the three forms Reasoning about minimum and maximum values:

40 Propagation Rules for X = Y + Z Xmin := max {min(D,X), min(D,Y) + min(D,Z)} Xmax := min {max(D,X), max(D,Y) + max(D,Z)} D(X) := { X  D(X) | Xmin ≤ X ≤ Xmax } Ymin := max {min(D,Y), min(D,X) - max(D,Z)} Ymax := min {max(D,Y), max(D,X) - min(D,Z)} D(Y) := { Y  D(Y) | Ymin ≤ Y ≤ Ymax } Zmin := max {min(D,Z), min(D,X) - max(D,Y)} Zmax := min {max(D,Z), max(D,X) - min(D,Y)} D(Z) := { Z  D(Z) | Zmin ≤ Z ≤ Zmax }

41 Achieving Bounds Consistency-- Example The propagation rules determine that: Hence the domains can be reduced to

42 More propagation rules Given initial domain: We determine that new domain:

43 Dis-equations Y≠Z Dis-equations give weak propagation rules. Only when one side takes a fixed value that equals the minimum or maximum of the other is there propagation

44 Non-Linear Constraints X=Y  Z If all variables are positive it is simple enough Much harder if some domains are non-positive or span zero See Marriott&Stuckey, 1998 Example: becomes:

45 Bounds Consistency Example Smugglers knapsack problem (no whiskey available) Step 1 examines profit constraint no change

46 Bounds Consistency Example Smugglers knapsack problem (no whiskey available) Step 2 examines capacity constraint

47 Bounds Consistency Example Smugglers knapsack problem (no whiskey available) Step 3 re-examines profit constraint, because of max(D(P,C)) change no further change after this

48 Bounds Consistency Solver bounds_consistent D := bounds_consistent(C,D) if D is a false domain return false if D is a valuation domain return satisfiable(C,D) return unknown

49 B&BC Combo Constraint Solver u Like arc and node consistency we can combine bounds consistency methods with the backtracking solver u Apply bounds consistency before starting the backtracking solver and after each variable is given a value u This reduces the search space

50 B&BC Combo Constraint Solver-- Example Smugglers knapsack problem (whiskey available) Current domain: after bounds consistency W = 0 P = 1 (0,1,3) Solution Found: return true

51 B&BC Combo Constraint Solver -- Example Smugglers knapsack problem (whiskey available) Current domain: Initial bounds consistencyBacktrack P = 2 false Backtrack P = 3 W = 0 P = 1 (0,1,3) (0,3,0) W = 1 (1,1,1) W = 2 (2,0,0) No more solutions

52 Generalized Consistency u Can use any consistency method with any other by communicating through the shared domain, u node consistency for primitive constraints with 1 variable u arc consistency for primitive constraints with 2 variables u bounds consistency for primitive constraints with >2 variables u We can do even better by using complex primitive constraints with specialised consistency methods These are often called global constraints

53 All_different u all_different([V 1,...,V n ]) holds when each variable V 1,..,V n takes a different value u Not needed for expressiveness. E.g. all_different([X, Y, Z]) is equivalent to u But conjunctions of disequations are not handled well by arc (or bounds) consistency. E.g. The following domain is arc consistent with the above u BUT there is no solution! u Specialized consistency techniques for all_different can find this

54 All_different Consistency One algorithm for handling all_different is let c be of the form all_different([V 1,…,V n ]) let W = {V 1,…,V n } while exists V in W where D(V) = {d} W := W - {V} for each V’ in W D(V’) := D(V’) - {d} DV := union of all D(V) for V in W if |DV| < |W| then return false return D

55 All_different Example DV = {1,2}, W={X,Y,Z} so |DV| < |W| hence detect unsatisfiability Other algorithms are also possible

56 Other Complex Constraints u element(I,[V 1,…,V n ], X) array access if I = i, then X = V i and if X ≠ V i then I ≠i u cumulative([S 1,…,S n ], ([D 1,…,D n ], ([R 1,…,R n ], L) schedule n tasks with start times S i and durations D i needing R i units of a single resource where L units are available at each moment. Very complex propagation rules.

57 Cumulative Example Bernd is moving house again. He has 4 friends to help and must move in one hour. He has the following furniture ItemTime to moveNo. of people needed piano30 min3 chair10 min1 bed15 min3 table15 min2 How can we model this? D(P) = D(C) = D(B) = D(T) = [0..60], P + 30 ≤ 60, C + 10 ≤ 60, B + 15 ≤ 60, T+ 15 ≤ 60, cumulative([P,C,B,T], [30,10,15,15], [3,1,3,2], 4)

58 Cumulative Example with B&BC Combo Solver D(P) = D(C) = D(B) = D(T) = [0..60], P + 30 ≤ 60, C + 10 ≤ 60, B + 15 ≤ 60, T+ 15 ≤ 60, cumulative([P,C,B,T], [30,10,15,15], [3,1,3,2], 4) D(P) = [0..30], D(C) = [0..50], D(B) = [0..45], D(T) = [0..45] D(P) = {0}, D(C) = [0..50], D(B) = [30..45], D(T) = [30..45] Choose PP = 0 D(P) = {0}, D(C) = {0}, D(B) = [30..45], D(T) = [30..45] Choose CC = 0 D(P) = {0}, D(C) = {0}, D(B) = {30}, D(T) = {45} Choose BB = 30 D(P) = {0}, D(C) = {0}, D(B) = {30}, D(T) = {45} Choose TT = 45

59 Optimization for CSPs u So far only looked at finding a solution: this is satisfiability u However often we want to find an optimal solution: One that minimizes/maximizes the objective function f. u Because the domains are finite we can use a solver to build a simple optimizer retry_int_opt retry_int_opt(C, D, f, best_so_far) for minimization solv D2 := solv(C,D) if D2 is a false domain then return best_so_far let sol be the solution corresponding to D2 retry_int_opt return retry_int_opt(C /\ f < sol(f), D, f, sol)

60 Retry Optimization Example Smugglers knapsack problem (optimize profit) First solution found: Corresponding solution Next solution found:No next solution! Return best solution Corresponding solution

61 Backtracking Optimization u Since the solver may use backtracking search anyway combine it with the optimization u At each step in backtracking search, if best is the best solution so far add the constraint f < best(f)

62 Back. Optimization Example Smugglers knapsack problem (whiskey available) Current domain: after bounds consistency W = 0 P = 1 (0,1,3) Solution Found: add constraint Smugglers knapsack problem (whiskey available)

63 Back. Optimization Example Initial bounds consistency P = 2 false P = 3 false W = 1 (1,1,1) Add constraint W = 0 P = 1 (0,1,3) Smugglers knapsack problem (whiskey available) W = 2 false Return last sol (1,1,1)

64 Summary u CSPs form an important class of problems u CSPs are NP-hard (from NP-hardness of SAT) u Solving of CSPs is essentially based on backtracking search u Reduce the search using consistency methods u incomplete but faster u node, arc, bound, generalized u Optimization for CSPs can be based on a backtracking search

65 Homework u Read Chapter 3 of Marriott&Stuckey, 1998 u Solve the Australian Map Colouring problem by hand using Simple Backtracking, then the B&ANC Combo. u Give propagation rules for constraints of form a 1 X 1 + … + a n X n ≤ b 1 Y 1 + … + b m Y m + c where each a i,, b i > 0.


Download ppt "CSE461: Constraint Programming & Modelling --Constraint Satisfaction Problems-- u Constraint satisfaction problems (CSPs) u A backtracking solver u Node."

Similar presentations


Ads by Google