420 likes | 837 Views
Constraint Satisfaction Problems. KAIST CS570 lecture note based on AIMA and UCB slides & Slides by Park, Koo, Lee of 2003 KAIST class. Outline. What is CSP? CSP examples Solving CSP by search Backtracking search Heuristics for CSP search Local search Summary.
E N D
Constraint Satisfaction Problems KAIST CS570 lecture note based on AIMA and UCB slides & Slides by Park, Koo, Lee of 2003 KAIST class
Outline • What is CSP? • CSP examples • Solving CSP by search • Backtracking search • Heuristics for CSP search • Local search • Summary
Constraint Satisfaction Problem : Example • Map-Coloring Problem
Constraint Satisfaction Problem • A special kind of problem: States are defined by values of a fixed set of variables variables : {X1, X2, …, Xn} Domain according to a variable Xi : (Di) Goal test is defined by constraints on variable values constraints : {C1, C2, … Cn} Solutions are assignment of Di to Xi satisfying all constraints • Each constraint restricts the combination of values that a set of variables may take simultaneously
Has different color VAR: WA,NT,SA,Q,NSW,V,T CSP : Terminologies • Consistent (or legal) assignment • An assignment that does not violate any constraints • Complete assignment • An assignment in which every variable is mentioned • Solution • A complete and consistent assignment • A complete assignment that satisfies all the constraints • Constraint graph • Node : variable • Arc : constraint
Formulation as Standard Search Problem • States • Set of value assignments to some or all of the variables • Initial state • The empty assignment {} • Successor function • Value assignment to any unassigned variable without conflicts with previously assigned variables • Goal test • Finding out the current assignment is complete • Path Cost • A constant cost
Benefits from treating a problem as a CSP • The successor function and goal test can be written in a generic way that applies to all CSPs. • We can develop effective, generic heuristics that require no additional domain-specific expertise. • The structure of the constraint graph can be used to simplify the solution process.
Example of CSP : 8-queen problem • Variables • {Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8} • Domain • Q1, …, Q8 ∊ {(1,1), (1,2), …, (8,8)} • Constraint set • No queen attacks any other
Classification of CSP : by variable’s domain • Discrete variables • Finite domains • 8-queen problem, map-coloring problem • Boolean CSP • Includes 3SAT(NP-complete) • => Don’t expect that in the worst case, we can solve finite-domain CSP in less than exponential time. • Infinite domains • Job scheduling • StartJob1 + 5 < StartJob3 • Continuous variables • Linear programming problem
Classification of CSP - by Constraints’ Characteristic • Absolute Constraint • Unary Constraint • Ex. SA ≠ green • Binary Constraint • Ex. SA ≠ WA • Higher-order Constraint • Ex. Crypt-arithmetic column constraints • Preference Constraint • ex) Prof. X might prefer teaching in the morning. • ex) red is better than yellow • A cost for each variable assignment • Many real-world CSP • Constrained optimization problems
Real-world CSPs • Assignment problems • Who teaches what class • Job-Shop scheduling • Timetabling Problems • Which class is offered when and where ? • Hardware configuration • Transportation scheduling • Floor-planning Notice that many real-world problems involve real-valued variables
Solving CSP by search : Backtracking Search • BFS vs. DFS • BFS terrible! • A tree with n!dn leaves : (nd)*((n-1)d)*((n-2)d)*…*(1d) = n!dn • Reduction by commutativity of CSP • A solution is not in the permutations but in combinations. • A tree with dn leaves • DFS • Used popularly • Every solution must be a complete assignment and therefore appears at depth n if there are n variables • The search tree extends only to depth n. • A variant of DFS : Backtracking search • Chooses values for one variable at a time • Backtracts when failed even before reaching a leaf. • Better than BFS due to backtracking, but still inefficient!!
function BACKTRACKING-SEARCH (csp) returns a solution, or failure return RECURSIVE-BACKTRACKING({}, csp) function RECURSIVE-BACKTRACKING(assignment, csp) returns a solution, or failure ifassignment is complete then returnassignment var SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp], assignment, csp) for eachvalue in ORDER-DOMAIN-VALUES(var, assignment, csp) do ifvalue is consistent with assignment according to CONSTRAINTS[csp] then add {var=value} to assignment result RECURSIVE-BACKTRACKING(assignment, csp) ifresult != failurethen returnresult remove {var = value} from assignment return failure ☜ BACKTRACKING OCCURS HERE!! Solving CSP by search : Backtracking Search
Early failure-detection to decrease the likelihood to fail Restructuring to reduce the problem’s complexity Improving Backtracking Efficiency Variable & value ordering to increase the likelihood to success • Which variable should be assigned next? • Minimum Remaining Values heuristic • In what order should its values be tried? • Least Constraining Values heuristic • Can we detect inevitable failure early? • Forward checking • Constraint propagation (Arc Consistency) • When a path fails, can the search avoid repeating this failure? • Backjumping • Can we take advantage of problem structure? • Tree-structured CSP General purpose methods
Improving backtracking efficiency function BACKTRACKING-SEARCH (csp) returns a solution, or failure return RECURSIVE-BACKTRACKING({}, csp) function RECURSIVE-BACKTRACKING(assignment, csp) returns a solution, or failure ifassignment is complete then returnassignment var SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp], assignment, csp) for eachvalue in ORDER-DOMAIN-VALUES(var, assignment, csp) do ifvalue is consistent with assignment according to CONSTRAINTS[csp] then add {var=value} to assignment result RECURSIVE-BACKTRACKING(assignment, csp) ifresult != failurethen returnresult remove {var = value} from assignment return failure
Heuristics for variable & value ordering • Variable selection • Most constrained variables • MRV(minimum remaining values) heuristic • Choose the variable with the fewest legal values. • Most containing variables • Degree heuristic • In first variable selection, MRV heuristic doesn’t help at all. • Use degree heuristic (selects the variable with the largest degree.) Tie-breaker !! • Value ordering • Least-Constraining-value heuristic • prefer variables that rule out fewest choices of neighbors
Heuristics for variable & value orderingExample for degree heuristic NT NT Q SA SA SA NT ={R, G} Q = {R, G} WA = {R, G} SA = B NSW = {R, G} V = {R, G} T = {R, G, B} 3 3 2 5 3 2 0 NT ={R, G, B},3 Q = {R, G, B},3 WA = {R, G, B},2 SA = {R, G, B},5 NSW = {R, G, B},3 V = {R, G, B},2 T = {R, G, B},0 NT = G Q = {R} WA = {R} SA = B NSW = {R, G} V = {R, G} T = {R, G, B}
Heuristics for variable & value orderingExample for MRV NT NT Q SA SA SA NT ={G, B} Q = {R, G, B} WA = R SA = {G, B} NSW = {R, G, B} V = {R, G, B} T = {R, G, B} NT ={R, G, B} Q = {R, G, B} WA = {R, G, B} SA = {R, G, B} NSW = {R, G, B} V = {R, G, B} T = {R, G, B} NT =G Q = {R, B} WA = R SA = {B} NSW = {R, G, B} V = {R, G, B} T = {R, G, B}
Heuristics for variable & value orderingExample for LCV Terminate search when any variable has no legal values Red in Q is least constraint value NT Q WA NT WA WA Q NT WA NT ={G, B} Q = {R, G, B} WA = R SA = {G, B} NSW = {R, G, B} V = {R, G, B} T = {R, G, B} NT ={R, G, B} Q = {R, G, B} WA = {R, G, B} SA = {R, G, B} NSW = {R, G, B} V = {R, G, B} T = {R, G, B} NT =G Q = {R, B} WA = R SA = {B} NSW = {R, G, B} V = {R, G, B} T = {R, G, B} NT =G Q = R WA = R SA = {B} NSW = {G, B} V = {R, G, B} T = {R, G, B} NT =G Q = B WA = R SA = ? NSW = {R,G} V = {R, G, B} T = {R, G, B}
Forwarding checking(1/4)Heuristics for early failure-detection • How can we find out which variable is minimum remaining? • What does eliminate the values from domain? NT = red, Q = green WA = {red, green, blue} SA = {red, green, blue} NSW = {red, green, blue} V = {red, green, blue} T = {red, green, blue} ⅹ ⅹ ⅹ ⅹ
Forwarding checking(2/4)Heuristics for early failure-detection • Forward checking • A variable X is assigned. • Looks at each unassigned variable Y connected to X by a constraint • Deletes any values of Y’s domain that is inconsistent with X • If there is any Y with empty domain, undo this assigning. Backtrack!! If not, MRV !! • Forward checking is an obvious partner of MRV heuristic !! MRV heuristic is activated after forward checking.
Heuristics for early failure-detectionForwarding checking(3/4) – an example NT Q WA SA NSW V T
Heuristics for early failure-detectionForwarding checking(4/4) - Heuristic flow Forward Checking Yes Tie? No Degree heuristic Select the MRV Forward Checking Select the LCV Goal?
ⅹ o Heuristics for early failure-detectionConstraint propagation: arc consistency • F/C propagates information from assigned to unassigned variables, but doesn’t provide early detection for all failures • Simplest form of propagation makes each arc consistent • The (directed) arc (XY) isconsistent ≡ For all x ∊ X, there is some y ∊ Y. • If X loses a value, neighbors of X need to be rechecked
Heuristics for early failure-detectionConstraint propagation: arc consistency • Detects an inconsistency that is not detected by pure F/C. • Detect failure earlier than forward checking • Makes CSP possibly with reduced domain. • Provides a fast method of constraint propagation O(n2d3) n2 : the maximum number of arcs d : the maximum number of insertion for one node d2 : the number of forward checking • Can be run as a preprocessor or after each assignment
O(n2d3) Heuristics for early failure-detectionConstraint propagation: arc consistency function AC-3(csp) returns the CSP, possibly with reduced domains inputs: csp, a binary CSP with variables {X1, X2, …, Xn} local variables:queue, a queue of arcs, initially all the arcs in csp whilequeue is not empty do (Xi,Xj) REMOVE-FIRST(queue) if REMOVE-INCONSISTENT-VALUES(Xi,Xj) then foreachXkin NEIGHBORS[Xi] do add(Xk,Xi) to queue function REMOVE-INCONSISTENT-VALUES() returns true iff we remove a value removed false for eachx in DOMAIN[Xi] do if no value y in DOMAIN[Xj] allows (x,y) to satisfy the constraint between Xiand Xj thendelete x from DOMAIN[Xi]; removed true returnremoved
Heuristics for early failure-detectionSpecial constraints – Alldiff • Problem-dependent constraints • Alldiff constraint • If there are m variables involved in the constraint and n possible distinct values, m > n inconsistent for Alldiff constraint • An example • 3 variables: NT,SA,Q • NT, SA, Q ∊ {green, blue} • m=3 > n=2 • inconsistent WA NSW
Heuristics for early failure-detectionSpecial constraints – Almost • Resource constraint • Atmost constraint (Use limited resource) • An example • Totally, no more than 10 personnel should be assigned to jobs. • Atmost(10, PA1, PA2, PA3, PA4) • Job1~4 ∊ {3,4,5,6} ( min value = 3 ) • 3+3+3+ 3 > 10 (minimum resource 12 but available resource 10) • inconsistent • Job1~4 ∊ {2, 3,4,5,6} • 2+2+2+2 <10 (consistent) • 2+2+2+5(or 6) > 10(inconsistent) • Delete 5,6 from domain
Heuristics for early failure-detection - Backjumping • The weakness of Backtracking • If failed in SA, backtrack to T. • But, T is not relevant to SA. • Backjumping • Backtracks to the most recent variable in the conflict set. • Conflict set : the set of variables that caused the failure • If failed in SA, backjump to V. 1 Q NSW 2 5 V 3 Next variable T 4
Demo Applets • From MIT Open courseware • Constraint-Propagation and Resource Allocation Java® Demonstration • From UBC, Canada • http://www.cs.ubc.ca/labs/lci/CIspace/Version4/Constraint/
General Heuristic for complexity reduction • Decompose into many sub-problems. • Independent sub-problems • Solve them respectively • Ex) The mainland and Tasmania. • Analysis of connected components of constraint graph
Algorithm for Tree-structured CSP • If constraint graph is a tree, we solve the CSP in time linear in the number of variables 1. Choose a variable as root 2. Order variables from root to leaves such that every node’s parent precedes it in the orderingMakes constraint graph arc-consistent 3. For j from n down to 2, apply arc consistency to the arc(parent(Xj), Xj) Just finds a consistent value.(no backtrack because of arc-consistency) 4. For j from 1 to n, assign Xj consistently with Parent(Xj) • Time complexity : O(nd2)
Heuristics for complexity reductionTransforming to Tree-Structured CSP • Conditioning • Instantiate a variable, prune its neighbor’s domains • Cutset conditioning • Instantiate a set of variable such that the remaining graph is tree • S is called cycle cutset when constraint graph is a tree after the removal of S • Time Complexity : O(dc(n-c)d2) • c : cutset size • dc: the # of possible cutset assignment : • Time complexity in tree-structred CSP : (n-c)d2 • Runtime O(dc . (n-c)d2)
Heuristics for complexity reductionTransforming to Tree-Structured CSP
Iterative algorithm with Min-conflicts heuristic • Initially assigns a (random) value to every variable • reassign the value of one variable at a time • Variable selection : • randomly select any conflict variables • minimum of conflicts with other variables • Hill-climb with h(n) = total number of violated constraints • Min-conflicts is effective for many CSPs • particularly with reasonable initial state • Specially important in scheduling problems • Airline schedule (when the schedule is changed)
Summary • CSPs are a special kind of problem • States defined by values of a fixed set of variables • Goal test defined by constraints on variable values • Backtracking = depth-first search with one variable assigned per node • Variable ordering and value selection heuristics help significantly • Forward checking prevents assignments that gurantee later failure • Constraint propagation (e.g. arc consistency) does additional work to constrain values and detect inconsistencies • The CSP representation allows analysis of problem structure • Tree-structured CSPs can be solved in linear time • Iterative min-conflicts is usually effective in practice