Constraint Satisfaction CPSC 386 Artificial Intelligence Ellen Walker Hiram College Constraint Satisfaction Problem • Variables: X1, X2, … XN • Constraints: C1, C2, … CN – Often mathematical e.g. X1+X2=0 • Domains: D1, D2, … DN (possible values for variables) • A solution assigns values to variable (X1=V1, X2=V2 etc.) so all constraints are satisfied • A partial solution (or consistent assignment) doesn’t violate any constraints, but may leave some variables unassigned • If there are multiple solutions, choose the one that maximizes an objective function (e.g. contiinuous problems) Domain Types • Finite domain (fixed number of variables, possible assignments) – Depth is #variables, breadth is #assignments/variable • Discrete variables with infinite domains (e.g. integers, strings) – Cannot search by enumeration, need a language to represent constraints • Continuous variables (e.g. real numbers) – Linear programming and other continuous optimization techniques Example Problems • Map Coloring (see text) • Course / room scheduling – Each course has a room – Each room has no more than one course at a given time – Additional constraints (e.g. size, projector) • Cryptarithmetic (see text) • Line labeling • Sudoku, Kakuro, KenKen, etc. A Cryptarithmetic Problem • SEND + MORE = MONEY • Constraints – – – – S≠E≠N≠D≠M≠O≠R≠Y (rules) M=1 (It is a carry out of the high-order digit) D+E = Y or D+E=10+Y N+R+ (Y-(D+E))/10 = E (or 10+E) (etc) • Solution – One technique: trial and error (with backtracking…) – Set M=1, that constrains S to be 8 or 9 and 0 can’t be 1, so it must be 0… Consistency • Node Consistency – Every value in the variable’s domain meets all unary constraints • Arc Consistency – Every value in the variable’s domain meets all binary constraints (with at least one value in the relevant neighbor) • If not consistent, remove value(s) from domain(s) until it is. – Book calls this “applying {Node, Arc} consistency Arc Consistency Algorithm AC-3 • Initialize a queue (really a set) of all arcs (pairs of constrained nodes) in the problem • While the queue isn’t empty: – – – – Remove an arc from the queue Remove values that do not satisfy the constraint If domain is empty return false If domain changed, then add pairs of this node and all neighbors (except the one just checked) to the queue • Return true (domains reduced as much as possible) Arc Consistency Isn’t Enough • Consider 2-coloring problem – Color Texas Flag with 3 colors, no two that touch and are the same. – Arc consistency does not reveal the problem – Need Path Consistency: for every pair of consistent elements, there is a third element so that first and third are consistent, and second and third are also consistent. (I.e. there is a path of consistency across two constraint pairs) Line Labeling 1 Label each line in the picture (left) 2 5 3 4 > – > + > – + < Both ends of the segment must have the same label! + – + > Constraints based on vertices (“L” and “arrow” vertices shown) < + < < + – > > – Constraint Problem as Search • Formulation – Initial state: no assignments – Successor function: assign a value to an unassigned variable without violating constraints – Goal test: all variables are assigned – Path cost: constant for each step • Order of assignments doesn’t matter • Known solution depth (# of variables) • Appropriate search is depth-limited DFS Backtracking Search for CSP’s Pick an unassigned variable For each possible value that meets constraints Assign the value If all variables are assigned, return the solution Else Recursively solve the rest of the problem If the recursion succeeded, return the solution Return failure Searching More Efficiently • Choose the most constrained variable (fewest legal values) to assign next – # of iterations of loop is minimized – “MRV” Minimum Remaining Values heuristic • In case of tie, choose the variable that is involved in the most constraints (highest degree) • Once a variable is chosen, choose the least constraining value, i.e. rule out the fewest future choices Constraint Propagation • Forward checking – Whenever a variable is assigned, delete inconsistent values from other domains – Works well with MRV heuristic • Maintaining Arc Consistency (MAC) – Consider results of constraints between variables affected by forward checking – Needs to be recursively applied, because removal could make a future arc inconsistent; after each change use AC3 starting with only the neighbors of changed nodes. – Tradeoff between consistency checking (takes time) and overall search time, but polynomial time is likely impossible Chronological vs. Intelligent Backtracking • Chronological Backtracking – Undoes most recent decision • Intelligent Backtracking – Undoes the decision that caused the problem in the first place! – Set of variables that caused the failure is “conflict set” – Backjumping: undo most recent variable in conflict set (redundant with forward-checking; see text) Local Search for CSP • Hill Climbing with min conflicts as heuristic • Every state is a complete assignment, count how many variables are conflicted (violate constraints) • At each step: choose a conflicted variable at random, change its assignment to the value that causes the minimum number of conflicts • Surprisingly effective for many CSP’s e.g. N-queens problem! • Does depend (like all hill climbing) on initial state though.