Download presentation
Presentation is loading. Please wait.
Published byLilian Barrett Modified over 9 years ago
1
1 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart CSC384: Lecture 16 Last time Searching a Graphplan for a plan, and relaxed plan heuristics. Today Constraint Satisfaction Problems Backracking Search.. Readings Chaper 4.7
2
2 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart CSPs Constraint Satisfaction Problems, like planning problems, involve a particular generic (i.e., general) state representation. Specialized search techniques can be developed for this particular state representation. Many problems can be expressed in this generic representation.
3
3 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart CSPs Example Car Sequencing Problem. Some number of cars are to be produced. Each requires different options (e.g., some a sunroof, some a manual transmission). The assembly line has different stations for installing these different options. Each each station only has a fixed capacity expressed by two numbers n/m. In any sequence of m cars at most n of them can be processed by the station. For example, the sunroof station might only be able to handle 1/4 of the cars. This means that as the cars in the assembly line pass by we only have time to install a sunroof to every fourth car. Thus cars requiring the same options cannot be bunched together, rather they the cars must be sequenced so that no station every has its capacity exceeded. The problem is given a set of cars each with specified options find a sequencing so that no option station has its capacity exceeded.
4
4 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart CSPs The Formalism 1. A set of variables V 1, V 2, …, V n. 2. For each variable a domain of possible values, D i 3. A set of constraints C 1,…,C m. The aim is assign a value to each of the variables. These values must come from the variable’s domain. Each constraint is over some set of variables. For example, C 3 might be over the set of variables {V 2,V 4,V 5 }. Each constraint specifies the legal combinations of assignments to the variables it is over. For example, C 3 might be the constraint that all values must be different. An assignment to the variables of the constraint that is allowed by the constraint is said to satisfy the constraint. A solution to a CSP is an assignment to all of the variables satisfying all of the constraints.
5
5 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Car Sequencing Continued. Let there be N cars. A sequence will order these cars. Variables One variable for each position in the sequence, the domain of values for the variable is the set of cars {1,…,N}. V i = j, means that position i is occupied by car j. Then we have a constraint for each of the capacity constraints. For example, if the sunroof station has capacity 1/2, we would have a constraint over every pair of adjacent positions C(V 1,V 2 ) over V 1 and V 2, which only allows one of V 1 and V 2 to be assigned a car requiring a sunroof. In general an n/m capacity station will be represented as a contraint over every sequence of m variables, which restricts the assignment to these variables so that only n of them can require the use of the station.
6
6 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Car Sequencing Continued. Let 1, 2, 3, 4, 5 be the cars, with 1 and 2 requiring sunroofs, while 2 and 3 require ABS. Say that the sunroof station has capacity 1/3. While the ABS station has capacity 1/2. 4 variables V 1,V 2,V 3,V 4, V 5. Each with domain of values {1,2,3,4,5}. E.g., V 2 =4 means that car 4 is in position 2. The sunroof station would generate the constraints: C(V 1,V 2,V 3 ), C(V 2,V 3,V 4 ), C(V 3,V 4,V 5 ). Each constraint would be satisfied only by assignments where at most one of the variables is assigned 1 or 2. The ABS station would generate the constraints: C(V 1,V 2 ), C(V 2,V 3 ), C(V 3,V 4 ), C(V 4,V 5 ). Each constraint would be satisfied only by assignments where at most one of the variables is assigned 2 or 3.
7
7 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart How not to solve a CSP—Generate and Test. Examine every possible assignment to all of the variables and test to see if that assignment is a solution. In the case of the N-car sequencing there are N N different assignments.
8
8 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart How not to solve a CSP—Generate and Test. V 1 =1V 1 =2V 1 =3 V 2 =1V 2 =2V 2 =3V 2 =1V 2 =2V 2 =3 Complete assignments to the variables starting with V 1 =1,V 2 =1 Complete assignments to the variables starting with V 1 =3
9
9 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart How not to solve a CSP—Generate and Test. V 1 =1V 1 =2V 1 =3 V 2 =1V 2 =2V 2 =3V 2 =1V 2 =2V 2 =3 Leaves of this tree are all possible assignments to the variables. Generate and test would visit each of these leaves until a solution is found.
10
10 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Backtracking Techniques Backtracking techniques are based on doing a depth-first search in this tree. It improves over generate and test in two ways. Since constraints are over subsets of the variables, it checks these constraints as soon as all of their variables become assigned. If the constraint is not satisfied by the current assignment to these variables, we stop descending the tree and backtrack instead to try an alternate variable assignment It dynamically reorders the tree. That is, after making the assignment V1=1, it might decide to examine V2=1, but after making V1=2, it might decide to examine V3=1.
11
11 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Backtracking, Checking Constraints V 1 =1V 1 =2V 1 =3 V 2 =1V 2 =2V 2 =3V 2 =1V 2 =2V 2 =3 Check C(V 1,V 2 )
12
12 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Backtracking, Dynamic Reordering V 1 =1V 1 =2V 1 =3 V 2 =1V 2 =2V 2 =3V 3 =1V 3 =2V 3 =3 Check C(V 1,V 2 ) Check C(V 1,V 3 )
13
13 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Backtracking. Both improvements can yield exponential improvements in performance. A violated constraint means that we don’t need to explore any of the subtree below. The intuition behind dynamic reordering is that by choosing V 3 instead of V 2, we might find violated constraints much sooner.
14
14 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Backtracking Algorithm. Algorithm BT (Backtracking) BT(Level) If all variables assigned PRINT Value of each Variable (solution!) RETURN or EXIT V := PickUnassignedVariable() Variable[Level] := V Assigned[V] := TRUE for d := each member of Domain(V) Value[V] := d OK := TRUE for each constraint C such that V is a variable of C and all other variables of C are assigned if C is not satisfied by the current set of assignments OK := FALSE if(OK) BT(Level+1) return
15
15 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Backtracking Algorithm. Algorithm BT (Backtracking) BT(Level) If all variables assigned PRINT Value of each Variable (solution!) RETURN or EXIT (return if want all solutions, exit if want only one) V := PickUnassignedVariable() Variable[Level] := V Assigned[V] := TRUE for d := each member of Domain(V) Value[V] := d OK := TRUE for each constraint C such that V is a variable of C and all other variables of C are assigned if C is not satisfied by the current set of assignments OK := FALSE if(OK) BT(Level+1) return
16
16 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Backtracking Algorithm. Algorithm BT (Backtracking) BT(Level) If all variables assigned PRINT Value of each Variable (solution!) RETURN or EXIT V := PickUnassignedVariable() (Dynamic ordering) Variable[Level] := V Assigned[V] := TRUE for d := each member of Domain(V) Value[V] := d OK := TRUE for each constraint C such that V is a variable of C and all other variables of C are assigned if C is not satisfied by the current set of assignments OK := FALSE if(OK) BT(Level+1) return
17
17 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Backtracking Algorithm. Algorithm BT (Backtracking) BT(Level) If all variables assigned PRINT Value of each Variable (solution!) RETURN or EXIT V := PickUnassignedVariable() Variable[Level] := V Assigned[V] := TRUE for d := each member of Domain(V) Value[V] := d OK := TRUE for each constraint C such that V is a variable of C and all other variables of C are assigned (These conditions ensure we only check each constraint at most once along each path). if C is not satisfied by the current set of assignments OK := FALSE if(OK) BT(Level+1) return
18
18 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Example N-Queens N-Queens. Want to place N queens on an NXN chess board so that no two queens can attack each other. Variables, V 1, …, V N. Domains {1,…,N} V i = j means that we place the queen in column i in row j. Constraints, one between every pair of variables: C(V i,V j ) : V i ≠ V j AND abs(V i – V j ) != abs(i – j) What do these constraints mean?
19
19 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart 4-Queens Level 1 2 3 4 V1=1 V2=1 (fail) V2=2 (fail) V2=3 V3=1 (fail) V3=2 (fail) V3=3 (fail) V3=4 (fail) V2=4 V3=1 (fail) V3=2 V4=1 (fail) V4=2 (fail) V4=3 (fail) V4=4 (fail) V3=3 (fail) V3=4 (fail) V1=2 V2=1 (fail) V2=2 (fail) V2=3 (fail) V2=4 V3=1 V4=1 (fail) V4=2 (fail) V4=3 SOLUTION (V1=2, V2=4, V3=1, V4=3)
20
20 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Forward Checking The backtracking algorithm is old Newer algorithms have been found that are much more efficient. One of the most common algorithms is Forward Checking. The idea behind forward checking is not to wait until the constraints have had all of their variables assigned, but rather to check forward into the set of as yet unassigned variables. Forward checking is easiest to describe in the context of binary CSPs. A binary CSP is one in which every constraint is over only two variables.
21
21 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Forward Checking Forward checking maintains a two-dimensional array as it searches the tree of variable assignments. Curdom[V][d] is indexed by the variable and a value in that variable’s domain. Curdom is used to keep track of the active elements of every variables domain. If Curdom[V][d] = 0 then d is still a legal value for V. If Curdom[V][d] = i, then d became inconsistent (violates some constraint) at level i.
22
22 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Forward Checking Initially Curdom[V][d] = 0 for all variables V and all values d in Domain(V). FCCheck(C,V,d,V”,Level) //We have just assigned V the value d, at Level //V” is an unassigned variable //and C is a constraint between V and V” //This routine updates CurDom[V”]. DWO := TRUE; for d" := each member of Domain(V") such that Curdom[V"][d"] ≠ 0 if (V=d,V”=d") does not satisfy C Curdom[V"][d"] := Level else DWO := FALSE; return(DWO); //DWO is true if V” has no domain values remaining.
23
23 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Forward Checking FC(Level) If all variables assigned PRINT Value of each Variable RETURN or EXIT V := PickUnassignedVariable() Assigned[V] := TRUE for d := each member of Domain(V) such that Curdom[V][d] ≠ 0 Value[V] := d for each constraint C over V and an unassigned V” DWO := FCCheck(C,V,d,V”,Level) if DWO == TRUE break; //Abort for loop. Some future variable can’t be assigned if (DWO == FALSE) FC(Level+1) Restore(Level,V) return;
24
24 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Forward Checking Restore(Level,V) //Return pruned values to variable domains. for each constraint C over V and V“ such that V" is currently unassigned for d" := each member of Domain(V") if Curdom[V"][d"] == Level Curdom[V"][d"] := 0
25
25 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart 4-Queens Forward Checking Level 1 2 3 4 V1=1 Curdom[V2] = [1,1,0,0] Curdom[V3] = [1,0,1,0] Curdom[V4] = [1,0,0,1] V2=3 Curdom[V3] = [1,2,1,2] (fail) V2=4 Curdom[V3] = [1,0,1,2] Curdom[V4] = [1,2,0,1] V3=2 Curdom[V4] = [1,2,3,1] (fail) After backtrack Curdom[V2] = [0,0,0,0] Curdom[V3] = [0,0,0,0] Curdom[V4] = [0,0,0,0] V1=2 Curdom[V2] = [1,1,1,0] Curdom[V3] =[0,1,0,1] Curdom[V4] = [1,1,0,0] V2=4 Curdom[V3] = [0,1,2,1] Curdom[V4] = [1,1,0,2] V3=1 Curdom[V4] = [1,1,0,2] V4=3 (Solution)
26
26 CSC 384 Lecture Slides (c) 2002-2003, C. Boutilier and P. Poupart Forward Checking A good heuristic for forward checking is always to instantiate next the variable with minimum remaining domain. Forward Checking can be orders of magnitude faster than backtracking.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.