Lecture 04 - CSP
Lecture 04 - CSP
International University
School of Computer Science and Engineering
2
Standard Search Problems
Previous search problems:
Problems can be solved by searching in a space
of states
state is a “black box” – any data structure that
supports successor function, heuristic function,
and goal test – problem-specific
3
Constraint Satisfaction Problems (CSP)
CSP is defined by 3 components (X, D, C):
state: a set of variables X, each Xi , with values from
domain Di
goal test: a set of constraints C, each Ci involves some
subset of the variables and specifies the allowable
combinations of values for that subset
Each constraint Ci consists of a pair <scope, rel>, where
scope is a tuple of variables and rel is the relation, either
represented explicitly or abstractly
X1 and X2 both have the domain {A, B}
Constraints:
<(X1, X2), [(A, B), (B, A)]>, or
<(X1, X2), X1 ≠ X2>
4
Solution
Each state in a CSP is defined by an assignment of
values to some or all of the variables
An assignment that does not violate any constraints
is called a consistent or legal assignment
A complete assignment is one in which every
variable is assigned
A solution to a CSP is consistent and complete
assignment
Allows useful general-purpose algorithms with more
power than standard search algorithms
5
Example: Map Coloring
6
Solution: Complete and Consistent Assignment
7
Constraint Graph
Constraint graph: nodes are variables, arcs are
constraints
Binary CSP: each constraint relates two variables
CSP conforms to a standard pattern
a set of variables with assigned values
generic successor function and goal test
generic heuristics
reduce complexity
8
CSP as a Search Problem
Initial state:
{} – all variables are unassigned
Successor function:
a value is assigned to one of the unassigned variables with
no conflict
Goal test:
a complete assignment
Path cost:
a constant cost for each step
Solution appears at depth n if there are n variables
Depth-first or local search methods work well
9
CSP Solvers Can be Faster
CSP solver can quickly eliminate large part of search
space
If {SA = blue}
Then 35=243 assignments can be reduced to
25=32 assignments, a reduction of 87%
10
Exercise
Consider the problem of Sudoku
puzzle: fitting digits into a grid.
Assume that a list of digits is
provided and that the task is to
fill in the blank squares using
any subset of the list. Formulate
this problem precisely in two
ways:
As a general search problem. Choose
an appropriate search algorithm.
As a constraint satisfaction problem.
Digits
11
Types of Variables
Discrete variables
finite domains:
n variables, domain size d => O (dn) complete assignments
e.g., Boolean CSPs, such as 3-SAT (NP-complete)
Worst case, can’t solve finite-domain CSPs in less than
exponential time
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
13
Types of Variables
14
Types 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
What Search Algorithm to Use?
Since we can formulate CSP problems as standard
search problems, we can apply search algorithms
from chapter 3 & 4
If breadth-first search were applied
branching factor? nd
tree size? nd * (n-1)d * … * d = n! * dn leaves
complete assignments? dn
17
Backtracking Search
Only need to consider assignments to a single
variable at each node => b = d and there are dn
leaves
18
Backtracking Search Algorithm
19
Backtracking Example
20
Backtracking Example
21
Backtracking Example
22
Backtracking Example
23
Improving Backtracking Efficiency
We can solve CSPs efficiently without
domain-specific knowledge, addressing the
following questions:
24
Variable and Value Ordering 1/3
Minimum remaining values (MRV)
choose the variable with the fewest
possible or “legal” values
also called most constrained variable or
fail-first heuristic
does it help in choosing the first variable?
25
Variable and Value Ordering 2/3
Degree heuristic:
selecting the variable that has the largest
number of constraints on other
unassigned variables
26
Variable and Value Ordering 3/3
Given a variable, choose the least
constraining value:
the one that rules out the fewest number of
values in the remaining variables
27
Propagating Information through Constraints
Forward checking:
Keep track of remaining legal values for unassigned
variables
Terminate search when any variable has no legal values
28
Propagating Information through Constraints
Forward checking:
Keep track of remaining legal values for unassigned
variables
Terminate search when any variable has no legal values
29
Propagating Information through Constraints
Forward checking:
Keep track of remaining legal values for unassigned
variables
Terminate search when any variable has no legal values
30
Propagating Information through Constraints
Forward checking:
Keep track of remaining legal values for unassigned
variables
Terminate search when any variable has no legal values
31
Propagating Information through Constraints
Forward checking propagates information from assigned to
unassigned variables, but doesn't provide early detection for
all failures:
32
Node Consistency
A single variable is node-consistent if all the
values in the variable’s domain satisfy the
variable’s unary constraints
33
Arc Consistency
Simplest form of propagation makes each arc consistent
An arc X (tail) =>Y (head) is consistent iff
for every value x of X there is some y in Y could be assigned without violating
a constraint
34
Arc Consistency
Simplest form of propagation makes each arc consistent
An arc X (tail) =>Y (head) is consistent iff
for every value x of X there is some y in Y could be assigned without violating
a constraint
35
Arc Consistency
Simplest form of propagation makes each arc consistent
An arc X (tail) =>Y (head) is consistent iff
for every value x of X there is some y in Y could be assigned without violating
a constraint
36
Arc Consistency
Simplest form of propagation makes each arc consistent
An arc X (tail) =>Y (head) is consistent iff
for every value x of X there is some y in Y could be assigned without violating
a constraint
37
AC-3 Algorithm
Time complexity O (n 2d 3)
38
Example
Use the AC-3 algorithm to show that arc consistency is
able to detect the inconsistency of the partial
assignment {WA = red, V = blue} in the following map
coloring problem.
39
One Possible Trace
One possible trace of the algorithm:
remove SA-WA, delete R from SA
remove SA-V, delete B from SA, leaving only G
remove NT-WA, delete R from NT
remove NT-SA, delete G from NT, leaving only B
remove NSW-SA, delete G from NSW
remove NSW-V, delete B from NSW, leaving only R
remove Q-NT, delete B from Q
remove Q-SA, delete G from Q
remove Q-NSW, delete R from Q, leaving no proper assignment
for Q
40
Path Consistency
Consider map-coloring with only two colors
Every arc is consistent initially
Check the set {WA, SA} path consistenty
with respect to NT
41
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
42
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
43
8-Queen Example
44
Min-Conflicts Algorithms for CSP
45
Summary
CSPs are a special kind of problem:
states defined by values of a fixed set of variables
goal test defined by constraints on variable values
46