Constraint Satisfaction Problems 4 AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 1
4 Constraint Satisfaction Problems 4.1 Constraint satisfaction problems 4.2 Backtracking search 4.3 Constraint propagation 4.4 Local search 4.5 Structure and decomposition AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 2
Constraint satisfaction problems Standard search problem state is a “black box” — any old data structure that supports goal test, evaluation, successor operators CSP: a simple formal representation language – a factored representation for each state – a vector of variables and their (attribute) values Allows useful general-purpose algorithms with more power than standard (problem-specific) search algorithms AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 3
CSP CSP= ( X, D, C ) – X = { X 1 , · · · , X n } : a set of variables – D = { D 1 , · · · , D n } : a set of domains – C = { C 1 , · · · , C n } : a set of constraints Each domain D i ∈ D consists of a set of allowable values { v 1 , · · · , v k } for variable X i ∈ X Each constrain C i = ( scope, rel ) – scope : a tuple of variables that participate in the constraint – rel : a relation that defines the allowable values for scope C specifies allowable combinations of values for subsets of X AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 4
CSP Each state in a CSP is defined by an assignment of values to some (partial assignment) or all (complete assignment) variables { X i = v i , X j = v j , · · ·} Consistent assignment: an assignment that does not violate any con- straints A solution to a CSP is a consistent and complete assignment AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 5
Example: 4-Queens as a CSP Assume one queen in each column. Which row does each one go in? Variables Q 1 , Q 2 , Q 3 , Q 4 Domains D i = { 1 , 2 , 3 , 4 } Constraints Q i � = Q j (cannot be in same row) Q = 1 Q = 3 1 2 | Q i − Q j | � = | i − j | (or same diagonal) Translate each constraint into set of allowable values for its variables E.g., values for ( Q 1 , Q 2 ) are (1 , 3) (1 , 4) (2 , 4) (3 , 1) (4 , 1) (4 , 2) AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 6
Example: Map-Coloring Northern Territory Western Queensland Australia South Australia New South Wales Victoria Tasmania 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 (if the language allows this), or ( WA, NT ) ∈ { ( red, green ) , ( red, blue ) , ( green, red ) , ( green, blue ) , . . . } AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 7
Example: Map-Coloring Northern Territory Western Queensland Australia South Australia New South Wales Victoria Tasmania Solutions are assignments satisfying all constraints, e.g., { WA = red, NT = green, Q = red, NSW = green, V = red, SA = blue, T = green } AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 8
Constraint graph Constraint graph: nodes are variables, arcs show constraints Constraint hypergraph: adding hypermodes for n -ary constraint NT Q WA SA NSW V Victoria T CSP algorithms use the graph structure to speed up search E.g., Tasmania is an independent subgraph AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 9
Varieties of CSPs Discrete variables finite domains; size d ⇒ O ( d n ) complete assignments • e.g., Boolean CSPs, incl. Boolean satisfiability (NP-complete) infinite domains (integers, strings, etc.) • e.g., job scheduling, variables are start/end days for each job • need a constraint language, e.g., StartJob 1 +5 ≤ StartJob 3 • linear constraints solvable, nonlinear undecidable Continuous variables • e.g., start/end times for Hubble Telescope observations • linear constraints solvable in poly time by LP (linear program- ming) methods AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 10
Varieties of constraints Unary constraints involve a single variable, e.g., SA � = green Binary constraints involve pairs of variables, e.g., SA � = WA Higher-order constraints involve 3 or more variables, e.g., cryptarithmetic column constraints Preferences (soft constraints), e.g., red is better than green often representable by a cost for each variable assignment → constrained optimization problems AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 11
Example: Cryptarithmetic F T U W R O T W O + T W O F O U R C 3 C 1 C 2 (a) (b) (a) a substitution of digits for letters s.t. the resulting sum is arith- metically correct (b) constraint hypergraph with the squares for hypernodes Variables: F T U W R O C 1 C 2 C 3 Domains: { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } Constraints alldiff ( F, T, U, W, R, O ) (square at the top) O + O = R +10 · C 10 ( C i for carry digits, square at the mostright), etc. AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 12
Inference in CSPs CSPs • Search - choose new variable assignment for several possibilities • Inference - using the constraints to reduce the number of legal values for a variable, which in turn can reduce the legal values for another variable, and so on – called constraint propagation Constraint propagation may be intertwined with search may be done as a preprocessing step before search starts (Sometimes the preprocessing can solve the whole problem) AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 13
CSPs as search problems CSP as search: states are defined by the values assigned so far Initial state: the empty assignment {} , in which all variables are unassigned Action: a value can be assigned to any unassigned variable, pro- vided that it does not conflict with previously assigned variables Goal test: the current assignment is complete Path cost: a constant cost (say, 1) for every step Start with the straightforward, dumb approach, then fix it 1) This is the same for all CSPs 2) Every solution appears at depth n with n variables ⇒ use depth-first search 3) Path is irrelevant, so can also use complete-state formulation 4) b = ( n − ℓ ) d at depth ℓ , hence n ! d n leaves AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 14
Backtracking search Variable assignments are commutative, i.e., [ WA = red then NT = green ] same as [ NT = green then WA = red ] Only need to consider assignments to a single variable at each node ⇒ b = d and there are d n leaves Depth-first search for CSPs with single-variable assignments is called backtracking search Backtracking search is the basic uninformed algorithm for CSPs Can solve n -queens for n ≈ 25 AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 15
Backtracking search function Backtracking-Search ( csp ) returns solution/failure return Recursive-Backtracking ( { } , csp ) function Recursive-Backtracking ( assignment , csp ) returns soln/fail if assignment is complete then return assignment var ← Select-Unassigned-Variable ( csp ) for each value in Order-Domain-Values ( var , assignment , csp ) do if value is consistent with assignment then add { var = value } to assignment inferences ← Inference ( csp , var , value ) if inferences � = failure then add inferences to assignment result ← Recursive-Backtracking ( assignment , csp ) if result � = failure then return result remove { var = value } from assignment return failure AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 16
Backtracking example AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 17
Backtracking example AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 18
Backtracking example AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 19
Backtracking example AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 20
Improving backtracking efficiency General-purpose methods can give huge gains in speed 1. Which variable should be assigned next ( Select-Unassigned-Variable )? (heuristic: minimum remaining values) 2. In what order should its values be tried ( Order-Domain-Values )? (heuristic: least constraining value) 3. Can we detect inevitable failure early? What inferences should be performed at each step ( Inference )? (i.e., constraint propagation, e.g., forward checking) 4. Can we take advantage of problem structure? (graph theory) AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 21
Minimum remaining values var ← Select-Unassigned-Variable ( csp ) MRV (minimum remaining values, or most constrained variable) Choose the variable with the fewest legal values – “fail-first” (for pruning) heuristic, better than random ordering Doesn’t help in choosing the first variable (region to color) AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 22
Degree heuristic var ← Select-Unassigned-Variable ( csp ) DH (degree heuristic) Choose the variable with the most constraints on remaining vari- ables e.g., SA (blue) with highest degree 5 – choose the first variable to assign AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 23
Least constraining value Order-Domain-Values ( var , assignment , csp ) LCV (least constraining value) Given a variable, choose the least constraining value (the one that rules out the fewest values in the remaining vari- ables) – “fail-last” (for one solution) heuristic, irrelevant to all solutions Allows 1 value for SA Allows 0 values for SA Combining these heuristics makes 1000 queens feasible AI Slides (6e) c � Lin Zuoquan@PKU 2003-2020 4 24
Recommend
More recommend