CH 05
CH 05
Problems (CSP)
Chapter 5
Outline
Constraint Satisfaction 2
Constraint Satisfaction
Problems
Definition:
Constraint Satisfaction Problem
(CSP) is defined as set of Variables
X1, X2 , ... XN where by each
Variable Xi has a non empty
Domain Di of possible values and a
Constraint Satisfaction 3
Constraint satisfaction
problems (CSPs)
1. Stateisisdefined
State a "black box any
by variables datavalues
Xi with structure
from that
supports
domain Di successor function, heuristic function,
and goal
Goal testtestis a set of constraints specifying
2. CSP:
allowable
variables
combinations of values for subsets of
Example: Map-Coloring
Constraint Satisfaction 6
Example 1: Map-Coloring
Constraint Satisfaction 7
Example 1: Map-Coloring
Assignments:-
WA = red, NT = green, Q = red, NSW =
green, V = red, SA = blue, T = green
Constraint Satisfaction 8
Example 2: Map-Coloring
Constraint Satisfaction 9
Example 2: Map-Coloring
10
Example 2: Map-Coloring
Assignments . ?
11
Example 2: Map-Coloring
Assignments:-
SA = Red 12
Example 2: Map-Coloring
Assignment:-
13
SA=red, WA =green
Example 2 : Map-Coloring
Assignment:-
14
SA=red, WA =green, NT = blue,
Example 2: Map-Coloring
Assignment:-
15
SA=red, WA =green, NT = blue, Q = green
Example 2: Map-Coloring
Assignment:-
16
SA=red, WA =green, NT = blue, Q = green , NSW = blue
Example 2: Map-Coloring
Assignment:-
Constraint Satisfaction 17
SA=red, WA =green, NT = blue, Q = green , NSW = blue, V = green
Example 2: Map-Coloring
Assignment:-
Constraint Satisfaction 18
SA=red, WA =green, NT = blue, Q = green , NSW = blue, V = green, T = blue
Example 3: Map-Coloring
Constraint Satisfaction 19
Example 3: Map-Coloring
Constraint Satisfaction 21
Example 3: Map-Coloring
Solution: ?
Constraint Satisfaction 23
Example: Map-Coloring
How many solutions there might be
for coloring this map with three
colors(RGB) ?
Solutions = 18
24
Constraint Satisfaction
Constraint graph
Constraint Satisfaction 25
Example 4
Constraint Satisfaction 26
Example 5
How many solutions there might be
for coloring this figure with three
colors(RGB) ?
Solution?
Constraint Satisfaction 27
Example 5
Solution = 15
Constraint Satisfaction 28
Example 5
How many solutions there might be
for coloring this figure with three
colors(RGBW) ?
Solution?
Constraint Satisfaction 29
Example 5
How many solutions there might be
for coloring this figure with three
colors(RGBW) ?
Solution = 20
Constraint Satisfaction 30
Varieties of CSPs
Constraint Satisfaction 31
Varieties of constraints
e.g.,Unary
South Australians
constraintsdislike the green
involve colorvariable,
a single
SA green
Binary constraints involve pairs of
variables,
e.g., SA WA
Variables: F T U W R O X1 X2 X3
Domains: {0,1,2,3,4,5,6,7,8,9}
O + O = R + 10 X1
X1 + W + W = U + 10 X2
X2 + T + T = O + 10 X3
X3 = F,
FT,T 0, F 0
33
Constraint Satisfaction
Example: Cryptarithmetic
Variables: F T U W R O X1 X2 X3
Domains: {0,1,2,3,4,5,6,7,8,9}
+734
O + O = R + 10 X1
X1 + W + W = U + 10 X2 ----
X2 + T + T = O + 10 X3
X3 = F, 1468
F=1, O=4, R=8, T=7, U=6, 34W=3
FT,T 0, F 0
Example2: Cryptarithmetic
Variables: F T U W R O X1 X2 X3
Domains: {0,1,2,3,4,5,6,7,8,9}
O + O = R + 10 X1
X1 + W + W = U + 10 X2
X2 + T + T = O + 10 X3
X3 = F,
FT,T 0, F 0
35
Constraint Satisfaction
Example2: Cryptarithmetic
Variables: F T U W R O X1 X2 X3
Domains: {0,1,2,3,4,5,6,7,8,9}
765
Constraints: Alldiff (F,T,U,W,R,O)
+765
O + O = R + 10 X1
----
X1 + W + W = U + 10 X2
X2 + T + T = O + 10 X3 1530
X3 = F,
FT,T 0, F 0
F=1 O=5 R=0 T=7 U=3 W=6
Example3: Cryptarithmetic
Variables: F T U W R O X1 X2 X3
Domains: {0,1,2,3,4,5,6,7,8,9}
O + O = R + 10 X1
X1 + W + W = U + 10 X2
X2 + T + T = O + 10 X3
X3 = F,
FT,T 0, F 0
37
Constraint Satisfaction
Example3: Cryptarithmetic
Variables: F T U W R O X1 X2 X3
Domains: {0,1,2,3,4,5,6,7,8,9}
+836
O + O = R + 10 X1
X1 + W + W = U + 10 X2
X2 + T + T = O + 10 X3 ----
X3 = F,
FT,T 0, F 0
Constraint Satisfaction
1672
F=1 O=6 R=2 T=8 U=7 W=3
Example4: Cryptarithmetic
Variables: F T U W R O X1 X2 X3
Domains: {0,1,2,3,4,5,6,7,8,9}
O + O = R + 10 X1
X1 + W + W = U + 10 X2
X2 + T + T = O + 10 X3
X3 = F,
FT,T 0, F 0
39
Constraint Satisfaction
Example4: Cryptarithmetic
Variables: F T U W R O X1 X2 X3
Domains: {0,1,2,3,4,5,6,7,8,9}
+846
O + O = R + 10 X1
X1 + W + W = U + 10 X2 ----
X2 + T + T = O + 10 X3
X3 = F,
FT,T 0, F 0
1692
F=1 O=6 R=2 T=8 U=9 W=4
Example5: Cryptarithmetic
Variables: F T U W R O X1 X2 X3
Domains: {0,1,2,3,4,5,6,7,8,9}
O + O = R + 10 X1
X1 + W + W = U + 10 X2
X2 + T + T = O + 10 X3
X3 = F,
FT,T 0, F 0
41
Constraint Satisfaction
Example5: Cryptarithmetic
Variables: F T U W R O X1 X2 X3
Domains: {0,1,2,3,4,5,6,7,8,9} 867
+867
O + O = R + 10 X1
X1 + W + W = U + 10 X2
X2 + T + T = O + 10 X3 ----
X3 = F,
FT,T 0, F 0
Constraint Satisfaction
173442
F=1 O=7 R=4 T=8 U=3 W=6
Example6: Cryptarithmetic
Variables: F T U W R O X1 X2 X3
Domains: {0,1,2,3,4,5,6,7,8,9}
O + O = R + 10 X1
X1 + W + W = U + 10 X2
X2 + T + T = O + 10 X3
X3 = F,
FT,T 0, F 0
43
Constraint Satisfaction
Example6: Cryptarithmetic
Variables: F T U W R O X1 X2 X3
Domains: {0,1,2,3,4,5,6,7,8,9} 928
O + O = R + 10 X1
X1 + W + W = U + 10 X2
----
X2 + T + T = O + 10 X3
X3 = F, 1856
FT,T 0, F 0
Constraint Satisfaction F=1 O=8 R=6 T=9 U=5 W=2
Constraint Satisfaction
9567
+1085
Constraint Satisfaction
Ans: 10652
Example: Cryptarithmetic
21
Variables: T G O U X1 X2 +81
Domains: {0,1,2,3,4,5,6,7,8,9} Ans: 102
Class Work Example: Cryptarithmetic
Variables: E A T H P L X1 X2 X3 X4 819
T + T = E + 10 X1
X1 + A + A =L + 10 X2
X2 + E + H =P + 10 X3
X4 Satisfaction
X3 = T + 10 Constraint
Class Work Example2:
Cryptarithmetic
Variables: C O A L I S X1 X2 X3 X4
Domains: {0,1,2,3,4,5,6,7,8,9}
Constraints: Alldiff (C, O, A, L, I, S)
A + A = S + 10 X1
X1 + C + L = I + 10 X2
X2 + O + O =S + 10 X3 8186
X3 = C + C + 10 X4 +8106
X4 = O Constraint Satisfaction Ans: 16292
More Cryptarithmatic
examples .
Ans: 100
Constraint Satisfaction 49
Real-world CSPs
Assignment problems
e.g., who teaches what class
Timetabling problems
e.g., which class is offered when and where?
Transportation scheduling
Factory scheduling
real-valued variables
Constraint Satisfaction 50
Backtracking search
Breadth first search
n
Generates n!(d ) leaves
n
Possible complete assignment d
Constraint Satisfaction 51
Variable and value ordering
By default:
var Select-Unassigned-variable
assigns the next unassigned
variable in the order given the
list Variables
Constraint Satisfaction 52
Minimum remaining values (MRV)
Constraint Satisfaction 53
Degree heuristics
SA = 5
T =0
V=2
Constraint Satisfaction 54
Least Constraint Value
Constraint Satisfaction 57
Backtracking example
Constraint Satisfaction 58
Backtracking example
Constraint Satisfaction 59
Backtracking example
Constraint Satisfaction 60
Backtracking example
Constraint Satisfaction 61
Improving backtracking efficiency
Constraint Satisfaction 62
Most constrained variable
Constraint Satisfaction 63
Most constraining variable
Tie-breaker among most constrained
variables
Most constraining variable:
Constraint Satisfaction 64
Least constraining value
Given a variable, choose the least
constraining value:
Idea:
Keep track of remaining legal
values for unassigned
variables
Constraint Satisfaction 66
Constraint Satisfaction 67
Constraint Satisfaction 68
Constraint Satisfaction 69
Constraint Satisfaction 70
Constraint Satisfaction 71
Constraint Satisfaction 72
Constraint Satisfaction 73
Forward checking Using Australia
Idea:
Keep track of remaining legal values
for unassigned variables
Terminate search when any variable
has no legal values
Constraint Satisfaction 74
Forward checking
Constraint Satisfaction 75
Forward checking
Constraint Satisfaction 76
Forward checking
Idea:
Keep track of remaining legal
values for unassigned variables
Terminate search when any
variable has no legal values
Constraint Satisfaction 77
Constraint propagation
Forward checking propagates information from
assigned to unassigned variables, but doesn't
provide early detection for all failures:
Constraint Satisfaction 79
Arc consistency
Simplest form of propagation makes each arc
consistent
X Y is consistent iff
Constraint Satisfaction 80
Arc consistency
Simplest form of propagation makes each arc
consistent
X Y is consistent iff
To apply to CSPs:
Constraint Satisfaction 85
Programming Assignment
Constraint Satisfaction 87