Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

Constraint Satisfaction Problem

(CSP)

Abu Saleh Musa Miah


CSE, BAUST
Problem Reduction: AND-OR Graph
• Useful for representing the solution of problems
that can be solved by decomposing them into a set
of smaller problems, all which must then be solved
• Decompositions/reduction generates arcs: AND
• One AND arc may point to any number of
successor nodes, all of which must be solved in
order for the arc to point to a solution
• Just as in an OR graph, several arcs may emerge
from a single node, indicating a variety of ways in
which the original problem might be solved
AND-OR Graph
• This is why the structure is called not simply
an AND graph but rather an AND-OR graph.
Goal: Acquire TV set A line connecting
all components

Goal: Steal TV set Goal: Earn some money Goal: Buy new TV set
Why BFS is not Adequate for Searching AND-OR Graph?
-A expanded to A
two arcs: one
leading to B &
(9)
one leading to
C&D B C D
f`: numbers (5) (3) (4)
-Every operation uniform
cost
-Each arc with a single Which expand next? Which expand next?
successor has a cost of 1 -C (3) lowest cost -B (5+1=06)
- Each AND arc with -C+D+2 (3+4+2=09) -lowest compared to C
multiple successors has
a cost of 1 for each of The choice of which node to expand next
its components must depend not only on the f` value of that node
But also on whether that node is part of the current
Best path from the initial node
AND-OR Graph
• Most promising single node:
G (f`=3)
• Arc: G-H (7+2=9) A

• But that arc is not part of the (38)


current best path since to B C D

use it, we must also use the (27)


(17) (9)
arc: I-J (27) E F G H I J
• Path A-B-E-F is better (5) (10) (3) (4) (15) (10)

(5+10+2+1=18)
• Should not expand G next;
rather should examine either
E/F
Algorithm: Problem Reduction
1. Initialize the graph to the starting node
2. Loop until the starting node is labeled SOLVED or until
its cost goes above FUTILITY
(a) Traverse the graph, starting at the initial node &
following the current best path, & accumulate the set
of nodes that are on that path & have not yet been
expanded/labeled as solved
(b) Pick one of these unexpanded nodes & expand
it. if there are no successors, assign FUTILITY as the
value of this node. Otherwise, add its successors to
the graph & for each of them compute f` (use only h`
& ignore g). If f` of any node is 0, mark that node as
SOLVED
Algorithm: Problem Reduction
(c) Change the f` estimate of the newly expanded node to reflect the new
info provided by its successors
-propagate this change backward through the graph
-if any node contains a successor are whose descendants are all solved,
label the node itself as SOLVED
-at each node that is visited while going up the graph, decide which of its
successor arcs is the most promising & mark it as part of the current
best path
- This may cause the current best path to change
- This propagation of revised cost estimates back up the tree was not
necessary in the BFS algo because only unexpanded nodes were
examined
- But now expanded nodes must be reexamined so that the best current
path can be selected
- Thus it is important that their f` value estimates available
Problem Reduction Example
Before Step 1 Before Step 2 Before Step 3
A (5) A (6) A

(9) (9)
B C D B C D (10)
(3) (4) (5) (3) (4)
Before Step 4 E F
A (4) (4)

(12) -A expanded to B,C,D


B (6) C D (10) -The arc D is labeled as the most promising
(4)
(10)
one Emerging from A, since it cost 6
G H D D compared to B & C (9)
(5) (7) (4) (4)
-
Problem Reduction: Limitation
A A

B C D B C D

E F G H E F
G H

Unsolvable Unsolvable
I J I J

-suppose J is expanded at the next -This new path to E is longer than


step the previous path to E going
-that one of its successors in node through C
E -but since the path through C will only
Long path may be better lead to a solution if there is also a
solution to D, which we Know there is
not, the path through J is better
Problem Reduction: Limitation
• Fails to take into account any interaction A
between sub goals
• Both node C/E ultimately lead to a solution,
algorithm will report a complete solution that
includes both of them D
• AND-OR graph states that for A to be solved, C
both C/D must be solved (5) E
• The algo considers the solution of D as a (2)
completely separate process from the solution
of C
• Looking just at the alternatives from D, E is the
best path
• But it turns out that C is necessary anyway, so
it would be better also to use it to satisfy D
• But the algo does not consider such
interactions, it will find a non-optimal path.
CSP
• Standard search problem:
– state is a "black box“ – any data structure that supports successor function, heuristic
function, and goal test
• CSP:
– goal test is a set of constraints specifying allowable combinations of values for
subsets of variables

• Simple example of a formal representation language

• Allows useful general-purpose algorithms with more power than standard


search algorithms


» state is defined by variables Xi with values from
domain Di
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)}
Example: Map-Coloring

• Solutions are complete and consistent assignments satisfying all


constraints,
e.g.,{WA = red, NT = green, Q = red, NSW = green, V = red,
SA = blue, T = green}
Constraint graph
• Binary CSP: each constraint relates at most two variables
• Constraint graph: nodes are variables, arcs show constraints

• General-purpose CSP algorithms use the graph structure to


speed up search.
-E.g., Tasmania is an independent subproblem!
Varieties of CSPs
• Discrete variables
finite domains; size d  O(dn) complete assignments
• e.g., Boolean CSPs, incl. Boolean satisfiability (NP-
complete) infinité domaines (integers, strings, etc.)
• e.g., job scheduling, variables are start/end days for each
job
• need a constraint language, e.g., Start Job1 + 5 StartJob3
• linear constraints solvable, nonlinear undecidable
• Continuous variables
• e.g., start/end times for Hubble Telescope observations
• linear constraints solvable in poly time by LP methods
Varieties of CSPs
 Unary constraints involve a single variable,
SA  green
 Binary constraints involve pairs of variables,
SA  WA
 Higher-order constraints involve 3 or more variables,
cryptarithmetic column constraints
 Preferences (soft constraints), e.g., red is better than
green
-often representable by a cost for each variable
assignment constrained optimization problems
Example: Cryptarithmetic
SEND  Initial State: No two letters have
+MORE the same value
--------------- The sums of the digits must be as
MONEY shown in the problem
 Goal state: all letters have been assignedd a digit in
such a way that all the initial constraints are satisfied
Solution in cycles:
-constraints are propagate by using rules that
correspond to the properties of arithmetic
-a value is guessed for some letter whose value is
not yet determined
Example: Cryptarithmetic
• CONSTRAINTS:
1. no two digit can be assigned to same letter Initial state of problem.
D=?
2. only single digit number can be assign to a E=?
letter. Y=?
N=?
3. no two letters can be assigned same digit. R=?
O=?
4. Assumption can be made at various levels
such that they do not contradict each other. S=?
M=?
5. The problem can be decomposed into secured C1=?
constraints. A constraint satisfaction C2=?
approach may be used. C1 ,C 2, C3 stands for the
carry variables
6. Any of search techniques may be used. respectively.

7. Backtracking may be performed as applicable


us applied search techniques.

8. Rule of arithmetic may be followed.


Initial state
S E N D
+ M OR E
M=1 - --------------
S=8 or 9 M O N E Y
O=0 or 1O=0
N=E or E+1 N=E+1
C2=1 M=1, since two single-digit numbers
N+R>8 plus a carry cannot total more than 19
E<>9 S=8/9, since S+M+C3>9 (to generate a carry)
E=2 & M=1, S+1+C3>9, so S+C3>8, & C3 is at most 1
N=3
O=0, Since S + M(1)+C3(<=1) must be at least
R=8 or 9 10 to generate a carry & it can be at most 11
2+D=Y or 2+D=10+Y But M is already 1, So, O must be 0.
N=E/E+1, depending on the value of C2. But N
C1=0 C1=1
2+D=Y Cannot have the same value as E. So, N=E+1
2+D=10+Y
N+R=10+E & C2=1
D=8+Y
R=9 D=8 or 9
In order to C2=1, the Sum N+R+C1>9, N+R>8
S=8 N+R cannot be greater than 18, even a carry in
D=8 D=9
So, E cannot be 9
Y=0 Y=1

Conflict Conflict We need to guess, let E=2


Step 1
Step 2
Step 3
Solution!!!
• Y= 2 C3 (0) C2 (1) C1 (1)
• D=7 S (9) E (5) N (6) D (7)
• S=9 + M (1) O (0) R (8) E (5)
--------------------------------------------------------
• R=8 M (1) O (0) N (6) E (5) Y (2)
• N=6
• E=5
• O=0
• M=1
• C1=1
• C2=0
• C3=0
More Cryptarithmetic Problem
BLACK 7 9 2 0 8 CRASH 3 6 84 5
GREEN 5 3 4 4 6 HACKER 5 8 3 9 2 6
------------ ----------------- ------------ -------------------
ORANGE 1 3 2 6 5 4 REBOOT 6 2 0 7 7 1

BASIC 6 0 8 5 2 CROSS 9 6 2 3 3
LOGIC 4 7 3 5 2 ROADS 6 2 5 1 3
------------ ----------------- ------------ -----------------
ORANGE 1 0 8 2 0 4 DANGER 1 5 8 7 4 6
More Cryptarithmetic Problem
TAKE 3 9 6 1 BASE 7 4 8 3
A 9 BALL 7 4 5 5
CAKE 2 9 6 1 ------------ -----------------
------------ ----------------- GAMES 1 4 9 3 8
KATE 6 9 3 1

EAT 8 1 9 FOOT ? ? ? ?
THAT 9 1 1 9 BALL ? ? ? ?
------------ ----------------- ------------ -----------------
APPLE 10 0 3 8 GAMES ? ? ? ? ?
16 possible
solutions

You might also like