Constraint Satisfaction Problems Chapter 3, Section 7 and Chapter 4, Section 4.4 AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 1
Outline } CSP examples } General search applied to CSPs } Backtracking } Forward checking } Heuristics for CSPs AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 2
CSP: is dened by variables V i with values from domain D i state Simple example of a formal representation language Constraint satisfaction problems (CSPs) search problem: Standard is a \black box" any old data structure state that supports goal test, eval, successor test is a set of constraints specifying goal combinations of values for subsets of variables allowable useful general-purpose algorithms with more power Allows standard search algorithms than AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 3
Variables Q 1, Q 2, Q 3, Q 4 E.g., values for (Q 1 ;Q 2 ) are (1; 3) (1; 4) (2; 4) (3; 1) (4; 1) (4; 2) Example: 4-Queens as a CSP Assume one queen in each column. Which row does each one go in? Domains D i = f1; 2; 3; 4g Constraints i 6= Q j (cannot be in same row) Q jq i, Q j j6= ji, jj (or same diagonal) 1 Q = 1 Q 2 = 3 Translate each constraint into set of allowable values for its variables AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 4
Constraint graph Binary CSP: each constraint relates at most two variables Constraint graph: nodes are variables, arcs show constraints Q1 Q 2 Q 3 Q 4 AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 5
= D + E or Y = D + E, 10, etc. Y 6= E, D 6= M, D 6= N, etc. D Example: Cryptarithmetic Variables N ORSY DEM S E N D Domains 1; 2; 3; 4; 5; 6; 7; 8; 9g f0; M O R E + O N E Y M Constraints 6= 0, S 6= 0(unary constraints) M AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 6
Variables C i Countries Example: Map coloring C C 1 2 Color a map so that no adjacant countries have the same color C 3 Domains Blue; Greeng fred; Constraints 1 6= C 2, C 1 6= C 5, etc. C C 5 C 6 C 4 C1 C2 Constraint graph: C 5 C 3 C 6 C 4 AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 7
Real-world CSPs problems Assignment who teaches what class e.g., problems Timetabling which class is oered when and where? e.g., Hardware conguration Spreadsheets Transportation scheduling Factory scheduling Floorplanning Notice that many real-world problems involve real-valued variables AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 8
Applying standard search Let's start with the straightforward, dumb approach, then x it States are dened by the values assigned so far Initial state: all variables unassigned Operators: assign a value to an unassigned variable Goal test: all variables assigned, no constraints violated Notice that this is the same for all CSPs! AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 9
Implementation state keeps track of which variables have values so far CSP variable has a domain and a current value Each CSP-State datatype Unassigned, a list of variables not yet assigned components: Assigned, a list of variables that have values CSP-Var datatype Name, for i/o purposes components: Domain, a list of possible values Value, current value (if any) can be represented Constraints as sets of allowable values, or explicitly implicitly by a function that tests for satisfaction of the constraint AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 10
Standard search applied to map-coloring UNASSIGNED ASSIGNED C1 C2 C3 UNASSIGNED ASSIGNED C2 C3 C1 = RED UNASSIGNED ASSIGNED C1 C3 C2 = BLUE UNASSIGNED ASSIGNED C1 C2 C3 = GREEN AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 11
Complexity of the dumb approach Max. depth of space m =?? Depth of solution state d =?? Search algorithm to use?? Branching factor b =?? This can be improved dramatically by noting the following: Order of assignment is irrelevant, hence many paths are equivalent 1) Adding assignments cannot correct a violated constraint 2) AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 12
Complexity of the dumb approach Max. depth of space m =?? n (number of variables) Depth of solution state d =?? n (all vars assigned) Search algorithm to use?? depth-rst Branching factor b =?? i jd i j (at top of tree) This can be improved dramatically by noting the following: Order of assignment is irrelevant so many paths are equivalent 1) Adding assignments cannot correct a violated constraint 2) AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 13
Can solve n-queens for n 15 Backtracking search depth-rst search, but Use x the order of assignment, ) b = jd i j 1) be done in the Successors function) (can check for constraint violations 2) constraint violation check can be implemented in twoways: The modify Successors to assign only values that 1) allowed, given the values already assigned are 2) check constraints are satised before expanding a state or Backtracking search is the basic uninformed algorithm for CSPs AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 14
C 5 Can solve n-queens up to n 30 Forward checking Keep track of remaining legal values for unassigned variables Idea: search when any variable has no legal values Terminate Simplied map-coloring example: C 1 C C 5 4 red blue green 1 C 2 C C 2 3 C 4 C C 3 AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 15
... p AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 16
... p AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 17
... p AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 18
Can solve n-queens for n 1000 Heuristics for CSPs intelligent decisions on More value to choose for each variable which which variable to assign next C C 1 2 C 3 C 1 = Red, C 2 = Green, choose C 3 =?? Given. C 5 C 6 C 4 C 1 = Red, C 2 = Green, what next?? Given. AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 19
Can solve n-queens for n 1000 Heuristics for CSPs intelligent decisions on More value to choose for each variable which which variable to assign next C C 1 2 C 3 C 1 = Red, C 2 = Green, choose C 3 =?? Given 3 = Green: least-constraining-value C C 1 = Red, C 2 = Green, what next?? Given 5 : most-constrained-variable C C 5 C 6 C 4 AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 20
heuristic: min-conicts value that violates the fewest constraints choose Iterative algorithms for CSPs simulated annealing typically work with Hill-climbing, states, i.e., all variables assigned \complete" apply to CSPs: To states with unsatised constraints allow operators reassign variable values Variable selection: randomly select any conicted variable i.e., hillclimb with h(n) = total number of violated constraints AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 21
States: 4 queens in 4 columns (4 4 Example: 4-Queens = 256 states) Operators: move queen in column Goal test: no attacks Evaluation: h(n) = number of attacks h = 5 h = 2 h = 0 AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 22
of constraints number of variables number Performance of min-conicts random initial state, can solve n-queens in almost constant time Given arbitrary n with high probability (e.g., n = 10,000,000) for same appears to be true for any randomly-generated CSP The in a narrow range of the ratio except R = CPU time critical ratio R AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 23
Compare to general CSPs, where worst-case time is O(jDj n ) A B D E C F Tree-structured CSPs if the constraint graph has no loops, the CSP can be solved Theorem: O(njDj 2 ) time in property also applies to logical and probabilistic reasoning: This important example of the relation between syntactic restrictions and an complexity of reasoning. AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 24
Algorithm for tree-structured CSPs Basic step is called ltering: i ;V j ) Filter(V values of V i that are inconsistent with ALL values of V j removes Filtering example: Vi Vj allowed pairs: < 1, 1 > < 3, 2 > < 3, 3 > remove 2 from domain of V i AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 25
2) For j = n to 1, apply Filter(V i ;V j ) where V i isaparent of V j A B D E C F Algorithm contd. Order nodes breadth-rst starting from any leaf: 1) A B C D E F 3) For j =1to n, pick legal value for V j given parent value AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 26
Summary are a special kind of problem: CSPs dened by values of a xed set of variables states goal test dened by constraints on variable values = depth-rst search with Backtracking xed variable order 1) 2) only legal successors Forward checking prevents assignments that guarantee later failure Variable ordering and value selection heuristics help signicantly Iterative min-conicts is usually eective in practice Tree-structured CSPs can always be solved very eciently AIMA Slides cstuart Russell and Peter Norvig, 1998 Chapter 3, Section 7 and Chapter 4, Section 4.4 27