CS4700-CSP v6
CS4700-CSP v6
CS4700-CSP v6
Module:
Constraint Satisfaction
Chapter 6, R&N
(Completes part II – Problem Solving)
1
Bart Selman
CS4700
Outline
2
Bart Selman
CS4700
Key issue: So far, we have treated nodes in
search trees as “black boxes,” only looked
inside to check whether the node is a goal state.
3
Bart Selman
CS4700
Motivational Example:
8-Queens
Usual goal:
place 8 non-attacking
queens.
Already shown
effectiveness of local
search.
4
Bart Selman
CS4700
Generic (DFS/BFS) search
Hmm. Can a search program figure out that you can’t place 101 queens
on 100x100 board?
Not so easy! Most search approaches can’t. Need much more clever
reasoning, instead of just search. (Need to use Pigeon Hole principle.)
Aside: Factored representation does not even allow one to ask
the question. Knowledge is build in.)
Alternative question: With N queens is there a solution with queen
in bottom right hand corner on a N x N board?
7
Bart Selman
CS4700
How do we search for a solution?
Start with empty variable assignment (no vars assigned). Then, build
up partial assignments until all vars assigned.
Intuitive:
1) Fix some ordering on the variables. Eg x1, x2, x3 …
2) Fix some ordering on possible values. Eg 1, 2, 3, …
3) Assign first variable, first (legal) value.
4) Assign next variable, its first (legal) value.
5) Etc.
6) Until no remaining (feasible) value exist for variable x_i,
backtrack to previous var setting of x_(i-1), try next possible
setting. If none exists, move back another level. Etc.
Visually, very intuitive on the N-Queens board (“the obvious strategy”).
See figure 6.5 book. Recursive implementation. Practice: Iterative with
stack.
9
Bart Selman
CS4700
Reasoning, inference or “propagation.”
Message:
CSP propagation techniques can dramatically reduce search.
Sometimes to no search at all! Eg. Sudoku puzzles.
After placing the first queen, what would you do for the 2nd?
10
Bart Selman
CS4700
General Search vs.
Constraint satisfaction problems (CSPs)
Standard search problem:
– state is a "black box“ – can be accessed only in limited way:
successor function; heuristic function; and goal test.
11
Bart Selman
CS4700
Constraint satisfaction problems (CSPs)
12
Bart Selman
CS4700
Constraint Satisfaction Problem
13
Bart Selman
CS4700
Motivational Example:
8-Queens
l + k = 10 l–k=1
l – k =0
14
Bart Selman
CS4700
Example: 8-Queens Problem
15
Bart Selman
CS4700
Boolean Encoding
64 variables Xij, i = 1 to 8, j = 1 to 8
Domain for each variable {0,1} (or {False, True})
Xij = 1 iff “there is a
Constraints are of the form:
Row and columns
queen on location (i,j).”
– If (Xij = 1) then (Xik = 0) for all k = 1 to 8, kj (logical constraint)
– Xij = 1 Xkj = 0 for all k = 1 to 8, ki
Diagonals
– Xij = 1 Xi+l,j+l = 0 l = 1 to 7, i+l 8; j+l8 (right and up)
– Xij = 1 Xi-l,j+l = 0 l = 1 to 7, i-l 1; j+l8 (right and down)
– Xij = 1 Xi-l,j-l = 0 l = 1 to 7, i-l 1; j-l1 (left and down)
– Xij = 1 Xi+l,j-l = 0 l = 1 to 7, i+l 8; j-l1 (left and up)
17
Bart Selman
CS4700
Propositional Satisfiability problem
n possible assignments
2
19
19
Bart Selman
CS4700
Turing Award: Model Checking
21
21
Bart Selman
CS4700
Bounded Model Checking instance:
22
Bart Selman
CS4700
File format
Dimacs Format for CNF
The benchmark file format will be in a simplified version of the DIMACS format: c
c start with comments
c
c
p cnf 5 3
1 -5 4 0
-1 5 3 4 0
-3 -4 0
The file can start with comments, that is lines begining with the character c.
Right after the comments, there is the line p cnf nbvar nbclauses indicating that the
instance is in CNF format; nbvar is the exact number of variables appearing in the
file; nbclauses is the exact number of clauses contained in the file.
Then the clauses follow. Each clause is a sequence of distinct non-null numbers
between -nbvar and nbvar ending with 0 on the same line; it cannot contain the
opposite literals i and -i simultaneously. Positive numbers denote the corresponding
variables. Negative numbers denote the negations of the corresponding variables.
23
Bart Selman
CS4700
Example of Sat Solver
http://fmv.jku.at/lingeling/
Nqueen4-v1.cnf
Nquuens4-v2.cnf
24
Bart Selman
CS4700
p cnf 16 84
1234 0
-1 -2 0 1 5 9 13 0
-1 -6 0
-1 -5 0
-1 -3 0 -1 -11 0
-1 -9 0 -1 -16 0
-1 -4 0 -1 -13 0
-2 -3 0 -6 -11 0
-5 -9 0
-6 -16 0
-2 -4 0 -5 -13 0
-11 -16 0
-3 -4 0 -9 -13 0 -2 -7 0
5678 0 2 6 10 14 0
-2 -12 0
-5 -6 0
-2 -6 0
-2 -10 0
-7 -12 0 1 2 3 4
-3 -8 0
-5 -7 0
-5 -8 0
-2 -14 0
-6 -10 0
-5 -10 0 5 6 7 8
-5 -15 0
-6 -7 0
-6 -8 0
-6 -14 0
-10 -14 0
-10 -15 0 9 10 11 12
-9 -14 0
-7 -8 0 3 7 11 15 0
-3 -7 0
-4 -7 0 13 14 15 16
9 10 11 12 0 -4 -10 0
-3 -11 0 -4 -13 0
-9 -10 0 -3 -15 0
-9 -11 0 -7 -10 0
-7 -11 0
-7 -13 0
-9 -12 0 -7 -15 0
-10 -13 0
-10 -11 0 -11 -15 0 -3 -6 0
-10 -12 0 4 8 12 16 0
-3 -9 0
-4 -8 0
-11 -12 0 -6 -9 0
-4 -12 0 -2 -5 0
13 14 15 16 0 -4 -16 0
-13 -14 0 -8 -11 0
-8 -12 0 -8 -14 0
-13 -15 0 -8 -16 0 -11 -14 0
-13 -16 0 -12 -16 0 -12 -15 0
-14 -15 0
-14 -16 0
-15 -16 0
25
Bart Selman
CS4700
How Large are the Problems?
A bounded model checking problem:
Source: IBM
26
CS4700 26
Bart Selman
SAT Encoding
(automatically generated from problem specification)
27
CS4700 27
Bart Selman
10 Pages Later:
…
i.e., (x177 or x169 or x161 or x153 …
x33 or x25 or x17 or x9 or x1 or (not x185))
28
CS4700 28
Bart Selman
4,000 Pages Later:
29
CS4700 29
Bart Selman
Finally, 15,000 Pages Later:
30
CS4700 30
Bart Selman
Example of a Boolean Satisfiability (SAT) encoding.
Very “simple” but effective representation.
Example of a logical knowledge representation language.
31
Bart Selman
CS4700
Which encoding is better?Allows for faster solutions?
Search spaces:
32
Bart Selman
CS4700
N-Queens
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7
34 34
Bart Selman
CS4700
Linear congruence equations
N = 6 ;
N = 6 + 1;
N = 6 ± 2; (not N=4;)
N = 6 + 3; (not N=9;)
N = 6 - 2; (inc. N=4)
N = 12 - 3; (inc. N=9)
35 35
Bart Selman
CS4700
N-Queens
N=8
N = 6 2, =1
S(8) = {3}.
6 x + y 3 (mod 8),
where x = 0, 1, 2, 3.
x = 0 y=3; x=1 y = 5; x=2 y= 7;x=3 y = 1;
6 x + y 6 (mod 8),
where x = 4,5,6,7,8
X=4, y = 6; x=5 y = 0; x=6 y = 2; x=7 y=4;
36 36
Bart Selman
CS4700
Partially Filled Nqueens
Open question
37
Bart Selman
CS4700
Example: Crypt-arithmetic Puzzle
SEND
+ MOR E
----------
MONEY
Variables: S, E, N, D, M, O, R, Y Soln.:95
67
Domains: 1085
[0..9] for S, M, E, N, D, O, R, Y
====
Search space: 1,814,400 10652
Aside: could have [1..9] for S and M
38
Bart Selman
CS4700
Constraints
Option 1:
C1a) 1000 S + 100 E + 10 N + D +
1000 M + 100 O + 10 R + E
= 10000 M + 1000 O + 100 N + 10 E + Y SEND
Or use 5 equality constraints, using auxiliary +MORE
“carry” variables C1, …, C4 Є [0…9] ----------
Option 2: C1b)
MONEY
D+E = 10 C1 + Y Which constraint set better
C1 + N + R = 10 C2 + E for solving? C1a or C1b? Why?
C2 + E + O = 10 C3 + N
C3 + S + M = 10 C4 + O C1b, more “factored”. Smaller
C4 =M pieces. Gives more propagation!
40
Bart Selman
CS4700
1) M = 1, because M =/= 0 and …
the carry over of the addition of two
digits (plus previous carry) is at most 1.
43
Bart Selman
CS4700
Example: Map-Coloring
44
Bart Selman
CS4700
Example: Map-Coloring
45
Bart Selman
CS4700
Constraint graph:
Graph Coloring
Binary CSP: each constraint relates two variables
46
Bart Selman
CS4700
Application of Graph Coloring
5 4 5 4
Graph of finals for 7 courses CS4700 47
Bart Selman
Varieties of CSPs
Discrete variables
– infinite domains:
• integers, strings, etc.
• e.g., job scheduling, variables are start/end days for each job
• need a constraint language, e.g., StartJob1 + 5 ≤ StartJob3
Continuous variables
49
Bart Selman
CS4700
Remark
51
Bart Selman
CS4700
Solving CSP by search : Backtrack Search
BFS vs. DFS
– BFS not a good idea.
• A tree with n!dn leaves : (nd)*((n-1)d)*((n-2)d)*…*(1d) = n!dn
• Reduction by commutativity of CSP
– A solution is not in the permutations but in combinations.
– A tree with dn leaves
– DFS
• Used popularly
– Every solution must be a complete assignment and therefore
appears at depth n if there are n variables
– The search tree extends only to depth n.
• A variant of DFS: Backtrack search
– Chooses values for one variable at a time
– Backtracks when failed even before reaching a leaf.
• Better than BFS due to backtracking but still need more
“cleverness” (reasoning/propagation).
52
Bart Selman
CS4700
Backtrack Search
empty assignment
1st variable
2nd variable
3rd variable
Assignment = {}
54
Bart Selman
CS4700
Backtrack Search
empty assignment
1st variable
2nd variable
3rd variable
Assignment = {(var1=v11)}
55
Bart Selman
CS4700
Backtrack Search
empty assignment
1st variable
2nd variable
3rd variable
Assignment = {(var1=v11),(var2=v21)}
56
Bart Selman
CS4700
Backtrack Search
empty assignment
1st variable
2nd variable
3rd variable
Assignment = {(var1=v11),(var2=v21),(var3=v31)}
57
Bart Selman
CS4700
Backtrack Search
empty assignment
1st variable
2nd variable
3rd variable
Assignment = {(var1=v11),(var2=v21),(var3=v32)}
58
Bart Selman
CS4700
Backtrack Search
empty assignment
1st variable
2nd variable
3rd variable
Assignment = {(var1=v11),(var2=v22)}
59
Bart Selman
CS4700
Backtrack Search
empty assignment
1st variable
2nd variable
3rd variable
Assignment = {(var1=v11),(var2=v22),(var3=v31)}
60
Bart Selman
CS4700
Improving Backtracking Efficiency
Variable & value ordering to increase
Which variable should be assigned next? the likelihood to success
Minimum Remaining Values heuristic
In what order should its values be tried?
Least Constraining Values heuristic
Can we detect inevitable failure early?
Forward checking
Constraint propagation (Arc Consistency)
When a path fails, can the search avoid repeating
this failure?
Backjumping Early failure-detection to decrease
the likelihood to fail
Can we take advantage of problem structure?
Tree-structured CSP Restructuring to reduce the
problem’s complexity
64
Bart Selman
CS4700
Choice of Variable, cont.
65
Bart Selman
CS4700
Choice of Value:
Least constraining value
the one that rules out the fewest values in the remaining variables
–
66
Bart Selman
CS4700
Constraint Propagation
67
Bart Selman
CS4700
Forward Checking
After a variable X is assigned a value v, look at each unassigned
variable Y that is connected to X by a constraint and deletes
from Y’s domain any value that is inconsistent with v
68
Bart Selman
CS4700
Forward checking
69
Bart Selman
CS4700
70
Bart Selman
CS4700
71
Bart Selman
CS4700
72
Bart Selman
CS4700
Constraint propagation
NT
Q
WA
SA
NSW
V
Forward checking propagates information from assigned to unassigned
variables, but doesn't provide early detection for all failures:
What’s the problem here?
NT Q
WA
SA
NSW
V
T
≠
NT and SA cannot both be blue!
Use: constraint propagation repeatedly to enforce constraints locally.
73 Bart Selman
CS4700
Definition (Arc consistency)
74
Bart Selman
CS4700
When a CSP is not arc consistent, we can make it arc
consistent.
75
Bart Selman
CS4700
Example
Let domains be
D_x = {1, 2, 3}, D_y = {3, 4, 5, 6}
One constraint
C_xy = {(1,3), (1,5), (3,3), (3,6)} [“allowed value pairs”]
76
Bart Selman
CS4700
Arc consistency
77
Bart Selman
CS4700
NT
Q
WA
SA
NSW
V
78
Bart Selman
CS4700
NT
Q
WA
SA
NSW
V
79
Bart Selman
CS4700
If X loses a value, neighbors of X need to be rechecked.
Arc consistency detects failure earlier than forward checking.
NT
Q
WA
SA
NSW
V
80
Bart Selman
CS4700
Beyond Arc Consistency
{1, 2} {1, 2}
XY
X Y
Is this network arc consistent?
82
Bart Selman
CS4700
k - Consistency
{1, 2} {1, 2}
XY
X Y
A graph is K-consistent iff the following is true:
XZ YZ
Choose values of any K-1 variables that satisfy all
the constraints among these variables and choose
any K th variable. Then, there exists a value for Z
this K th variable that satisfies all the constraints
{1, 2}
among these K variables.
Algorithms exist for making a constraint graph strongly K-consistent for K>2
but in practice they are rarely used because of efficiency issues.
84
Bart Selman
CS4700
Summary: Solving a CSP
Search:
– can find solutions, but may examine many non-
solutions along the way
Constraint Propagation:
– can rule out non-solutions, but but may not lead
to full solution.
Interweave constraint propagation and search
– Perform constraint propagation at each search step.
– Goal: Find the right balance between search
(backtracking) and propagation (reasoning).
85
Bart Selman
CS4700