0% found this document useful (0 votes)
21 views

CH 05

Constraint satisfaction problems (CSPs) involve finding assignments of values to variables that satisfy a set of constraints. CSPs can model problems like map coloring, where variables represent regions to color and constraints require adjacent regions to have different colors. Cryptarithmetic puzzles are also CSPs, with constraints representing the mathematical equations that must be satisfied. Backtracking search is commonly used to systematically attempt to find solutions to CSPs.

Uploaded by

Abdel Aziz Yusuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

CH 05

Constraint satisfaction problems (CSPs) involve finding assignments of values to variables that satisfy a set of constraints. CSPs can model problems like map coloring, where variables represent regions to color and constraints require adjacent regions to have different colors. Cryptarithmetic puzzles are also CSPs, with constraints representing the mathematical equations that must be satisfied. Backtracking search is commonly used to systematically attempt to find solutions to CSPs.

Uploaded by

Abdel Aziz Yusuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 87

Constraint Satisfaction

Problems (CSP)
Chapter 5
Outline

Constraint Satisfaction Problems (CSP)

Backtracking search for CSPs

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)

Standard search problem:

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

Variables WA, NT, Q, NSW, V, SA, T

Domains Di = {red, green, blue}


Constraints: adjacent regions must have different colors
e.g., WA NT

Or (WA, NT) in {(red, green),(red, blue), (green, red), (green


blue), (blue, red), (blue, green)}
Constraint Satisfaction 5
Example 1: Map-Coloring

Constraint Satisfaction 6
Example 1: Map-Coloring

Solutions are complete and consistent

Constraint Satisfaction 7
Example 1: Map-Coloring

Solutions are complete and consistent

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

Try: Color using:-


1. Variables WA, NT, Q, NSW, V, SA, T
2. Domains Di = {red, green, blue}
3. Constraints: adjacent regions must have different colors

10
Example 2: Map-Coloring

Solutions are incomplete and inconsistent

Assignments . ?
11
Example 2: Map-Coloring

Solutions are incomplete and inconsistent

Assignments:-
SA = Red 12
Example 2: Map-Coloring

Solutions are incomplete and inconsistent

Assignment:-
13
SA=red, WA =green
Example 2 : Map-Coloring

Solutions are incomplete and inconsistent

Assignment:-
14
SA=red, WA =green, NT = blue,
Example 2: Map-Coloring

Solutions are incomplete and inconsistent

Assignment:-
15
SA=red, WA =green, NT = blue, Q = green
Example 2: Map-Coloring

Solutions are incomplete and inconsistent

Assignment:-
16
SA=red, WA =green, NT = blue, Q = green , NSW = blue
Example 2: Map-Coloring

Solutions are incomplete and inconsistent

Assignment:-
Constraint Satisfaction 17
SA=red, WA =green, NT = blue, Q = green , NSW = blue, V = green
Example 2: Map-Coloring

Solutions are now complete and consistent

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

Try: Color using :-

1.Variables WA, NT, Q, NSW, V, SA, T


2.Domains Di = {red, green, blue}
3.Constraints: adjacent regions must have different colors
Example 3: Map-Coloring

Solutions are complete and consistent

Constraint Satisfaction 21
Example 3: Map-Coloring

Solutions are complete and consistent


Asignments:-
WA = blue, NT = red, Q = blue, NSW = red,
T =Satisfaction
V = blue, SA = green, Constraint red 22
Example: Map-Coloring
How many solutions there might be
for coloring this map with three
colors(RGB) ?

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

Binary CSP: each constraint relates two


variables
Constraint graph: nodes are variables,

arcs are constraints

Constraint Satisfaction 25
Example 4

Construct a Constraint graph of the


following map

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

How many solutions there might be


for coloring this figure with three
colors(RGB) ?

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

Higher-order constraints involve 3 or


more variables,
e.g., cryptarithmetic column constraints
Constraint Satisfaction 32
Example: Cryptarithmetic

Variables: F T U W R O X1 X2 X3
Domains: {0,1,2,3,4,5,6,7,8,9}

Constraints: Alldiff (F,T,U,W,R,O)

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}

Constraints: Alldiff (F,T,U,W,R,O) 734

+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}

Constraints: Alldiff (F,T,U,W,R,O)

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}

Constraints: Alldiff (F,T,U,W,R,O)

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}

Constraints: Alldiff (F,T,U,W,R,O)


836

+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}

Constraints: Alldiff (F,T,U,W,R,O)

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}

Constraints: Alldiff (F,T,U,W,R,O)


846

+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}

Constraints: Alldiff (F,T,U,W,R,O)

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

Constraints: Alldiff (F,T,U,W,R,O)

+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}

Constraints: Alldiff (F,T,U,W,R,O)

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

Constraints: Alldiff (F,T,U,W,R,O)


+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

Domains: {0,1,2,3,4,5,6,7,8,9} +9219


Constraints: Alldiff (E, A, T, H, P, L) Ans: 10038

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

Notice that many real-world problems involve

real-valued variables
Constraint Satisfaction 50
Backtracking search
Breadth first search
n
Generates n!(d ) leaves
n
Possible complete assignment d

BT is used for Depth first search that


chooses values for one variable at a
time and backtracks when a variable
has no legal values left to assign

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)

Minimum remaining values


(MRV) is known as Most
constrained variable or fail
first heuristics.

Constraint Satisfaction 53
Degree heuristics

Degree h heuristics reduces the


branching factor on future choices by
selecting the variable that is involved
in the largest number of constraints on
other unassigned variables
Ex:

SA = 5

T =0

V=2
Constraint Satisfaction 54
Least Constraint Value

Least Constraint Value prefers the


value that rules out the fewest
choices for the neighboring values in
Ex:
the constraint graph
WA = red and NT =green
Q becomes blue but not good choice due
to Q neighbor SA
Constraint Satisfaction 55
Backtracking search

Variable assignments are commutative},


i.e.,[ WA = red then NT = green ] same as [
NT = green then WA = red ]

Only need to consider assignments to a


single variable at each node
b = d and there are d^n leaves

Depth-first search for CSPs with single-


Can solve
variable n-queensisforcalled
assignments n 25backtracking
Backtracking search

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

General-purpose methods can


give huge gains in speed:
Which variable should be assigned
next?
In what order should its values be
tried?
Can we detect inevitable failure early?

Constraint Satisfaction 62
Most constrained variable

Most constrained variable:


choose the variable with the fewest legal
values

Constraint Satisfaction 63
Most constraining variable
Tie-breaker among most constrained
variables
Most constraining variable:

choose the variable with the most constraints on


remaining variables

Constraint Satisfaction 64
Least constraining value
Given a variable, choose the least
constraining value:

the one that rules out the fewest values in the


remaining variables

Combining these heuristics makes 1000


queens feasible Constraint Satisfaction 65
Forward checking

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

Idea:Keep track of remaining legal values



for unassigned variables
Terminate search when any variable
has no legal values

Constraint Satisfaction 75
Forward checking

Idea:Keep track of remaining legal values for



unassigned variables
Terminate search when any variable has
no legal values

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:

NT and SA cannot both be blue!

Constraint propagation repeatedly enforces


constraints locally
Constraint Satisfaction
78
Arc consistency

Simplest form of propagation makes


X Y is consistent
each off
arc consistent
for every value x of X there is some
allowed y

Constraint Satisfaction 79
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

Constraint Satisfaction 80
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

If X loses a value, neighbors of X need to be


rechecked Constraint Satisfaction 81
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

If X loses a value, neighbors of X need to be rechecked


Arc consistency detects failure earlier than forward checking

Can be run as a preprocessor or after each assignment


82
Constraint Satisfaction
Arc consistency algorithm AC-3

Time complexity: O(n2d3)


Constraint Satisfaction 83
Local search for CSPs
Hill-climbing, simulated annealing typically work with "complete
states, i.e., all variables assigned

To apply to CSPs:

allow states with unsatisfied constraints




operators reassign variable values
Variable selection: randomly select any conflicted variable
Value selection by min-conflicts heuristic:

choose value that violates the fewest constraints




i.e., hill-climb with h(n) = total
Constraint number of violated constraints
Satisfaction 84
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

Constraint Satisfaction 85
Programming Assignment

Write a project that helps


solve Cryptarithmatic
Constraints

Constraint Satisfaction 87

You might also like