Solving Constraint Satisfaction Problems (CSPS) Using Search
Solving Constraint Satisfaction Problems (CSPS) Using Search
Solving Constraint Satisfaction Problems (CSPS) Using Search
Alan Mackworth
Textbook § 4.3-4.4
Lecture Overview
• CSPs: Motivation
• Solving CSPs
- Generate & Test
- Graph search
2
Course Overview Course Module
Representation
Environment Reasoning
Deterministic Stochastic Technique
Problem Type
Arc
Constraint Consistency
Satisfaction Variables + Search
Static
Constraints
Bayesian
Logics Networks
Logic Uncertainty
Search Variable
Elimination
Sequential Decision
STRIPS Networks
Planning Variable Decision
Search Elimination Theory
Now focus Markov Processes
Value
on CSPs Iteration 3
Standard Search vs. CSP
• First studied general state space search in isolation
– Standard search problem: search in a state space
4
Search in Specific R&R Systems
• Constraint Satisfaction Problems:
– State
– Successor function
– Goal test
– Solution
– Heuristic function
• Planning :
– State
– Successor function
– Goal test
– Solution
– Heuristic function
• Inference
– State
– Successor function
– Goal test
– Solution
– Heuristic function
Constraint Satisfaction Problems (CSPs): Definition
Definition:
A constraint satisfaction problem (CSP) consists of:
• a set of variables V
• a domain dom(V) for each variable V ∈V
• a set of constraints C
Simple example:
• V = {V1} All models for this CSP:
– dom(V1) = {1,2,3,4} {V1 = 3}
• C = {C1,C2} {V1 = 4}
– C1: V1 ≠ 2
– C2: V1 > 1 7
Constraint Satisfaction Problems (CSPs): Definition
Definition:
A constraint satisfaction problem (CSP) consists of:
• a set of variables V
• a domain dom(V) for each variable V ∈V
• a set of constraints C
Definition:
A model of a CSP is an assignment of values to all of
its variables that satisfies all of its constraints.
Another example:
Which are models for this CSP?
• V = {V1,V2}
– dom(V1) = {1,2,3} {V1=1, V2=1}
– dom(V2) = {1,2}
{V1=2, V2=1}
• C = {C1,C2,C3}
– C1: V2 ≠ 2 {V1=3, V2=1}
– C2: V1 + V2 < 5
{V1=3, V2=2} 8
– C3: V1 > V2
Possible Worlds
Definition:
A possible world of a CSP is an assignment of
values to all of its variables.
Definition:
A model of a CSP is an assignment of values to all of
its variables that satisfies all of its constraints.
i.e. a model is a possible world that satisfies all constraints
Another example: Possible worlds for this CSP:
• V = {V1,V2} {V1=1, V2=1}
– dom(V1) = {1,2,3} {V1=1, V2=2}
– dom(V2) = {1,2} {V1=2, V2=1} (a model)
• C = {C1,C2,C3} {V1=2, V2=2}
– C1: V2 ≠ 2 {V1=3, V2=1} (a model)
– C2: V1 + V2 < 5 {V1=3, V2=2}
9
– C3: V1 > V2
Constraints
• Constraints are restrictions on the values that one or more
variables can take
– Unary constraint: restriction involving a single variable
• E.g.: V2 ≠ 2
– k-ary constraint: restriction involving k different variables
• E.g. binary (k=2): V1 + V2 < 5
• E.g. 3-ary: V1 + V2 + V4 < 5
• We will mostly deal with binary constraints
– Constraints can be specified by
1. listing all combinations of valid domain values for the variables
participating in the constraint V V
1 2
– E.g. for constraint V1 > V2 2 1
and dom(V1) = {1,2,3} and
3 1
dom(V2) = {1,2}:
3 2
• Examples:
– V2 ≠ 2 has scope {V2}
– V1 > V2 has scope {V1,V2}
– V1 + V2 + V4 < 5 has scope {V1,V2,V4}
12
Finite Constraint Satisfaction
Problem: Definition
Definition:
A finite constraint satisfaction problem (FCSP) is a CSP
with a finite set of variables and a finite domain for
each variable.
13
Examples: variables, domains, constraints
• Crossword Puzzle:
– variables are words that have to be filled in
– domains are English words of correct length
– (binary) constraints: words have the same
letters at cells where they intersect
• Crossword 2:
– variables are cells (individual squares)
– domains are letters of the alphabet
– k-ary constraints: sequences of letters form valid English words
(k= 2,3,4,5,6,7,8,9)
14
Examples: variables, domains, constraints
• Sudoku
– variables are cells
– domain of each variable is {1,2,3,4,5,6,7,8,9}
– constraints: rows, columns, boxes contain all different numbers
• How many possible worlds are there? (say, 53 empty cells)
53*9 539 953
• How many models are there in a typical Sudoku?
About 253 1 953 15
Examples: variables, domains, constraints
• Scheduling Problem:
– variables are different tasks that need to be scheduled
(e.g., course in a university; job in a machine shop)
– domains are the different combinations of times and locations for
each task (e.g., time/room for course; time/machine for job)
– constraints: tasks can't be scheduled in the same location at the
same time; certain tasks can't be scheduled in different locations at
the same time; some tasks must come earlier than others; etc.
• n-Queens problem
– variable: location of a queen on a chess board
• there are n of them in total, hence the name
– domains: grid coordinates
– constraints: no queen can attack another
16
Constraint Satisfaction Problems: Variants
• We may want to solve the following problems with a CSP:
– determine whether or not a model exists
– find a model
– find all of the models
– count the number of models
– find the best model, given some measure of model quality
• this is now an optimization problem
– determine whether some property of the variables holds in all
models
17
Solving Constraint Satisfaction Problems
19
CSP/logic: formal verification
22
SAT Solvers
• Building algorithms and software that perform well in
practice
– On the type of instances they face
• Software and hardware verification instances
• 100,000s of variables, millions of constraints
• Runtime: seconds!
– But: there are classes of instances where current algorithms fail
• International SAT competition (http://www.satcompetition.org/)
– About 40 solvers from around the world compete, bi-yearly
– Best solver in 2007 and 2009:
24
Generate and Test (GT) Algorithms
• Systematically check all possible worlds
- Possible worlds: cross product of domains
dom(V1) × dom(V2) × ... × dom(Vn)
26
Lecture Overview
27
CSP as a Search Problem: one formulation
• States: partial assignment of values to variables
• Start state: empty assignment
• Successor function: states with the next variable assigned
– E.g., follow a total order of the variables V1, …, Vn
– A state assigns values to the first k variables:
• {V1 = v1,…,Vk = vk }
• Neighbors of node {V1 = v1,…,Vk = vk }:
nodes {V1 = v1,…,Vk = vk, Vk+1 = x} for each x ∈ dom(Vk+1)
28
CSP as Graph Searching
{}
V1 = v1 V1 = vk
V1 = v1 V1 = v1 V1 = v1
V2 = v1 V2 = v2 V2 = vk
V1 = v1 V1 = v1
V2 = v1 V2 = v1
V3 = v1 V3 = v2
Which search algorithm would be most
appropriate for this formulation of CSP?
A*
• Example:
- 3 variables A, B, C each with domain {1,2,3,4}
- {A = 1, B = 1} is inconsistent with constraint A ≠ B
regardless of the value of the other variables
⇒ Fail! Prune!
CSP as Graph Searching
Check unary constraints on V1 {}
If not satisfied = PRUNE
V1 = v1 V1 = vk
Check constraints on V1
and V2 If not satisfied =
PRUNE
V1 = v1
V1 = v1
V1 = v1 V2 = vk
V2 = v1
V2 = v2
V1 = v1 V1 = v1
V2 = v1 V2 = v1
V3 = v1 V3 = v2
CSP as Graph Searching
Check unary constraints on V1 {}
If not satisfied = PRUNE
V1 = v1 V1 = vk
Check constraints on V1
and V2 If not satisfied =
PRUNE
V1 = v1
V1 = v1
V1 = v1 V2 = vk
V2 = v1
V2 = v2
Problem?
V1 = v1 V1 = v1 Performance heavily depends
V2 = v1 V2 = v1
V3 = v1 V3 = v2 on the order in which
variables are considered.
E.g. only 2 constraints:
Vn=Vn-1 and Vn≠ Vn-1
CSP as a Search Problem: another formulation
• States: partial assignment of values to variables
• Start state: empty assignment
• Successor function: states with the next variable assigned
– Assign each previously unassigned variable
– A state assigns values to some subset of variables:
• E.g. {V7 = v1, V2 = v1, V15 = v1}
• Neighbors of node {V7 = v1, V2 = v1, V15 = v1}:
nodes {V7 = v1, V2 = v1, V15 = v1, Vx = y}
for any variable Vx ∈V \ {V7, V2, V15} and any value y∈dom(Vx)