You are on page 1of 11

CSP example: map coloring

Constraint Satisfaction Problems (CSPs)

Russell and Norvig Chapter 6


Given a map of Australia, color it using three
colors such that no neighboring territories have
the same color.

October 13, 2014 2

CSP example: map coloring Constraint satisfaction problems


n  A CSP is composed of:
q  A set of variables X1,X2,…,Xn with domains (possible values)
D1,D2,…,Dn
q  A set of constraints C1,C2, …,Cm
q  Each constraint Ci limits the values that a subset of variables can
take, e.g., V1 ≠ V2

October 13, 2014 3 October 13, 2014 4

Constraint satisfaction problems Constraint satisfaction problems


n  A CSP is composed of: n  A state is defined by an assignment of values to some or
q  A set of variables X1,X2,…,Xn with domains (possible values) all variables.
D1,D2,…,Dn n  Consistent assignment: assignment that does not violate
A set of constraints C1,C2, …,Cm
q 
the constraints.
q  Each constraint Ci limits the values that a subset of variables can
take, e.g., V1 ≠ V2 n  Complete assignment: every variable is mentioned.
n  Goal: a complete, legal assignment.
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.

q  E.g. WA ≠ NT (if the language allows this) or

q  (WA,NT) in {(red,green),(red,blue),(green,red),(green,blue),(blue,red), {WA=red,NT=green,Q=red,NSW=green,V=red,SA=blue,T=green}


(blue,green)}

October 13, 2014 5 10/13/14 6

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 

q  Standard representation language assignments.


q  Generic goal and successor functions n  The satisfiability problem: a Boolean CSP
q  Useful general-purpose algorithms with more power than q  Infinite domains (integers, strings, etc.)
standard search algorithms, including generic heuristics n  Continuous variables
n  Applications:
q  Linear constraints solvable in poly time by linear
q  Time table problems (exam/teaching schedules)
programming methods (dealt with in the field of
q  Assignment problems (who teaches what)
operations research).
n  Our focus: discrete variables and finite domains

October 13, 2014 7 October 13, 2014 8

Varieties of constraints Constraint graph


n  Unary constraints involve a single variable.
n  Binary CSP: each constraint relates two variables
q  e.g. SA ≠ green
n  Binary constraints involve pairs of variables. n  Constraint graph: nodes are variables, edges are
q  e.g. SA ≠ WA
constraints
n  Global constraints involve an arbitrary number of variables.
n  Preference (soft constraints) e.g. red is better than green often
representable by a cost for each variable assignment; not
considered here.

October 13, 2014 9 October 13, 2014 10

Example: cryptarithmetic puzzles CSP as a standard search problem


n  Incremental formulation
q  Initial State: the empty assignment {}.
q  Successor: Assign value to unassigned variable provided
there is no conflict.
q  Goal test: the current assignment is complete.
n  Same formulation for all CSPs !!!
The constraints are represented
n  Solution is found at depth n (n variables).
by a hypergraph
q  What search method would you choose?

October 13, 2014 11 October 13, 2014 12

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

October 13, 2014 13 10/13/14 14

Sudoku solving Sudoku solving


1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 A
Constraints: A B
B C
Alldiff(A1,A2,A3,A4,A5,A6,A7,A8,A9) C D
… D E
Alldiff(A1,B1,C1,D1,E1,F1,G1,H1,I1)
E F

F G
Alldiff(A1,A2,A3,B1,B2,B3,C1,C2,C3)
… G H
H I
Can be translated into constraints I
between pairs of variables. Let’s see if we can figure the value of the center grid point.

Images from wikipedia and http://www.instructables.com/id/Solve-Sudoku-Without-even-thinking!/



10/13/14 15 10/13/14 16

Solving Sudoku Constraint propagation


“In this essay I tackle the problem of solving every
Sudoku puzzle. It turns out to be quite easy (about
one page of code for the main idea and two pages
for embellishments) using two ideas: constraint
propagation and search.”
Peter Norvig
n  Enforce local consistency
n  Propagate the implications of each constraint

http://norvig.com/sudoku.html

10/13/14 17 October 13, 2014 18

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

October 13, 2014 19 October 13, 2014 20

Arc consistency limitations Path Consistency


n  Looks at triples of variables
q  The set {Xi, Xj} is path-consistent with respect to Xm if
for every assignment consistent with the constraints of
Xi, Xj, there is an assignment to Xm that satisfies the
constraints on {Xi, Xm} and {Xm, Xj}
n  The PC-2 algorithm achieves path consistency
n  X → Y is arc-consistent iff
for every value x of X there is some allowed y of Y
n  Consider mapping Australia with two colors. Each arc is consistent,
and yet there is no solution to the CSP.
n  So it doesn’t help

October 13, 2014 21 10/13/14 22

K-consistency Backtracking example


n  Stronger forms of propagation can be defined using the
notion of k-consistency.
n  A CSP is k-consistent if for any set of k-1 variables and
for any consistent assignment to those variables, a
consistent value can always be assigned to any k-th
variable.
n  Not practical!

October 13, 2014 23 October 13, 2014 24

4

Backtracking example Backtracking example

October 13, 2014 25 October 13, 2014 26

Backtracking example Improving backtracking efficiency


n  General-purpose methods/heuristics 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?

October 13, 2014 27 October 13, 2014 28

Backtracking search Most constrained variable


function BACKTRACKING-SEARCH(csp) return a solution or failure
return RECURSIVE-BACKTRACKING({} , csp)

function RECURSIVE-BACKTRACKING(assignment, csp) return a solution or


failure
if assignment is complete then return assignment
var ← SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp],assignment,csp)
for each value in ORDER-DOMAIN-VALUES(var, assignment, csp) do
if value is consistent with assignment according to
CONSTRAINTS[csp] then var ← SELECT-UNASSIGNED-VARIABLE(csp)
add {var=value} to assignment
result ← RECURSIVE-BACTRACKING(assignment, csp) Choose the variable with the fewest legal values
if result ≠ failure then return result
(most constrained variable)
remove {var=value} from assignment
return failure
a.k.a. minimum remaining values (MRV) or “fail first” heuristic
q  What is the intuition behind this choice?

October 13, 2014 29 October 13, 2014 30

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?

October 13, 2014 31 October 13, 2014 32

Forward checking Forward checking

n  Can we detect inevitable failure early? n  Assign {WA=red}


q  And avoid it later? n  Effects on other variables connected by constraints with WA
q  NT can no longer be red
n  Forward checking: keep track of remaining legal values for
q  SA can no longer be red
unassigned variables.
n  Terminate search direction when a variable has no legal values.

October 13, 2014 33 October 13, 2014 34

Forward checking Forward checking

n  Assign {Q=green} n  If V is assigned blue


n  Effects on other variables connected by constraints with WA n  Effects on other variables connected by constraints with WA
q  NT can no longer be green q  SA is empty
q  NSW can no longer be green q  NSW can no longer be blue
q  SA can no longer be green n  FC has detected that partial assignment is inconsistent with the constraints and
backtracking can occur.

October 13, 2014 35 October 13, 2014 36

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}

October 13, 2014 37 October 13, 2014 38

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, ,4} { ,2,3, }

October 13, 2014 39 October 13, 2014 40

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
{ ,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}

October 13, 2014 41 October 13, 2014 42

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}

October 13, 2014 43 October 13, 2014 44

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, , , } {1, ,3, } {1, , , } {1, ,3, }

October 13, 2014 45 October 13, 2014 46

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, }

October 13, 2014 47 October 13, 2014 48

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

q  reassign variable values


n  Solving CSPs with combination of heuristics plus forward checking n  Select a variable: random conflicted variable
is more efficient than either approach alone.
n  Select a value: min-conflicts heuristic
n  FC does not provide early detection of all failures.
q  Value that violates the fewest constraints
q  Once WA=red and Q=green: NT and SA cannot both be blue!
q  Hill-climbing like algorithm with the objective function being the
n  MAC (maintaining arc consistency): calls AC-3 after assigning a
number of violated constraints
value (but only deals with the neighbors of a node that has been
assigned a value). n  Works surprisingly well in problems like n-Queens

October 13, 2014 49 October 13, 2014 50

Min-Conflicts Problem structure


function MIN-CONFLICTS(csp, max_steps) returns a solution or failure
inputs: csp, a constraint satisfaction problem
max_steps, the number of steps allowed before giving up
current ← an initial complete assignment for csp
for i = 1 to max_steps do
if current is a solution for csp then return current
var← a randomly chosen conflicted variable from csp.VARIABLES
value← the value v for var that minimizes CONFLICTS(var, v, current, csp) n  How can the problem structure help to find a solution
set var=value in current quickly?
return failure
n  Subproblem identification is important:
q  Coloring Tasmania and mainland are independent subproblems
q  Identifiable as connected components of constraint graph.
n  Improves performance

October 13, 2014 51 October 13, 2014 52

Problem structure Tree-structured CSPs

n  Suppose each problem has c variables out of a total of n.


n  Worst case solution cost is O(n/c dc) instead of O(dn) n  Theorem: if the constraint graph has no loops then CSP can be
n  Suppose n=80, c=20, d=2 solved in O(nd2) time
q  280 = 4 billion years at 1 million nodes/sec. n  Compare with general CSP, where worst case is O(dn)
q  4 * 220= .4 second at 1 million nodes/sec

October 13, 2014 53 October 13, 2014 54

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-

VALUES(Parent(Xj), Xj) if there is no consistent value then return failure


q  For j from 1 to n assign Xj consistently with Parent(Xj )
return assignment

October 13, 2014 55 October 13, 2014 56

Nearly tree-structured CSPs Nearly tree-structured CSPs

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

October 13, 2014 57 October 13, 2014 58

Nearly tree-structured CSPs Summary


n  CSPs are a special kind of problem: states defined by values of a
fixed set of variables, goal test defined by constraints on variable
values
n  Backtracking=depth-first search with one variable assigned per node
n  Variable ordering and value selection heuristics help significantly
n  Forward checking prevents assignments that lead to failure.
n  Constraint propagation does additional work to constrain values and
detect inconsistencies.
n  This approach is effective if cycle cutset is small. n  Structure of CSP affects its complexity. Tree structured CSPs can
be solved in linear time.
n  Finding the smallest cycle cutset is NP-hard
q  Approximation algorithms exist

n  This approach is called cutset conditioning.

October 13, 2014 59 October 13, 2014 60

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

You might also like