Chapter 4 CSP Problem
Chapter 4 CSP Problem
Hussein
Faculty of computer & Information Science
Suez Canal University
Constraint Satisfaction Problems
(CSPs)
• Standard search problem:
– state is a "black box“ – any data structure that
supports successor function, heuristic function, and
goal test
• CSP:
– state is defined by variables Xi with values from
domain Di
– goal test is a set of constraints specifying allowable
combinations of values for subsets of variables
• A solution to a CSP is
– A complete assignment that satisfies all the constraints
Dr. Mohamed K. Hussein
Example: Map-Coloring
NT Q
WA
NS
W
SA
• Continuous variables
– e.g., start/end times for Hubble Space Telescope observations
– linear constraints solvable in polynomial time by linear
programming
We need:
• Initial state: none of the variables has a value (color)
• Successor state: one of the variables without a value will get some
value.
• Goal: all variables have a value and none of the constraints is violated.
NxD
N layers
WA WA WA NT T
[NxD]x[(N-1)xD]
WA WA WA NT
NT NT NT WA
Equal! N! x DN
Dr. Mohamed K. Hussein
There are N! x DN nodes in the tree but only DN distinct states??
Backtracking search
• Better search tree: First order variables, then assign them values one-by-one.
D
WA WA WA
WA
NT D2
WA WA
NT NT
DN
Dr. Mohamed K. Hussein
Backtracking search
B C D E
F G H I J
K L M N
O
Dr. Mohamed K. Hussein
Depth First Search
• A,
B C D E
B C D E
F G
B C D E
F G
B C D E
F G
K L
B C D E
F G
K L
B C D E
F G
K L
O
Dr. Mohamed K. Hussein
Depth First Search
• A,B,F,
• G,K,
• L, O: Goal State
A
B C D E
F G
K L
O
Dr. Mohamed K. Hussein
Depth First Search
The returned solution is the sequence of operators in the path:
A, B, G, L, O : assignments when using CSP !!!
B C D E
F G
K L
O
Dr. Mohamed K. Hussein
Backtracking
Example 1
A B
E
C
F G
A B
{blue, green, red}
E
C {blue, green, red}
H
{blue, green, red}
F G {blue, green, red}
{blue, green, red}
A B
E
C
F G
1 = red
2 = blue
3 = green
A B
E
C
F G
1 = red
2 = blue
3 = green
A B
E
C
F G
1 = red
2 = blue
3 = green
A B
E
C
F G
1 = red
2 = blue
3 = green
A B
E
C
F G
1 = red
2 = blue
3 = green
A B
E
C
F G
1 = red
2 = blue
3 = green
A B
E
C
F G
1 = red
2 = blue
3 = green
A B
E
C
F G
1 = red
2 = blue
3 = green
A B
E
C
F G
1 = red
2 = blue
3 = green
A B
E
C
F G
1 = red
2 = blue
3 = green
A B
E
C
F G
1 = red
2 = blue
3 = green
A B
E
C
F G
1 = red
2 = blue
3 = green
A B
E
C
H Solution !!!!
F G
1 = red
2 = blue
3 = green
Example 2
[R]
[R,B,G] [R,B,G]
[R]
[R,B,G] [R,B,G]
[R]
[R,B,G] [R,B,G]
[R]
[R,B,G] [R,B,G]
[R]
[R,B,G] [R,B,G]
[R]
[R,B,G] [R,B,G]
Dead End → Backtrack
[R]
[R,B,G] [R,B,G]
[R]
[R,B,G] [R,B,G]
[R]
[R,B,G] [R,B,G]
Solution !!!
[R]
[R,B,G] [R,B,G]
[R]
[R,B,G] [R,B,G]
[R]
[R,B,G] [R,B,G]
[R]
[R,B,G] [R,B,G]
[R]
[R,B,G] [R,B,G]
[R]
[R,B,G] [R,B,G]
Solution !!!
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
Dead End 3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
Dead End 3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
Solution !!!
Dr. Mohamed K. Hussein
Least constraining value
LCV
• Given a variable, choose the least
constraining value:
– the one that rules out (eliminate) the fewest
values in the remaining variables
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
Dead End
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
[R,B,G] [R,B,G]
2 arcs
[R]
3 arcs
3 arcs
Solution !!! [R,B,G] [R,B,G]
• Idea:
– Keep track of remaining legal values for unassigned
variables
– Terminate search when any variable has no legal values
–
• Idea:
– Keep track of remaining legal values for unassigned
variables
– Terminate search when any variable has no legal values
–
• Idea:
– Keep track of remaining legal values for unassigned
variables
– Terminate search when any variable has no legal values
–
• Idea:
– Keep track of remaining legal values for unassigned
variables
– Terminate search when any variable has no legal values
–
Examples
X1 X2
1 2 3 4 {1,2,3,4} {1,2,3,4}
1
2
3
4
X3 X4
{1,2,3,4} {1,2,3,4}
[4-Queens slides copied from B.J. Dorr CMSC 421 course on AI]
Dr. Mohamed K. Hussein
Example: 4-Queens Problem
X1 X2
1 2 3 4 {1,2,3,4} {1,2,3,4}
1
2
3
4
X3 X4
{1,2,3,4} {1,2,3,4}
X1 X2
1 2 3 4 {1,2,3,4} { , ,3,4}
1
2
3
4
X3 X4
{ ,2, ,4} { ,2,3, }
X1 X2
1 2 3 4 {1,2,3,4} { , ,3,4}
1
2
3
4
X3 X4
{ ,2, ,4} { ,2,3, }
X1 X2
1 2 3 4 {1,2,3,4} { , ,3,4}
1
2
3
4
X3 X4
{ , , , } { ,2, , }
X1 X2
1 2 3 4 {1,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{ ,2, , } { , ,3, }
X1 X2
1 2 3 4 {1,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{ ,2, , } { ,,, }
X1 X2
1 2 3 4 { ,2,3,4} {1,2,3,4}
1
2
3
4
X3 X4
{1,2,3,4} {1,2,3,4}
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, ,3, } {1, ,3,4}
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, ,3, } {1, ,3,4}
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } {1, ,3, }
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } {1, ,3, }
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } { , ,3, }
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } { , ,3, }
Solution !!!!
[R]
[R,B,G] [R,B,G]
[R]
[ ,B,G] [R,B,G]
[R]
[ ,B,G] [R,B,G]
[R]
[R]
[R]
[ , ,G] [ , ,G]
[R]
[ , ,G] [ , ,G]
[R]
[ , ,] [ , ,G]
[R]
[ , ,] [ , ,G]
Dead End
[R]
[R]
[ , ,G] [R, , ]
[R]
[ , ,G] [R, , ]
[R]
[ , ,G] [R, , ]
Solution !!!
• Note:
– If the arc from A to B is consistent, the (reverse) arc from B to A
is not necessarily consistent!
– Arc consistency doesDr.not detect
Mohamed every possible inconsistency!!
K. Hussein
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
[R]
[R,B,G] [R,B,G]
[R]
[R,B,G] [R,B,G]
[R]
[ ,B,G] [R,B,G]
[R]
[ ,B,G] [R,B,G]
[R]
[ ,B,G] [R,B,G]
[R]
[ ,B,G] [R,B,G]
[R]
[ ,B,G] [R,B,G]
[R]
[ ,B,G] [R,B,G]
[R]
[ ,B,G] [R,B,G]
[R]
[ ,B,G] [R,B,G]
[R]
[R]
[R]
[ , ,G] [R, , ]
[R]
[ , ,G] [R, , ]
[R]
[ , ,G] [R, , ]
[R]
[ , ,G] [R, , ]
[R]
[ , ,G] [R, , ]
Solution !!!
• To apply to CSPs:
– allow states with unsatisfied constraints
– operators reassign variable values
– hill-climb with value(state) = total number of violated constraints
• Variable selection:
– randomly select any conflicted variable
• Value selection:
– choose value that violates the fewest constraints
– called the min-conflictsDr.
heuristic
Mohamed K. Hussein
Example: 4-Queens
• States: 4 queens in 4 columns (44 = 256 states)
• Actions: move queen in column
• Goal test: no attacks
• Evaluation: h(n) = number of attacks