An Introduction to Artificial Intelligence Lecture 5: Constraint Satisfaction Problems Ramin Halavati (halavati@ce.sharif.edu) In which we see how treating states as more than just black boxes leads to the invention of a range of powerful new search methods and a deeper understanding of problem structure and complexity. Overview • States/Goal(s) – Black Box vs. Structured – General Purpose Heuristics What is Constraint Satisfaction? • A set of variables: X1,X2,…,Xn. • A set of constraints: C1,C2,…,Cm. • Each variable, a domain: D1,D2,…,Dn. • State: An assignment of values to Xis. • Consistent: Obeying all constraints. • Complete: All variables having values. • Solution: A complete, consistent state. • Objective Function: Extra! Real World Samples of CSP • Assignment problems – e.g., who teaches what class – • Timetabling problems • – e.g., which class is offered when and where? – • Transportation scheduling • • Factory scheduling CSP, Sample • To assign one of {red, green, blue} to each of 7 variables: WA, BT, SA, Q, NSW, V, T. • Constraints: – WA ≠ NT,WA ≠ SA , NT ≠ Q, SA ≠ Q , … • Solution: – {WA = red , NT = green, Q = red , NSW = green, … Formulation • Initial State: { } • Successor Function: Value assignment to one variable with no conflict. • Goal Test: Consistent, Complete, assignment. • Path Cost: 1 for each step. Constraint Types • Constraint Types: – Unary / Binary / More… • Auxiliary Variables • Variables: F T U W, R O X1 X2 X3 • Constraints: All Different (F, T, U, W, R, O) • – – – – – – O + O = R + 10 · X1 X1 + W + W = U + 10 · X2 X2 + T + T = O + 10 · X3 X3 = F, T ≠ 0, F ≠ 0 TWO +TWO FOUR Constraint Graph Recursive Back Track Algorithm • Recursive Back Track( assignment , CSP ) If assignment is complete, return assignment. Var Select an unassigned variable. For each Value in Domain (Var), if Consistent( assignment + Var Value ) Then result RBT (assignment + Var Value ) if ( result ≠ failure ) return result. Return failure. Major Questions… 1. Which variable to assign. 2. What to assign. 3. How to prevent further repetitions of a failure. Minimum Remaining Values (MRV) • To choose one which is most likely to fail • Immediate recognition of failure Benchmark Problem Backtracking Backtracking + MRV USA 4-Coloring > 1000 K > 1000 K N-Queens (2-50) > 40000 K 13500 K 3859 K 1K Zebra Most Constraining Variable (MCS) • To choose one who interferes the others most! • Major reduction in 3 3 branching factor. 2 5 3 2 0 Least Constraining Value (LCS) • To choose a value which puts minimum constraint on other variables. • To leave maximum flexibility. Forward Checking • Keep track of remaining legal values for unassigned variables • Terminate search when any variable has no legal values Benchmark Problem BT + Forward FC + MRV Checking MRV >1M >1M 2K 60 N-Queens > 40 M 13.5 K >40 M 817 K Zebra 3859 K 1K 35 K 500 USA BT Constraint Propagation • How to find fast that implications of one variable on other variables. Arc Consistency • AB is consistent if for each remaining value in domain of A, there may be a consistent value in domain of B. – Consistent: • SANSW, NSWV,… – Not C.: • NSWSA, NTSA,… Arc Consistency Checking Algorithm (AC-3!) • AC3( csp ) Fill queue with all available arcs of csp. While queue not empty, (Xi,Xj) RemoveHead( queue ) If RemoveInconsistentValues(Xi,Xj) Then for each Xk in Neighbor (Xi) do Add (Xk,Xi) to queue. • Remove Inconsistent Values (Xi,Xj) removed false. For each x in Domain (Xi) if no allowed value in Domain (Xj) after assignment of x to Xi, delete x from Domain (Xi); removed true. Return removed. Special Constraints… • All Different – Sort remaining variables based on their number of choices… • Resource Constraints – Checking the sums, … • Flight271 [0,165] , Flight272 [0,385] • Flight271+Flight272 = 420 Flight271 [35,165] , Flight272 [255,385] • Bounds Propagation Intelligent Backtracking 1. Q Red 1 2. NSW Green 3. V Blue 4. T Red 2 ? 5. SA ? • Chronological Backtracking 3 4 Back Jumping 1. Q Red 2. NSW Green 1 3. V Blue 4. T Red 5. SA ? • 2 ? 3 Conflict Set: {Q, V, NSW} 4 NOTE: Back Jumping doesn’t help Forward Checking. Local Search for Constraint Satisfaction Problems • Min Conflict Algorithm – Create a random complete state. – For a fixed number of times • If current state is consistent, return. • Choose a random variable v, and change assignment of v to a value that causes minimum conflict. • Scheduling Hubble’s weekly observation: – From 3 weeks to 10 minutes! • Extra advantage: Online Updating Benchmark Problem BT + Forward FC + Min MRV Checking MRV Conflicts >1M >1M 2K 60 64 N-Queens > 40 M 13.5 K >40 M 817 K 4K Zebra 3859 K 1K 35 K 500 2K USA BT Heuristics based on Structure • Sub Problems – Finding Connected Components • Constraint Trees A E B C D F Heuristics based on Structure A • E B Constraint Trees: D C F – Order nodes. A – From last to first, remove all 1 values from domain of parent 2 B which may violate arc-consistency. – From first to last, assign a remaining value. 3 C 4 D 5 F 6 E How to convert Constraint Graph to Constraint Tree • Cut-Set Conditioning – To select a set of nodes that subtracting them results in a tree. – To check the rest for all valid assignments of this set. How to convert Constraint Graph to Constraint Tree • Tree Decomposition Tree Decomposition • Include all variables • Each constraint must be in at least one sub problem. • If a variable is in two sub-probs, it must be in all subprobs along the path. Summery…