05 CSP
05 CSP
G. Lakemeyer
Northern
Territory
Queensland
Western
© G. Lakemeyer
Australia
South
Australia New
South
Wales
Victoria
Tasmania
AI/WS-2024/25 2 / 20
Sudoku
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
A 9 A 9 7 2 8 5 3 6 1 4
B 9 1 2 7 3 B 6 5 8 4 9 1 2 7 3
C 1 C 1 3 4 7 6 2 9 5 8
© G. Lakemeyer
D 4 5 7 6 D 4 8 5 2 7 6 3 9 1
E 7 8 E 3 9 7 5 1 8 4 2 6
F 6 3 5 F 2 1 6 9 3 4 5 8 7
G 5 7 G 8 6 9 1 4 5 7 3 2
H 3 6 H 5 2 3 6 8 7 1 4 9
I 7 4 1 2 9 I 7 4 1 3 2 9 8 6 5
AI/WS-2024/25 3 / 20
What is a Constraint Satisfaction Problem (CSP)?
A CSP is defined as a tuple ⟨X , D , C ⟩, where
X = {X1 , . . . , Xn } is a set of variables;
D = {D1 , . . . , Dn } is a set of domains, one for each variable;
C is a set of constraints that specify allowable combinations of values.
A constraint has the form ⟨scope, rel⟩, where scope is a tuple of variables and
rel a relation over those variables. Rel can be given explicitly or as a function
© G. Lakemeyer
Example Constraint
Let X1 and X2 have domain {1, 2, 3} The constraint that X1 must be bigger
than X2 can be given as
⟨(X1 , X2 ), {(3, 1), (3, 2), (2, 1)}⟩ or simply ⟨(X1 , X2 ), X1 > X2 ⟩
AI/WS-2024/25 5 / 20
3-coloring of Australia
Northern
Territory
Queensland
Western
3CA = ⟨X , D , C ⟩ with Australia
South
Australia New
C = {SA ̸= WA, SA ̸= NT , SA ̸= Q ,
SA ̸= NSW , SA ̸= V , WA ̸= NT ,
NT ̸= Q , Q ̸= NSW , NSW ̸= V }
© G. Lakemeyer
SA NSW
AI/WS-2024/25 T 6 / 20
More on Constraint Graphs
AI/WS-2024/25 7 / 20
Inference: Constraint Propagation
Idea:
Before assigning values to variables, use constraints among variables to
reduce the number of consistent assignments for each variable.
Distinction between local and global consistency.
© G. Lakemeyer
Node consistency:
Unary constraint on variables.
AI/WS-2024/25 8 / 20
Arc Consistency
AI/WS-2024/25 9 / 20
Algorithm to compute arc-consistency
© G. Lakemeyer
AI/WS-2024/25 10 / 20
In General: k -Consistency
A CSP is k -consistent if, for any k − 1 variables and for any consistent
assignment to those variables, a consistent value can always be assigned to
any k the variable.
A CSP is strongly k -consistent, if it is i-consistent for all 1 ≤ i ≤ k .
© G. Lakemeyer
AI/WS-2024/25 11 / 20
Global Constraints
Global constraints are constraints that involve an arbitrary number (not
necessily all) of variable.
A 9 A 9 7 2 8 5 3 6 1 4
B 9 1 2 7 3 B 6 5 8 4 9 1 2 7 3
© G. Lakemeyer
C 1 C 1 3 4 7 6 2 9 5 8
D 4 5 7 6 D 4 8 5 2 7 6 3 9 1
E 7 8 E 3 9 7 5 1 8 4 2 6
F 6 3 5 F 2 1 6 9 3 4 5 8 7
G 5 7 G 8 6 9 1 4 5 7 3 2
H 3 6 H 5 2 3 6 8 7 1 4 9
I 7 4 1 2 9 I 7 4 1 3 2 9 8 6 5
Alldiff (A1, A2, A3, A4, A5, A6, A7, A8, A9)
Alldiff (A1, A2, A3, B1, B2, B3, C1, C2, C3)
etc.
Other examples are resource constraints in scheduling problems.
AI/WS-2024/25 12 / 20
Backtracking
AI/WS-2024/25 13 / 20
Example 4-Queens
© G. Lakemeyer
AI/WS-2024/25 14 / 20
Variable and Value Ordering
Minimum-remaining-value Heuristic
Choose a variable for assignment with the least number of values that can be
assigned consistently.
Idea behind: Such a variable has a high chance of failure, avoiding useless
tries of other variables.
Degree Heuristic
© G. Lakemeyer
NT
Q
Choose a variable with the highest WA
SA NSW
number of constraints on other unas-
V
signed variables.
T
Least-constraining Value
Choose a value that rules out fewest choices for neighboring variables.
Suppose WA = red, NT = green, and the next choice is for Q. Blue is a bad
choice as it leaves no value for SA.
AI/WS-2024/25 15 / 20
Inference and Backjumping for Intelligent Backtracking
Backjumping
© G. Lakemeyer
Partial assignment: NT
Q
WA
{Q = red , NSW = green, V = blue, T = red } SA NSW
Conflict set: V
AI/WS-2024/25 16 / 20
Local Search
8-Queens Problem
© G. Lakemeyer
AI/WS-2024/25 17 / 20
Tree-structured Constraint Graphs
Constraint Tree
A constraint graph is a tree if any two variable are connected by only one path.
Choose an ordering such that each variable appears after its parent in
the tree
AI/WS-2024/25 18 / 20
Solving Tree-structured CSPs
AI/WS-2024/25 19 / 20
Turning Constraint Graphs into Trees
AI/WS-2024/25 20 / 20