Constraint satisfaction problems Lirong Xia Spring, 2017
Project 1 Ø You can use Windows Ø Read the instruction carefully, make sure you understand the goal search for YOUR CODE HERE Ø Ask and answer questions on Piazza we may give bonus score to insightful answers Please be careful about spoilers Ø State (in the search graph) vs. node (in the search tree) add node in the search tree to fringe add state to the visited sets 2
Today s schedule ØConstraint satisfaction problems ØAlgorithms backtracking search filtering forward checking constraint propagation 3
Constraint Satisfaction Problems Ø Standard search problems: State is a black box : arbitrary data structure Goal test: any function over states Successor function can be anything Ø Constraint satisfaction problems (CSPs): A special subset of search problems State is defined by variables X i with values from a domain D (sometimes D depends on i ) Goal test is a set of constraints specifying allowable combinations of values for subsets of variables Ø Allows useful general-purpose algorithms with more power than standard search algorithms 4
Ø Variables: WA, NT, Q, NSW, V, SA, T Ø Domains: Example: Map-Coloring D = { red, green, blue} Ø Constraints: adjacent regions must have different colors WA NT { } ( WA, NT ) ( red, green ), ( red, blue ), ( green, red ),... Ø Solutions are assignments satisfying all constraints, e.g.: WA = red, NT = green, Q = red, NSW = green, V = red, SA = blue, T = green 5
Example: N-Queens ØFormulation 1: Variables: Domains: X ij { 0,1} Constraints i, j,k ( X ij, X ) ik {( 0, 0), ( 0, 1), ( 1, 0) } i, j,k ( X ij, X ) kj {( 0, 0), ( 0, 1), ( 1, 0) } i, j,k ( X ij, X ) i+k, j+k ( 0, 0), ( 0, 1), 1, 0 i, j,k X ij, X i+k, j k ( ), ( 0, 1), 1, 0 i, j X ij = { ( )} ( ) { 0, 0 ( )} N 6
Example: N-Queens ØFormulation 2: Variables: Domains: Constraints Q k { 1, 2,3,...N } Q 1 Q 2 Q 3 Q 4 Implicit: -or- Explicit: ( ) i, j non-threatening Q, Q { } ( Q 1,Q ) 2 ( 1, 3), ( 1, 4),... i j 7
Constraint Graphs: Goal test Ø Binary CSP: each constraint relates (at most) two variables Ø Binary constraint graph: nodes are variables, arcs show constraints Ø General-purpose CSP algorithms use the graph structure to speed up search. E.g., Tasmania is an independent subproblem! 8
Example: Cryptarithmetic Ø Variables (circles): FTUWROX1 X2 X3 Ø Domains: T W O + T W O F O U R { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9} Ø Constraints (boxes): alldiff ( F, T, U, W, R, O) O +O = R +10 X 1 9
Example: Sudoku Ø Variables: Each (open) square Ø Domains: { 1, 2,, 9} Ø Constraints: 9-way alldiff for each column 9-way alldiff for each row 9-way alldiff for each region 10
ØDiscrete Variables: Finite domains Varieties of CSPs Size d means O(d n ) complete assignments E.g., Boolean CSPs, including Boolean satisfiability (NPcomplete) Infinite domains (integers, strings, etc.) Linear constraints solvable ØContinuous variables: Linear constraints solvable in polynomial time by LP methods 11
Varieties of Constraints Ø Variables of Constraints Unary constraints involve a single variable (equiv. to shrinking domains): SA Binary constraints involve pairs of variables: SA WA green 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 Gives constrained optimization problems 12
Standard Search Formulation ØStandard search formulation of CSPs (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 Goal test: the current assignment is complete and satisfies all constraint 13
Search Methods Ø What would BFS do? Ø What would DFS do? Ø Is BFS better or DFS better? Ø Can we use A*? 14
Early detection of non-goal 15
Backtracking Search Ø Idea 1: only consider a single variable at each point Variable assignments are commutative, so fix ordering 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 step Ø Idea 2: only allow legal assignments at each point Incremental goal test Ø DFS for CSPs with these two improvements is called backtracking search Ø Backtracking search is the basic uninformed algorithm for CSPs Ø Can solve n-queens for n 25 16
Backtracking Example 17
Improving Backtracking ØOrdering: Which variable should be assigned next? In what order should its values be tried? ØFiltering: Can we detect inevitable failure early? 18
Minimum Remaining Values ØMinimum remaining values (MRV): Choose the variable with the fewest legal values ØWhy min rather than max? ØAlso called most constrained variable Ø Fail-fast ordering 19
Example For every variable, keep track of which values are still possible Q X X X Q X X Q Q X X X Q X X Q Q X X X Q X X Q X X Q Q X X X Q X X Q X X X X Q Q X X Q Q X X X Q X X Q only one possibility for last column; might as well fill in now only one left for other two columns done! (no real branching needed!)
Choosing a value Ø Given a choice of variable: Choose the least constraining value The one that rules out the fewest values in the remaining variables May take some computation to determine this! Ø Why least rather than most? value 1 for SA value 0 for SA 21
Filtering: Forward Checking Ø Idea: keep track of remaining values for unassigned variables (using immediate constraints) Ø Idea: terminate when any variable has no legal values 22
Filtering: Constraint Propagating Ø Forward checking propagates information from assigned to unassigned variables, but doesn t provide early detection for all failures: step 1 step 2 step 1 step 2 Ø NT and SA cannot both be blue! Ø Why didn t we detect this yet? 23
Consistency of An Arc X Y Ø An arc is consistent iff for every x in the tail there is some y in the head which could be assigned without violating a constraint Delete from tail! Ø Forward checking = Enforcing consistency of each arc pointing to the new assignment 24
Arc Consistency of a CSP Ø A simple form of propagation makes sure all arcs are consistent: Delete from tail! X X X Ø If V loses a value, neighbors of V need to be rechecked! Ø Arc consistency detects failure earlier than forward checking Ø Can be run as a preprocessor or after each assignment Ø Might be time-consuming 25