Professional Documents
Culture Documents
Lecture 5 - Constraint Satisfaction Problems
Lecture 5 - Constraint Satisfaction Problems
Outline
1. Definition of CSPs
2. CSP Examples
3. Backtracking Search
4. Domain Update
5. Dynamic Ordering
1
Defining Constraint Satisfaction Problems
Solving CSPs
2
CSPs vs Search Problems
Standard search problem: CSP:
• A state is a “black box” – any old data • A state is a factored representation –
support goal test, successor, cost. defined by variables 𝑋𝑖 with values from
• The order of the sequence of actions to domain 𝐷𝑖 .
reach the goal is important. • A goal test is a set of constraints
specifying allowable values for variables.
• We don’t often care about how do we get
to the goal, as long as we reach the goal
in a reasonable amount of time
Outline
1. Definition of CSPs
2. CSP Examples
3. Backtracking Search
4. Domain Update
5. Dynamic Ordering
3
Example: Map Coloring
Tasmania
Exercise
Victoria
Tasmania
4
Map Coloring Solution
Northern
Territory
Western Queensland
Australia
South
Australia
New South Wales
Victoria
Tasmania
Constraint Graph
NT
• Binary CSP: each constraint relates at Q
most two variables. WA
• Constrain graph: nodes are variables,
arcs show constraints. SA NSW
10
10
5
Varieties of Variables
• Discrete variables:
◦ Finite domains:
− Assume 𝑛 variable, 𝑑 values, the number of complete assignment is 𝑂(𝑑 𝑛 ).
− E.g., map coloring, 8-queens, …
◦ Infinite domains (integers, strings, etc.)
− Need a constraint language
− E.g., job scheduling 𝑆𝑡𝑎𝑟𝑡𝐽𝑜𝑏1 + 5 ≤ 𝑆𝑡𝑎𝑟𝑡𝐽𝑜𝑏2
• Continuous variables:
◦ Common in operations research: e.g., energy optimization in a production facility.
◦ Linear programming (LP) methods.
11
11
Varieties of Constraints
12
12
6
Example: Cryptarithmetic
Find digit-letter mapping that
solves the equation.
• Variables: 𝐹 𝑇 𝑈 𝑊 𝑅 𝑂 𝑋1 𝑋2 𝑋3
X3 X2 X1
T WO
• Domains: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Constraints
Hypergraph + T WO • Constraints:
F O U R ◦ 𝐴𝑙𝑙𝑑𝑖𝑓𝑓 (𝐹, 𝑇, 𝑈, 𝑊, 𝑅, 𝑂)
◦ 𝑂 + 𝑂 = 𝑅 + 10 . 𝑋1
◦ 𝑋1 + 𝑊 + 𝑊 = 𝑈 + 10 . 𝑋2
F T U W R O ◦ 𝑋2 + 𝑇 + 𝑇 = 𝑂 + 10 . 𝑋3
◦ 𝑋3 = 𝐹
X3 X2 X1
13
13
Real-World CSPs
• Assignment problems
◦ E.g., who teaches what
• Timetabling problems
◦ E.g., which class is offered when and where
• Hardware configurations
• Datacenter layout planning
• Factory scheduling
• Logistics and supply chain
• More example of such CSPs: https://www.csplib.org/
14
14
7
Exercise
Formulate the following cryptarithmetic as a CSP. Draw the constraints hyper graph.
X3 X2 X1
D I G
+ D I G
H O L E
15
15
Exercise: Solution
Formulate the following cryptarithmetic as a CSP. Draw the constraints hyper graph.
X3 X2 X1
D I G
+ D I G H O D L I E G
H O L E
Variables: 𝐻 𝑂 𝐷 𝐿 𝐼 𝐸 𝐺 𝑋1 𝑋2 𝑋3
Domains: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Constraints:
• 𝐴𝑙𝑙𝑑𝑖𝑓𝑓 (𝐻, 𝑂, 𝐷, 𝐿, 𝐼, 𝐸, 𝐺)
X3 X2 X1
• 𝐺 + 𝐺 = 𝐸 + 10 . 𝑋1
• 𝑋1 + 𝐼 + 𝐼 = 𝐿 + 10 . 𝑋2
• 𝑋2 + 𝐷 + 𝐷 = 𝑂 + 10 . 𝑋3
• 𝑋3 = 𝐻
16
16
8
Outline
1. Definition of CSPs
2. CSP Examples
3. Backtracking Search
4. Domain Update
5. Dynamic Ordering
17
17
• Let us start with the straightforward, naive approach, then fix it.
• States are defined by values assigned so far, i.e., partial assignments.
• Solving CSP as a search problem:
◦ Initial state: the empty assignment {}
◦ Successor function: assign a value to an unassigned variable that does not conflict
with current assignment
− Fail if no legal values (not fixable!)
◦ Goal test: the current assignment is complete
• Every solution appears at depth 𝑛, where 𝑛 is the number of variables
◦ Same as games, use DFS
• Path is irrelevant, we care about the solution at the end.
18
18
9
Backtracking Search
Backtracking search (BTS) is a basic uninformed search for CSPs. It uses DFS such
that:
1. Assign one variable at a time: assignments are commutative, e.g., (𝑊𝐴 = 𝑟𝑒𝑑, 𝑁𝑇 =
𝑔𝑟𝑒𝑒𝑛) is the same as (𝑁𝑇 = 𝑔𝑟𝑒𝑒𝑛, 𝑊𝐴 = 𝑟𝑒𝑑).
2. Check constraints on the go: consider values that don’t conflict with previous
assignment.
3. If an assignment fails, backtrack to upper level, and assign other value (expensive).
19
19
BTS Example
NT
Q
WA
Use the BTS to color the shown map. Pick
variable according to the following order: SA NSW
𝑊𝐴, 𝑁𝑇, 𝑆𝐴, 𝑄, 𝑉, 𝑁𝑆𝑊, and 𝑇. The order of
values is 𝑅, 𝐺, and 𝐵.
V
20
20
10
BTS Example
NT
Q
WA
SA NSW
21
21
BTS Example
NT
Q
WA
SA NSW
22
22
11
BTS Example
NT
Q
WA
SA NSW
23
23
BTS Example
NT
Q
WA
SA NSW
24
24
12
BTS Example
NT
Q
WA
SA NSW
25
25
BTS Example
NT
Q
WA
SA NSW
26
26
13
BTS Example
NT
Q
WA
SA NSW
27
27
BTS Example
NT
Q
WA
SA NSW
28
28
14
BTS Example
NT
Q
WA
SA NSW
29
29
BTS Example
NT
Q
WA
SA NSW
30
30
15
BTS Example
NT
Q
WA
SA NSW
31
31
BTS Example
NT
Q
WA
SA NSW
32
32
16
BTS Example
NT
Q
WA
SA NSW
33
33
NT
Q
WA
SA NSW
34
34
17
BTS Exercise
NT
Q
WA
Use the BTS to color the shown map. Pick
variable according to the following order: SA NSW
𝑊𝐴, 𝑁𝑇, 𝑄, 𝑁𝑆𝑊, 𝑉, 𝑆𝐴, and 𝑇. The order of
values is 𝑅, 𝐺, and 𝐵.
V
35
35
Outline
1. Definition of CSPs
2. CSP Examples
3. Backtracking Search
4. Domain Update
5. Dynamic Ordering
36
36
18
Improving BTS
37
37
Improving BTS
38
38
19
Backtracking Search Algorithm
def backtracking_search(csp):
return backtrack({}, csp)
39
Domain Update
• Key idea: Forward checking provides one-step lookahead.
• After assigning a variable 𝑋𝑖 , eliminate inconsistent values from the
domains of 𝑋𝑖 ’s neighbors.
• If any domain becomes empty, don’t recurse, continue with next values.
NT NT
Q Q
Assign Q
value B
WA WA
SA NSW SA NSW
V V
T T
40
40
20
BTS+FC Example
NT
Q
WA
Use the BTS+FC to color the shown map.
Pick variable according to the following SA NSW
order: 𝑊𝐴, 𝑁𝑇, 𝑆𝐴, 𝑄, 𝑉, 𝑁𝑆𝑊, and 𝑇. The
order of values is 𝑅, 𝐺, and 𝐵.
V
41
41
NT
Q
WA
SA NSW
42
42
21
Backtracking Search (With Forward Checking)
NT
Q
WA
SA NSW
43
43
NT
Q
WA
SA NSW
44
44
22
Backtracking Search (With Forward Checking)
NT
Q
WA
SA NSW
45
45
NT
Q
WA
SA NSW
46
46
23
Backtracking Search (With Forward Checking)
NT
Q
WA
SA NSW
47
47
NT
Q
WA
SA NSW
48
48
24
Backtracking Search (With Forward Checking)
NT
Q
WA
SA NSW
49
49
NT
Q
WA
SA NSW
50
50
25
BTS +FC Solution Tree
NT
Q
WA
SA NSW
51
51
BTS+FC Exercise
NT
Q
52
52
26
Outline
1. Definition of CSPs
2. CSP Examples
3. Backtracking Search
4. Domain Update
5. Dynamic Ordering
53
53
• The algorithms we considered so far require the order in which variables or values
picked to be specified.
• Choosing the right order of variables (and values) can noticeably improve the efficiency
of the CSP algorithm.
• Dynamic ordering: let the algorithm decide the next variable or value to be picked for
consideration.
• To implement dynamic ordering, we use two heuristics:
◦ Most constraint variable (MCV) for variable ordering
− Also called Minimum Remaining Value (MRV).
◦ Least constraining value (LCV) for value ordering.
• Note, these heuristics are useful iff domain update is used. Why?
54
54
27
Most Constrained Variable (MCV)
• Key Idea: Choose the variable with the fewest legal values in its domain.
NT
Q
55
55
NT
Q
WA
Use the BTS, with variable ordering to
color the shown map. Pick variables SA NSW
according to the MCV heuristic. Assign WA
first. The order of values is 𝑅, 𝐺, and 𝐵.
V
56
56
28
Backtracking Search (With Variable Ordering)
NT
Q
WA
SA NSW
57
57
NT
Q
WA
SA NSW
58
58
29
Backtracking Search (With Variable Ordering)
NT
Q
WA
SA NSW
59
59
NT
Q
WA
SA NSW
60
60
30
Backtracking Search (With Variable Ordering)
NT
Q
WA
SA NSW
61
61
NT
Q
WA
SA NSW
62
62
31
Backtracking Search (With Variable Ordering)
NT
Q
WA
SA NSW
63
63
NT
Q
WA
SA NSW
64
64
32
Backtracking Search (With Variable Ordering)
NT
Q
WA
SA NSW
65
65
NT
Q
WA
Use the BTS, with variable ordering to
color the shown map. Pick variables SA NSW
according to the MCV heuristic. Assign SA
first. The order of values is 𝑅, 𝐺, and 𝐵.
V
66
66
33
Least Constraining Value (LCV)
NT
Q
WA
SA NSW
What value should we assign to Q?
67
67
NT
Q
WA
68
68
34
Least Constraining Value (LCV)
NT
Q
WA
69
69
Key Idea: Given a variable 𝑋𝑖 , pick the value that rules out the
fewest choices for the neighboring variables. So, this heuristic
allows you to pick the value which gives maximum flexibility for
subsequent variable assignment.
70
70
35
BTS + Value Ordering Example
NT
Q
71
71
NT
Q
WA
SA NSW
Next variable: WA
V
Consistent values:
R=4, G=4, B=4
72
72
36
Backtracking Search (With Value Ordering)
NT
Q
WA
SA NSW
Next variable: NT
V
Consistent values:
G=3, B=3
73
73
NT
Q
WA
SA NSW
Next variable: SA
V
Consistent values:
B=5
74
74
37
Backtracking Search (With Value Ordering)
NT
Q
WA
SA NSW
Next variable: NSW
V
Consistent values:
R=1, G=2
75
75
NT
Q
WA
SA NSW
Next variable: Q
V
Consistent values:
R=0
76
76
38
Backtracking Search (With Value Ordering)
NT
Q
WA
SA NSW
Next variable: V
V
Consistent values:
R=0
77
77
NT
Q
WA
SA NSW
Next variable: T
V
Consistent values:
R=0, G=0, B=0
78
78
39
Backtracking Search (With Value Ordering)
NT
Q
WA
SA NSW
79
79
NT
Q
WA
SA NSW
80
80
40
Homework
NT
Q
WA
Use the BTS with forward checking (FC).
Pick the variables according to the MCV SA NSW
heuristic. The order of the values should
be to the LCV heuristic.
V
81
81
Recommended Reading
For more on the topics covered in this lecture please refer to the following
sources:
• Russell-Norvig Book (Russell & Norvig, 2020): Sections 5.1 – 5.3.
82
82
41
References
83
83
42