06 CSP
06 CSP
Artificial Intelligence
Constraint
Satisfaction
Problems
AIMA Chapter 6
Definition:
• State is defined by a set of variables Xi (= factored state description)
• Each variable can have a value from domain Di or be unassigned (partial solution).
• Constraints are a set of rules specifying allowable combinations of values for subsets of variables (e.g., 𝑋1 ≠
𝑋7 or 𝑋2 > 𝑋9 + 3)
General-purpose algorithms for CSP with more power than standard search algorithms exit.
Example: Map Coloring (Graph coloring)
Problem Constraint graph
• Constraints:
i
i,j Xij = N
(Xij, Xik) {(0, 0), (0, 1), (1, 0)} # cannot be in same col.
(Xij, Xkj) {(0, 0), (0, 1), (1, 0)} # cannot be in same row. for 𝑖, 𝑗, 𝑘 ∈ {1, 2, … , 𝑁}
(Xij, Xi+k, j+k) {(0, 0), (0, 1), (1, 0)} # cannot be diagonal
(Xij, Xi+k, j–k) {(0, 0), (0, 1), (1, 0)} # cannot be diagonal
N-Queens: Alternative formulation
Q1 Q2 Q3 Q4
• Variables: 𝑄1 , 𝑄2 , … , 𝑄𝑁
• Domains: {1, 2, … , 𝑁} # row for each col.
4
3
• Constraints:
2
i, j non-threatening (Qi , Q j)
1
Example:
Q1 = 2, Q2 = 4, Q3 = 1, Q4 = 3
Example: Cryptarithmetic Puzzle
• Variables: T, W, O, F, U, R Given Puzzle:
X1, X2 Find values for the letters.
• Domains: {0, 1, 2, …, 9} Each letter stands for a
• Constraints: different digit.
Alldiff(T, W, O, F, U, R) X2 X1
O + O = R + 10 * X1
W + W + X1 = U + 10 * X2
T + T + X2 = O + 10 * F
T ≠ 0, F ≠ 0
Example: Sudoku
• Variables: Xij
• Domains: {1, 2, …, 9}
• Constraints:
Alldiff(Xij in the same unit)
Alldiff(Xij in the same row)
Xij
Alldiff(Xij in the same column)
Some Popular Types of CSPs
NP-complete
(x1 ∨ ¬x2) ∧ (¬x1 ∨ x2 ∨ x3) ∧ ¬x1 = True
• Integer Programming
Variables are restricted to integers. Find a feasible solution that
satisfies all constraints. The traveling salesman problem can be
expressed as an integer program.
• Linear Programming
Variables are continuous and constraints
are linear (in)equalities.
Find a feasible solution using, e.g.,
the simplex algorithm.
Real-world CSPs
• Assignment problems
e.g., who teaches what class for a fixed schedule. Teacher cannot
be in two classes at the same time!
• Timetable problems
e.g., which class is offered when and where? No two classes in
the same room at the same problem.
• Scheduling in transportation and production (e.g., order
of production steps).
• Many problems can naturally also be formulated as
CSPs.
fail
Backtracking search algorithm
Local Search (e.g., Hill-climbing and simulated annealing) works only with
“complete” states, i.e., all variables assigned, but we can allow states with
unsatisfied constraints.