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.
September 28, 2015 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
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
{WA=red,NT=green,Q=red,NSW=green,V=red,SA=blue,T=green} q (WA,NT) in {(red,green),(red,blue),(green,red),(green,blue),(blue,red),
(blue,green)}
September 28, 2015 3 September 28, 2015 4
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}
9/28/15 5 September 28, 2015 6
Varieties of CSPs Varieties of constraints
n Discrete variables n Unary constraints involve a single variable.
q Finite domains of size d ⇒O(dn) complete assignments. q e.g., SA ≠ green
n The satisfiability problem: a Boolean CSP n Binary constraints involve pairs of variables.
q (AvBvC)^(~BvCvD)^(~AvBv~D)… q e.g., SA ≠ WA
q Infinite domains (integers, strings, etc.) n Global constraints involve an arbitrary number of variables.
n e.g., job scheduling where variables are start/end times for each job.
n Preference (soft constraints), e.g., red is better than green; often
n Need a constraint language, e.g., StartJob1 +5 ≤ StartJob2. representable by a cost for each variable assignment; not
n Continuous variables considered here.
q e.g., start/end times for Hubble Telescope observations.
q Linear constraints solvable in poly time by linear programming
methods (dealt with in the field of operations research).
September 28, 2015 7 September 28, 2015 8
2
Constraint graph Example: cryptharithmetic puzzles
n Binary CSP: each constraint relates two variables Hypergraph
n Constraint graph: nodes are variables, edges are
constraints
Variables: F,T,U,W, R,O, C10 , C100 , C1000
Domains: {0,1, 2, 3, 4, 5, 6, 7,8, 9}
Constraints:
alldiff (F,T,U,W, R,O)
O + O = R +10 ∗ C10
September 28, 2015 9 September 28, 2015 10
CSP as a standard search problem Constraint propagation
n Incremental formulation
q Initial State: the empty assignment {}.
q Successor function: Assign value to unassigned variable
provided that there is not conflict.
q Goal test: the current assignment is complete.
n Same formulation for all CSPs !!!
n Solution is found at depth n (n variables). n Is a type of inference
q What search method would you choose? q Enforce local consistency
q Propagate the implications of each constraint
September 28, 2015 11 September 28, 2015 12
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
September 28, 2015 13 September 28, 2015 14
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}
n X → Y is arc-consistent iff
and {Xm, Xj}
for every value x of X there is some allowed y
n Yet SA → WA is consistent under all of the following:
q {(red, green), (red, blue), (green, red), (green, blue), (blue, red)}
n So it doesn’t help
September 28, 2015 15 9/28/15 16
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
September 28, 2015 17 September 28, 2015 18
K-consistency Backtracking search
n Stronger forms of propagation can be defined using the n Observation: the order of assignment doesn’t matter
notion of k-consistency. ⇒ can consider assignment of a single variable at a time.
n A CSP is k-consistent if for any set of k-1 variables and Results in dn leaves.
for any consistent assignment to those variables, a n Backtracking search: DFS for CSPs with single-
consistent value can always be assigned to any kth variable assignments (backtracks when a variable
variable. has no value that can be assigned)
n Not practical! n The basic uninformed algorithm for CSP
September 28, 2015 19 September 28, 2015 20
5
Backtracking search Backtracking example
function BACKTRACKING-SEARCH(csp) returns a solution or failure
return BACKTRACK({} , csp)
function BACKTRACK(assignment, csp) returns a solution or failure
if assignment is complete then return assignment
var ← SELECT-UNASSIGNED-VARIABLE(csp)
for each value in ORDER-DOMAIN-VALUES(var, assignment, csp) do
if value is consistent with assignment then
add {var=value} to assignment
inferences ← INFERENCE(csp, var, value)
if inferences ≠ failure then
add inferences to assignment
result ← BACKTRACK(assignment, csp)
if result ≠ failure then return result
remove {var=value} and inferences from assignment
return failure
September 28, 2015 21 September 28, 2015 22
Backtracking example Backtracking example
September 28, 2015 23 September 28, 2015 24
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?
September 28, 2015 25 September 28, 2015 26
Most constrained variable Degree heuristic
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?
September 28, 2015 27 September 28, 2015 28
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.
September 28, 2015 29 September 28, 2015 30
Forward checking Forward checking
n Assign {WA=red} n Assign {Q=green}
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 red q NT can no longer be green
q SA can no longer be red q NSW can no longer be green
q SA can no longer be green
September 28, 2015 31 September 28, 2015 32
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.
September 28, 2015 33 September 28, 2015 34
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} { , ,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, }
September 28, 2015 35 September 28, 2015 36
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, }
September 28, 2015 37 September 28, 2015 38
Example: 4-Queens Problem Example: 4-Queens Problem
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}
September 28, 2015 39 September 28, 2015 40
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, }
September 28, 2015 41 September 28, 2015 42
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, , , } { , ,3, }
September 28, 2015 43 September 28, 2015 44
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
q Hill-climbing like algorithm with the objective function being the
4
X3 X4 number of violated constraints
{1, , , } { , ,3, } n Works surprisingly well in problem like n-Queens
September 28, 2015 45 September 28, 2015 46
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
September 28, 2015 47 September 28, 2015 48
12
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 Perform a topological sort of the variables
n Suppose n=80, c=20, d=2 n Theorem: if the constraint graph has no loops then CSP can be
q 280 = 4 billion years at 1 million nodes/sec. solved in O(nd2) time
q 4 * 220= .4 second at 1 million nodes/sec n Compare with general CSP, where worst case is O(dn)
September 28, 2015 49 September 28, 2015 50
Tree-structured CSPs Nearly 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
MAKE-ARC-CONSISTENT(PARENT(Xj),Xj)
if it cannot be made consistent then return failure n Can more general constraint graphs be reduced to trees?
for i = 1 to n do n Two approaches:
assignment[Xi] ← any consistent value from Di q Remove certain nodes
if there is no consistent value then return failure q Collapse certain nodes
return assignment
September 28, 2015 51 September 28, 2015 52
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.
September 28, 2015 53 September 28, 2015 54
14