Constraint Satisfaction Problems Objectives Constraint satisfaction problems Backtracking Iterative improvement Constraint propagation Reference Russell & Norvig: Chapter 5. 1 Constraints Constraints are encountered regularly in daily live. Ex Seats in the car. Ex Travel to a holiday resort. Ex Register for courses. Problems involving constraints may be complex. Ex University course timetabling. As the complexity of the problem grows, we turn to intelligent agents for finding a solution. In general, problems involving constraints are NP-hard. 2 University Course Timetablling Each semester, hundreds of courses are offered by hundreds of instructors. Room-slot: No two lectures can be offered in the same room at the same time slot. Instructor: Lectures by the same instructor cannot be scheduled at the same time slot. Course: Lectures of the same course cannot be offered at the same time slot. Course group: If two courses are to be taken in the same semester, then their lectures cannot be scheduled at the same time slots. 3 Constraint Networks To study agents for solving these problems, we model their environments as constraint networks. A constraint network has two components: 1. a set V of variables with associated domains, and 2. a set C of constraints. V is a finite set of discrete variables and is denoted as V={x 1,,x n. Each variable x i in V is associated with a domain D i ={x i 1,,x ik of possible values. 4 1
Constraint Networks (CN) An assignment over a subset X V is a combination of values for variables in X. An assignment over X is complete if X = V. C is a set of constraints denoted C={C 1,,C m. Each constraint C i is relative to a subset X V and is a set of acceptable assignments over X. Subset X is called the scope of constraint C i. An assignment that satisfies all relevant constraints is called a consistent or legal assignment. A solution is a consistent, complete assignment. A CN is inconsistent if it has no solution. 5 Constraint Satisfaction Problems (CSPs) Tasks of determining whether a solution exists for a CN and finding solutions if they exist are referred to as constraint satisfaction problems (CSP). Focus: whether a solution exists & find one if exists. Ex Map coloring. Ex The n-queens. Other CSPs. Arity of a constraint is the cardinality of its scope. Unary constraint and binary constraint. Binary constraint network has only unary and binary constraints. 6 Ex Map Coloring N Q W Color each region with one of {r,g,b s.t. no adjacent regions have the S E same color. R Constraint network What is V and what are variable domains? What is an assignment over X = {W,N,S? What is the constraint b/w W and N? How many constraints are in C and what are their scopes? Is assignment {N=r,Q=g,S=b,E=g consistent? What is a solution? 7 Ex The n-queesn Place n queens on an n n chessboard s.t. no queen attacks another. Constraint network for 4-queen What is V if we use one variable for each row? What is the domain of x 2? What is the constraint b/w x 1 and x 3? When the order of variables is clear, we write an assignment as a tuple. How many constraints are in C? What is a solution? 8 2
Other CSPs Crossword puzzles. 3SAT. Job shop scheduling. Radio link frequency assignment. Space telescope scheduling. 3D interpretation of 2D drawing. Hospital nurse scheduling. Airline flight scheduling. Floor plan layout. Automobile transmission design. Constraint Graphs Structure of CNs can be expressed as constraint graphs, and they are useful in solving CSPs. There are three common forms of constraint graphs. In a primal graph, each node represents a variable and each link connects two constrained variables. Ex Map coloring. In a dual graph, each node (drawn as a cluster) is labeled by the scope of a constraint. Each link connects two clusters that share some variables and is labeled by the intersection. Ex Map coloring. 9 10 Constraint Graphs (Cont) In a hypergraph, each node represents a variable. Each hyperlink represents a constraint and connects all variables in its scope. It is drawn by a link from a box to each variable in the scope. Ex Map coloring. Ex Crypt-arithmetic Puzzle. Ex Crypt-arithmetic Puzzle T W O + T W O ----------- F O U R Substitute each letter by a distinct digit without leading zero s.t. the sum is arithmetically correct. Constraint network and constraint graph What are the variables? How to represent distinct digit? How to represent no leading zero? How to represent arithmetic sum? What is the constraint hypergraph? 11 12 3
Solve CSPs by Search Chronological Backtracking Tree search such as BFS, DFS, etc are applicable to CSPs. How? How many leaf nodes are at level n+1? How many complete assignments are there? Observation Unlike problems such as 8-puzzle, neither the order of variable assignment, nor the search path matters in CSPs. Hence, search can focus on a single order only. Idea: Depth-first search that assigns one variable each time (forwards) and backtracks when a variable has no legal values to assign. An recursive algorithm. Ex The 4-queen. Chronological: Always backtrack to the most recent value assignment. 13 14 backtracking(config[]) { // assume access to a CN if config is complete, return config; v = getvariable(config); vu[] = orderdomainvalue(v, config); for each u in vu, if u is consistent with config, add {v=u to config; result = backtracking(config); if result!= null, return result; remove {v=u from config; return null; 15 Complexity of Backtracking Suppose V =n and D i k. In the worst case, the full search tree is explored. What is the total number of nodes in full search tree? How many levels are in the search tree? What is the branching factor? Sum of geometric series Sum k 0 + k 1 + + k n is (k n+1-1)/(k-1). Time complexity of backtracking. 16 4
Variable Ordering Does the order of variables make any difference to the efficiency of chronological backtracking? Ex Map coloring: D=(r,g,b) except D Q =(b,g,r). Order 1: (W,N,Q,E,R,S). Order 2: (W,N,S,Q,E,R). Styles of variable ordering Fixed Dynamic 17 Minimum Remaining Values (MRV) Heuristic Choose the next variable x that has the fewest legal values. Ex Map coloring. Rational behind MRV If x has no legal value, the search tree is pruned immediately. Otherwise, x is most likely to cause failure soon, thereby pruning the search tree. Overhead: At each forward step, for each remaining variable x, each constraint that involves x must be checked. 18 Maximum Degree (MDeg) Heuristic Choose the next variable x that involves the largest number of constraints with unassigned variables. Ex Map coloring. Rational behind Reduce the branching factor on future choices. Overhead: At each forward step, for each remaining variable x, count its current degree. MRV and MDeg can be combined in chronological backtracking, with MRV as the primary heuristic and MDeg as the tie-breaker. Value Selection Given variable order, does the order in which values are assigned make any difference to efficiency? Ex Map coloring with variable ordering (W,N,Q,E,R,S) and value order (r,g,b). Styles of value ordering Fixed Dynamic 19 20 5
Forward Checking To assign variable x with value u, for each unassigned variable y connected to x by a constraint C i, delete from D y each value inconsistent with x=u by C i. If for any y, D y becomes empty, consider another value u of x. If for each y, D y is non-empty, assign x=u. Ex Map coloring with variable order (W,Q,R,N,S,E). Benefit: Avoid a value that will cause failure before it is assigned and hence prune search space. 21 Looking Back in Search x 1 {r,b,g When a branch of the search x 2 {b,g fails, chronological backtracking backs up to the preceding variable. x 3 {r,b,g x 4 {r,b This is often inefficient. Ex A coloring problem. It is more efficient to go all the way back to the source of failure. However, jumping too far back may skip potential solutions. Ex Jumping back to x 2. Challenge: What is the adequate jump back point? x 7 {r,b x 6 {r,g,y x 5 {b,g 22 Dead-end and Conflict Set If a consistent assignment u i =(u 1,,u i ) for x 1,,x i is inconsistent with every possible value of x i+1, then (u 1,,u i ) is a dead-end state, and x i+1 is a dead-end variable. Let u i =(u 1,,u i ) for x 1,,x i be a consistent assignment and x be an unassigned variable. If there is no value u in the domain of x s.t. (u i, x=u) is consistent, then u i is a conflict set of x. If u i does not contain a subtuple that is in conflict with x, then u i is a minimal conflict set of x. 23 Culprit Variable Let u i =(u 1,,u i ) be a tuple. Then u k =(u 1,,u k ), where k i, is a prefix tuple of u i. Let u i =(u 1,,u i ) for x 1,,x i be a dead-end state. The culprit variable relative to u i is x k where k = min{j j i and u j is a conflict set of x i+1. Ex What is the culprit variable wrt (x 1 =r, x 2 =b, x 3 =b, x 4 =b, x 5 =g, x 6 =r)? The culprit variable is the adequate jump back point. Why? 24 6
Backjumping Algorithm backjump() { // assume access to a CN i =1; D i = D i ; latest i =0; // latest: a pointer while 1 i n, x i = selectvalue(i); if x i == null, i = latest i ; else i++; D i = D i ; latest i =0; end while; if i == 0, return no solution ; else return assignment over {x 1,,x n ; Ex The coloring problem. 25 selectvalue(i) { while D i {, select u D i and remove u from D i ; consistent = true; k = 1; while k<i and consistent, if k > latest i, latest i =k; if (u k, x i =u) is inconsistent, consistent = false; else k++; if consistent, return u; return null; Does backjump() jumps back to the culprit variable? 26 Local Search Backtracking builds up a solution by assigning one variable at a time consistently. Local search iteratively improves a complete but inconsistent assignment, one variable at a time. Challenges Which variable to improve next? Which value of the variable to select? Time Bounded Min-Conflicts Algorithm minconflicttb(c[], maxsteps) { // c: constraints in CN current = a complete assignment; for i=1 to maxsteps, if current is legal, return current; v = randomly selected variable violating c; u = value of v minimizing # constraint violations; set v = u in current; return failure; 27 28 7
Example and Properties Ex Solving 8-queens. Can agent avoid repeating the same assignments? An algorithm for CSPs is complete if it always finds a solution when one exists, or terminates when no solution exists. Is the algorithm complete? Nogood An intelligent CSP agent should use learning to avoid regenerating a conflict set. Given a CN, any partial assignment that does not appear in any solution is a nogood. Is every conflict set a nogood? Is every nogood a conflict set? 29 30 Min-Conflicts Algorithm With Nogood Learning minconflictnl(c[]) { // c[]: constraints from CN parsol[] = ; varleft[] = a complete assignment; return minconflictnl(parsol, varleft, c); Each element of parsol and varleft is a pair in the form (variable, value). parsol and varleft have no variables in common. The union of parsol and varleft is a complete assignment. minconflictnl(parsol[], varleft[], c[]) { nogood[] = ; loop if varleft parsol is legal, return varleft parsol; (v,u) = element in varleft violating c; vu[] = values of v consistent with parsol wrt c & nogood; if vu is emtpy, if parsol is empty, return no-solution; add parsol to nogood; move most recently added element of parsol to varleft; else u = element of vu minimizing # constraint violations with varleft wrt c; remove (v,u) from varleft and add (v,u ) to parsol; end loop 31 32 8
Ex The 4-queens. Example and Properties Is the algorithm complete? Does it always find a solution when one exist? Does it terminate when there is no solution? 33 Relative Arc Consistency Forward checking propagates the implications of a constraint from variable x to y. Constraint propagation is the general concept to propagate implications of a constraint on one variable to others, and arc consistency is an effective method of constraint propagation. Let C xy be a constraint of scope {x,y. Variable x is arc-consistent relative to y iff for every value u D x there exists a value w D y s.t. (u,w) C xy. Ex Constraint C xy : x < y with D x = D y ={1,2,3. Is x arc-consistent relative to y? 34 Enforce Relative Arc-Consistency If x is not arc-consistent relative to y, consistency can be enforced by executing algorithm revise(): revise(x, y, C xy ) { for each u D x, if there is no w D y s.t. (u,w) C xy, D x = D x /{u; Ex Given C xy : x < y with D x = D y ={1,2,3, how to make x arc-consistent relative to y? Is y arc-consistent relative to x? How to make both arcs consistent? What is the complexity of revise()? 35 Arc Consistency Let C xy be a constraint of scope {x,y. Variables x and y are arc-consistent iff x is arc-consistent relative to y and y is arc-consistent relative to x. A CN is arc-consistent iff every pair of variables constrained are arc-consistent. Arc consistency in a CN can be enforced by the algorithm arcconsistency(). Ex Map coloring with order (W,Q,R,N,S,E). After assigning W=r, is the CN arc consistent? After Q=g, what happens if arcconsistency() is run? 36 9
Enforce Arc-Consistency arcconsistency() { agenda = {; for each pair {x, y of variables adjacent in CN, agenda = agenda {(x,y),(y,x); while agenda {, remove (x,y) from agenda and revise(x, y, C xy ); if D x is revised, for each z adjacent to x in CN and z y, agenda = agenda {(z,x); What is the complexity of arcconsistency()? 37 The k-consistency Can an arc-consistent CN contain inconsistency? Could an arc-consistent CN have no solution? Ex A triangle-structured coloring CSP. A CN is k-consistent if for every set X of k-1 variables and for every consistent assignment over X, a consistent value can be assigned to any k th variable. 1-consisency = node consisency 2-consisency = arc consisency 3-consisency = path consisency Ex Is CN of 4-queens k-consistent for k=1,2,3? 38 Strong k-consistency A CN is strongly k-consistent if it is i-consistent for all i k. If a CN with V =n is strongly n-consistent, what would happen if backtracking() is applied to it? For a general CN, what is the complexity to establish strong n-consistency? In general, CSPs are NP-hard. Between strong n-consistency and arc-consistency, a range of middle ground exists to trade efficiency of search with efficiency of consistency checking. 39 Solving Tree-Structured CSPs Structure info in constraint graphs can provide guidance for limited consistency checking and efficient search. Let the constraint graph of a CN be a tree T. The CN can be solved using solvetreecn(). After 1 st for loop, every parent is arc-consistent relative to its child. The 2 nd for loop is backtracking free. Ex A coloring problem with domain {r,g for each variable. Complexity of solvetreecn(). 40 10
solvetreecn() { pick a node as root of T; define an order r for nodes s.t. pi(x) precedes x in r; denote r = (x 1,x 2,,x n ); for i=n to 2, revise(pi(x i ), x i ); if Dpi(xi)={, return null; for i=1 to n, assign x i a value consistent with the value assigned to pi(x i ); return complete assignment; Cutset Conditioning What if the CN is not tree-structured? Can solvetreecn() be extended to solve more general CNs? A cycle cutset of a graph is a subset of nodes whose removal renders the graph a tree. Solving binary CNs using cutsetcondition(). Ex Map coloring. Complexity of cutsetcondition(). 41 42 cutsetcondition() { choose a cycle cutset X and denote Z=V/X; let A(X) be the set of consistent assignments of X; if A(X)={, return null; for each x A(X), for each y Z, remove each u D y inconsistent with x ; apply solvetreecn() to CN over Z; if solution z is found, return x z ; return null; 43 Cluster Tree Decomposition Idea: Decompose CN into a set of small subcns; enforce consistency in each subcn; and search the solution for CN efficiently. solvecnbyclustertree() { decompose CN into a cluster tree T; for each cluster Q in T, find the set S(Q) of consistent assignments of Q; if S(Q)={, return null; set T into a binary CN; apply solvetreecn() to T; 44 11
Decompose CN into Cluster Tree Divide V into overlapping clusters s.t. each x V is contained in at least one cluster. For every constraint in C, all variables in its scope must be contained in at least one cluster. Organize clusters into a tree T s.t. every two adjacent clusters in T have at least one common variable. Any variable contained by two clusters in T must be contained by all clusters along the path. Ex Map coloring. Set Cluster Tree into Binary CN For each cluster Q and its set S(Q) of consistent assignments, create a mega-variable q of domain D q =S(Q). For each two adjacent clusters Q and R, denote Z=Q R and corresponding mega-variables q and r. Constraint over {q,r is that assignment of q and assignment of r must agree on the value of variables in Z. Ex Map coloring. 45 46 How to Decompose CN into Cluster Tree Conditions of the cluster tree have been presented. Algorithms to generate such cluster trees from CNs are needed. The decomposition steps Triangulate constraint graph. Identify clusters. Organizing clusters into cluster tree. 47 Triangulating Constraint Graph triangulatecg(g) { G = G; fillin = {; for i=1 to n, select node x i in G with adjacent node set adj(x i ); if nodes in adj(x i ) are not pairwise linked, add links in G to make adj(x i ) pairwise linked; record links added to fillin; remove x i and its incident links from G; add links in fillin to G ; return G ; 48 12
Identify Clusters Idea: Bookkeeping during triangulatecg(g). During triangulatecg(g): Record Q i = adj(x i ) {x i before each x i is removed from G. After triangulatecg(g): identifycluster({q 1, Q 2,, Q n ) { Clus = {Q 1, Q 2,, Q n ; for i=n to 1, if Q i is a subset of Q k (k<i), remove Q i from Clus; return Clus; 49 Construct Cluster Tree buildclustertree(clus) { init cluster tree T with no clusters; pick Q Clus; add Q to T; remove Q from Clus; while Clus {, select Q Clus and Q from T s.t. Q Q is maximal; add Q to T; connect Q to Q ; remove Q from Clus; return T; 50 Triangulation Revisited How to select node x i in each iteration? Ex Map coloring problem. What are the clusters produced if triangulation is performed in order (N,W,U,E,S,R)? What is the consequence to computation in solvecnbyclustertree()? Conclusion: The less number of fillins, the better. Finding triangulations with minimum # fillins is NPhard. Heuristic: Select x i with minimum # of fillins. 51 Solving CSPs by Cluster Tree: Complexity What is the complexity of finding set S(Q) for cluster Q? The tree width of a cluster tree decomposition of a constraint graph is one less than the size of the largest subproblem. What is the above complexity in terms of tree width? What is the complexity of solvecnbyclustertree()? 52 13