csc384 Lecture03 BacktrackingSearch
csc384 Lecture03 BacktrackingSearch
Feature
Vectors
We have
A set of k variables (or features)
Each variable has a domain of different values.
A state is specified by an assignment of a value for
each variable.
height = {short, average, tall},
weight = {light, average, heavy}
Example: Sudoku
Example:
Sudoku
81 variables, each representing the value of a cell.
Domain of Values: a single value for those cells that
are already filled in, the set {1, 9} for those cells
that are empty.
State: any completed board given by specifying
the value in each cell (1-9, or blank).
Partial State: some incomplete filling out of the
board.
Fahiem
Bacchus,
CSC384
Introduc8on
to
Ar8cial
Intelligence,
University
of
Toronto
Example:
8-Puzzle
2
10
sunroof
Heated
seats
Slot2
Slot3
Slot4
Slot5
Slot6
Slot7
Car2
Car3
Car4
Car5
Car6
Max 2
Max 2
Max 2
Max 2
Car7
Car2
Car3
Car4
Car5
Car6
Car7
Formaliza1on
of
a
CSP
A CSP consists of
A set of variables V1, , Vn
For each variable a (finite) domain of possible
values Dom[Vi].
A set of constraints C1,, Cm.
A solution to a CSP is an assignment of a value
to all of the variables such that every constraint
is satisfied.
A CSP is unsatisfiable if no solution exists.
16
Formaliza1on
of
a
CSP
Each variable can be assigned any value from
its domain.
Vi = d where d Dom[Vi]
Each constraint C
Has a set of variables it is over, called its
scope
E.g., C(V1, V2, V4) is a constraint over the
variables V1, V2, and V4. Its scope is {V1, V2, V4}
17
Formaliza1on
of
a
CSP
We can specify the constraint with a table
C(V1, V2, V4) with Dom[V1] = {1,2,3} and
Dom[V2] = Dom[V4] = {1, 2}
V1
V2
V4
C(V1,V2,V4)
False
False
False
False
True
False
False
False
False
True
True
False
18
Formaliza1on
of
a
CSP
Often we can specify the constraint
more compactly with an expression:
C(V1, V2, V4) = (V1 = V2+V4)
V1
V2
V4
C(V1,V2,V4)
False
False
False
False
True
False
False
False
False
True
True
False
19
Formaliza1on
of
a
CSP
Unary Constraints (over one variable)
e.g. C(X):X=2; C(Y): Y>5
20
Example:
Sudoku
Variables: V11, V12, , V21, V22, , V91, , V99
Domains:
Dom[Vij] = {1-9} for empty cells
Dom[Vij] = {k} a fixed value k for filled cells.
Constraints:
Row constraints:
Column Constraints:
Sub-Square Constraints:
21
Example:
Sudoku
Each of these constraints is over 9 variables, and they
are all the same constraint:
Any assignment to these 9 variables such that each variable
has a different value satisfies the constraint.
Any assignment where two or more variables have the same
value falsifies the constraint.
22
Example:
Sudoku
Thus Sudoku has 3x9 ALL-DIFF constraints, one
over each set of variables in the same row, one
over each set of variables in the same column,
and one over each set of variables in the same
sub-square.
23
Solving
CSPs
Because CSPs do not require finding a
paths (to a goal), it is best solved by a
specialized version of depth-first search.
Key intuitions:
All extensions of this partial assignment will falsify that constraint, and
thus none can be solutions.
24
25
26
Backtracking
Search
The algorithm searches a tree of partial assignments.
Children of a node are
all possible values of
some (any) unassigned
variable
Root {}
Vi=a
Vi=b
Vj=1
Vi=c
Vj=2
Subtree
Fahiem
Bacchus,
CSC384
Introduc8on
to
Ar8cial
Intelligence,
University
of
Toronto
Search stops
descending if the
assignments on
path to the node
violate a constraint
27
Backtracking
Search
Heuristics are used to determine
the order in which variables are assigned:
PickUnassignedVariable()
the order of values tried for each variable.
28
Example:
N-Queens
Place N Queens on an N X N chess board so that no
Queen can attack any other Queen.
29
Example:
N-Queens
Problem formulation:
N variables (N queens)
N2 values for each variable representing the
positions on the chessboard
Value i is ith cell counting from the top left as 1,
going left to right, top to bottom.
30
Example:
N-Queens
Q1 = 1, Q2 = 15, Q3 = 21, Q4 = 32,
Q5 = 34, Q6 = 44, Q7 = 54, Q8 = 59
31
Example:
N-Queens
This representation has (N)N states
(different possible assignments in the
search space)
For 8-Queens: 648 = 281,474,976,710,656
32
Example:
N-Queens
Better Modeling:
N variables Qi, one per row.
Value of Qi is the column the Queen in row i
is placed; possible values {1, , N}.
33
Example:
N-Queens
Q1 = 1, Q2 = 7, Q3 = 5, Q4 = 8,
Q5 = 2, Q6 = 4, Q7 = 6, Q8 = 3
34
Example:
N-Queens
Constraints:
Cant put two Queens in same column
Qi Qj for all i j
Diagonal constraints
abs(Qi-Qj) abs(i-j)
i.e., the difference in the values assigned
to Qi and Qj cant be equal to the
difference between i and j.
35
Example: N-Queens
36
Example: N-Queens
37
Example: N-Queens
38
Example: N-Queens
Solution!
Fahiem
Bacchus,
CSC384
Introduc8on
to
Ar8cial
Intelligence,
University
of
Toronto
39
40
41
42
Constraint
Propaga1on
Constraint propagation refers to the technique
of looking ahead at the yet unassigned
variables in the search .
Try to detect obvious failures: Obvious means
things we can test/detect efficiently.
Even if we dont detect an obvious failure we
might be able to eliminate some possible part
of the future search.
43
Constraint
Propaga1on
Propagation has to be applied during the
search; potentially at every node of the search
tree.
Propagation itself is an inference step that
needs some resources (in particular time)
44
45
46
47
4-Queens
Problem
Encoding with Q1, , Q4 denoting a queen per row
cannot put two queens in same column
Q1
{1,2,3,4}
Q2
{1,2,3,4}
Q3
{1,2,3,4}
Q4
{1,2,3,4}
1
2
3
4
48
4-Queens
Problem
Forward checking reduced the domains of all variables that are
involved in a constraint with one uninstantiated variable:
Here all of Q2, Q3, Q4
Q1
{1,2,3,4}
Q2
{1,2,3,4}
Q3
{1,2,3,4}
Q4
{1,2,3,4}
1
2
3
4
49
4-Queens Problem
Q1
{1,2,3,4}
Q2
{1,2,3,4}
Q3
{1,2,3,4}
Q4
{1,2,3,4}
1
2
3
4
50
4-Queens Problem
Q1
{1,2,3,4}
Q2
{1,2,3,4}
Q3
{1,2,3,4}
Q4
{1,2,3,4}
1
2
3
4
DWO
Fahiem
Bacchus,
CSC384
Introduc8on
to
Ar8cial
Intelligence,
University
of
Toronto
51
4-Queens Problem
Q1
{1,2,3,4}
Q2
{1,2,3,4}
Q3
{1,2,3,4}
Q4
{1,2,3,4}
1
2
3
4
52
4-Queens Problem
Q1
{1,2,3,4}
Q2
{1,2,3,4}
Q3
{1,2,3,4}
Q4
{1,2,3,4}
1
2
3
4
53
4-Queens Problem
Q1
{1,2,3,4}
Q2
{1,2,3,4}
Q3
{1,2,3,4}
Q4
{1,2,3,4}
1
2
3
4
54
4-Queens Problem
Q1
{1,2,3,4}
Q2
{1,2,3,4}
Q3
{1,2,3,4}
Q4
{1,2,3,4}
1
2
3
4
DWO
Fahiem
Bacchus,
CSC384
Introduc8on
to
Ar8cial
Intelligence,
University
of
Toronto
55
4-Queens
Problem
Exhausted the subtree with Q1=1; try now Q1=2
Q1
{1,2,3,4}
Q2
{1,2,3,4}
Q3
{1,2,3,4}
Q4
{1,2,3,4}
1
2
3
4
56
4-Queens Problem
Q1
{1,2,3,4}
Q2
{1,2,3,4}
Q3
{1,2,3,4}
Q4
{1,2,3,4}
1
2
3
4
57
4-Queens Problem
Q1
{1,2,3,4}
Q2
{1,2,3,4}
Q3
{1,2,3,4}
Q4
{1,2,3,4}
1
2
3
4
58
4-Queens Problem
Q1
{1,2,3,4}
Q2
{1,2,3,4}
Q3
{1,2,3,4}
Q4
{1,2,3,4}
1
2
3
4
59
4-Queens Problem
Q1
{1,2,3,4}
Q2
{1,2,3,4}
Q3
{1,2,3,4}
Q4
{1,2,3,4}
1
2
3
4
60
4-Queens
Problem
We have now find a solution: an assignment of all variables to
values of their domain so that all constraints are satisfied
Q1
{1,2,3,4}
Q2
{1,2,3,4}
Q3
{1,2,3,4}
Q4
{1,2,3,4}
1
2
3
4
61
62
63
64
66
67
68
69
70
DWO
Fahiem
Bacchus,
CSC384
Introduc8on
to
Ar8cial
Intelligence,
University
of
Toronto
71
Empirically
FC often is about 100 times faster than BT
FC with MRV (minimal remaining values) often
10000 times faster.
But on some problems the speed up can be
much greater
Converts problems that are not solvable to problems
that are solvable.
72
73
74
75
76
77
78
79
GAC
Algorithm
We make all constraints GAC at every node of the
search space.
This is accomplished by removing from the domains of
the variables all arc inconsistent values.
80
81
82
Enforce
GAC
A support for V=d in constraint C is an assignment A to
all of the other variables in scope(C) such that
A U {V=d} satisfies C. (A is what the algorithms inner
loop looks for).
Smarter implementations keep track of supports to
avoid having to search though all possible assignments
to the other variables for a satisfying assignment.
83
Enforce
GAC
Rather than search for a satisfying assignment to C
containing V=d, we check to see if the current support
is still valid: i.e., all values it assigns still lie in the
variables current domains
Also we take advantage that a support for V=d, e.g.
{V=d, X=a, Y=b, Z=c}
is also a support for X=a, Y=b, and Z=c
84
Enforce
GAC
However, finding a support for V=d in constraint C still in
the worst case requires O(2k) work, where k is the arity
of C, i.e., |scope(C)|.
Another key development in practice is that for some
constraints this computation can be done in
polynomial time.
E.g., all-diff(V1, . Vn) we can be check if Vi=d has a
support in the current domains of the other variables in
polynomial time using ideas from graph theory.
We do not need to examine all combinations
of values for the other variables looking for a
support
Fahiem
Bacchus,
CSC384
Introduc8on
to
Ar8cial
Intelligence,
University
of
Toronto
85
= All-diff
CSS2 = All-diff(V1,4, V1,5, V1,6, V2,4, V2,5, V2,6, V3,4, V3,5, V3,6)
CR1 = All-diff(V1,1, V1,2, V1,3, V1,4, V1,5, V1,6, V1,7, V1,8, V1,9)
CC5 = All-diff(V1,5, V2,5, V35, V4,5, V5,5 V6,5, V7,5, V8,5, V9,5)
86
Timetabling problems
exam schedule
Transportation scheduling
Floor planning
Factory scheduling
Hardware configuration
a set of compatible components
87