05 Constraint Satisfaction Problems (Us)
05 Constraint Satisfaction Problems (Us)
initial state
Representation
• In the search tree, a variable is assigned at each
level.
• Solutions have to be complete assignment,
therefore they appear at depth n, the number of
variable and maximum depth of the tree.
• Depth-first search algorithms are popular in
CSPs.
• The simplest class of CSP (map coloring, n-
queens) are characterized by:
– discrete variables
– finite domains
Finite domains
• If the maximum size of the domain of any
variable is d, then the number of possible
complete assignments is O(dn), exponential in
the number of variables.
• CSPs with finite domain include Boolean CSPs,
whose variables can only be true or false.
• In most practical applications, CSP algorithms
can solve problems with domains orders of
magnitude larger than the ones solvable by
uninformed search algorithms.
Infinite domains
• With infinite domains (e.g., integers and strings), to
describe constraints enumerating all legal combinations
of values is not possible.
• A constraint language has to be used, for example:
– job-start1 + 5 ≤ job-start3
(It is possible to use heuristics to select variables (2.) and values (3.).
Backtracking search algorithm
Example: 4-queens
• Place 4 queens, one per row, so that they
do not attack each others
– Variables: R1 … R4 (queens)
– Domains: [1 … 4] for each Ri (columns)
– Constraints: Ri does not attack Rj
Ri not attacking Rj
[1 .. 4] [1 .. 4]
R1=1 R1=2
–
Arc consistency
• Simplest form of propagation makes each arc consistent
• X Y is consistent iff
for every value x of X there is some allowed y
–
Arc consistency
• Simplest form of propagation makes each arc consistent
• X Y is consistent iff
for every value x of X there is some allowed y
•
Algorithm for arc consistency
Algorithm for arc consistency:
AC-3
• It uses a queue to keep track of the arcs
that need to be checked for inconsistency.
• Each arc (Xi, Xj) in turn is removed from
the agenda and checked.
• If any values need to be deleted from the
domain of Xi, then every arc (Xk, Xj)
pointing to Xi must be reinserted on the
queue for checking.
Algorithm for arc consistency:
AC-3
(C1,C2): eliminate AZUL
AZUL, ROJO C1
(C2,C1): ok
C2
AZUL (C2,C3): ok
C3
(C3,C2): eliminate AZUL
C4
AZUL, ROJO, VERDE AZUL, VERDE (C2,C4): ok
(C4,C2): eliminate AZUL
(C3,C4): eliminate VERDE
Initial list: add (C2,C3)
(C1,C2), (C2,C1), (C2,C3), (C3,C2),
(C4,C3): ok
(C2,C4), (C4,C2), (C3,C4), (C4,C3)
(C2,C3): ok