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