Constraint Satisfaction Problems
Constraint Satisfaction Problems
Constraint Satisfaction
Problems
Artificial Intelligence
Graph Coloring
10
Constraint Graphs
Example: N-Queens
• Formulation 1:
– Variables:
– Domains:
– Constraints
Example: N-Queens
• Formulation 2:
– Variables:
– Domains:
– Constraints:
Implicit:
Explicit:
Example: Sudoku
• Variables: Each
(open) square
• Domains: {1,2,…,9}
• Constraints:
9-way alldiff for each column
9-way alldiff for each row
9-way alldiff for each region
(or can have a bunch
of pairwise inequality
constraints)
Varieties of CSPs
• Discrete Variables
– Finite domains
• Size d means O(dn) complete assignments
• E.g., Boolean CSPs, including Boolean satisfiability (NP-complete)
– Infinite domains (integers, strings, etc.)
• E.g., job scheduling, variables are start/end times for each job
• Linear constraints solvable, nonlinear undecidable
• Continuous variables
– E.g., start/end times for Hubble Telescope observations
– Linear constraints solvable in polynomial time
Varieties of Constraints
Varieties of Constraints
– Unary constraints involve a single variable
(equivalent to reducing domains), e.g.:
Example: Cryptarithmetic
• Variables: T, W, O, F, U, R
X1, X2 X2 X 1
• Domains: {0, 1, 2, …, 9}
• Constraints:
O + O = R + 10 * X1
W + W + X1 = U + 10 * X2
T + T + X2 = O + 10 * F
Alldiff(T, W, O, F, U, R)
T ≠ 0, F ≠ 0
30
An example polyhedron:
Variables: edges
Domains: +, –, ,
Desired output:
Real-world CSPs
• Assignment problems
– e.g., who teaches what class
• Timetable problems
– e.g., which class is offered when and where?
• Transportation scheduling
• Factory scheduling
{A ≠ Monday}
• node consistency when all the values in a variable's domain
satisfy the variable's unary constraints
{A ≠ B}
• arc consistency when all the values in a variable's domain
satisfy the variable's binary constraints
Solving CSPs
• States:
– Variables and values assigned so far
• Initial state:
– The empty assignment
• Action:
– Choose any unassigned variable and assign to it a value that
does not violate any constraints
• Fail if no legal assignments
• Goal test:
– The current assignment is complete and satisfies all
constraints
56
Backtracking Search
Backtracking Search
Backtracking search is the basic uninformed algorithm for
solving CSPs
Backtracking search = DFS + two improvements
Backtracking Search
Backtracking DFS
Backtracking Example
Improving Backtracking
• Ordering:
– Which variable should be assigned next?
– In what order should its values be tried?
Filtering
NT
Q
WA
SA
NSW
V
T
Recall: Binary constraint graph for a binary CSP (i.e., each constraint has
most two variables): nodes are variables, edges show constraints
Constraint propagation
Arc consistency
Consistent!
150
Arc consistency
Arc consistency
Arc consistency
Arc consistency
Arc consistency
Recall: Binary constraint graph for a binary CSP (i.e., each constraint has
most two variables): nodes are variables, edges show constraints
NT
Q
WA
SA
NSW
V
T
Queue:
NT
Q
SA->WA
WA NT->WA
SA
NSW
V
T
Queue:
NT
Q
NT->WA
WA WA->SA
SA
NSW NT->SA
V Q->SA
T
NSW->SA
V->SA
Queue:
NT
Q
WA->SA
WA NT->SA
SA
NSW Q->SA
V NSW->SA
T
V->SA
WA->NT
SA->NT
Q->NT
Queue:
NT
Q
WA->SA
WA NT->SA
SA
NSW Q->SA
V NSW->SA
T
V->SA
WA->NT
SA->NT
Q->NT
Queue:
NT
Q
NT->SA
WA Q->SA
SA
NSW NSW->SA
V V->SA
T
WA->NT
SA->NT
Q->NT
Queue:
NT
Q
Q->SA
WA NSW->SA
SA
NSW V->SA
V WA->NT
T
SA->NT
Q->NT
Queue:
NT
Q
NSW->SA
WA V->SA
SA
NSW WA->NT
V SA->NT
T
Q->NT
Queue:
NT
Q
WA
SA
NSW
V
T
Queue:
NT
Q
WA
SA
NSW
V
T
Queue:
NT
Q
NT->Q
WA SA->Q
SA
NSW NSW->Q
V
T
Queue:
NT
Q
SA->Q
WA NSW->Q
SA
NSW WA->NT
V SA->NT
T
Q->NT
Queue:
NT
Q
NSW->Q
WA WA->NT
SA
NSW SA->NT
V Q->NT
T
WA->SA
NT->SA
Q->SA
NSW->SA
V->SA
Queue:
WA->NT
NT
SA->NT
WA
Q Q->NT
SA
NSW
WA->SA
V NT->SA
T Q->SA
NSW->SA
V->SA
V->NSW
Q->NSW
SA->NSW
Queue:
WA->NT
NT
SA->NT
WA
Q Q->NT
SA
NSW
WA->SA
V NT->SA
T Q->SA
NSW->SA
V->SA
V->NSW
Q->NSW
SA->NSW
Queue:
SA->NT
NT
Q->NT
WA
Q WA->SA
SA
NSW
NT->SA
V Q->SA
T NSW->SA
!!! V->SA
V->NSW
Q->NSW
SA->NSW
Queue:
SA->NT
NT
Q->NT
WA
Q WA->SA
SA
NSW
NT->SA
V Q->SA
T NSW->SA
!!! V->SA
V->NSW
Q->NSW
• Backtrack on the assignment of Q SA->NSW
• Arc consistency detects failure earlier than forward checking
• Can be run as a preprocessor or after each assignment
• What’s the downside of enforcing arc consistency?
Ordering
Summary: CSPs