6 Constraint Satisfaction Problems
6 Constraint Satisfaction Problems
Email: [email protected]
2
ODS 2001
Variables WA, NT, Q, NSW, V, SA, T Solutions are complete and consistent assignments,
Domains Di = {red,green,blue} e.g., WA = red, NT = green,Q = red,NSW =
green,V = red,SA = blue,T = green
Constraints: adjacent regions must have different colors
e.g., WA ≠ NT
3 4
1
11/15/2024
Continuous variables
e.g., start/end times for Hubble Space Telescope observations
linear constraints solvable in polynomial time by linear programming
5 6
2
11/15/2024
Equal! N! x D^N
9 10
There are N! x D^N nodes in the tree but only D^N distinct states??
• Better search tree: First order variables, then assign them values one-by-one.
D
WA WA WA
WA
NT D^2
WA WA
NT NT
D^N
11 12
3
11/15/2024
13 14
15 16
4
11/15/2024
Which variable should be assigned next? Which variable should be assigned next?
minimum remaining values heuristic degree heuristic
17 18
LCV: tries to avoid failure by assigning values that leave maximal flexibility for the
Leaves maximal flexibility for a solution. remaining variables.
Combining these heuristics makes 1000 queens feasible
19 20
5
11/15/2024
21 22
23 24
6
11/15/2024
7
11/15/2024
Since we only remove values from domains when they can never be
part of a solution, an empty domain means no solution possible at all
back out of that branch.
If X loses a value, neighbors of X need to be rechecked
Arc consistency detects failure earlier than forward checking Forward checking is simply sending messages into a variable that just
Can be run as a preprocessor or after each assignment got its value assigned. First step of arc-consistency.
Time complexity: O(n d )
2 3
29 30
Try it yourself
[R,B,G] [R,B,G]
[R]
[R,B,G] [R,B,G]
8
11/15/2024
33 34
In Constraint Satisfaction Problems (CSPs), a nearly tree-structured CSP is one that is almost a
tree but has a small number of cycles. While trees can be solved efficiently using arc consistency methods,
nearly tree-structured CSPs require special handling due to the presence of cycles.
9
11/15/2024
Cutset conditioning is especially useful for CSPs that are "almost" tree-like, where a small
cutset can simplify the problem significantly.
•Step 1: Identify a cutset. Removing D breaks the cycle. So, the cutset is {D}.
•Step 2: Assign values to D. For each possible value of D, solve the tree-structured subproblem
that remains (involving A, B, C, and E).
•Step 3: Combine solutions. Once each subproblem is solved, combine them to obtain a solution
for the original CSP.
37 38
Junction tree decomposition is a technique to organize the constraints of a Constraint Satisfaction Problem
(CSP) into a tree structure that allows for efficient propagation and solution finding.
39 40
10
11/15/2024
Let’s go through a numerical example of junction tree decomposition using a simple map-coloring Step 1: Formulating Constraints
problem with specific regions (variables) and constraints. The constraint graph can be visualized as follows:
Problem Setup
Suppose we have a map with four regions AAA, BBB, CCC, and DDD that need to be colored. Each
region should have a different color from its neighboring regions. Here’s the adjacency graph (where
each edge represents a constraint):
•Region A is adjacent to regions B and C.
•Region B is adjacent to regions A, C, and D.
•Region C is adjacent to regions A, B, and D.
•Region D is adjacent to regions B and C.
We have three possible colors: Red, Green, and Blue.
In this graph:
•Edges represent constraints that neighboring regions
must have different colors.
41 42
43 44
11
11/15/2024
45 46
47 48
12
11/15/2024
Summary
CSPs are a special kind of problem:
states defined by values of a fixed set of variables
goal test defined by constraints on variable values
Constraint propagation (e.g., arc consistency) does additional work to constrain values
and detect inconsistencies
49 50
CSP:
CSP? – state is defined by variables Xi with values from domain Di
– goal test is a set of constraints specifying allowable combinations of values for
Backtracking for CSP subsets of variables
Local search for CSPs Allows useful general-purpose algorithms with more power than
standard search algorithms
Problem structure and decomposition
Pag. 51 Pag.
13
11/15/2024
Variables WA, NT, Q, NSW, V, SA, T Solutions are complete and consistent
Domains Di = {red,green,blue} assignments, e.g., WA = red, NT = green,Q =
red,NSW = green,V = red,SA = blue,T =
Constraints: adjacent regions must have different colors
green
e.g., WA ≠ NT
Pag. Pag.
Continuous variables
– e.g., start/end times for Hubble Space Telescope observations
– linear constraints solvable in polynomial time by linear programming
Pag. Pag.
14
11/15/2024
Pag. Pag. 60
15
11/15/2024
Pag. 61 Pag.
62
CSP benefits
– Standard representation pattern
– Generic goal and successor
functions
– Generic heuristics (no domain
specific expertise).
Pag. Pag.
63 64
16
11/15/2024
Continuous variables Preference (soft constraints) e.g. red is better than green often
– e.g. start/end times for Hubble Telescope observations. representable by a cost for each variable assignment constrained
– Linear constraints solvable in poly time by LP methods. optimization problems.
Pag. 65 Pag. 66
Pag. 67 Pag. 68
17
11/15/2024
This is the same for all CSP’s !!! CSPs are commutative.
Solution is found at depth n (if there are n variables).
– The order of any given set of actions has no effect on the
– Hence depth first search can be used.
outcome.
Path is irrelevant, so complete state representation can also
be used. – Example: choose colors for Australian territories one at a time
– [WA=red then NT=green] same as [NT=green then WA=red]
Branching factor b at the top level is nd. – All CSP search algorithms consider a single variable assignment at a time
b=(n-l)d at depth l, hence n!dn leaves (only dn complete there are dn leaves.
assignments).
AI 1
Pag. 69 15 november Pag. 70
2024
Chooses values for one variable at a time and function RECURSIVE-BACKTRACKING(assignment, csp) return a solution or failure
backtracks when a variable has no legal values if assignment is complete then return assignment
var SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp],assignment,csp)
Pag. 71 Pag. 72
18
11/15/2024
Pag. 73 Pag. 74
Pag. 75 Pag. 76
19
11/15/2024
Pag. 77 Pag.
78
Pag. Pag.
79 80
20
11/15/2024
Pag. Pag.
83 84
21
11/15/2024
X1 X2 X1 X2
1 2 3 4 {1,2,3,4} {1,2,3,4} 1 2 3 4 {1,2,3,4} {1,2,3,4}
1 1
2 2
3 3
4 4
X3 X4 X3 X4
{1,2,3,4} {1,2,3,4} {1,2,3,4} {1,2,3,4}
Pag. Pag.
85 86
X1 X2 X1 X2
1 2 3 4 {1,2,3,4} { , ,3,4} 1 2 3 4 {1,2,3,4} { , ,3,4}
1 1
2 2
3 3
4 4
X3 X4 X3 X4
{ ,2, ,4} { ,2,3, } { ,2, ,4} { ,2,3, }
Pag. Pag.
87 88
22
11/15/2024
X1 X2 X1 X2
1 2 3 4 {1,2,3,4} { , ,3,4} 1 2 3 4 { ,2,3,4} {1,2,3,4}
1 1
2 2
3 3
4 4
X3 X4 X3 X4
{ , , , } { ,2,3, } {1,2,3,4} {1,2,3,4}
Pag. Pag.
89 90
X1 X2 X1 X2
1 2 3 4 { ,2,3,4} { , , ,4} 1 2 3 4 { ,2,3,4} { , , ,4}
1 1
2 2
3 3
4 4
X3 X4 X3 X4
{1, ,3, } {1, ,3,4} {1, ,3, } {1, ,3,4}
Pag. Pag.
91 92
23
11/15/2024
X1 X2 X1 X2
1 2 3 4 { ,2,3,4} { , , ,4} 1 2 3 4 { ,2,3,4} { , , ,4}
1 1
2 2
3 3
4 4
X3 X4 X3 X4
{1, , , } {1, ,3, } {1, , , } {1, ,3, }
Pag. Pag.
93 94
X1 X2 X1 X2
1 2 3 4 { ,2,3,4} { , , ,4} 1 2 3 4 { ,2,3,4} { , , ,4}
1 1
2 2
3 3
4 4
X3 X4 X3 X4
{1, , , } { , ,3, } {1, , , } { , ,3, }
Pag. Pag.
95 96
24
11/15/2024
Pag. Pag.
97 98
Pag. Pag.
99 100
25
11/15/2024
K-consistency K-consistency
Arc consistency does not detect all inconsistencies: A graph is strongly k-consistent if
– Partial assignment {WA=red, NSW=red} is inconsistent. – It is k-consistent and
Stronger forms of propagation can be defined using the notion of k- – Is also (k-1) consistent, (k-2) consistent, … all the way down to 1-consistent.
consistency.
A CSP is k-consistent if for any set of k-1 variables and for any This is ideal since a solution can be found in time O(nd)
consistent assignment to those variables, a consistent value can instead of O(n2d3)
always be assigned to any kth variable.
– E.g. 1-consistency or node-consistency YET no free lunch: any algorithm for establishing n-
– E.g. 2-consistency or arc-consistency consistency must take time exponential in n, in the worst
– E.g. 3-consistency or path-consistency
case.
26
11/15/2024
AI 1 AI 1
15 november Pag. 107 15 november Pag.
2024 2024 108
27
11/15/2024
A two-step solution for an 8-queens problem using min- Local search can be used in an online setting.
conflicts heuristic.
At each stage a queen is chosen for reassignment in its
– Backtrack search requires more time
column.
The algorithm moves the queen to the min-conflict square
breaking ties randomly.
AI 1 AI 1
15 november Pag. 15 november Pag. 110
2024 109 2024
How can the problem structure help to find a solution quickly? Suppose each problem has c variables out of a total of n.
Subproblem identification is important: Worst case solution cost is O(n/c dc), i.e. linear in n
– Coloring Tasmania and mainland are independent subproblems – Instead of O(d n), exponential in n
– Identifiable as connected components of constrained graph. E.g. n= 80, c= 20, d=2
Improves performance – 280 = 4 billion years at 1 million nodes/sec.
– 4 * 220= .4 second at 1 million nodes/sec
AI 1 AI 1
15 november Pag. 15 november Pag.
2024 111 2024 112
28
11/15/2024
Theorem: if the constraint graph has no loops In most cases subproblems of a CSP are connected as a tree
Any tree-structured CSP can be solved in time linear in the
then CSP can be solved in O(nd 2) time number of variables.
Compare difference with general CSP, where – Choose a variable as root, order variables from root to leaves such that every node’s
parent precedes it in the ordering. (label var from X1 to Xn)
worst case is O(d n) – For j from n down to 2, apply REMOVE-INCONSISTENT-VALUES(Parent(Xj),Xj)
– For j from 1 to n assign Xj consistently with Parent(Xj )
AI 1 AI 1
15 november Pag. 15 november Pag.
2024 113 2024 114
29
11/15/2024
AI 1 AI 1
15 november Pag. 15 november Pag.
117 118
2024 2024
Summary
CSPs are a special kind of problem: states defined by values of a fixed set of
variables, goal test defined by constraints on variable values
Backtracking=depth-first search with one variable assigned per node
Variable ordering and value selection heuristics help significantly
Forward checking prevents assignments that lead to failure.
Constraint propagation does additional work to constrain values and detect
inconsistencies.
The CSP representation allows analysis of problem structure.
Tree structured CSPs can be solved in linear time.
Iterative min-conflicts is usually effective in practice.
AI 1
15 november Pag. 119
2024
30