CSP Example: Map Coloring: Constraint Satisfaction Problems (CSPS)
CSP Example: Map Coloring: Constraint Satisfaction Problems (CSPS)
1
Constraint satisfaction problems Varieties of CSPs
n Simple example of a formal representation language n Discrete variables
n CSP benefits Finite domains of size d ⇒O(dn) complete
q
2
Backtracking search Sudoku solving
1 2 3 4 5 6 7 8 9
n Observation: the order of assignment doesn’t matter A
⇒ can consider assignment of a single variable at a time. B
Results in dn leaves (d: number of values per variable). C
n Backtracking search: DFS for CSPs with single- D
variable assignments (backtracks when a variable E
has no value that can be assigned) F
G
n The basic uninformed algorithm for CSP
H
I
http://norvig.com/sudoku.html
3
Arc consistency The Arc Consistency Algorithm
function AC-3(csp) returns false if an inconsistency is found and true otherwise
n X → Y is arc-consistent iff inputs: csp, a binary csp with components {X, D, C}
for every value x of X there is some allowed value y of Y local variables: queue, a queue of arcs initially the arcs in csp
while queue is not empty do
(Xi, Xj) ← REMOVE-FIRST(queue)
n Example: X and Y can take on the values 0…9 with the if REVISE(csp, Xi, Xj) then
if size of Di=0 then return false
constraint: Y=X2. Can use arc consistency to reduce for each Xk in Xi.NEIGHBORS – {Xj} do
the domains of X and Y: add (Xk, Xi) to queue
q X → Y reduce X’s domain to {0,1,2,3} function REVISE(csp, Xi, Xj) returns true iff we revise the domain of Xi
revised ← false
q Y → X reduce Y’s domain to {0,1,4,9}
for each x in Di do
if no value y in Dj allows (x,y) to satisfy the constraints between Xi and Xj
then delete x from Di
revised ← true
return revised
4
Backtracking example Backtracking example
5
Most constraining variable Least constraining value heuristic
n Select the variable that is involved in the largest number of n Guides the choice of which value to assign next.
constraints on other unassigned variables. n Given a variable, choose the least constraining value:
n Also called the degree heuristic because that variable has the q the one that rules out the fewest values in the remaining
largest degree in the constraint graph. variables
n Often used as a tie breaker e.g. in conjunction with MRV. q why?
6
Example: 4-Queens Problem Example: 4-Queens Problem
X1 X2 X1 X2
1 2 3 4
{1,2,3,4} {1,2,3,4} 1 2 3 4
{1,2,3,4} {1,2,3,4}
1 1
2 2
3 3
4 4
X3 X4 X3 X4
{1,2,3,4} {1,2,3,4} {1,2,3,4} {1,2,3,4}
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, ,4} { ,2,3, }
X1 X2 X1 X2
1 2 3 4
{1,2,3,4} { , ,3,4} 1 2 3 4
{ ,2,3,4} {1,2,3,4}
1 1
2 2
3 3
4 4
X3 X4 X3 X4
{ , , , } { ,2,3, } {1,2,3,4} {1,2,3,4}
7
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, ,3, } {1, ,3,4}
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, , , } {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, , , } { , ,3, } {1, , , } { , ,3, }
8
Forward checking Local search for CSP
n Local search methods use a “complete” state representation, i.e.,
all variables assigned.
n To apply to CSPs
q Allow states with unsatisfied constraints
9
Tree-structured CSPs Tree-structured CSPs
Any tree-structured CSP can be solved in time linear in the number of variables.
Function TREE-CSP-SOLVER(csp) returns a solution or failure
inputs: csp, a CSP with components X, D, C
n ← number of variables in X
assignment ← an empty assignment
root ← any variable in X
X ← TOPOLOGICALSORT(X, root)
for j = n down to 2 do
n Any tree-structured CSP can be solved in time linear in the number of MAKE-ARC-CONSISTENT(PARENT(Xj),Xj)
variables.
if it cannot be made consistent then return failure
q Choose a variable as root, order variables from root to leaves such that
for i = 1 to n do
every node’s parent precedes it in the ordering. (label var from X1 to Xn)
assignment[Xi] ← any consistent value from Di
q For j from n down to 2, apply REMOVE-INCONSISTENT-
n Idea: assign values to some variables so that the remaining variables form a
n Can more general constraint graphs be reduced to trees? tree.
n Two approaches: n Assign {SA=x} ← cycle cutset
q Remove certain nodes q Remove any values from the other variables that are inconsistent.
q The selected value for SA could be the wrong: have to try all of them
q Collapse certain nodes
10
Interim class summary
n We have been studying ways for agents to solve problems.
n Search
q Uninformed search
n Easy solution for simple problems
n Basis for more sophisticated solutions
q Informed search
n Information = problem solving power
q Adversarial search
n αβ-search for play against optimal opponent
n Early cut-off when necessary
q Constraint satisfaction problems
n What’s next?
q Logic
q Machine learning
10/13/14 61
11