Constraint Satisfaction Problems Chapter 5 Section 1 3
Constraint Satisfaction Problems Chapter 5 Section 1 – 3 2021/2/23 1
Outline l l l Constraint Satisfaction Problems (CSP) Backtracking search for CSPs Local search for CSPs 2021/2/23 2
Constraint satisfaction problems (CSPs) l Standard search problem: l l l state is a "black box“ – any data structure that supports successor function, heuristic function, and goal test CSP: l state is defined by variables Xi with values from domain Di l goal test is a set of constraints specifying allowable combinations of values for subsets of variables Simple example of a formal representation language 2021/2/23 3
Example: Map-Coloring l l Variables WA, NT, Q, NSW, V, SA, T Domains Di = {red, green, blue} Constraints: adjacent regions must have different colors e. g. , WA ≠ NT, or (WA, NT) in {(red, green), (red, blue), (green, red), (green, blue), (blue, red), (blue, green)} 2021/2/23 4
Example: Map-Coloring l Solutions are complete and consistent assignments, e. g. , WA = red, NT = green, Q = red, NSW = green, V = red, SA = blue, T = green 2021/2/23 5
Constraint graph l Binary CSP: each constraint relates two variables l Constraint graph: nodes are variables, arcs are constraints 2021/2/23 6
Varieties of CSPs l Discrete variables l finite domains: l l l infinite domains: l l n variables, domain size d O(dn) complete assignments e. g. , Boolean CSPs, incl. ~Boolean satisfiability (NP-complete) integers, strings, etc. e. g. , job scheduling, variables are start/end days for each job need a constraint language, e. g. , Start. Job 1 + 5 ≤ Start. Job 3 Continuous variables l l 2021/2/23 e. g. , start/end times for Hubble Space Telescope observations linear constraints solvable in polynomial time by linear programming 7
Varieties of constraints l Unary constraints involve a single variable, l l Binary constraints involve pairs of variables, l l e. g. , SA ≠ green e. g. , SA ≠ WA Higher-order constraints involve 3 or more variables, l 2021/2/23 e. g. , cryptarithmetic column constraints 8
Example: Cryptarithmetic l l l Variables: F T U W R O X 1 X 2 X 3 Domains: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} Constraints: Alldiff (F, T, U, W, R, O) l O + O = R + 10 · X 1 l X 1 + W = U + 10 · X 2 2021/2/23 l l X 2 + T = O + 10 · X 3 X = F, T ≠ 0, F ≠ 0 9
Real-world CSPs l Assignment problems l l e. g. , who teaches what class Timetabling problems l e. g. , which class is offered when and where? l Transportation scheduling l Factory scheduling Notice that many real-world problems involve realvalued variables 2021/2/23 l 10
Standard search formulation (incremental) Let's start with the straightforward approach, then fix it States are defined by the values assigned so far l l Initial state: the empty assignment { } Successor function: assign a value to an unassigned variable that does not conflict with current assignment fail if no legal assignments l Goal test: the current assignment is complete 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 2021/2/23 4. b = (n - l )d at depth l, hence n! · dn leaves 1. 11
Backtracking search Variable assignments are commutative}, i. e. , [ WA = red then NT = green ] same as [ NT = green then WA = red ] l l Only need to consider assignments to a single variable at each node b = d and there are $d^n$ leaves l Depth-first search for CSPs with single-variable assignments is called backtracking search l Backtracking search is the basic uninformed algorithm for CSPs l Can solve n-queens for n ≈ 25 2021/2/23 12
Backtracking search 2021/2/23 13
Backtracking example 2021/2/23 14
Backtracking example 2021/2/23 15
Backtracking example 2021/2/23 16
Backtracking example 2021/2/23 17
Improving backtracking efficiency l General-purpose methods can give huge gains in speed: l Which variable should be assigned next? l In what order should its values be tried? l Can we detect inevitable failure early? 2021/2/23 18
Most constrained variable l Most constrained variable: choose the variable with the fewest legal values l a. k. a. minimum remaining values (MRV) heuristic 2021/2/23 19
Most constraining variable l l Tie-breaker among most constrained variables Most constraining variable: l 2021/2/23 choose the variable with the most constraints on remaining variables 20
Least constraining value l Given a variable, choose the least constraining value: l l the one that rules out the fewest values in the remaining variables Combining these heuristics makes 1000 2021/2/23 21
Fig 5. 6 Forward checking l Idea: l Keep track of remaining legal values for unassigned variables l Terminate search when any variable has no legal values 2021/2/23 22
Forward checking l Idea: l Keep track of remaining legal values for unassigned variables l Terminate search when any variable has no legal values 2021/2/23 23
Forward checking l Idea: l Keep track of remaining legal values for unassigned variables l Terminate search when any variable has no legal values 2021/2/23 24
Forward checking l Idea: l Keep track of remaining legal values for unassigned variables l Terminate search when any variable has no legal values 2021/2/23 25
Constraint propagation l Forward checking propagates information from assigned to unassigned variables, but doesn't provide early detection for all failures: l NT and SA cannot both be blue! l Constraint propagation repeatedly enforces constraints locally 2021/2/23 26
Arc consistency l l Simplest form of propagation makes each arc consistent X Y is consistent iff for every value x of X there is some allowed y 2021/2/23 27
Arc consistency l l Simplest form of propagation makes each arc consistent X Y is consistent iff for every value x of X there is some allowed y 2021/2/23 28
Arc consistency l l Simplest form of propagation makes each arc consistent X Y is consistent iff for every value x of X there is some allowed y l If X loses a value, neighbors of X need to be rechecked 2021/2/23 29
Arc consistency l l Simplest form of propagation makes each arc consistent X Y is consistent iff for every value x of X there is some allowed y l l If X loses a value, neighbors of X need to be rechecked Arc consistency detects failure earlier than forward checking 2021/2/23 l Can be run as a preprocessor or after each assignment 30
Arc consistency algorithm AC-3 l Time complexity: O(n 2 d 3) 2021/2/23 31
Local search for CSPs l Hill-climbing, simulated annealing typically work with "complete" states, i. e. , all variables assigned l To apply to CSPs: l allow states with unsatisfied constraints l operators reassign variable values l Variable selection: randomly select any conflicted variable l Value selection by min-conflicts heuristic: 2021/2/23 32
Example: 4 -Queens l States: 4 queens in 4 columns (44 = 256 states) l Actions: move queen in column l Goal test: no attacks l Evaluation: h(n) = number of attacks Given random initial state, can solve n-queens in almost constant time for arbitrary n with high probability (e. g. , n = 10, 000) 33 2021/2/23 l
Summary l CSPs are a special kind of problem: l states defined by values of a fixed set of variables l goal test defined by constraints on variable values l Backtracking = depth-first search with one variable assigned per node l Variable ordering and value selection heuristics help significantly l Forward checking prevents assignments that guarantee later failure Constraint propagation (e. g. , arc consistency) does additional work to constrain values and detect inconsistencies 2021/2/23 l 34
- Slides: 34