CONSTRAINT
SATISFACTION PROBLEMS
CS-323 Artificial Intelligence
TE- CIS
Course Teacher: Anita Ali
What is a CSP?
◦ Real world is full of constraints
◦ Consider example of time table generation
Constraint Satisfaction Problem - CSP
◦ A problem is solved when each variable has been assigned
a value that satisfies all the constraints on variable
◦ A problem described this way is called a Constraint
Satisfaction Problem - CSP
Standard Search Problems vs. CSPs
◦ State is a "black box“ – any data structure that supports
successor function, heuristic function, and goal test
◦ CSP uses internal structure of states to guide the solution of
complex problems
◦ CSP uses general purpose heuristics rather than problem
specific heuristic to enable the solution of complex
problems
Standard Search Problems vs. CSPs
◦ With CSP we care about the final node (goal) rather than
the path to reach that node
◦ Main idea is to eliminate large portion of search space all
at once by identifying variables/value combinations that
violate the constraints – Speed advantage
CSPs…
◦ For a color domain of {R,G,B}
◦ Standard state space search will be
considering 35 = 243 color assignments
◦ No mechanism to incorporate constraints
Unary Constraint:
◦ With CSP, considering unary constraint only This node cannot be colored
as Green
25=32 assignments are to be considered
Constraint Satisfaction Problems
A Constraint Satisfaction Problems (CSP) consists of three
components;
◦ V: A set of variables {V1, V2, . . . , Vn}
◦ D: is a set of domain {D1, D2, …. Dn} one for each
variable
◦ C: is a set of constraints that specify allowable
combination of values
CSP Representations
◦ Constraint Graph
◦ Nodes – Variables
◦ Arcs – Binary Constraints
◦ Self Loop – Unary Constraint
Basic Terms
Assignment
Each state is defined in terms of an assignment of values
to all or some of the variables
Complete Assignment
An assignment which assigns values to all variables
Consistent Assignment – legal assignment
An assignment that does not violate any constraint
Basic Terms
Solution
An assignment that is both complete and consistent
Binary CSP
Constraints apply to two variables
Note: Any n-ary CSP can be converted to a binary CSP
Some CSP Examples
◦ N- Queen ◦ Line Drawing Interpretation
◦ Graph Coloring ◦ Crypt-arithmetic Puzzle
◦ Job Scheduling ◦ Floor planning for VLSI
◦ Boolean Satisfiability ◦ Robot Planning Tasks
◦ Molecular Structure (analyzing
◦ Puzzles (Sudoku, Magic
biological structure)
Square)
i.e. Find a value for each variable
(drawn from its domain) such that all
Solving CSPs… the constraints are satisfied
In general, ∀ 𝐕𝐢 , find a di ∈ Di, such that all constraints are
satisfied
◦ Constraint Propagation (CP)
◦ Backtracking (BT)
◦ CP + BT → FC
◦ Variable Ordering
Constraint Propagation (CP)
◦ Idea came from success of Waltz's constraint propagation algorithm
◦ A CSP can be solved by applying constraint propagation
◦ Use constraints to reduce the number of legal values for one variable
◦ Which in turn reduces the number of legal values for another variable
& so on
◦ That is why it is called constraint propagation
Constraint Propagation (CP)
◦ Narrowing down the search space by eliminating values that couldn’t
be part of any solution
◦ Search → Looking for complete and consistent assignments
Constraint Propagation (CP)
◦ Basic idea in CP is to enforce ARC CONSISTENCY
◦ A Directed arc (Vi, Vj) is consistent if
∀ 𝒙 ∈ 𝑫𝒊 ∃𝒚 ∈ 𝑫𝒋
such that (x, y) satisfies the constraint on the arc
Vj
Vi
∀ 𝒙 ∈ 𝑫𝒊 ∃𝒚 ∈ 𝑫𝒋
such that (x, y) satisfies the constraint on the arc
ARC CONSISTENCY
◦ Network Z = {x , y} ◦ Network Z = {x , y}
◦ R xy: x < y ◦ R xy: x < y
◦ Dx= { 1, 2 } , Dy = { 2 , 3} ◦ Dx= { 1, 2 , 3 } , Dy = { 1 , 2 , 3 }
Constraint Propagation (CP)
◦ Arc consistency on arc (Vi, Vj) can be achieved
by deleting values from Di that fail this condition Vj
Vi
◦ & then we need to check any other constraint
arc that has Di at its head (we are propagating
the constraint ☺)
Graph Coloring
◦ A classical example to understand &
solving CSPs
◦ From a small set of colors, assign color to
each region on a map so that no two
neighbors have identical color
Graph Coloring
Four Color Theorem
◦ Four colors are sufficient for any planar
(2D) map. (Francis Guthrie)
◦ It remained conjecture for more than a century until
proven (by Appel and Haken ) in 1976
Graph Coloring
Variables: Regions (nodes)
Domains: Colors
Constraints: Neighbors should have different colors
CP – Example (Graph Coloring)
V2 R,
G, B
V1 V3
R,
G
G
Arc Consistency
◦ Necessary but not sufficient condition for solution
R, R,
G G
R, R,
G G
Arcs are consistent, but no solution is possible
Arc Consistency
◦ Necessary but not sufficient condition for solution
B
,G
R, R,
G G
Arcs are consistent, two solutions are possible
Time Complexity Arc Consistency
◦ d: size of domains
◦ e: number of binary constraints
◦ Checking consistency for each arc requires ___________
[since we have to look at all pairs of values in 2 domains]
◦ With e constraints, going through and checking an arc once requires
_________
◦ We need to reexamine an arc
(as the deletions to a node's domain propagate, but no arc can
require checking more than d times,
◦ so cost of arc tests in worst case is _______
Activity
{R, G, B}
V5
{G, B}
V4 V3 {R,B}
V1 V2
{R} {G, B}