backtracking
backtracking
In our example:
n Variables: WA, NT, Q, NSW, V, SA, T
n Domains: Di={red,green,blue}
n Constraints: adjacent regions must have different colors.
n Solutions are assignments satisfying all constraints, e.g.: q E.g., WA ≠ NT (if the language allows this) or
1
Constraint satisfaction problems Constraint satisfaction problems
n A state is defined by an assignment of values to some or n Simple example of a factored representation: splits each state
all variables. into a fixed set of variables, each of which has a value
n Consistent (or legal) assignment: assignment that does n CSP benefits
not violate the constraints. q Standard representation language
q Generic goal and successor functions
n Complete assignment: every variable is mentioned.
q Useful general-purpose algorithms with more power than
n Goal: a complete, consistent assignment. standard search algorithms, including generic heuristics
n Applications:
q Time table problems (exam/teaching schedules)
q Assignment problems (who teaches what)
{WA=red,NT=green,Q=red,NSW=green,V=red,SA=blue,T=green}
2
Constraint graph Example: cryptharithmetic puzzles
n Binary CSP: each constraint relates two variables Hypergraph
n Constraint graph: nodes are variables, edges are
constraints
3
Arc consistency Arc Consistency Algorithm
function AC-3(csp) returns false if an inconsistency is found and true otherwise
inputs: csp, a binary csp with components {X, D, C}
local variables: queue, a queue of arcs initially the arcs in csp
while queue is not empty do
n X → Y is arc-consistent iff (Xi, Xj) ← REMOVE-FIRST(queue)
for every value x of X there is some allowed y if REVISE(csp, Xi, Xj) then
if size of Di=0 then return false
for each Xk in Xi.NEIGHBORS – {Xj} do
n Constraint: Y=X2 or ((X,Y), {(0,0), (1,1), (2,4), (3,9)} add (Xi, Xj) to queue
q X → Y reduce X’s domain to {0,1,2,3} return true
function REVISE(csp, Xi, Xj) returns true iff we revise the domain of Xi
q Y → X reduce Y’s domain to {0,1,4,9}
revised ← false
for each x in Di do
if no value y in Di allows (x,y) to satisfy the constraints between Xi and Xj
then delete x from Di
revised ← true
return revised
4
Path consistency Path consistency
n If SA=blue and NSW=red is a consistent assignment wrt Q, then n But need to RECHECK neighbors !!
SA → Q → NSW is consistent. qRemove red and blue from V to ensure path-consistency for
n Arc can be made consistent by removing blue from NSW SA → V→ NSW
5
Backtracking search Backtracking example
function BACKTRACKING-SEARCH(csp) returns a solution or failure
return BACKTRACK({} , csp)
6
Backtracking example Improving backtracking efficiency
n General-purpose methods can give huge
gains in speed:
q Which variable should be assigned next?
q In what order should its values be tried?
q Can we detect inevitable failure early?
var ← SELECT-UNASSIGNED-VARIABLE(csp) n Select the variable that is involved in the largest number of
constraints on other unassigned variables.
Choose the variable with the fewest legal values n Often used as a tie breaker, e.g., in conjunction with MRV.
(most constrained variable)
a.k.a. minimum remaining values (MRV) or “fail first” heuristic
q What is the intuition behind this choice?
7
Least constraining value heuristic Forward checking
n Guides the choice of which value to assign next. n Can we detect inevitable failure early?
n Given a variable, choose the least constraining value: q And avoid it later?
q the one that rules out the fewest values in the remaining n Forward checking: keep track of remaining legal values for
variables unassigned variables.
q why? n Terminate search direction when a variable has no legal values.
8
Forward checking Example: 4-Queens Problem
X1 X2
1 2 3 4
{1,2,3,4} {1,2,3,4}
1
2
If V is assigned blue
n
3
n Effects on other variables connected by constraints with WA
q SA is empty 4
q NSW can no longer be blue X3 X4
n FC has detected that partial assignment is inconsistent with the constraints and {1,2,3,4} {1,2,3,4}
backtracking can occur.
X1 X2 X1 X2
1 2 3 4
{1,2,3,4} {1,2,3,4} 1 2 3 4
{1,2,3,4} { , ,3,4}
1 1
2 2
3 3
4 4
X3 X4 X3 X4
{1,2,3,4} {1,2,3,4} { ,2, ,4} { ,2,3, }
9
Example: 4-Queens Problem Example: 4-Queens Problem
X1 X2 X1 X2
1 2 3 4
{1,2,3,4} { , ,3,4} 1 2 3 4
{1,2,3,4} { , ,3,4}
1 1
2 2
3 3
4 4
X3 X4 X3 X4
{ ,2, ,4} { ,2,3, } { , , , } { ,2,3, }
X1 X2 X1 X2
1 2 3 4
{ ,2,3,4} {1,2,3,4} 1 2 3 4
{ ,2,3,4} { , , ,4}
1 1
2 2
3 3
4 4
X3 X4 X3 X4
{1,2,3,4} {1,2,3,4} {1, ,3, } {1, ,3,4}
10
Example: 4-Queens Problem Example: 4-Queens Problem
X1 X2 X1 X2
1 2 3 4
{ ,2,3,4} { , , ,4} 1 2 3 4
{ ,2,3,4} { , , ,4}
1 1
2 2
3 3
4 4
X3 X4 X3 X4
{1, ,3, } {1, ,3,4} {1, , , } {1, ,3, }
X1 X2 X1 X2
1 2 3 4
{ ,2,3,4} { , , ,4} 1 2 3 4
{ ,2,3,4} { , , ,4}
1 1
2 2
3 3
4 4
X3 X4 X3 X4
{1, , , } {1, ,3, } {1, , , } { , ,3, }
11
Example: 4-Queens Problem Local search for CSP
n Local search methods use a “complete” state representation, i.e.,
all variables assigned.
X1 X2 n To apply to CSPs
q Allow states with unsatisfied constraints
1 2 3 4
{ ,2,3,4} { , , ,4}
q operators reassign variable values
1
n Select a variable: random conflicted variable
2 n Select a value: min-conflicts heuristic
3 q Value that violates the fewest constraints
12
Problem structure Tree-structured CSPs
13
Nearly tree-structured CSPs Nearly tree-structured CSPs
n Idea: assign values to some variables so that the remaining variables form a n This approach is effective if cycle cutset is small.
tree.
n Assign {SA=x} ← cycle cutset
n Finding the smallest cycle cutset is NP-hard
q Remove any values from the other variables that are inconsistent.
q Approximation algorithms exist
q The selected value for SA could be the wrong: have to try all of them n This approach is called cutset conditioning.
14