Constraint Satisfaction Problem
Constraint Satisfaction Problem
Intelligence
Constraint
Satisfaction
Problems
Constraint + variables
Satisfaction can have no
value!
Problems (CSPs)
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
• Variables: 𝑄𝑄1, 𝑄𝑄2, … , 𝑄𝑄𝑁
Q1 Q2 Q3 Q4
• Domains: {1, 2, … , 𝑁} # row for each
4
col. 3
• Constraints:
2
i, j non-threatening (Qi , Qj)
1
Example:
Q1 = 2, Q2 = 4, Q3 = 1, Q4 = 3
Example: Cryptarithmetic
•
Puzzle Given Puzzle:
Variables: T, W, O, F, U, R
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)
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(X ij 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