csc384-lecture03-backtrackingsearch
TRANSCRIPT
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
1/87
!!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
#$#%&'( )*+,-./01-* +- 2,130456 )*+766487*07
Constraint Satisfaction Problems(Backtracking Search)
• Chapter 6 – 6.1: Formalism – 6.2: Constraint Propagation – 6.3: Backtracking Search for CSP – 6.4 is about local search which is a very
useful idea but we won’t cover it in class.
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
2/87
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
3/87
%!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
?75+/,7 @70+-,<
• 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}
– A partial state is specified by an assignment of avalue to some of the variables.
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
4/87
'!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67( $/.-D/
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
5/87
E!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• 81 variables , each representing the value of a cell.
• Domain of Values : a single value for those cells thatare already filled in, the set {1, …9} for those cells
that are empty.
• State: any completed board given by specifyingthe value in each cell (1-9, or blank).
• Partial State: some incomplete filling out of theboard.
AB5C;67( $/.-D/
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
6/87
F!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• Variables: 9 variables Cell 1,1 , Cell 1,2 , …, Cell 3,3• Values: {‘B’, 1, 2, …, 8}
• State: Each “Cell i,j” variable specifies what is in thatposition of the tile. – If we specify a value for each cell we have completely
specified a state.
This is only one of many ways to specify the state.
AB5C;67( &GH/II67
2 3 7
6 4 8
5 1
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
7/87J!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• Notice that in these problems some settings ofthe variables are illegal. – In Sudoku, we can’t have the same number in any
column, row, or subsquare.
– In the 8 puzzle each variable must have a distinctvalue (same tile can’t be in two places)
#-*
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
8/87&!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• In many practical problems finding whichsetting of the feature variables yields a legalstate is difficult.
• We want to find a state (setting of thevariables) that satisfies certain constraints.
#-*
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
9/87M!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• In Suduko: The variables that form – a column must be distinct – a row must be distinct – a sub-square must be distinct.
#-*
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
10/87!N!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• In these problems we do not care about thesequence of moves needed to get to a goalstate.
• We only care about finding a feature vector (a
setting of the variables) that satisfies the goal. – A setting of the variables that satisfies someconstraints.
• In contrast, in the 8-puzzle, the feature vectorsatisfying the goal is given. We care about thesequence of moves needed to move the tilesinto that configuration
#-*
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
11/87
1 2 3 4 6
!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
Car Factory Assembly Line—back to the days ofHenry Ford
Move the items to be assembled don’t movethe workers
AB5C;67 #5, $7O/7*04*8
The assembly line is divided into stations. A particulartask is preformed at each station.
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
12/87
1 sunroof 3 Heatedseats 6
!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67 #5, $7O/7*04*8
Some stations install optional items…not every car inthe assembly line is worked on in that station.
As a result the factory is designed to have lower
capacity in those stations.
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
13/87
Slot1 Slot2 Slot3 Slot4 Slot5 Slot6 Slot7
!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67 #5, $7O/7*04*8
Cars move through the factory on an assembly line
which is broken up into slots.
The stations might be able to process only a limitednumber of slots out of some group of slots that is passing
through the station at any time.
E.g., the sunroof station might accommodate 4 slots, butonly has capacity to process 2 slots out of the 4 at any
one time.
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
14/87
!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67 #5, $7O/7*04*8
Car1 Car2 Car3 Car4 Car5 Car6 Car7
Max 2
Max 2
Max 2
Max 2
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
15/87
!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67 #5, $7O/7*04*8
Car1 Car2 Car3 Car4 Car5 Car6 Car7
Each car to be assembled has a list of required options.
We want to assign each car to be assembled to a slot onthe line.But we want to ensure that no sequence of 4 slots hasmore than 2 cars assigned that require a sun roof.
Finding a feasible assignment of cars with differentoptions to slots without violating the capacity constraintsof the different stations is hard.
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
16/87
!F!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• A CSP consists of – A set of variables V1, …, V n – For each variable a (finite) domain of possible
values Dom[V i].
– A set of constraints C 1,…, C m .
– A solution to a CSP is an assignment of a valueto all of the variables such that every constraintis satisfied .
– A CSP is unsatisfiable if no solution exists.
?-,C564I51-* -K 5 #$H
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
17/87
!J!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• 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}
– Given an assignment to its variablesthe constraint returns:
• True—this assignment satisfies the constraint• False—this assignment falsifies the constraint.
?-,C564I51-* -K 5 #$H
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
18/87
!&!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• 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}
?-,C564I51-* -K 5 #$H
V1 V2 V4 C(V1,V2,V4)
1 1 1 False
1 1 2 False
1 2 1 False
1 2 2 False
2 1 1 True
2 1 2 False
2 2 1 False
2 2 2 False
3 1 1 False
3 1 2 True
3 2 1 True
3 2 2 False
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
19/87
!M!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• Often we can specify the constraintmore compactly with an expression:C(V1, V2, V4) = (V1 = V2+V4)
?-,C564I51-* -K 5 #$H
V1 V2 V4 C(V1,V2,V4)
1 1 1 False
1 1 2 False
1 2 1 False
1 2 2 False
2 1 1 True
2 1 2 False
2 2 1 False
2 2 2 False
3 1 1 False
3 1 2 True
3 2 1 True
3 2 2 False
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
20/87
9N!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• Unary Constraints (over one variable)
– e.g. C(X):X=2; C(Y): Y>5
• Binary Constraints (over two variables) – e.g. C(X,Y): X+Y
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
21/87
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
22/87
99!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• Each of these constraints is over 9 variables, and they
are all the same constraint: – Any assignment to these 9 variables such that each variablehas a different value satisfies the constraint.
– Any assignment where two or more variables have the samevalue falsifies the constraint.
• This is a special kind of constraint called an ALL-DIFF constraint.
– ALL-Diff(V1, .., Vn) could also be encoded as a set of binary
not-equal constraints between all possible pairs of variables:V1 ! V2, V1 ! V3, …, V2 ! V1, …, Vn ! V1, …, Vn ! Vn-1
AB5C;67( $/.-D/
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
23/87
9%!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• Thus Sudoku has 3x9 ALL-DIFF constraints, oneover each set of variables in the same row, oneover each set of variables in the same column,and one over each set of variables in the same
sub-square.
AB5C;67( $/.-D/
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
24/87
9'!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• Because CSPs do not require finding a
paths (to a goal), it is best solved by aspecialized version of depth-first search.
• Key intuitions: – We can build up to a solution by searching through thespace of partial assignments. – Order in which we assign the variables does not matter –
eventually they all have to be assigned. We can decideon a suitable value for one variable at a time!
! This is the key idea of backtracking search. – If we falsify a constraint during the process of building up a
solution, we can immediately reject the current partialassignment:
• All extensions of this partial assignment will falsify that constraint, andthus none can be solutions.
$-6P4*8 #$H<
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
25/87
9E!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
#$H 5< 5 $75,0> H,-L67C
A CSP could be viewed as a more traditionalsearch problem
• Initial state: empty assignment• Successor function: a value is assigned to
any unassigned variable, which does notcause any constraint to return false.
• Goal test: the assignment is complete
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
26/87
9F!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
BT(Level)If all variables assigned
PRINT Value of each VariableRETURN or EXIT (RETURN for more solutions)
(EXIT for only one solution)V := PickUnassignedVariable()Assigned[V] := TRUEfor d := each member of Domain(V) (the domain values of V)
Value[V] := dConstraintsOK = TRUE for each constraint C such that
a) V is a variable of C andb) all other variables of C are assigned:
IF C is not satisfied by the set of currentassignments:ConstraintsOK = FALSE
If ConstraintsOk == TRUE:BT(Level+1)
Assigned[V] := FALSE // UNDO as we have tried all of V’s values return
Q50D+,50D4*8 $75,0>( R>7 268-,4+>C QR
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
27/87
9J!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• The algorithm searches a tree of partial assignments.
Root {}
Vi=a Vi=b Vi=c
Vj=1 Vj=2
The root has the empty setof assignments
Children of a node areall possible values of
some (any) unassignedvariable
Subtree
Search stopsdescending if theassignments onpath to the node
violate a constraint
Q50D+,50D4*8 $75,0>
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
28/87
9&!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• Heuristics are used to determine – the order in which variables are assigned:
PickUnassignedVariable() – the order of values tried for each variable.
• The choice of the next variable can vary frombranch to branch, e.g., – under the assignment V1=a we might choose to
assign V4 next, while under V1=b we might choose
to assign V5 next.
• This “dynamically ” chosen variable ordering hasa tremendous impact on performance.
Q50D+,50D4*8 $75,0>
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
29/87
9M!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67( SGT/77*<
• Place N Queens on an N X N chess board so that noQueen can attack any other Queen.
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
30/87
%N!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67( SGT/77*<
• Problem formulation: – N variables (N queens) – N2 values for each variable representing the
positions on the chessboard• Value i is i’th cell counting from the top left as 1,
going left to right, top to bottom.
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
31/87
%!!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67( SGT/77*<
• Q1 = 1, Q2 = 15, Q3 = 21, Q4 = 32,Q5 = 34, Q6 = 44, Q7 = 54, Q8 = 59
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
32/87
%9!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67( SGT/77*<
• This representation has (N ")N states(different possible assignments in thesearch space) – For 8-Queens: 64 8 = 281,474,976,710,656
• Is there a better way to represent the N-queens problem?
– We know we cannot place two queens in asingle row " we can exploit this fact in thechoice of the CSP representation
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
33/87
%%!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67( SGT/77*<
• 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}.
• This representation has N N states: – For 8-Queens: 8 8 = 16,777,216
• The choice of a representation can make theproblem solvable or unsolvable!
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
34/87
%'!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67( SGT/77*<
• Q1 = 1, Q2 = 7, Q3 = 5, Q4 = 8,Q5 = 2, Q6 = 4, Q7 = 6, Q8 = 3
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
35/87
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
36/87
%F!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67( SGT/77*<
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
37/87
%J!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67( SGT/77*<
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
38/87
%&!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67( SGT/77*
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
39/87
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
40/87
'N!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67( SGT/77*< Q50D+,50D4*8 $75,0> $;507
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
41/87
'!!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
H,-L67C< =4+> H654* Q50D+,50D4*8
Sudoku: The 3,3 cell has no possible value.
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
42/87
'9!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• In the backtracking search we won’t detect that the(3,3) cell has no possible value until all variables of therow/column (involving row or column 3) or the sub-square constraint (first sub-square) are assigned.So we have the following situation:
• Leads to the idea of constraint propagation
Variable has no possiblevalue, but we don’t
detect this. Until we try toassign it a value
H,-L67C< =4+> H654* Q50D+,50D4*8
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
43/87
'%!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• Constraint propagation refers to the techniqueof “ looking ahead ” at the yet unassignedvariables in the search .
• Try to detect obvious failures: “ Obvious ” meansthings we can test/detect efficiently.
• Even if we don’t detect an obvious failure we
might be able to eliminate some possible partof the future search.
#-*
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
44/87
''!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• Propagation has to be applied during the
search; potentially at every node of the searchtree.
• Propagation itself is an inference step thatneeds some resources (in particular time)
– If propagation is slow, this can slow the search downto the point where using propagation makes findinga solution take longer!
– There is always a tradeoff between searching fewernodes in the search, and having a higher nodes/
second processing rate.• We will look at two main types of propagation:
Forward Checking & Generalized ArcConsistency
#-*
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
45/87
'E!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• Forward checking is an extension ofbacktracking search that employs a “modest”amount of propagation (look ahead).
• When a variable is instantiated we check allconstraints that have only one uninstantiatedvariable remaining.
• For that uninstantiated variable, we check allof its values, pruning those values that violatethe constraint.
#-*70D4*8
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
46/87
'F!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
FCCheck (C,x)// C is a constraint with all its variables already// assigned, except for variable x.for d := each member of CurDom[x]
IF making x = d together with previous assignmentsto variables in scope C falsifies C
THEN remove d from CurDom[x]IF CurDom[x] = {} then return DWO ( Domain Wipe Out)ELSE return ok
?-,=5,. #>70D4*8 268-,4+>C
• For a single constraint C:
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
47/87
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
48/87
'&!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
1BC*%%3+ D56E;%&
• Encoding with Q1, …, Q4 denoting a queen per row – cannot put two queens in same column
1
3
2
4
32 41
Q1{1,2,3,4}
Q3{1,2,3,4} Q4{1,2,3,4}
Q2{1,2,3,4}
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
49/87
'M!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
1BC*%%3+ D56E;%&
• Forward checking reduced the domains of all variables that are
involved in a constraint with one uninstantiated variable: – Here all of Q2, Q3, Q4
1
3
2
4
32 41
Q1{1,2,3,4}
Q3{1,2, 3,4} Q4{1,2,3, 4}
Q2{1,2,3,4}
1BC*%%3 D56E %&
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
50/87
EN!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
1BC*%%3+ D56E;%&
1
3
2
4
32 41
Q1{1,2,3,4}
Q3{1,2, 3,4} Q4{1,2,3, 4}
Q2{1,2,3,4}
1BC*%%3 D56E %&
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
51/87
E!!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
1BC*%%3+ D56E;%&
Q1{1,2,3,4}
Q3{1,2,3,4} Q4{1,2,3, 4}
Q2{1,2,3,4}
DWO
1
3
2
4
32 41
1BC*%%3 D56E %&
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
52/87
E9!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
1BC*%%3+ D56E;%&
Q1{1,2,3,4}
Q3{1,2, 3,4} Q4{1,2,3, 4}
Q2{1,2,3,4}
1
3
2
4
32 41
1BC*%%3 D56E %&
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
53/87
E%!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
1BC*%%3+ D56E;%&
Q1{1,2,3,4}
Q3{1,2, 3,4} Q4{1,2,3, 4}
Q2{1,2,3,4}
1
3
2
4
32 41
1BC*%%3+ D56E %&
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
54/87
E'!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
1BC*%%3+ D56E;%&
Q1{1,2,3,4}
Q3{1,2,3,4} Q4{1,2,3, 4}
Q2{1,2,3,4}
1
3
2
4
32 41
1BC*%%3+ D56E %&
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
55/87
EE!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
1BC*%%3+ D56E;%&
Q1{1,2,3,4}
Q3{1,2,3,4} Q4{1,2,3,4}
Q2{1,2,3,4}
DWO
1
3
2
4
32 41
1BC*%%3+ D56E %&
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
56/87
• Exhausted the subtree with Q1=1; try now Q1=2
EF!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
1BC*%%3+ D56E;%&
Q1{1,2,3,4}
Q3{1,2,3,4} Q4{1,2,3,4}
Q2{1,2,3,4}
1
3
2
4
32 41
1BC*%%3+ D56E %&
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
57/87
EJ!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
1BC*%%3+ D56E;%&
Q1{1,2,3,4}
Q3{1, 2,3, 4} Q4{1, 2,3,4}
Q2{1,2,3,4}
1
3
2
4
32 41
1BC*%%3+ D56E;%&
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
58/87
E&!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
1BC*%%3+ D56E;%&
Q1{1,2,3,4}
Q3{1, 2,3,4} Q4{1, 2,3, 4}
Q2{1,2,3,4}
1
3
2
4
32 41
1BC*%%3+ D56E;%&
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
59/87
EM!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
1BC*%%3+ D56E;%&
Q1{1,2,3,4}
Q3{1,2,3,4} Q4{1, 2,3, 4}
Q2{1,2,3,4}
1
3
2
4
32 41
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
60/87
1BC*%%3+ D56E;%&
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
61/87
F!!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
1BC*%%3+ D56E;%&
• 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}
Q3{1,2,3,4} Q4{1,2,3,4}
Q2{1,2,3,4}
1
3
2
4
32 41
AB5C;67( SGT/77*< ?# $;507
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
62/87
F9!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67( SGT/77*< ?# $;507
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
63/87
?#( U4*4C/C :7C54*4*8 @56/7< V7/ 4
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
64/87
F'!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• FC also gives us for free a very powerfulheuristic to guide us which variables to try next:
– Always branch on a variable with the smallestremaining values (smallest CurDom).
– If a variable has only one value left, that value is
forced, so we should propagate its consequencesimmediately.
– This heuristic tends to produce skinny trees at thetop. This means that more variables can beinstantiated with fewer nodes searched, and thusmore constraint propagation/DWO failures occurwhen the tree starts to branch out (we startselecting variables with larger domains)
– We can find a inconsistency much faster
?#( U4*4C/C :7C54*4*8 @56/7< V7/,4
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
65/87
AB5C;67 Z U5; #-6-/ 4*8
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
66/87
FF!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67 Z U5; #-6-/,4 8
• Color the following map using red , green , andblue such that adjacent regions have differentcolors.
AB5C;67 Z U5; #-6-/ 4*8
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
67/87
FJ!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• Modeling – Variables : WA, NT, Q, NSW, V, SA, T – Domains : Di={red, green, blue} – Constraints : adjacent regions must have
different colors.• E.g. WA ! NT
AB5C;67 Z U5; #-6-/,4 8
AB5C;67 Z U5; #-6-/ 4*8
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
68/87
F&!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67 Z U5; # 6 /,4 8• Forward checking idea : keep track of remaining legal
values for unassigned variables.
• Terminate search when any variable has no legalvalues.
AB5C;67 Z U5; #-6-/ 4*8
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
69/87
FM!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67 Z U5; # 6 /,4 8• Assign {WA=red}
• Effects on other variables connected by constraints toWA – NT can no longer be red – SA can no longer be red
AB5C;67 Z U5; #-6-/ 4*8
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
70/87
JN!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67 Z U5; # 6 /,4 8• Assign {Q=green} (Note: Not using MRV)• Effects on other variables connected by constraints with Q
– NT can no longer be green – NSW can no longer be green – SA can no longer be green
• MRV heuristic would automatically select NT or SA next
AB5C;67 Z U5; #-6-/ 4*8
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
71/87
J!!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
AB5C;67 Z U5; # 6 /,4 8• Assign {V=blue} (not using MRV)
•
Effects on other variables connected by constraints with V – NSW can no longer be blue – SA is empty
• FC has detected that partial assignment is inconsistent with theconstraints and backtracking can occur.
DWO
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
72/87
#-*
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
73/87
J%!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
GAC—Generalized Arc Consistency
1. C(V 1, V2, V3, …, V n) is GAC with respect tovariable Vi, if and only if
For every value of V i, there exists values ofV1, V2, V i-1, V i+1, …, V n that satisfy C.
Note that the values are removed from thevariable domains during search. So variablesthat are GAC in a constraint might becomeinconsistent (non-GAC).
# ,54 H, ;5851 ( [7 7,564I7. 2,0 # 4 7 0Y
#-*
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
74/87
J'!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• C(V1, V2, …, Vn) is GAC if and only if
It is GAC with respect to every variable in itsscope.
• A CSP is GAC if and only if
all of its constraints are GAC.
# ,54 H, ;5851 ( [7 7,564I7. 2,0 # 4 7 0Y
#-*
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
75/87
JE!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• Say we find a value d of variable Vi that is not consistent:That is, there is no assignments to the other variables thatsatisfy the constraint when Vi = d – d is said to be Arc Inconsistent – We can remove d from the domain of Vi—this value cannot lead
to a solution (much like Forward Checking, but more powerful).
• e.g. C(X,Y): X>Y Dom(X)={1,5,11} Dom(Y)={3,8,15}
– For X=1 there is no value of Y s.t. 1>Y => so we canremove 1 from domain X
– For Y=15 there is no value of X s.t. X>15, so remove 15from domain Y
– We obtain more restricted domains Dom(X)={5,11}and Dom(Y)={3,8}
# ,54 H, ;5851 ( 2,0 # 4 7 0Y
#-*
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
76/87
JF!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• If we apply arc consistency propagation during searchthe search tree’s size will typically be much reduced insize.
• Removing a value from a variable domain may triggerfurther inconsistency, so we have to repeat theprocedure until everything is consistent.
– We put constraints on a queue and add new constraints to thequeue as we need to check for arc consistency.
# ,54 H, ;5851 ( 2,0 # 4 7 0Y
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
77/87
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
78/87
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
79/87
[2# 268-,4+>C
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
80/87
&N!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
[ ,• 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.
[2# 268-,4+>C\ 7*K-,07 [2# ./,4*8
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
81/87
&!!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
[ , , [ , ,
GAC(Level) /*Maintain GAC Algorithm */If all variables are assigned
PRINT Value of each VariableRETURN or EXIT (RETURN for more solutions)(EXIT for only one solution)
V := PickAnUnassignedVariable()Assigned[V] := TRUEfor d := each member of CurDom(V)
Value[V] := dPrune all values of V ! d from CurDom[V]for each constraint C whose scope contains V
Put C on GACQueueif(GAC_Enforce() != DWO)
GAC(Level+1) /*all constraints were ok*/RestoreAllValuesPrunedFromCurDoms()
Assigned[V] := FALSE
return;
A*K-,07 [2# W;,/*7 566 [2# 4*0-*
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
82/87
&9!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
GAC_Enforce()// GAC-Queue contains all constraints one of whose variables has// had its domain reduced. At the root of the search tree
// first we run GAC_Enforce with all constraints on GAC-Queuewhile GACQueue not empty
C = GACQueue.extract()for V := each member of scope(C)
for d := CurDom[V]Find an assignment A for all other
variables in scope(C) such thatC( A ! V=d) = Trueif A not found
CurDom[V] = CurDom[V] – dif CurDom[V] = "
empty GACQueuereturn DWO //return immediately
elsepush all constraints C’ such thatV # scope(C’) and C’ $ GACQueueon to GACQueue
return TRUE //while loop exited without DWO
, ;,
A*K-,07 [2#
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
83/87
&%!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• A support for V=d in constraint C is an assignment A toall of the other variables in scope(C) such thatA U {V=d} satisfies C. ( A is what the algorithm’s innerloop looks for).
• Smarter implementations keep track of “supports” toavoid having to search though all possible assignmentsto the other variables for a satisfying assignment.
A*K-,07 [2#
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
84/87
&'!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• Rather than search for a satisfying assignment to Ccontaining V=d, we check to see if the current supportis still valid: i.e., all values it assigns still lie in thevariable’s 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
A*K-,07 [2#
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
85/87
&E!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
• However, finding a support for V=d in constraint C still inthe worst case requires O(2 k ) work, where k is the arityof C, i.e., |scope(C)|.
• Another key development in practice is that for someconstraints this computation can be done inpolynomial time.E.g., all-diff(V1, …. Vn) we can be check if Vi=d has asupport in the current domains of the other variables inpolynomial time using ideas from graph theory.
We do not need to examine all combinationsof values for the other variables looking for asupport
[2# 7*K-,07 7B5C;67
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
86/87
&F!"#$%& ("))#*+, -.-/01 234567*)863 46 958:)$"; 234%;;$%5+$4? 6@ A656346
GAC(C SS8 ) ! CurDom of V 1,5 , V1,6 , V2,4 , V3,4 , V3,6 ) ={1, 2, 3, 4, 8}
GAC(C R1 ) ! CurDom of V 1,7 , V1,8 = {2, 3, 7, 9}CurDom of V 1,5 , V1,6 , = {2, 3}
GAC(C SS8 ) ! CurDom of V 2,4 , V3,4 , V3,6 ) = {1, 4, 8}GAC(C C5 ) ! CurDom of V 5,5 ,V9,5 = {2, 6, 8}
! CurDom of V 1,5 = {2}GAC(C SS8 ) ! CurDom of V 1,6 = {3}
= All-diff
By going back and forthbetween constraints we getmore values pruned.
C SS2 = All-diff(V 1,4 , V1,5 , V1,6 , V2,4 , V2,5 , V2,6 , V3,4 , V3,5 , V3,6 )C
R1 = All-diff(V 1,1 , V1,2 , V1,3 , V1,4 , V1,5 , V1,6 , V1,7 , V1,8 , V1,9 )
C C5 = All-diff(V 1,5 , V2,5 , V35 , V4,5 , V5,5 V6,5 , V7,5 , V8,5 , V9,5 )
U5*Y ,756G=-,6. 5;;64051-*< -K #$H
-
8/17/2019 csc384-Lecture03-BacktrackingSearch
87/87
• Assignment problems – who teaches what class
• Timetabling problems – exam schedule
• Transportation scheduling• Floor planning• Factory scheduling• Hardware configuration
– a set of compatible components