930 likes | 945 Views
This text discusses constraint satisfaction problems (CSPs), constraint solvers, and various search algorithms such as backtracking, forward checking, and maintaining arc-consistency. Examples and explanations are provided with a focus on the n-queens problem.
E N D
Logic & AR Summer School, 2002 Constraints and Search Toby WalshCork Constraint Computation Centre (4C) tw@4c.ucc.ie
Constraint satisfaction • Constraint satisfaction problem (CSP) is a triple <V,D,C> where: • V is set of variables • Each X in V has set of values, D_X • Usually assume finite domain • {true,false}, {red,blue,green}, [0,10], … • C is set of constraints Goal: find assignment of values to variables to satisfy all the constraints
Constraint solver • Tree search • Assign value to variable • Deduce values that must be removed from future/unassigned variables • Constraint propagation • If any future variable has no values, backtrack else repeat • Number of choices • Variable to assign next, value to assign Some important refinements like nogood learning, non-chronological backtracking, …
Constraint propagation • Enfrocing arc-consistency (AC) • A binary constraint r(X1,X2) is AC iff for every value for X1, there is a consistent value (often called support) for X2 and vice versa E.g. With 0/1 domains and the constraint X1 =/= X2 Value 0 for X1 is supported by value 1 for X2 Value 1 for X1 is supported by value 0 for X2 … • A problem is AC iff every constraint is AC
Tree search • Backtracking (BT) • Forward checking (FC) • Maintaining arc-consistency (MAC) • Limited discrepany search (LDS) • Non-chronological backtracking & learning
Example: n-queens Place n-queens on an n n board so that no pair of queens attacks each other
Example: 4-queens Variables: x1, x2 , x3 , x4 Domains: {1, 2, 3, 4} Constraints: xi xjand | xi - xj | | i- j| x1 x2 x3 x4 1 2 3 4
Example: 4-queens x1 x2 x3 x4 One solution: x1 2 x2 4 x3 1 x4 3 1 Q 2 Q 3 Q Q 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 1 Q 2 3 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 1 1 Q Q 2 3 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 1 Backtrack 1 Q Q 2 3 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 2 1 Q 2 Q 3 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 2 Backtrack 1 Q 2 Q 3 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 3 1 Q 2 3 Q 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 3 x3 1 1 Q Q 2 3 Q 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 3 x3 1 Backtrack 1 Q Q 2 3 Q 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 3 x3 2 1 Q 2 Q 3 Q 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 3 x3 2 Backtrack 1 Q 2 Q 3 Q 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 3 x3 3 1 Q 2 3 Q Q 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 3 x3 3 Backtrack 1 Q 2 3 Q Q 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 3 x3 4 1 Q 2 3 Q Q 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 3 x3 4 Backtrack 1 Q 2 3 Q Q 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 4 1 Q 2 3 Q 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 4 x3 1 1 Q Q 2 3 Q 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 4 x3 1 Backtrack 1 Q Q 2 3 Q 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 4 x3 2 1 Q 2 Q 3 Q 4
BT on 4-queens x1 x2 x3 x4 Try: x1 1 x2 4 x3 2 And so on! 1 Q 2 Q 3 Q 4
BT search tree for 4-queens x1 1 2 3 4 … x2 … x3 x4 (2,4,1,3) (3,1,4,2)
Forward checking (FC) • Maintains limited form of arc-consistency • Makes constraints involving current var and any future var arc-consistent • Only looks at each constraint once! • No need to re-visit constraints to ensure support remains when a domain value is pruned • Very cheap, little extra cost over BT
FC on 4-queens x1 x2 x3 x4 Try: x1 1 1 Q 2 3 4
FC on 4-queens x1 x2 x3 x4 Try: x1 1 Forward check 1 Q X X X X 2 3 X 4 X
FC on 4-queens x1 x2 x3 x4 Try: x1 1 x2 3 1 Q X X X X 2 3 X Q 4 X
FC on 4-queens x1 x2 x3 x4 Try: x1 1 x2 3 Forward check 1 Q X X X X 2 X 3 X Q X 4 X X
FC on 4-queens x1 x2 x3 x4 Try: x1 1 x2 3 Domian wipeout for x3 1 Q X X X X 2 X 3 X Q X 4 X X
FC on 4-queens x1 x2 x3 x4 Try: x1 1 x2 3 Backtrack 1 Q X X X X 2 X 3 X Q X 4 X X
FC on 4-queens x1 x2 x3 x4 Try: x1 1 x2 3 Backtrack 1 Q X X X X 2 3 X 4 X
FC on 4-queens x1 x2 x3 x4 Try: x1 1 x2 4 1 Q X X X X 2 3 X 4 Q X
FC on 4-queens x1 x2 x3 x4 Try: x1 1 x2 4 Forward check 1 Q X X X X 2 X 3 X 4 Q X X
FC on 4-queens x1 x2 x3 x4 Try: x1 1 x2 4 x3 2 1 Q X X X X Q 2 X 3 X 4 Q X X
FC on 4-queens x1 x2 x3 x4 Try: x1 1 x2 4 x3 2 Forward check 1 Q X X X X Q 2 X 3 X X 4 Q X X
FC on 4-queens x1 x2 x3 x4 Try: x1 1 x2 4 x3 2 Domain wipeout for x4 1 Q X X X X Q 2 X 3 X X 4 Q X X
FC on 4-queens x1 x2 x3 x4 Try: x1 1 x2 4 Backtrack 1 Q X X X X 2 X 3 X 4 Q X X
FC on 4-queens x1 x2 x3 x4 Try: x1 1 Backtrack 1 Q X X X X 2 3 X 4 X
FC on 4-queens x1 x2 x3 x4 Try: x1 2 1 Q 2 3 4
FC on 4-queens x1 x2 x3 x4 Try: x1 2 Forward check 1 X Q 2 X X X X 3 4 X
FC on 4-queens x1 x2 x3 x4 Try: x1 2 x2 4 1 X Q 2 X X X X 3 Q 4 X
FC on 4-queens x1 x2 x3 x4 Try: x1 2 x2 4 Forward check 1 X Q 2 X X X X 3 X Q 4 X X
FC on 4-queens x1 x2 x3 x4 Try: x1 2 x2 4 x3 1 1 X Q Q 2 X X X X 3 X Q 4 X X
FC on 4-queens x1 x2 x3 x4 Try: x1 2 x2 4 x3 1 Forward check 1 X Q X Q 2 X X X X 3 X Q 4 X X
FC on 4-queens x1 x2 x3 x4 Try: x1 2 x2 4 x3 1 x4 3 1 X Q X Q 2 X X X X 3 X Q Q 4 X X