The document provides an overview of constraint satisfaction problems (CSPs). It defines a CSP as consisting of variables with domains of possible values, and constraints specifying allowed value combinations. CSPs can represent many problems using variables and constraints rather than explicit state representations. Backtracking search is commonly used to solve CSPs by trying value assignments and backtracking when constraints are violated.
2. CSP – Constraint Satisfaction Problem
Standard Search Problem >>
▸Searches a space of states for the solution.
▸Heuristics are domain specific.
▸Each state is atomic/indivisible i.e. a black box with no internal structure.
CSP >>
▸CSP uses a factored representation for each state: a set of variables, each of which has a value.
▸A problem is solved when each variable has a value that satisfies all the constraints in the variable.
▸A problem described this way is called a constraint satisfaction problem.
Advantages
▸Uses general-purpose rather than problem-specific heuristics to enable the solution of complex problems.
▸Represents each state as a set of variables and takes advantage of the structure of states.
▸Eliminates large portions of the search space all at once by identifying variable/value combinations that violate the
constraints.
2
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
3. CSP >> Formal Definition
A constraint satisfaction problem consists of three components, X, D, and C:
▸X is the set of variables, { X1, . . . , Xn }
▸D is a set of domains, { D1, . . . , Dn }, one for each variable.
▸C is a set of constraints that specify allowable combinations of values.
Here,
▸Each domain Di consists of a set of allowable values, { v1, . . . , vk } for each variable Xi
▸Each constraint Ci consists of a pair 𝑠𝑐𝑜𝑝𝑒, 𝑟𝑒𝑙 ,
where scope is a tuple of variables that participate in the constraint
and rel is a relation that defines the values that those variables can take on.
▸A relation can be represented as an explicit list of all tuples of values that satisfy the constraint,
or as an abstract relation. For example,
𝑋1, 𝑋2 , [ 𝐴, 𝐵 , (𝐵, 𝐴)] or 𝑋1, 𝑋2 , 𝑋1 ! = 𝑋2
3
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
4. CSP >> Solution
▸Each state in a CSP is defined by an assignment of values to some or all of the variables.
{ Xi = vi , Xj = vj , . . . }
▸An assignment that does not violate any constraints is called a consistent or legal assignment.
▸A complete assignment is one in which every variable is assigned.
▸A partial assignment is one that assigns values to only some of the variables.
▸A solution to a CSP is consistent and complete assignment.
Advantages of formulating a problem as a CSP
▸CSPs yield natural representation for a wide variety of problems.
▸CSP solvers can be faster than state-space searchers because the CSP solver can quickly eliminate large swatches of the
search space.
▸With CSP, once we find out that a partial assignment is not a solution, we can immediately discard further refinements of
the partial assignment.
▸We can see why a assignment is not a solution – which variables violate a constraint – we can focus attention on the
variables that matter.
4
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
5. CSP >> Problem Formulation
Map Coloring Problem >>
your task is to color each region either red, green, or blue in such a way that no neighboring regions have the
same color.
CSP Formulation
Each region is a variable, X = { WA, NT, Q, NSW, V, SA, T }
Domain of each variable, Di = { red, green, blue }
Set of constraints,
C = { SA ≠ WA, SA ≠ NT, SA ≠ Q, SA ≠ NSW, SA ≠ V,
WA ≠ NT, NT ≠ Q, Q ≠ NSW, NSW ≠ V }
Here, SA ≠ WA is a shortcut for 𝑠𝑐𝑜𝑝𝑒, 𝑟𝑒𝑙 = 𝑆𝐴, 𝑊𝐴 , 𝑆𝐴 ≠ 𝑊𝐴
5
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
6. CSP >> Possible Solution
There are many possible solutions to this problem,
Such as
{ WA = red, NT = green, Q = red, NSW = green, V = red, SA = blue, T = green }
6
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
7. CSP >> Constraint Graph
Binary CSP >> each constraint relates at most two variables.
Constraint Graph >>
▸Nodes correspond to the variables of the problem.
▸An edge connects any two variables that participate in a constraint.
7
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
8. CSP >> Job-shop Scheduling
Let’s consider a small part(wheel installation) of the car assembly, consisting of 15 tasks.
▸Install axles (front and back) – requires 10 mins to install
▸Affix all four wheels (right and left, front and back) – takes 1 mins
▸Tighten nuts for each wheel – takes 2 mins
▸Affix hubcaps and – requires 1 mins
▸Inspect the final assembly – takes 3 mins
And get the whole assembly done in 30 mins
8
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
I
n
s
p
e
c
t
9. CSP >> Job-shop Scheduling
We can represent the tasks with 15 variables:
X = { AxleF, AxleB, WheelRF, WheelLF, WheelRB, WheelLB, NutsRF, NutsLF, NutsRB, NutsLB, CapRF, CapLF, CapRB, CapLB, Inspect }
Precedence constraints >> whenever a task T1 must occur before task T2 and task T1 takes duration d1 to complete, then we
can represent the constraint as
T1 + d1 <= T2
So the constraints are,
C = {
AxleF + 10 <= WheelRF , AxleF + 10 <= WheelLF , AxleB + 10 <= WheelRB , AxleB + 10 <= WheelLB ,
WheelRF + 1 <= NutsRF , WheelLF + 1 <= NutsLF , WheelRB + 1 <= NutsRB , WheelLB + 1 <= NutsLB ,
NutsRF + 2 <= CapRF , NutsLF + 2 <= CapLF , NutsRB + 2 <= CapRB , NutsLB + 2 <= CapLB ,
CapRF + 1 <= Inspect, CapLF + 1 <= Inspect, CapRB + 1 <= Inspect, CapLB + 1 <= Inspect,
AxleF + 10 <= Inspect, AxleB + 10 <= Inspect,
WheelRF + 1 <= Inspect , WheelLF + 1 <= Inspect , WheelRB + 1 <= Inspect , WheelLB + 1 <= Inspect ,
NutsRF + 2 <= Inspect , NutsLF + 2 <= Inspect , NutsRB + 2 <= Inspect , NutsLB + 2 <= Inspect
}
As inspection takes 3 mins to complete and the total task need to complete within 30 minutes,
So domain of each variable, Di = { 1, 2, 3, . . . . . . , 27 }
9
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
10. CSP >> N-Queens
Set of variables, X = { Q1, Q2, … …, QN }
Each variable Qi represents the position of each queen in columns 1, 2, … …, N
Domains for each variable, Di = { 1, 2, … …, N }
Constraints:
implicit form: ∀𝑖, 𝑗 no-threatening (Qi, Qj)
explicit form: (Q1, Q2) ∈ { (1,3), (1,4), … … }
… …
10
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
11. CSP >> Cryptarithmetic puzzle
Variables, X = { F, T, U, W, R, O, X1, X2, X3 }
Domains for variables X1, X2, X3 is, { 0, 1 }
Domains for all other variables, Di = { 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, T ≠ 0, F ≠ 0
11
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Constraint hypergraph
12. CSP >> Sudoku
Variables, X = each open square
Domains for each variable, Di = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
Constraints:
9-way alldiff for each column
9-way alldiff for each row
9-way alldiff for each region
12
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
13. CSP >> Example 1
You are in charge of scheduling for computer science classes that meet Mondays, Wednesdays and Fridays. There are 5
classes that meet on these days and 3 professors who will be teaching these classes. You are constrained by the fact that
each professor can only teach one class at a time.
The classes are:
▸Class 1 - Intro to Programming: meets from 8:00-9:00am
▸Class 2 - Intro to Artificial Intelligence: meets from 8:30-9:30am
▸Class 3 - Natural Language Processing: meets from 9:00-10:00am
▸Class 4 - Computer Vision: meets from 9:00-10:00am
▸Class 5 - Machine Learning: meets from 9:30-10:30am
The professors are:
▸Professor A, who is available to teach Classes 3 and 4.
▸Professor B, who is available to teach Classes 2, 3, 4, and 5.
▸Professor C, who is available to teach Classes 1, 2, 3, 4, 5
13
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
14. CSP >> Example 1 Solution
14
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Variables Domains
C1 C
C2 B, C
C3 A, B, C
C4 A, B, C
C5 B, C
Constraints:
C1≠C2
C2≠C3
C3≠C4
C4≠C5
C2≠C4
C3≠C5
15. CSP >> Constraint Graph Practice 1
Suppose that a delivery robot must carry out a number of delivery activities, a, b, c, d, and e. Suppose that each activity
happens at any of times 1, 2, 3, or 4. Let A be the variable representing the time that activity a will occur, and similarly for the
other activities.
Suppose the following constraints must be satisfied:
▸Activity b cannot be done at time 3.
▸Activity c cannot be done at time 2.
▸Activity a and d must be done at the same time.
▸Activity a and b cannot be done at the same time.
▸Activity b and c cannot be done at the same time.
▸Activity b and d cannot be done at the same time.
▸Activity c must be done before activity d.
▸Activity e must be done before all other activities.
15
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
16. CSP >> Constraint Graph Practice 2
Imagine the following scenario:
You are trying to divide eight students in three groups for a project show. The three groups are for Machine learning,
Software engineering and Networking. The assignment of groups to students is subject to the following constraints:
▸Student 1, 2 and 3 must be in different groups as they will be group leaders.
▸Student 4 and 7 want the same group as they are close friends.
▸Student 5 will not be in Software engineering group.
▸Student 3 and 6 don’t like each other and won’t be in the same group.
16
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
17. CSP >> Constraint Graph Practice 3
Suppose you are designing a CSP for a job shop scheduling problem with five jobs namely J1, J2, J3, J4 and J5. The required
time to complete each job is given below. In the job shop scheduling problem, the task is to order or schedule the jobs.
▸J1 has to be completed before starting all other jobs.
▸J2 has to be completed before starting J3 and J4.
▸J4 and J5 cannot be done parallelly (one has to be completed before the other can be started).
▸All jobs must be completed within one hour.
17
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Job 1 2 3 4 5
Required Time(mins) 10 5 15 10 20
18. CSP >> Types of Variables
Discrete variables >>
▸Finite domains:
▹n variables, each domain size d; so O(dn) complete assignments.
▹e.g. Map coloring problem, 8-queens problem.
▸Infinite domains:
▹Set of integers, strings, etc.
▹e.g. job scheduling problem with no deadline, variables are start/end days for each job.
▹Need a constraint language to describe constraints, ex. StartJob1 + 5 ≤ StartJob3
Continuous variables >>
▸e.g., start/end times for Hubble Telescope observations
▸linear constraints solvable in poly time by LP methods
18
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
19. CSP >> Types of Constraints
▸Unary constraints involve a single variable,
e.g., SA ≠ green
▸Binary constraints involve pairs of variables,
e.g., SA ≠ WA
only binary constraints can be represented as a constraint graph.
▸Higher-order constraints involve 3 or more variables,
e.g., SA ≠ WA ≠ NT
▸Global constraints involve arbitrary number of variables,
e.g. alldiff that represents all of the variables involved in the constraint must have different values.
19
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
20. CSP >> Solving CSPs
We can formulate CSP problems as standard search problems.
▸States: defined by the values assigned so far
▸Initial state: the empty assignment { }
▸Successor function: assign value to variable without conflict
-fail, if no legal assignments possible
▸Goal test: the current assignment is complete
▸Path cost: constant step cost
Standard search algorithms can be applied directly to solve CSPs.
Let, n variables and the domain size for each variable is d
For BFS:
Branching factor, b = nd
Nodes at level 1 = nd
Nodes at level 2 = nd * (n-1) d
Nodes/leaves at level n = nd * (n-1)d * … … * d = n! * dn
20
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
21. CSP >> Backtracking Search
A crucial property to all CSPs: commutativity
▸The order of application of any given set of actions has no effect on the outcome.
▸Variable assignments are commutative, i.e. [WA=red then NT=green] same as [NT=green then WA=red]
So new need to consider only assignments to a single variable at each node. So branching factor becomes b=d and total no
of leaves reduces to bd
Depth-first search for CSPs with single-variable assignment and backtracks when a variable has no legal values left to
assign is called Backtracking search.
▸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.
▸Backtracking search is the basic uninformed algorithm for CSPs.
21
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
22. CSP >> How Backtrack Works!
22
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
23. CSP >> Backtracking Search Algorithm
23
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
24. CSP >> Backtracking Search Example 1
24
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
25. CSP >> Backtracking Search Example 1
25
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
26. CSP >> Backtracking Search Example 1
26
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
27. CSP >> Backtracking Search Example 1
27
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
28. CSP >> Backtracking Search Example 2
28
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
29. CSP >> Backtracking Search Practice
Let,
X = { A, B, C}
D = { DA, DB, DC }
where DA = DB = DC = { 1, 2, 3 }
C = {
A > B,
B ≠ C,
A ≠ C
}
Draw the Constraint Graph and perform Backtrack Search.
29
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
30. CSP >> Improving Backtracking Efficiency
General-purpose methods can give huge gains in speed:
Issue 1 >> Which variable should be assigned next?
sol >> Minimum remaining values heuristic
sol >> Degree heuristic
Issue 2 >> In what order should its values be tried?
sol >> Least constraining value heuristic
Issue 3 >> Can we detect inevitable failure early?
sol >> Forward checking constraint propagation
sol >> Maintaining Arc-Consistency inference
30
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
31. CSP >> Variable Selection [MRV Heuristic]
Minimum remaining values / Most constrained variable / Fail-first heuristic >>
▸Choose the variable with the fewest legal values.
Can’t answer the following questions:
▸Which variable to choose first?
▸Which variable to choose if they have the same minimum remaining number of legal values?
31
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
MRV MRVMRV
32. CSP >> Variable Selection [Degree Heuristic]
Degree / Most constraining variable heuristic >>
▸Selecting the variable that has the largest number of constraints on other unassigned variables.
▸Tie-breaker among MRV
32
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
MRV+DH MRV+DH MRV
33. CSP >> Value Ordering [Least-constraining-value]
Least-constraining-value heuristic >>
▸Choose the value that rules out fewest choices for the neighboring variables in the constraint graph.
▸Leaves maximum flexibility for the neighbors.
33
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
LCV suggests RED
34. CSP >> Early Failure Detection [Forward-checking]
Forward-checking Inference >>
▸Keep track of remaining legal values for unassigned variables.
▸Terminate search when any variable has no legal values.
34
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
35. CSP >> Early Failure Detection [Forward-checking]
Forward-checking Inference >>
▸Keep track of remaining legal values for unassigned variables.
▸Terminate search when any variable has no legal values.
35
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
36. CSP >> Early Failure Detection [Forward-checking]
Forward-checking Inference >>
▸Keep track of remaining legal values for unassigned variables.
▸Terminate search when any variable has no legal values.
36
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
37. CSP >> Early Failure Detection [Forward-checking]
Forward-checking Inference >>
▸Keep track of remaining legal values for unassigned variables.
▸Terminate search when any variable has no legal values.
Problems >>
▸FC only propagates information from assigned to unassigned variables, but doesn’t provide early detection for all failures.
▸For example, NT and SA can’t both be blue!
37
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
38. CSP >> Problem 1 [Backtrack + MRV + DH + FC]
Consider the constraint graph on the right.
The domain for every variable is [1,2,3,4].
There are 2 unary constraints:
▸variable “a” cannot take values 3 and 4.
▸variable “b” cannot take value 4.
There are 8 binary constraints stating that variables connected by an edge cannot have
the same value.
Find a solution for this CSP by using backtracking search with the following heuristics:
minimum value heuristic, degree heuristic, forward checking. Explain each step of your
answer.
38
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
39. CSP >> Problem 2 [Backtrack + Forward Checking]
39
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
40. CSP >> Problem 2 (Backtrack + Forward Checking)
40
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
41. CSP >> Problem 2 (Backtrack + Forward Checking)
41
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
42. CSP >> Problem 2 (Backtrack + Forward Checking)
42
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
43. CSP >> Problem 2 (Backtrack + Forward Checking)
43
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
44. CSP >> Problem 2 (Backtrack + Forward Checking)
44
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
45. CSP >> Problem 2 (Backtrack + Forward Checking)
45
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
46. CSP >> Problem 2 (Backtrack + Forward Checking)
46
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
47. CSP >> Problem 2 (Backtrack + Forward Checking)
47
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
48. CSP >> Problem 2 (Backtrack + Forward Checking)
48
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
49. CSP >> Problem 2 (Backtrack + Forward Checking)
49
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
50. CSP >> Problem 2 (Backtrack + Forward Checking)
50
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
51. CSP >> Practice 1
Suppose that a delivery robot must carry out a number of delivery activities, a, b, c, d, and e. Suppose that each activity
happens at any of times 1, 2, 3, or 4. Let A be the variable representing the time that activity a will occur, and similarly for
the other activities.
Suppose the following constraints must be satisfied:
▸Activity b cannot be done at time 3.
▸Activity c cannot be done at time 2.
▸Activity a and d must be done at the same time.
▸Activity a and b cannot be done at the same time.
▸Activity b and c cannot be done at the same time.
▸Activity b and d cannot be done at the same time.
▸Activity c must be done before activity d.
▸Activity e must be done before all other activities.
Show the steps followed by the backtracking algorithm with the minimum remaining values heuristic, the degree heuristic
and forward checking inference.
51
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
52. CSP >> Practice 2
You want to color the vertices of the graph shown below with the colors red, green and blue in a way that adjacent vertices
have different colors.
Construct this problem as a CSP, show the steps followed by the backtracking algorithm with minimum value, degree
heuristic and forward checking inference.
52
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
53. CSP >> Practice 3
You want to color the vertices of the graph shown given below with the colors red, green and blue in a way that adjacent
vertices have different colors.
Construct this problem as a CSP, show the steps followed by the backtracking algorithm with the minimum remaining
values heuristic, degree heuristic and forward checking inference.
53
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
54. CSP >> Arc Consistency
54
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Out of Syllabus
55. CSP >> Local Search
▸Local search use complete-state formulation
- The initial state assigns a value to every variable
▸Variable selection: randomly select any conflicted variable
▸Value selection: min-conflicts heuristic
- Select the value that results in the minimum number of conflicts with other variables
55
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
56. CSP >> Local Search
56
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU