You are on page 1of 32

Session No: CO2-session 12 & 13

Session Topic: Constraint satisfaction problem

AI for DATA Science


(Course code: 21CS2213RB)
Session Objective
• Students will be able to learn about Constraint Satisfaction Problems
(CSP) and its application to real world examples.
Topic to be Covered
• Constraint Satisfaction problems (CSPs)
• Example : Map-Coloring
• Constraint-Graph
• Algorithm : Constraint Satisfaction
• Example : Constraint Satisfaction
• Crypt-arithmetic Problems
• Types of Constraints
• Forward Checking
• Backtracking

• Conclusion

• References
Constraint satisfaction problems (CSPs)
• A problem is solved when each variable has a value that satisfies all the constraints on the
variable.

• A problem described this way is called a constraint satisfaction problem, or CSP.

• A constraint satisfaction problem consists of three components X, D, and C:


• X is a set of variables, {Xi, ., Xn,}.
• D is a set of domains, {Di, , Dn}one for each variable.
• C is a set of constraints that specify allowable combinations of values.
Each domain Di consists of a set of allowable values,{v1,v2,…vn} for variable X.
Each constraint Ci consists of a pair scope rely, where scope is a tuple of variables that participate in the
constraint.
And is a relation that defines the values that those variables can take on.
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

• e.g., WA = red, NT = green, Q = red, NSW = green, V = red, SA = blue, T = green.


Constraint graph

• Constraint graph: nodes are variables, arcs are constraints


Constraint Satisfaction
Constraint Satisfaction problems in AI have goal of discovering some problem state that
satisfies a given set of constraints.
• Constraint satisfaction is a search procedure that operates in a space of constraint sets. The
initial state contains the constraints that are originally given in the problem description. A
goal state is any state that has been constrained “enough” where “enough” must be defined
for each problem. For example, in cryptarithmetic, enough means that each letter has been
assigned a unique numeric value.
• Constraint Satisfaction is a two step process:
• First constraints are discovered and propagated as far as possible throughout the
system.
• Then if there still not a solution, search begins. A guess about something is made and
added as a new constraint.

8
Algorithm: Constraint Satisfaction
1. Propagate available constraints. To do this first set OPEN to set of all objects that
must have values assigned to them in a complete solution. Then do until an
inconsistency is detected or until OPEN is empty:
a. Select an object OB from OPEN. Strengthen as much as possible the set of
constraints that apply to OB.
b. If this set is different from the set that was assigned the last time OB was
examined or if this is the first time OB has been examined, then add to OPEN
all objects that share any constraints with OB.
c. Remove OB from OPEN.
2. If the union of the constraints discovered above defines a solution, then quit and
report the solution.

9
3. If the union of the constraints discovered above defines a contradiction, then
return the failure.
4. If neither of the above occurs, then it is necessary to make a guess at something in
order to proceed. To do this loop until a solution is found or all possible solutions
have been eliminated:
a) Select an object whose value is not yet determined and select a way of
strengthening the constraints on that object.
b) Recursively invoke constraint satisfaction with the current set of constraints
augmented by strengthening constraint just selected.

10
Constraint Satisfaction: Example

Crypt arithmetic Problem:


SEND
+MORE
-----------
MONEY

Initial State:
• No two letters have the same value
• The sums of the digits must be as shown in the problem

Goal State:
• All letters have been assigned a digit in such a way that all the initial constraints are satisfied.

11
 
Constraint Satisfaction: Example
                                           

• Keeping all the constraints in mind, the final resultant is as follows:


Crypt arithmetic Problem
The solution process proceeds in cycles. At each cycle, two significant things are done:

1. Constraints are propagated by using rules that correspond to the properties of


arithmetic.
2. A value is guessed for some letter whose value is not yet determined.
A few Heuristics can help to select the best guess to try first:
• If there is a letter that has only two possible values and other with six possible values,
there is a better chance of guessing right on the first than on the second.
• Another useful Heuristic is that if there is a letter that participates in many constraints
then it is a good idea to prefer it to a letter that participates in a few.

13
Initially, rules for propagating constraints generate the following additional constraints.

 M=1, since two single digit numbers plus a carry can not total more than 19
 S=8 or 9, since S+M+C3>9 (to generate the carry) and M=1, S+1+C3>9, So S+C3>8
and C3 IS at most 1.
 O=0, since S+M(1)+C3 (<=1) must be at least 10 to generate a carry and it can be at
most 11. ButM is already 1, so O must be0.
 N=E or E+1 depending on the value of C2. But N can not have the same value as E. So
N=E+1 and C2 is 1.
 In order for C2 to be 1, the sum of N+R+C1 must be greater than 9, so N+R must be
greater than 8.
 N+R can not be greater than 18, even with a carry in, so E can not be 9.
At this point, let us assume that no more constraints can be generated. Then, to make
progress from here, we must guess. Suppose E is assigned the value 2.
Now the next cycle begins.
The constraint propagator now observes that:
N=3, since n+E+1
 R=8 or 9, since R+N(3)+C1 (1 or 0) = 2 or 12. But since N is already 3, the sum of
these nonnegative numbers can not be less than 3. Thus R+3+(0 or 1)=12 and R=8 or
9.
2+D=y or 2+D=10+y, from the sum in the right most column.
Initial state:
• No two letters have
the same value. M=1
S = 8 or 9 SEND
• The sum of the digits
O=0 
must be as shown. MORE
N=E+1
C2 = 1 MONEY
N+R>8
E9
E=2
N=3
R = 8 or 9
2 + D = Y or 2 + D = 10 + Y
C1 = 0 C1 = 1

2+D=Y 2 + D = 10 + Y
N + R = 10 + E D=8+Y
R=9 D = 8 or 9
S =8
D=8 D=9
Y=0 Y=1
16
Types of constraints
• Unary constraints involve a single variable,
• e.g., SA ≠ green

• Binary constraints involve pairs of variables,


• e.g., SA ≠ WA

• Higher-order constraints involve 3 or more variables,


• e.g., crypt arithmetic column constraints
Forward checking
• Idea:
• Keep track of remaining legal values for unassigned variables
• Terminate search when any variable has no legal values
Forward checking
• Idea:
• Keep track of remaining legal values for unassigned variables
• Terminate search when any variable has no legal values
Forward checking
• Idea:
• Keep track of remaining legal values for unassigned variables
• Terminate search when any variable has no legal values
Forward checking
• Idea:
• Keep track of remaining legal values for unassigned variables
• Terminate search when any variable has no legal values
Forward checking
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 algorithms repeatedly enforce constraints locally…
Constraint propagation: Inferences in CSPs
•In regular state-space search, an algorithm can do only one thing: search.

• In CSPs there is a choice: an algorithm can search (choose a new variable assignment from
several possibilities) or do a specific type of inference called constraint propagation.

•Constraint Propagation is nothing but using the constraints to reduce the number of legal
values for a variable, which in turn can reduce the legal values for another variable, and so
on.

•Constraint propagation may be intertwined with search, or it may he done as a


preprocessing step, before search starts_ Sometimes this preprocessing can solve the whole
problem, so no search is required at all.
•The key idea is local consistency.

•If we treat each variable as a node in a and each binary constraint as an arc, then the
process of enforcing local consistency in each pan of the graph causes inconsistent values
to be eliminated throughout the graph.

•There are different types of local consistency.

 Node consistency
 Arc consistency
 Path consistency
 K-consistency
Backtracking search
• The term backtracking search is used for a depth-first search that chooses values for one
variable at a time and backtracks when a variable has no legal values left to assign.

• It repeatedly chooses an unassigned variable, and then tries all values in the domain of that
variable in turn, trying to find a solution.

• If an inconsistency is detected, then BACKTRACK returns failure, causing the previous


call to try another value

• Only need to consider assignments to a single variable at each node

• Depth-first search for CSPs with single-variable assignments is called backtracking search
Backtracking example
Backtracking example
Backtracking example
Backtracking example
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?


Thank You

Query?
References

•https://towardsdatascience.com/ai-planning-using-constraint-satisfaction-problems-eb1be5
466af6

• https://www.cpp.edu/~ftang/courses/CS420/notes/CSP.pdf
• https://artint.info/html/ArtInt_76.html

You might also like