3. Constraint Satisfaction Problems
Standard search problem
State is a "black box“ – any data structure that supports successor function, heuristic
function, and goal test.
Constraint Satisfaction Problem
State is defined by variables Xi with values from domain Di.
Goal test is a set of constraints specifying allowable combinations of values for subsets
of variables.
A CSP is defined by
a set of variables
a domain of values for each variable
a set of constraints between variables.
A solution is an assignment of a value to each variable that satisfies the constraints.
3
4. Constraint Satisfaction Problems
4
Variables : WA, NT, Q, NSW, V, SA, T
Domains : Di = {red,green,blue}
Constraints : adjacent regions must have different colors
e.g., WA ≠ NT
Example: Map Coloring
5. Constraint Satisfaction Problems
5
Example: Map Coloring
Solutions are complete and consistent
assignments.
e.g., WA = red, NT = green, Q = red,
NSW = green, V = red, SA = blue, T = green
A state may be incomplete.
e.g., just WA=red
6. Constraint Satisfaction Problems
6
Binary CSP : each constraint relates two variables
Constraint graph : nodes are variables, arcs are
constraints.
7. Constraint Satisfaction Problems
7
Varieties of CSPs
Discrete variables
Finite Domains :
n variables, domain size d O(dn) 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., StartJob1 + 5 ≤ StartJob3
Continuous variables
e.g., start/end times for Hubble Space Telescope observations
linear constraints solvable in polynomial time by linear programming
8. Constraint Satisfaction Problems
8
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
9. Backtracking Search For CSPs
9
Standard Search Formulation
Initial state : none of the variables has a value.
Successor State : one of the variables without a value will get some value.
Goal : all variables have a value and none of the constraints is violated.
10. Backtracking Search For CSPs
10
Backtracking Search
Every solution appears at depth n with n variables use depth-first search.
Depth-first search for CSPs with single-variable assignments is called backtracking search.
Backtracking search is the basic uninformed algorithm for CSPs.
It can solve n-queens for n ≈ 25.
Backtracking (Depth-First ) Search
Special property of CSPs (commutative): the order in which we assign variables does not
matter. (NT=green, WA=blue) = (WA=blue, NT=green)
Better search tree: First order variables, then assign them values one by one.
11. Backtracking Search For CSPs
11
A simple Backtracking Algorithm for Constraint Satisfaction Problems.
function BACKTRACKING-SEARCH (csp) returns a solution, or failure
return RECURSIVE-BACKTRACKING ({}, csp)
function RECURSIVE-BACKTRACKING (assignment, csp) return a solution or failure
if assignment is complete then return assignment
var SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp], assignment, csp)
for each value in ORDER-DOMAIN-VALUES (var, assignment, csp) do
if value is consistent with assignment according to CONSTRAINTS[csp] then
add {var = value} to assignment
result RECURSIVE-BACKTRACING (assignment, csp)
if result failure then return result
remove {var = value} from assignment
return failure
13. Backtracking Search For CSPs
13
Depth First Search (DFS)
Given the following state space (tree search), give the sequence of visited nodes when using DFS (assume that the
node 0 is the goal state).
A
B C D E
F G H I J
K L M N
O
14. Backtracking Search For CSPs
14
Depth First Search (DFS)
A,
A
B C D E
A,B,
A
B C D E
F G
15. Backtracking Search For CSPs
15
Depth First Search (DFS)
A, B, F,
A
B C D E
F G
A, B, F,
G,
A
B C D E
F G
K L
16. Backtracking Search For CSPs
16
Depth First Search (DFS)
A, B, F
G, K,
A
B C D E
F G
K L
A, B, F
G, K,
L, O: Goal State
A
B C D E
F G
K L
O
17. Backtracking Search For CSPs
17
Depth First Search (DFS)
The returned solution is the sequence of operators in the path:
A, B, G, L, O : assignments when using CSP.
A
B C D E
F G
K L
O
18. Backtracking Search For CSPs
18
Example 1 of a CSP
A
B
C
D
E
F
G
H
{red, green, blue}
{red, green, blue}
{red, green, blue}
{red, green, blue}
{red, green, blue}
{red, green, blue}
{red, green, blue}
{red, green, blue}
1 = red
2 = blue
3 = green
19. Backtracking Search For CSPs
19
Example 1 of a CSP
A B
C
D
E
F
G
H
1 = red
2 = blue
3 = green
A B
C
D
E
F
G
H
A B
C
D
E
F
G
H
20. Backtracking Search For CSPs
20
Example 1 of a CSP
A
B
C
D
E
F
G
H
1 = red
2 = blue
3 = green
A
B
C
D
E
F
G
H
21. Backtracking Search For CSPs
21
Example 1 of a CSP
1 = red
2 = blue
3 = green
A
B
C
D
E
F
G
H
A
B
C
D
E
F
G
H
Solution!
22. Backtracking Search For CSPs
22
Example 2 of a CSP
1 = red
2 = blue
3 = green
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
23. Backtracking Search For CSPs
23
Example 2 of a CSP
1 = red
2 = blue
3 = green
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
24. Backtracking Search For CSPs
24
Example 2 of a CSP
1 = red
2 = blue
3 = green
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
Dead End Backtrack
25. Backtracking Search For CSPs
25
Example 2 of a CSP
1 = red
2 = blue
3 = green
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
26. Backtracking Search For CSPs
26
Example 2 of a CSP
1 = red
2 = blue
3 = green
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
Solution!
27. Backtracking Search For CSPs
27
Imporving Backtracing Efficiency
General-purpose methods can give huge gains in speed:
Which variable should be assigned next?
In what order should its values be tried?
Can we detect inevitable failure early?
Minimum Remaining Values (MRV)
Most constrained variable
Choose the variable with the fewest legal values.
Called minimum remaining values (MRV) heuristic
Picks a variable which will cause failure as soon as possible, allowing the tree to be pruned.
28. Backtracking Search For CSPs
28
Backpropagation-MRV (Mininum Remaining Value)
Choose the variable with the fewest legal values.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
29. Backtracking Search For CSPs
29
Backpropagation-MRV (Mininum Remaining Value)
Choose the variable with the fewest legal values.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
30. Backtracking Search For CSPs
30
Backpropagation-MRV (Mininum Remaining Value)
Choose the variable with the fewest legal values.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
Solution!
31. Backtracking Search For CSPs
31
Most Constraining Variable (MCV)
Tie-breaker among most constrained variables.
Most constraining variable:
Choose the variable with the most constraints on remaining variables (most edges in graph).
32. Backtracking Search For CSPs
32
Backpropagation-MCV (Most Constrainted Variable)
Choose the variable with the most constraints on remaining variables.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs {R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
33. Backtracking Search For CSPs
33
Backpropagation-MCV (Most Constrainted Variable)
Choose the variable with the most constraints on remaining variables.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
34. Backtracking Search For CSPs
34
Backpropagation-MCV (Most Constrainted Variable)
Choose the variable with the most constraints on remaining variables.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
Dead End
35. Backtracking Search For CSPs
35
Backpropagation-MCV (Most Constrainted Variable)
Choose the variable with the most constraints on remaining variables.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
36. Backtracking Search For CSPs
36
Backpropagation-MCV (Most Constrainted Variable)
Choose the variable with the most constraints on remaining variables.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
Dead End
37. Backtracking Search For CSPs
37
Backpropagation-MCV (Most Constrainted Variable)
Choose the variable with the most constraints on remaining variables.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
38. Backtracking Search For CSPs
38
Backpropagation-MCV (Most Constrainted Variable)
Choose the variable with the most constraints on remaining variables.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
Solution!
39. Backtracking Search For CSPs
39
Least Constraining Value (LCV)
Given a variable, choose the least constraining value:
The one that rules out (eliminate) the values
Combining these heuristics makes 1000 queens feasible.
40. Backtracking Search For CSPs
40
Backpropagation LCV
Choose the value which eliminates the fewest number of values in the remaining variables.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
41. Backtracking Search For CSPs
41
Backpropagation LCV
Choose the value which eliminates the fewest number of values in the remaining variables.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
42. Backtracking Search For CSPs
42
Backpropagation LCV
Choose the value which eliminates the fewest number of values in the remaining variables.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
Dead End
43. Backtracking Search For CSPs
43
Backpropagation LCV
Choose the value which eliminates the fewest number of values in the remaining variables.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
44. Backtracking Search For CSPs
44
Backpropagation LCV
Choose the value which eliminates the fewest number of values in the remaining variables.
{R,B,G}
{R,B,G}
{R,B,G} {R,B,G}
{R}
2 arcs
4 arcs
3 arcs
3 arcs
2 arcs
Solution!
45. Backtracking Search For CSPs
45
Forward Checking
Assigns variable X, say
Looks at each unassigned variable, Y say, connected to X and delete from Y’s domain any value
inconsistent with X’s assignment.
Eliminates branching on certain variables by propagating information.
If forward checking detects a dead end, algorithm will backtrack immediately.
Idea :
Keep track of remaining legal values for unassigned variables.
Terminate search when any variable has no legal values.