Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

ARTIFICIAL INTELLIGENCE

CSE 3201

Constraint Satisfaction Problems


Constraint Satisfaction Problems
• A CSP consists of:
• Finite set of variables X1, X2, …, Xn
• Nonempty domain of possible values for each variable
D1, D2, … Dn where Di = {v1, …, vk}
• Finite set of constraints C1, C2, …, Cm
—Each constraint Ci limits the values that variables can take, e.g., X1 ≠
X2 .
A state is defined as an assignment of values to some or all variables.
A consistent assignment does not violate the constraints.
• Example: Sudoku.

2
Constraint Satisfaction Problems
 An assignment is complete when every variable is assigned a value.
 A solution to a CSP is a complete assignment that satisfies all constraints.
 Applications:
• Map coloring (coloring different regions of map, ensuring no adjacent
regions have the same color)
• Cryptarithmetic Problems (alphabets to numbers)
• n-Queen (In an n-queen problem, n queens should be placed in an n X n
matrix such that no queen shares the same row, column or diagonal.)
• Sudoku (a number grid)
• Crossword puzzle etc.

3
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
—So (WA,NT) must be in {(red,green),(red,blue),(green,red), …}

4
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

5
Benefits of CSP
 Clean specification of many problems, generic goal, successor function
• Just represent problem as a CSP & solve with general package

 CSP “knows” which variables violate a constraint


• And hence where to focus the search

 CSPs: Automatically prune off all branches that violate constraints


• (State space search could do this only by hand-building constraints
into the successor function)

6
CSP Representations
 Constraint graph:
• nodes are variables
• arcs are constraints

 Standard representation pattern:


• variables with values

 This problem: A binary CSP:


• each constraint relates two variables

7
Varieties 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

8
CSP as a search problem
 A CSP can easily be expressed as a search problem
• Initial State: the empty assignment {}.
• Successor function: Assign value to any unassigned variable provided that
there is not a constraint conflict.
• Goal test: all the variables have assignments.
• Path cost: a constant cost for every step.

 Solution is always found at depth n, for n variables


• Depth First Search can be used.

9
Backtracking search
 Note that variable assignments are commutative
• Eg [ step 1: WA = red; step 2: NT = green ]
equivalent to [ step 1: NT = green; step 2: WA = red ]

 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 search is the basic algorithm for CSPs.

10
Example

11
Example

12
Example: 4-Queens Problem

13
Example: 4-Queens Problem

14
Example: 4-Queens Problem

15
Example: 4-Queens Problem

16
Example: 4-Queens Problem

17
Example: 4-Queens Problem

18
Example: 4-Queens Problem

19
Example: 4-Queens Problem

20
Example: 4-Queens Problem

21
Example: 4-Queens Problem

22
Example: 4-Queens Problem

23
Example: 4-Queens Problem

24
Example: 4-Queens Problem

25
Cryptarithmetic Problem
■ Cryptarithmetic Problem is a type of constraint satisfaction problem where
the game is about digits and its unique replacement either with alphabets or
other symbols. In cryptarithmetic problem, the digits (0-9) get substituted
by some possible alphabets or symbols. The task in cryptarithmetic problem
is to substitute each digit with an alphabet to get the result arithmetically
correct.

26
Cryptarithmetic Problem Rules
1. Digit range should be 0 to 9
2. Each letter represents only one digit throughout the problem and each variable
should have unique value.
3. Numbers must not begin with zero (0134 is wrong, 1043 is correct)
4. You have to find the value of each letter in the problem.
5. After replacing letters by their corresponding digits, the result of the arithmetic
operation must be correct.

27
Cryptarithmetic Problem

28
Cryptarithmetic Problem

29
Cryptarithmetic Problem

30
Cryptarithmetic Problem

31
Tracing cryptarithmetic problem

32
Tracing cryptarithmetic problem

33
Tracing cryptarithmetic problem

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

35
Exercise

36
Solution

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

38

You might also like