Lec4 CSP
Lec4 CSP
In which we see how treating states as more than just little black
boxes leads to the invention of a range of powerful new search
methods and a deeper understanding of problem structure and
complexity.
1
Outline
• Constraint Satisfaction Problems (CSP)
• Backtracking search for CSPs
• AC-3 algorithm
• Local search for CSPs
2
What is Search For?
• Planning: sequences of actions
– The path to the goal is the important thing
– Paths have various costs, depths
– Heuristics give problem-specific guidance
• Identification: assignments to variables
– The goal itself is important, not the path
– All paths at the same depth (for some formulations)
• CSP is a special class of search problems
– Mostly identification problems
– Have specialized algorithms for them
3
State Representation
B C
B C
(a) Atomic (b) Factored (c) Structured
4
Constraint Satisfaction Problems (CSPs)
• Standard search problem:
– state is a “black box” – any data structure that supports successor function,
heuristic function, and goal test
• CSP:
– state is defined by variables Xi (i=1,…,n) with values from domain Di
– goal test is a set of constraints Ci (i=1,…,m) specifying allowable
combinations of values for subsets of variables
5
Example: Map-Coloring
6
Example: Map-Coloring
7
Constraint graph
• Binary CSP: each constraint relates two variables
8
CSP: Incremental Formulation
• Initial state: the empty assignment {}, in which all variables are
unassigned.
• Successor function: a value can be assigned to any unassigned
variable, provided that it does not conflict with previously
assigned variables.
• Goal test: the current assignment is complete.
• Path cost: a constant cost (e.g., 1) for every step.
9
Example: N-Queens
• Formulation 1:
– Variables:
– Domains:
– Constraints
10
Example: N-Queens
• Formulation 2:
– Variables:
– Domains:
– Constraints: Implicit:
Explicit:
11
Example: Cryptarithmetic
• Variables: F T U W R O X1 X2 X3
• Domains: {0,1,2,3,4,5,6,7,8,9}
12
Example: Sudoku
13
Varieties of CSPs
• Discrete variables
– finite domains:
• n variables, domain size d → O(dn) complete assignments
– infinite domains:
• integers, strings, etc.
• e.g., job scheduling, variables are start/end days for each job
• need a constraint language, e.g., StartJob1 + 5 ≤ StartJob3
• Continuous variables
– e.g., start/end times for Hubble Space Telescope observations
– linear constraints solvable in polynomial time by linear
programming
14
Varieties of constraints
• Unary constraints involve a single variable,
– e.g., SA ≠ green
15
Real-world CSPs
• Assignment problems
– e.g., who teaches what class
• Timetabling problems
– e.g., which class is offered when and where?
• Transportation scheduling
• Factory scheduling
16
Standard search formulation (incremental)
18
Backtracking Search
• Backtracking search = DFS + two improvements
19
Backtracking Search
https://inst.eecs.berkeley.edu/~cs188/fa19/assets/demos/csp/csp_demos.html
20
Backtracking Search
21
Backtracking Search
22
Backtracking Search
23
Backtracking example
24
Backtracking example
25
Backtracking example
26
Backtracking example
27
Improving backtracking efficiency
• Consider the order of variables to be assigned
and their values to be tried
– Which variable should be assigned next?
– In what order should its values be tried?
– Can we detect inevitable failure early?
28
Which variable should be
assigned next?
29
Which variable should be assigned next?
• Most constrained variable:
choose the variable with the fewest legal values
– a.k.a. minimum remaining values (MRV) heuristic
• Most constraining variable (Tie-breaker):
– choose the variable with the most constraints on
remaining variables
– the degree heuristic
30
Least constraining value
• Given a variable, choose the least constraining
value:
– The one that rules out the fewest values in the
remaining variables
31
Interleaving search and inference
• Inference in CSPs
• In regular state-space search, algorithms only
do search. In CSPs, algorithms can search and
inference
– Search: choose a new variable assignment from
several possibilities
– Inference (a specific type): constraint propagation
• Constraint propagation may be intertwined
with search, or it may be done as a
preprocessing step, before search starts.
32
Forward checking
(Interleaving search and inference)
• Idea:
– Keep track of remaining legal values for unassigned
variables
– Terminate search when any variable has no legal values
NT
Q
WA
SA
NSW
V
T
33
Forward checking
• Idea:
– Keep track of remaining legal values for unassigned
variables
– Terminate search when any variable has no legal values
34
Forward checking
• Idea:
– Keep track of remaining legal values for unassigned
variables
– Terminate search when any variable has no legal values
35
Forward checking
• Idea:
– Keep track of remaining legal values for unassigned
variables
– Terminate search when any variable has no legal values
36
Constraint propagation
• Forward checking propagates information from assigned to
unassigned variables, but doesn't provide early detection for all
failures:
37
Limitation of forward checking
38
Arc consistency
• Simplest form of propagation: making each arc consistent
• X →Y is consistent iff
for every value x of X there is some allowed y
39
Consistency of A Single Arc
40
Consistency of A Single Arc
41
Arc consistency
• Simplest form of propagation: making each arc consistent
• X →Y is consistent iff
for every value x of X there is some allowed y
NSW → SA
42
Arc consistency
• Simplest form of propagation: making each arc consistent
• X →Y is consistent iff
for every value x of X there is some allowed y
V → NSW
43
Arc consistency
• Simplest form of propagation: making each arc consistent
• X →Y is consistent iff
for every value x of X there is some allowed y
• If X loses a value, neighbors of X need to be rechecked
• Arc consistency detects failure earlier than forward checking
• Can be run as a preprocessor or after each assignment
SA → NT
44
How to Enforce Arc Consistency
of Entire CSP
• A simplistic algorithm: Cycle over the pairs of variables,
enforcing arc-consistency, repeating the cycle until no domains
change for a whole cycle
• AC-3 (short for Arc Consistency Algorithm #3): A more efficient
algorithm ignoring constraints that have not been modified since
they were last analyzed
NT
Q
WA
SA
NSW
V
T
45
AC-3: Enforce Arc Consistency
of Entire CSP
Constraint Propagation!
46
AC-3: Enforce Arc Consistency
of Entire CSP
Queue:
NT
Q
SA->WA
WA
SA
NT->WA
NSW
V
T
47
AC-3: Enforce Arc Consistency of
Entire CSP
Queue:
NT
Q
NT->WA
WA
SA
WA->SA
NSW NT->SA
V Q->SA
T
NSW->SA
V->SA
48
AC-3: Enforce Arc Consistency of
Entire CSP
Queue:
NT
Q
WA->SA
WA
SA
NT->SA
NSW Q->SA
V NSW->SA
T
V->SA
WA->NT
SA->NT
Q->NT
49
AC-3: Enforce Arc Consistency of
Entire CSP
Queue:
NT
Q
WA->SA
WA
SA
NT->SA
NSW Q->SA
V NSW->SA
T
V->SA
WA->NT
SA->NT
Q->NT
50
AC-3: Enforce Arc Consistency of
Entire CSP
Queue:
NT
Q
NT->SA
WA
SA
Q->SA
NSW NSW->SA
V V->SA
T
WA->NT
SA->NT
Q->NT
51
AC-3: Enforce Arc Consistency of
Entire CSP
Queue:
NT
Q
Q->SA
WA
SA
NSW->SA
NSW V->SA
V WA->NT
T
SA->NT
Q->NT
52
AC-3: Enforce Arc Consistency of
Entire CSP
Queue:
NT
Q
NSW->SA
WA
SA
V->SA
NSW WA->NT
V SA->NT
T
Q->NT
53
AC-3: Enforce Arc Consistency of
Entire CSP
Queue:
NT
Q
WA
SA
NSW
V
T
54
After assigning Q to Green,
what gets added to the Queue?
Queue:
NT
Q
WA
SA
NSW
V
T
55
AC-3: Enforce Arc Consistency
of Entire CSP
Queue:
NT
Q
NT->Q
WA
SA
SA->Q
NSW NSW->Q
V
T
56
AC-3: Enforce Arc Consistency
of Entire CSP
Queue:
NT
Q
SA->Q
WA
SA
NSW->Q
NSW WA->NT
V SA->NT
T
Q->NT
57
AC-3: Enforce Arc Consistency
of Entire CSP
Queue:
NT
Q
NSW->Q
WA
SA
WA->NT
NSW SA->NT
V Q->NT
T
WA->SA
NT->SA
Q->SA
NSW->SA
V->SA
58
AC-3: Enforce Arc Consistency
of Entire CSP
Queue:
WA->NT
NT
SA->NT
WA
Q Q->NT
SA
NSW WA->SA
V NT->SA
T Q->SA
NSW->SA
V->SA
V->NSW
Q->NSW
SA->NSW
59
AC-3: Enforce Arc Consistency
of Entire CSP
Queue:
WA->NT
NT
SA->NT
WA
Q Q->NT
SA
NSW WA->SA
V NT->SA
T Q->SA
NSW->SA
V->SA
V->NSW
Q->NSW
SA->NSW
60
AC-3: Enforce Arc Consistency
of Entire CSP
Queue:
SA->NT
NT
Q->NT
WA
Q WA->SA
SA
NSW NT->SA
V Q->SA
NSW->SA
!!!
T
V->SA
V->NSW
Q->NSW
SA->NSW
61
AC-3: Enforce Arc Consistency
of Entire CSP
Queue:
SA->NT
Q->NT
NT
Q
WA->SA
WA
SA
NT->SA
NSW
Q->SA
V
NSW->SA
V->SA
!!!
T
V->NSW
Q->NSW
SA->NSW
• Backtrack on the assignment of Q
• Arc consistency detects failure earlier than forward checking
• Can be run as a preprocessor or after each assignment
• What’s the downside of enforcing arc consistency?
62
Limitations of Arc Consistency
• After enforcing arc consistency:
– Can have one solution left
– Can have multiple solutions left
– Can have no solutions left (and
not know it)
• Arc consistency only checks local
consistency conditions
• Arc consistency still runs inside a What went
wrong here?
backtracking search!
63
Backtracking Search with AC-3
Recall that the whole backtracking algorithm with AC-3 will call AC-3 many times
65
Complexity of a single run of AC-3
66
Complexity of a single run of AC-3
67
Complexity of a single run of AC-3
68
Local search for CSPs
• Hill-climbing, simulated annealing typically work
with "complete" states, i.e., all variables assigned
• To apply to CSPs:
– allow states with unsatisfied constraints
– operators reassign variable values
• Variable selection: randomly select any conflicted
variable
• Value selection by min-conflicts heuristic:
– choose value that violates the fewest constraints
– i.e., hill-climb with h(n) = total number of violated
constraints
69
Example: 4-Queens
• States: 4 queens in 4 columns (44 = 256 states)
• Actions: move queen in column
• Goal test: no attacks
• 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,000)
70
Summary
• CSPs are a special kind of problem:
Credit:
15-281, Artificial Intelligence: Representation and Problem Solving, CMU
71