Example:Map-Coloring CONSTRAINT SATISFACTION PROBLEMS CHAPTER 5 Variables WA.V7.Q.NsW.V.A.T Con must have diffe I.blue).(green,red).(green.blue).... Outline Example:Map-Coloring conto ◇CSP examples Problem structure and problem decompositior Local search for CSPs Constraint satisfaction problems(CSPs) Constraint graph Binary CSP: Constraint grap:nodes are variables.ars shoonrint CSP: (】 Simple example of formal representation language ⊙ Spp2SPETmnwipobal
Constraint Satisfaction Problems Chapter 5 Chapter 5 1 Outline ♦ CSP examples ♦ Backtracking search for CSPs ♦ Problem structure and problem decomposition ♦ Local search for CSPs Chapter 5 2 Constraint satisfaction problems (CSPs) Standard search problem: state is a “black box”—any old data structure that supports goal test, eval, successor CSP: state is defined by variables Xi with values from domain Di goal test is a set of constraints specifying allowable combinations of values for subsets of variables Simple example of a formal representation language Allows useful general-purpose algorithms with more power than standard search algorithms Chapter 5 3 Example: Map-Coloring Western Australia Northern Territory South Australia Queensland New South Wales Victoria Tasmania Variables WA, NT, Q, NSW, V , SA, T Domains Di = {red, green, blue} Constraints: adjacent regions must have different colors e.g., WA 6= NT (if the language allows this), or (WA, NT) ∈ {(red, green),(red, blue),(green, red),(green, blue), . . .} Chapter 5 4 Example: Map-Coloring contd. Western Australia Northern Territory South Australia Queensland New South Wales Victoria Tasmania Solutions are assignments satisfying all constraints, e.g., {WA = red, NT = green, Q = red, NSW = green, V = red, SA = blue, T = green} Chapter 5 5 Constraint graph Binary CSP: each constraint relates at most two variables Constraint graph: nodes are variables, arcs show constraints Victoria WA NT SA Q NSW V T General-purpose CSP algorithms use the graph structure to speed up search. E.g., Tasmania is an independent subproblem! Chapter 5 6
Varieties of CSPs Real-world CSPs Hardware configuration Spreadsheets Continuous variables 8n Transportation schedulng Factory scheduling Floorplanning Notice that Varieties of constraints Standard search formulation(incremental) SA≠rem Let'sththeddumbp thenxi States are defined by the values assigned so far olve pairs of variable Initial state:the empty asigment. Goal test:the currentassignment is complete 男 with n variables Example:Cryptarithmetic Backtracking search T WO T WO FOUR Can solve n-queens for
Varieties of CSPs Discrete variables finite domains; size d ⇒ O(d n ) complete assignments ♦ e.g., Boolean CSPs, incl. Boolean satisfiability (NP-complete) infinite domains (integers, strings, etc.) ♦ e.g., job scheduling, variables are start/end days for each job ♦ need a constraint language, e.g., StartJob1 + 5 ≤ StartJob3 ♦ linear constraints solvable, nonlinear undecidable Continuous variables ♦ e.g., start/end times for Hubble Telescope observations ♦ linear constraints solvable in poly time by LP methods Chapter 5 7 Varieties of constraints Unary constraints involve a single variable, e.g., SA 6= green Binary constraints involve pairs of variables, e.g., SA 6= WA Higher-order constraints involve 3 or more variables, e.g., cryptarithmetic column constraints Preferences (soft constraints), e.g., red is better than green often representable by a cost for each variable assignment → constrained optimization problems Chapter 5 8 Example: Cryptarithmetic F T U W R O + T W O T W O F O U R X3 X2 X1 Variables: F T U W R O X1 X2 X3 Domains: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} Constraints alldiff(F, T,U, W, R, O) O + O = R + 10 · X1, etc. Chapter 5 9 Real-world CSPs Assignment problems e.g., who teaches what class Timetabling problems e.g., which class is offered when and where? Hardware configuration Spreadsheets Transportation scheduling Factory scheduling Floorplanning Notice that many real-world problems involve real-valued variables Chapter 5 10 Standard search formulation (incremental) Let’s start with the straightforward, dumb approach, then fix it States are defined by the values assigned so far ♦ Initial state: the empty assignment, { } ♦ Successor function: assign a value to an unassigned variable that does not conflict with current assignment. ⇒ fail if no legal assignments (not fixable!) ♦ Goal test: the current assignment is complete 1) This is the same for all CSPs! 2) Every solution appears at depth n with n variables ⇒ use depth-first search 3) Path is irrelevant, so can also use complete-state formulation 4) b = (n − `)d at depth `, hence n!d n leaves!!!! Chapter 5 11 Backtracking search Variable assignments are commutative, i.e., [WA = red then NT = green] same as [NT = green then WA = red] Only need to consider assignments to a single variable at each node ⇒ b = d and there are d n leaves Depth-first search for CSPs with single-variable assignments is called backtracking search Backtracking search is the basic uninformed algorithm for CSPs Can solve n-queens for n ≈ 25 Chapter 5 12
Backtracking search Backtracking example VE-BACKTRACKIN f,cp)returns soln/failur 嗡的的 Backtracking example Backtracking example 倒 嗡嗡鈴 给给 Backtracking example Improving backtracking efficiency 哈 General-purpo methods ca ive huge gains in speed: 1.Which ariable soud be assigned next 哈哟玲 3.Can we detect inevitable falure erly? 4.Can we take advantage of problem structure?
Backtracking search function Backtracking-Search(csp) returns solution/failure return Recursive-Backtracking({ }, csp) function Recursive-Backtracking(assignment, csp) returns soln/failure if assignment is complete then return assignment var ← Select-Unassigned-Variable(Variables[csp], assignment, csp) for each value in Order-Domain-Values(var, assignment, csp) do if value is consistent with assignment given Constraints[csp] then add {var = value} to assignment result ← Recursive-Backtracking(assignment, csp) if result 6= failure then return result remove {var = value} from assignment return failure Chapter 5 13 Backtracking example Chapter 5 14 Backtracking example Chapter 5 15 Backtracking example Chapter 5 16 Backtracking example Chapter 5 17 Improving backtracking efficiency General-purpose methods can give huge gains in speed: 1. Which variable should be assigned next? 2. In what order should its values be tried? 3. Can we detect inevitable failure early? 4. Can we take advantage of problem structure? Chapter 5 18
Minimum remaining values Forward checking 哈一哈给一附 嗡 可可口 Degree heuristic Forward checking MRV variable 哈哈 NSW Least constraining value Forward checking 哈哈一哈 WA NT NSW SA 可可可可▣ 1000 queens feasib 高
Minimum remaining values Minimum remaining values (MRV): choose the variable with the fewest legal values Chapter 5 19 Degree heuristic Tie-breaker among MRV variables Degree heuristic: choose the variable with the most constraints on remaining variables Chapter 5 20 Least constraining value Given a variable, choose the least constraining value: the one that rules out the fewest values in the remaining variables Allows 1 value for SA Allows 0 values for SA Combining these heuristics makes 1000 queens feasible Chapter 5 21 Forward checking Idea: Keep track of remaining legal values for unassigned variables Terminate search when any variable has no legal values WA NT Q NSW V SA T Chapter 5 22 Forward checking Idea: Keep track of remaining legal values for unassigned variables Terminate search when any variable has no legal values WA NT Q NSW V SA T Chapter 5 23 Forward checking Idea: Keep track of remaining legal values for unassigned variables Terminate search when any variable has no legal values WA NT Q NSW V SA T Chapter 5 24
Forward checking Are consistency repntccecogrdas Simplest fom of propaiomakes eacharc c 吟玲鸱的 哈哈鸱 NSW SA O可O可O可O可O可O可▣ 可0可可可可0可 0可▣ 可▣ Constraint propagation Arc consistency Simplest form of 哈一玲一哈 哈的一哈 “"””“▣ VTand SAcannot both be bluel Constraint propagation repeatedly enforces constraints locally Arc consistency Arc consistency Simplest fom of 哈哈鸣 哈哈哈 “"。“ “"”"“ If X loses a value.neighbors of X need to be rechecked Arcdetects failureerer than foard checking
Forward checking Idea: Keep track of remaining legal values for unassigned variables Terminate search when any variable has no legal values WA NT Q NSW V SA T Chapter 5 25 Constraint propagation Forward checking propagates information from assigned to unassigned variables, but doesn’t provide early detection for all failures: WA NT Q NSW V SA T NT and SA cannot both be blue! Constraint propagation repeatedly enforces constraints locally Chapter 5 26 Arc consistency Simplest form of propagation makes each arc consistent X → Y is consistent iff for every value x of X there is some allowed y WA NT Q NSW V SA T Chapter 5 27 Arc consistency Simplest form of propagation makes each arc consistent X → Y is consistent iff for every value x of X there is some allowed y WA NT Q NSW V SA T Chapter 5 28 Arc consistency Simplest form of propagation makes each arc consistent X → Y is consistent iff for every value x of X there is some allowed y WA NT Q NSW V SA T If X loses a value, neighbors of X need to be rechecked Chapter 5 29 Arc consistency Simplest form of propagation makes each arc consistent X → Y is consistent iff for every value x of X there is some allowed y WA NT Q NSW V SA T If X loses a value, neighbors of X need to be rechecked Arc consistency detects failure earlier than forward checking Can be run as a preprocessor or after each assignment Chapter 5 30