Background — In previous searches, the state was a black box accessed only by successor functions and goal tests. In CSPs, we open the box.
Constraint Satisfaction Problem: States and goal test conform to a standard, structured representation.11. Modeling a problem as a CSP means you can use generic, highly-optimized constraint solvers rather than writing custom search algorithms. A CSP consists of 3 components:
Variables: WA, NT, Q, NSW, V, SA, T (Regions of Australia)
Domains: D_i = \{red, green, blue\}
Constraints: Adjacent regions must have different colors (e.g., WA \neq NT).
TODO future me, draw this shit. im tired
A form of DFS specifically for CSPs.22. Backtracking is the fundamental engine behind logic puzzles like Sudoku. We assign one variable at a time and backtrack immediately when a constraint is violated.
function BACKTRACKING-SEARCH(csp) returns a solution or failurereturn BACKTRACK({}, csp)function BACKTRACK(assignment, csp) returns a solution or failureif assignment is complete then return assignmentvar \leftarrow SELECT-UNASSIGNED-VARIABLE(csp)for each value in ORDER-DOMAIN-VALUES(var, assignment, csp) doif value is consistent with assignment thenadd {var = value} to assignmentinferences \leftarrow INFERENCE(csp, var, value)if inferences \ne failure thenadd inferences to assignmentresult \leftarrow BACKTRACK(assignment, csp)if result \ne failure then return resultremove {var = value} and inferences from assignmentreturn failure
Why? — Because standard DFS would blindly assign variables until the end and THEN fail the goal test. CSP backtracking fails early (the moment a constraint is violated).
We can speed up backtracking immensely by making smart choices in SELECT-UNASSIGNED-VARIABLE and ORDER-DOMAIN-VALUES.
Forward Checking: When variable X is assigned, look at each unassigned variable Y connected to X. Delete from Y’s domain any value inconsistent with X.
function AC-3(csp) returns csp, possibly with reduced domains// Input: csp, a binary CSP with variables {X1, X2, ..., Xn}// Output: the CSP, possibly with reduced domainsqueue \leftarrow all arcs in cspwhile queue is not empty do(Xi, Xj) \leftarrow REMOVE-FIRST(queue)if REVISE(csp, Xi, Xj) thenif size of Di = 0 then return falsefor each Xk in NEIGHBORS(Xi) \text{$-$} {Xj} doadd (Xk, Xi) to queuereturn truefunction REVISE(csp, Xi, Xj) returns true iff we revise the domain of Xirevised \leftarrow falsefor each x in Di doif no value y in Dj satisfies constraint between Xi and Xj thendelete x from Direvised \leftarrow truereturn revised
Analysis — Time complexity is O(n^2 d^3) where n is number of variables and d is domain size.
Local search works well on CSPs by starting with a complete (but likely inconsistent) assignment and iteratively reassigning variable values to reduce conflicts.
function MIN-CONFLICTS(csp, max_steps) returns solution or failure// Input: csp, a constraint satisfaction problem; max_steps, step limit// Output: a solution, or failurecurrent \leftarrow an initial complete assignment for cspfor i = 1 to max_steps doif current is a solution for csp then return currentvar \leftarrow a randomly chosen conflicted variable from cspvalue \leftarrow the value v for var that minimizes CONFLICTS(var, v, current, csp)set var = value in currentreturn failure
Note — Min-Conflicts is remarkably effective. Given a random initial state, it can solve n-queens in almost constant time for arbitrary n (e.g., n = 10{,}000{,}000). The key heuristic is min-conflicts: choose the value that violates the fewest constraints.
Modeling a problem as a CSP means you can use generic, highly-optimized constraint solvers rather than writing custom search algorithms.↩︎
Backtracking is the fundamental engine behind logic puzzles like Sudoku.↩︎
Also known as the “most constrained variable” or “fail-first” heuristic. If a variable is going to fail, we want to know as soon as possible to prune the tree early.↩︎
AC-3 is an algorithm that recursively propagates constraints until the entire network is “arc consistent”, often solving simple CSPs completely before search even begins.↩︎