Ai09 Constraint Satisfaction Problems Part I Post Handout
Ai09 Constraint Satisfaction Problems Part I Post Handout
Artificial Intelligence
09. CSP, Part I: Basics, and Naı̈ve Search
What to Do When Your Problem is to Satisfy All These Constraints
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 1/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Agenda
1 Introduction
2 Constraint Networks
4 Naı̈ve Backtracking
6 Conclusion
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 2/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
What is a constraint?
A constraint is a condition that every solution must satisfy.
Find:
An assignment of variables to values (from the respective domains),
so that every constraint is satisfied.
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 5/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
→ Problem: SuDoKu.
Northern
Territory
Western Queensland
Australia
South
Australia
New South Wales
Victoria
Tasmania
Northern
Territory
Western Queensland
Australia
South
Australia
New South Wales
Victoria
Tasmania
Bundesliga Constraints
Variables: vAvs.B where A and B are teams, with domain {1, . . . , 34}:
For each match, the (ID of the) “Spieltag” where it is scheduled.
(Some) Constraints:
For all A, B: vAvs.B ≤ 17 < vBvs.A or
vBvs.A ≤ 17 < vAvs.B (each pairing exactly
once in each half-season).
For all A, B, C, D where
{A, B} ∩ {C, D} = 6 ∅: vAvs.B 6= vCvs.D
(each team only one match per day).
For all A, B, D: vAvs.B + 1 6= vAvs.D (each
team alternates between home matches and
away matches).
...
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 8/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
How do they actually do it? Modern computers and CSP methods: fractions
of a second. 19th (20th/21st?) century: Combinatorics and manual work.
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 9/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Some Applications
Traveling Tournament Problem Scheduling
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 10/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 11/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Naı̈ve Backtracking: How does backtracking work? What are its main
weaknesses?
→ Serves to understand the basic workings of this wide-spread algorithm,
and to motivate its enhancements.
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 12/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Terminology:
It is common to say constraint satisfaction problem (CSP) instead of
constraint network.
Strictly speaking, however, “CSP” is the algorithmic problem of
finding solutions to constraint networks.
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 14/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Northern
Territory
Western Queensland
Australia
South
Australia
New South Wales
Victoria
Tasmania
Unary Constraints:
A unary constraint is a relation Cv over a single variable, i.e., a
subset Cv ⊆ Dv of that variable’s domain.
A unary constraint Cv is equivalent to reducing the variable domain,
setting Dv := Cv .
Unary constraints are not needed at the formal level. They are often
convenient for modeling, i.e., to state “exceptions”. (E.g., Australia:
D global as on previous slide, but SA 6= green.)
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 17/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Example: SuDoKu
Questionnaire
→ Problem: Place 8 queens so that they don’t attack each other.
Question!
How to encode this into a constraint network? Variables?
Domains? Constraints?
→ E.g.: Variables: V = {v1 , . . . , v8 }: vi =row of queen in i-th column.
Domains: Dv = D = {1, . . . , 8}. Constraints For 1 ≤ i < j ≤ 8:
Cvi vj = {(d, d0 ) ∈ D × D | d 6= d0 and |d − d0 | =
6 |i − j|}.
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 19/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
(its solution)
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 23/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Solutions
Victoria
Solution: {WA = red , NT = green, SA = blue,
Tasmania
Q = red , NSW = green, V = red , T = green}.
→ Note: This is not the only solution. E.g., we can permute the colors, and
Tasmania can be assigned an arbitrary color.
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 24/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Questionnaire
Question!
Which of the following statements imply that the empty
assignment, a0 , can always be extended to a solution?
(A): a0 is consistent. (B): The network is inconsistent.
(C): There are no binary (D): The network is solvable.
constraints.
→ (A): No. Being consistent does not imply being extensible to a solution (cf.
previous slide). For a0 in particular: a0 is always consistent; it can be extended to a
solution if and only if the network is solvable.
→ (B): No. If the network is inconsistent then there are no solutions, so no
assignment can be extended to a solution, in particular not a0 .
→ (C): If one of the unary constraints (variable domains) is empty, then the network is
inconsistent and we are in case (B). Otherwise, the network is solvable and a0 is
extensible to a solution, cf. answer to (A).
→ (D): Yes. The empty assignment can be extended to any solution for the network,
if such a solution does exist.
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 26/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Questionnaire
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 28/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Before We Begin
Basic Concepts
Search: Depth-first enumeration of partial assignments.
Backtracking: Backtrack at inconsistent partial assignments.
Inference: Deducing tighter equivalent constraints to reduce search
space (backtracking will occur earlier on).
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 30/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Naı̈ve Backtracking
Call with input constraint network γ and the empty assignment a:
function Naı̈veBacktracking(a) returns a solution, or “inconsistent”
if a is inconsistent then return “inconsistent”
if a is a total assignment then return a
select some variable v for which a is not defined
for each d ∈ Dv in some order do
a0 := a ∪ {v = d}
a00 := Naı̈veBacktracking(a0 )
if a00 6= “inconsistent” then return a00
return “inconsistent”
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 32/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Pro:
Naı̈ve backtracking is extremely simple. (You can implement it on a
Commodore 128.)
Despite this simplicity, it is much more efficient than enumerating
total assignments. (You can implement it on a Commodore 128,
and solve the Bundesliga.)
Naı̈ve backtracking is complete (if there is a solution, backtracking
will find it).
Contra:
Backtracking does not recognize a that cannot be extended to a
solution, unless a is already inconsistent.
→ Employ inference to improve this! (Chapter 9).
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 33/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 34/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Questionnaire
Question!
Say G is a clique of n vertices, and we run backtracking for graph
coloring with n different colors. How big is the search space
(consistent partial assignments) of naı̈ve backtracking?
(A): n (B): n!
Pn−1
(C): 1 + i=0 n ∗ · · · ∗ (n − i) (D): nn
→ (C): 1 for the root. At the first vertex, we have n consistent colors, then n − 1
consistent colors, etc. We need to add up the nodes at each layer of the search tree.
Question!
If G is a line and we order variables left-to-right?
(A): 1 + n−1 (B): 1 + n−1 i
P P
i=0 n ∗ · · · ∗ (n − i) i=0 n ∗ (n − 1)
→ (B): At the first vertex, we have n consistent colors; at each later vertex it’s n − 1.
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 35/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Questionnaire, ctd.
Northern
Territory
Western Queensland
Australia
South
Australia
New South Wales
Victoria
Tasmania
Question!
Variable order WA, NT , Q, NSW , V , T , SA. Tightest upper bound
on naı̈ve backtracking search space size?
(A): 145 (B): 382
(C): 433 (D): 37
→ (B): With this variable order, we have: 3 choices for WA (3 nodes); 2 (consistent!)
choices for NT (2 ∗ 3 = 6 new nodes); 2 choices each for Q, NSW , V (12, 24, 48 new
nodes); 3 choices for T (144 new nodes). For each of the 144 leaves, we have either 0
or 1 choices for SA. To get an upper bound, we conservatively assume it’s 1
everywhere, adding another 144 consistent nodes. Plus 1 for the root.
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 36/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
→ The order in which we consider variables and their values may have a
huge impact on search space size!
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 38/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
WA, NT , Q as on slide 32 =⇒ 3 ∗ 2 ∗ 2.
Any ideas for better variable orders? For SA, WA, NT it’s 3 ∗ 2 ∗ 1.
→ The “most important/most restricted variables” up front.
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 39/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Value Ordering:
Naı̈ve backtracking does not specify in which order the values of the
chosen variable are considered.
If no solution exists below current node: Doesn’t matter, we will
have to search the whole sub-tree anyway.
If solution does exist below current node: Does matter. If we always
chose a “correct” value (from a solution) then no backtracking is
needed.
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 40/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 41/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 42/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 43/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Questionnaire
Northern
Territory
Western Queensland
Australia
South
Australia
New South Wales
Victoria
Tasmania
Question!
Variable order SA, NT , Q, NSW , V , WA, T . Tightest upper bound
on naı̈ve backtracking search space size?
(A): 52 (B): 145
(C): 382 (D): 433
→ (A): With this variable order, we have 3 choices for SA and 2 choices for NT ,
yielding 1 + 3 + 3 ∗ 2 = 10 search nodes and 3 ∗ 2 = 6 tree leaves. For each of
Q, NSW , V , WA, we have only 1 choice, so each adds another layer of 6 nodes. We
have 3 choices for T , adding another 3 ∗ 6 = 18 nodes. This sums up to 52.
→ This is the strategy combination from slide 42. Compare with slide 36!
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 44/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Summary
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 46/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
Reading
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 47/48
Introduction Networks Consistency Backtracking V/V Ordering Conclusion References
References I
Stuart Russell and Peter Norvig. Artificial Intelligence: A Modern Approach (Third
Edition). Prentice-Hall, Englewood Cliffs, NJ, 2010.
Koehler and Torralba Artificial Intelligence Chapter 09: Constraint Satisfaction Problems, Part I 48/48