Chapter5 Constraint Satisfaction Problems 20070412 Chap5 1 Constraint Satisfaction Problems (CSPs) � Standard Search Problem - State is a “black box”, can be represented by an arbitrary data structure that can be accessed only by the problem-specific routines --- the successor functions, heuristic function, and goal test . � Constraint Satisfaction Problem - State and goal test conform to a standard, structured, and very simple representation . 20070412 Chap5 2 1
Constraint Satisfaction Problems ( cont.-1) � CSP is defined by a set of variables, X 1 , X 2 , …, Xn , with values from domain D 1 , D 2 , …, Dn , and a set of constraints, C 1 , C 2 , …, Cm, specifying allowable combinations of values for subsets of variables � State is defined by an assignment of values to some or all of the variables, { X i = v i , X j = v j , …} 20070412 Chap5 3 Constraint Satisfaction Problems ( cont.-2) � Consistent (or legal) assignment an assignment that does not violate any constraints. � Complete assignment one in which every variable is mentioned. � Solution is a complete assignment that satisfies all the constraints . � Some CSPs also require a solution that maximizes an objective function 20070412 Chap5 4 2
Example: Map-Coloring Variables: WA, NT, Q, NSW, V, SA, T Domains: D i = {red, green, blue} Constraints: adjacent regions must have different colors e.g. WA ≠ NT or (WA, NT) ∈ { (red, green), (red, blue), (green, red), (green, blue), … } 20070412 Chap5 5 Example: Map-Coloring (cont.) Solutions: assignments satisfying all constraints e.g. { WA = red , NT = green , Q = red , NSW = green , V = red , SA = blue , T = green } 20070412 Chap5 6 3
Constraint Graph • CSP benefits - Standard representation pattern - Generic goal and successor functions - Generic heuristics (no domain specific expertise). • It is helpful to visualize a CSP as a constraint graph. - Nodes are variable, and arcs show constraints. 20070412 Chap5 7 Varieties of CSPs � Discrete variables - finite domains , size d ⇒ O(d m ) complete assignments e. g. 8 Queens, Q 1 , … Q 8 , with the domain {1, 2, 3, 4, 5, 6, 7, 8} - infinite domains , (integers, strings, etc .) need a constraint language (cannot enumerate all allowed combinations of values) e. g. job scheduling, Job 1 which takes 5 days, must precede Job 3 StartJob 1 + 5 ≤ StartJob 3 � Continuous variables e g. Hubble Space Telescope observations - Linear constraints are solvable in polynomial time by Linear programming methods. 20070412 Chap5 8 4
Varieties of Constraints � Unary constraints, involves a single variable e.g. SA ≠ green � Binary constraints, involves pairs of variables e.g. SA ≠ WA � Higher-order constraints, involves 3 or more variables e.g. cryptarithmetic column constraints Every high-order, finite-domain constraint can be reduced to a set of binary constraints if enough auxiliary variables are introduced. (Exercise 5.11) . We deal only with binary constraints in this chapter. 20070412 Chap5 9 Varieties of Constraints (cont.) � Absolute vs. Preference constraints Preference constraints can often be encoded as costs on individual variable assignments e.g. red is better than green Prof. X might prefer teaching in the morning, whereas prof. Y prefers teaching in the afternoon. 20070412 Chap5 10 5
Example: Cryptarithmetic X 3 X 2 X 1 Variables: F T U W R O X 1 X 2 X 3 Domains: { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } Where X 1 , X 2 and X 3 Constraints: are auxiliary variables Alldiff(F, T, U, W, R, O) O + O = R + 10 * X 1 X 1 + W + W = U + 10 * X 2 X 2 + T + T = O + 10 * X 3 X 3 = F 20070412 Chap5 11 Standard Search Formulation (Incremental) � Initial state : the empty assignment, { } � Successor function : assign a value to an unassigned variable that does not conflict with current assignment � Goal test : the current assignment is complete. � Path cost : a constant cost for every step. 20070412 Chap5 12 6
Backtracking Search for CSPs � In all CSPs, variable assignments are commutative . [ WA = red then NT = green ] same as [ NT = green then WA = red ] � Only need to consider assignments to a single value at each node. there are d n leaves (where d : domain size, n : number of variables) � Backtracking search a form of depth-first search for CSP with single–variable assignments 20070412 Chap5 13 Backtracking Search for CSPs (cont.) 20070412 Chap5 14 7
Backtracking Example 20070412 Chap5 15 Backtracking Example (cont.-1) 20070412 Chap5 16 8
Backtracking Example (cont.-2) 20070412 Chap5 17 Backtracking Example (cont.-3) 20070412 Chap5 18 9
Improving backtracking efficiency � Previous improvements introduce heuristics � General-purpose methods can give huge gains in speed: - Which variable should be assigned next? - In what order should its values be tried? Can we detect inevitable failure early? - - Can we take advantage of problem structure? 20070412 Chap5 19 Some Key Questions of Backtracking Search � Variable and Value Ordering Which variable should be assigned next, and in what order should its values be tried? � Propagating information through constraints What are the implications of the current variable assignments for the other unassigned variables? � Intelligent backtracking When a path fails --- that is, a state is reached in which a variable has no legal values --- can the search avoid repeating this failure in subsequent paths? 20070412 Chap5 20 10
Minimum Remaining Values � Minimum remaining value (MRV) heuristic or Most constrained variable heuristic or Fail-first heuristic - choose the variable with the fewer legal values var ← SELECT-UNASSIGNED-VARIABLE(VARIABLES[ csp ], assignment , csp ) After WA = red and NT = green , SA is assigned next rather than assigning Q 20070412 Chap5 21 Degree heuristic � Degree heuristic ( Most Constraining Variable ) - tie-breaker among most constrained variables - choose the variable with the most constraints on remaining variables SA (degree is 5) is assigned first 20070412 Chap5 22 11
Least Constraining Value • Least constraining value heuristic - try to leave the maximum flexibility for subsequent variable assignment - prefer the value that rules out the fewest choices for the neighboring variables in the constraint graph After WA = red and NT = green , Q is assigned to red Blue is bad choice since it eliminated the last legal value left for Q ’s neighbor, SA 20070412 Chap5 23 Constraint Propagation � Propagating the implications of a constraint on one variable onto other variables - Forward checking - Arc consistency (more stronger) 20070412 Chap5 24 12
Forward Checking Idea: Keep track of remaining legal values for unassigned variables Terminate search when any variable has no legal values Whenever a variable X is assigned, the forward checking process looks at each unassigned variable Y that is connected to X by a constraint and deletes from Y ’s domain any value that is inconsistent with the value chosen for X. 20070412 Chap5 25 Forward Checking (cont.-1) 20070412 Chap5 26 13
Forward Checking (cont.-2) NT can no longer be red After WA = red SA can no longer be red 20070412 Chap5 27 Forward Checking (cont.-3) After WA = red and Q = green With MRV, NT = blue and SA = blue 20070412 Chap5 28 14
Forward Checking (cont.-4) After WA = red, Q = green and V = blue, leaving SA with no legal values FC has detected that partial assignment is inconsistent with the constraints and backtracking can occur. 20070412 Chap5 29 Forward Checking (cont.-5) Forward checking propagates information from assigned to unassigned variable, but does not provide early detection for all failures. After WA = red and Q = green With MRV, NT = blue and SA = blue, but they cannot both be blue 20070412 Chap5 30 15
Example: 4-Queens Problem X1 X2 { 1,2,3,4} { 1,2,3,4} 1 2 3 4 1 2 3 4 X3 X4 { 1,2,3,4} { 1,2,3,4} [4-Queens slides copied from B.J. Dorr CMSC 421 course on AI] 20070412 Chap5 31 Example: 4-Queens Problem (cont.-1) X1 X2 { 1,2,3,4} { 1,2,3,4} 1 2 3 4 1 2 3 4 X3 X4 { 1,2,3,4} { 1,2,3,4} 20070412 Chap5 32 16
Example: 4-Queens Problem (cont.-2) X1 X2 { 1,2,3,4} { , ,3,4} 1 2 3 4 1 2 3 4 X3 X4 { ,2, ,4} { ,2,3, } 20070412 Chap5 33 Example: 4-Queens Problem (cont.-3) X1 X2 { 1,2,3,4} { , ,3,4} 1 2 3 4 1 2 3 4 X3 X4 { ,2, ,4} { ,2,3, } 20070412 Chap5 34 17
Example: 4-Queens Problem (cont.-4) X1 X2 { 1,2,3,4} { , ,3,4} 1 2 3 4 1 2 3 4 X3 X4 { , , , } { ,2, , } 20070412 Chap5 35 Example: 4-Queens Problem (cont.-5) X1 X2 { ,2,3,4} { 1,2,3,4} 1 2 3 4 1 2 3 4 X3 X4 { 1,2,3,4} { 1,2,3,4} 20070412 Chap5 36 18
Example: 4-Queens Problem (cont.-6) X1 X2 { ,2,3,4} { , , ,4} 1 2 3 4 1 2 3 4 X3 X4 { 1, ,3, } { 1, ,3,4} 20070412 Chap5 37 Example: 4-Queens Problem (cont.-7) X1 X2 { ,2,3,4} { , , ,4} 1 2 3 4 1 2 3 4 X3 X4 { 1, ,3, } { 1, ,3,4} 20070412 Chap5 38 19
Example: 4-Queens Problem (cont.-8) X1 X2 { ,2,3,4} { , , ,4} 1 2 3 4 1 2 3 4 X3 X4 { 1, , , } { 1, ,3, } 20070412 Chap5 39 Example: 4-Queens Problem (cont.-9) X1 X2 { ,2,3,4} { , , ,4} 1 2 3 4 1 2 3 4 X3 X4 { 1, , , } { 1, ,3, } 20070412 Chap5 40 20
More recommend