Constraint Satisfaction Problems (CSPs)
Introduction and Backtracking Search
This lecture:
CSP Introduction and Backtracking Search
Chapter 6.1 – 6.4, except 6.3.3
Next lecture:
CSP Constraint Propagation & Local Search
Chapter 6.1 – 6.4, except 6.3.3
(Please read lecture topic material before
and after each lecture on that topic)
You Will Be Expected to Know
• Basic definitions (section 6.1)
– What is a CSP?
• Backtracking search for CSPs (6.3)
• Variable ordering or selection (6.3.1)
– Minimum Remaining Values (MRV) heuristic
– Degree Heuristic (DH) (to unassigned variables)
• Value ordering or selection (6.3.1)
– Least constraining value (LCV) heuristic
Constraint Satisfaction Problems
• What is a CSP?
– Finite set of variables X1, X2, …, Xn
– Nonempty domain of possible values for each variable
D1, D2, …, Dn
– Finite set of constraints C1, C2, …, Cm
• Each constraint Ci limits the values that variables can take,
• e.g., X1 ≠ X2
– Each constraint Ci is a pair <scope, relation>
• Scope = Tuple of variables that participate in the constraint.
• Relation = List of allowed combinations of variable values.
May be an explicit list of allowed combinations.
May be an abstract relation allowing membership testing and listing.
• CSP benefits
– Standard representation pattern
– Generic goal and successor functions
– Generic heuristics (no domain specific expertise).
Sudoku as a Constraint Satisfaction Problem
(CSP) 1 2 3 4 5 6
9
7 8
A
B
• Variables: 81 variables C
D
– A1, A2, A3, …, I7, I8, I9 E
F
– Letters index rows, top to bottom
G
– Digits index columns, left to right H
I
• Domains: The nine positive digits
– A1 {1, 2, 3, 4, 5, 6, 7, 8, 9}
– Etc.; all domains of all variables are {1,2,3,4,5,6,7,8,9}
• Constraints: 27 Alldiff constraints
– Alldiff(A1, A2, A3, A4, A5, A6, A7, A8, A9)
– Etc.; all rows, columns, and blocks contain all different digits
CSPs --- What is a solution?
• A state is an assignment of values to some or all variables.
– An assignment is complete when every variable has an assigned value.
– An assignment is partial when one or more variables have no assigned value.
• Consistent assignment:
– An assignment that does not violate the constraints.
• A solution to a CSP is a complete and consistent assignment.
– All variables are assigned, and none of the assignments violate the constraints.
• CSPs may require a solution that maximizes an objective function.
– For simple linear cases , an optimal solution can be obtained by Linear Programming.
• Examples of Applications:
– Scheduling the time of observations on the Hubble Space Telescope
– Airline schedules
– Cryptography
– Computer vision, image interpretation
CSP example: Map coloring problem
• Variables: WA, NT, Q, NSW, V, SA, T
• Domains: Di = {red,green,blue}
• Constraints: adjacent regions must have different colors.
• E.g. WA NT
CSP example: Map coloring solution
• A solution is:
– A complete and consistent assignment.
– All variables assigned, all constraints satisfied.
• E.g., {WA=red, NT=green, Q=red, NSW=green,
V=red, SA=blue, T=green}
Graph coloring
• More general problem than map coloring
• Planar graph = graph in the 2d-plane with no
edge crossings
• Guthrie’s conjecture (1852)
Every planar graph can be colored with 4 colors or less
– Proved (using a computer) in 1977 (Appel and Haken)
Constraint graphs
• Constraint graph:
• nodes are variables
• arcs are binary constraints
• Graph can be used to simplify search
e.g. Tasmania is an independent subproblem
(will return to graph structure later)
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.
– Professors A, B,and C cannot be on a committee together
– Can always be represented by multiple binary constraints
• Preference (soft constraints)
– e.g. red is better than green often can be represented by a cost for each
variable assignment
– combination of optimization with CSPs
Simplify: We restrict attention to
• Discrete and finite domains
– Variables have a discrete, finite set of values
• No objective function
– Any complete and consistent solution is OK
• Solution
– Find a complete and consistent assignment
• Example: Sudoku puzzles.
CSP Example: Cryptharithmetic puzzle
CSP Example: Cryptharithmetic puzzle
CSP Example: Cryptharithmetic puzzle
A Solution:
F=1, T=7, U=6, W=3, R=8, O=4,
X1=0, X2=0, X3=1
734
+734
1468
CSP Example: Cryptharithmetic puzzle
• Try it yourself at home:
S E N D
+ M O R E
M O N E Y
Backtracking search
• Similar to Depth-first search
– At each level, picks a single variable to explore
– Iterates over the domain values of that variable
• Generates kids one at a time, one per value
• Backtracks when a variable has no legal values
left
• Uninformed algorithm
– No good general performance
Backtracking search
function BACKTRACKING-SEARCH(csp) return 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-BACTRACKING(assignment, csp)
if result failure then return result
remove {var=value} from assignment
return failure
•
Backtracking search
Expand deepest unexpanded node
• Generate only one child at a time.
• Goal-Test when inserted.
– For CSP, Goal-test at bottom
Future= green dotted circles
Frontier=white nodes
Expanded/active=gray nodes
Forgotten/reclaimed= black nodes
19
Backtracking search
• Expand deepest unexpanded node
• Generate only one child at a time.
• Goal-Test when inserted.
– For CSP, Goal-test at bottom
Future= green dotted circles
Frontier=white nodes
Expanded/active=gray nodes
Forgotten/reclaimed= black nodes
20
Backtracking search
• Expand deepest unexpanded node
• Generate only one child at a time.
• Goal-Test when inserted.
– For CSP, Goal-test at bottom
Future= green dotted circles
Frontier=white nodes
Expanded/active=gray nodes
Forgotten/reclaimed= black nodes
21
Backtracking search
• Expand deepest unexpanded node
• Generate only one child at a time.
• Goal-Test when inserted.
– For CSP, Goal-test at bottom
Future= green dotted circles
Frontier=white nodes
Expanded/active=gray nodes
Forgotten/reclaimed= black nodes
22
Backtracking search
• Expand deepest unexpanded node
• Generate only one child at a time.
• Goal-Test when inserted.
– For CSP, Goal-test at bottom
Future= green dotted circles
Frontier=white nodes
Expanded/active=gray nodes
Forgotten/reclaimed= black nodes
23
Backtracking search
• Expand deepest unexpanded node
• Generate only one child at a time.
• Goal-Test when inserted.
– For CSP, Goal-test at bottom
Future= green dotted circles
Frontier=white nodes
Expanded/active=gray nodes
Forgotten/reclaimed= black nodes
24
Backtracking search
• Expand deepest unexpanded node
• Generate only one child at a time.
• Goal-Test when inserted.
– For CSP, Goal-test at bottom
Future= green dotted circles
Frontier=white nodes
Expanded/active=gray nodes
Forgotten/reclaimed= black nodes
25
Backtracking search
• Expand deepest unexpanded node
• Generate only one child at a time.
• Goal-Test when inserted.
– For CSP, Goal-test at bottom
Future= green dotted circles
Frontier=white nodes
Expanded/active=gray nodes
Forgotten/reclaimed= black nodes
26
Backtracking search
• Expand deepest unexpanded node
• Generate only one child at a time.
• Goal-Test when inserted.
– For CSP, Goal-test at bottom
Future= green dotted circles
Frontier=white nodes
Expanded/active=gray nodes
Forgotten/reclaimed= black nodes
27
Backtracking search
• Expand deepest unexpanded node
• Generate only one child at a time.
• Goal-Test when inserted.
– For CSP, Goal-test at bottom
Future= green dotted circles
Frontier=white nodes
Expanded/active=gray nodes
Forgotten/reclaimed= black nodes
28
Backtracking search
• Expand deepest unexpanded node
• Generate only one child at a time.
• Goal-Test when inserted.
– For CSP, Goal-test at bottom
Future= green dotted circles
Frontier=white nodes
Expanded/active=gray nodes
Forgotten/reclaimed= black nodes
29
Backtracking search
• Expand deepest unexpanded node
• Generate only one child at a time.
• Goal-Test when inserted.
– For CSP, Goal-test at bottom
Future= green dotted circles
Frontier=white nodes
Expanded/active=gray nodes
Forgotten/reclaimed= black nodes
30