2024 Lecture06 ConstraintSatisfactionProblems
2024 Lecture06 ConstraintSatisfactionProblems
SATISFACTION
PROBLEMS
2
State-space search problems
• Each state is atomic, or indivisible, from the point of view of
the search algorithm.
• Domain-specific code describing transitions between states
is required for each problem.
State-space
Formulation
Problem
…
Defined by
transition model State
3
Factored representation
• CSP factorizes each state into a set of variables, each of
which has a value.
• A problem is solved when every variable has a value that
satisfies all the constraints on that variable.
State
Value
Constraint
4
Constraint
satisfaction
problem
Problem formulation
• A CSP formulation has three main components.
𝑿 = 𝑋1 , . . , 𝑋𝑛 : a set of variables
• Nodes → variables
• Arcs → constraints
• 𝑆𝐴 ≠ 𝑊𝐴 is a shortcut of 𝑆𝐴, 𝑊𝐴 , 𝑆𝐴 ≠ 𝑊𝐴
• Some tasks must occur before another, and some tasks can go on at once
• E.g., a wheel must be installed before the hubcap is put on
• A task takes a certain amount of time to complete.
10
Example problem: Job-shop scheduling
• Variables: 𝑋 = {𝐴𝑥𝑙𝑒𝐹 , 𝐴𝑥𝑙𝑒𝐵 , 𝑊ℎ𝑒𝑒𝑙𝑅𝐹 , 𝑊ℎ𝑒𝑒𝑙𝐿𝐹 , 𝑊ℎ𝑒𝑒𝑙𝑅𝐵 , 𝑊ℎ𝑒𝑒𝑙𝐿𝐵 ,
𝑁𝑢𝑡𝑠𝑅𝐹 , 𝑁𝑢𝑡𝑠𝐿𝐹 , 𝑁𝑢𝑡𝑠𝑅𝐵 , 𝑁𝑢𝑡𝑠𝐿𝐵 ,
𝐶𝑎𝑝𝑅𝐹 , 𝐶𝑎𝑝𝐿𝐹 , 𝐶𝑎𝑝𝑅𝐵 , 𝐶𝑎𝑝𝐿𝐵 , 𝐼𝑛𝑝𝑠𝑒𝑐𝑡}
• Domains: The time that the task starts
• Assume that the tasks, 𝑇1 and 𝑇2 , take duration 𝑑1 and 𝑑2 to complete,
respectively
• Precedence constraints: The task 𝑇1 must occur before the task 𝑇2 , i.e.,
𝑻𝟏 + 𝒅𝟏 ≤ 𝑻𝟐
• Disjunctive constraints: The tasks 𝑇1 and 𝑇2 must not overlap in time, i.e.,
𝑻𝟏 + 𝒅𝟏 ≤ 𝑻𝟐 or 𝑻𝟐 + 𝒅𝟐 ≤ 𝑻𝟏
11
Example problem: Job-shop scheduling
• The axles must be in place before the wheels are put on. Installing an axle takes 10
minutes. 𝐴𝑥𝑙𝑒𝐹 + 10 ≤ 𝑊ℎ𝑒𝑒𝑙𝑅𝐹 𝐴𝑥𝑙𝑒𝐹 + 10 ≤ 𝑊ℎ𝑒𝑒𝑙𝐿𝐹
𝐴𝑥𝑙𝑒𝐵 + 10 ≤ 𝑊ℎ𝑒𝑒𝑙𝑅𝐵 𝐴𝑥𝑙𝑒𝐵 + 10 ≤ 𝑊ℎ𝑒𝑒𝑙𝐿𝐵
• For each wheel, affix the wheel (which takes 1 minute), then tighten the nuts (2
minutes), and finally attach the hubcap (1 minute)
𝑊ℎ𝑒𝑒𝑙𝑅𝐹 + 1 ≤ 𝑁𝑢𝑡𝑅𝐹 𝑁𝑢𝑡𝑠𝑅𝐹 + 2 ≤ 𝐶𝑎𝑝𝑅𝐹
𝑊ℎ𝑒𝑒𝑙𝐿𝐹 + 1 ≤ 𝑁𝑢𝑡𝐿𝐹 𝑊ℎ𝑒𝑒𝑙𝑅𝐵 + 1 ≤ 𝑁𝑢𝑡𝑅𝐵 𝑁𝑢𝑡𝑠𝐿𝐹 + 2 ≤ 𝐶𝑎𝑝𝐿𝐹 𝑁𝑢𝑡𝑠𝑅𝐵 + 2 ≤ 𝐶𝑎𝑝𝑅𝐵
𝑊ℎ𝑒𝑒𝑙𝐿𝐵 + 1 ≤ 𝑁𝑢𝑡𝐿𝐵 𝑁𝑢𝑡𝑠𝐿𝐵 + 2 ≤ 𝐶𝑎𝑝𝐿𝐵
• Suppose we have four workers to install wheels, but they must share one tool that
helps put the axle in place. 𝐴𝑥𝑙𝑒𝐹 + 10 ≤ 𝐴𝑥𝑙𝑒𝐵 𝑜𝑟 𝐴𝑥𝑙𝑒𝐵 + 10 ≤ 𝐴𝑥𝑙𝑒𝐹
• The inspection comes last and takes 3 minutes → for every variable except 𝐼𝑛𝑠𝑝𝑒𝑐𝑡,
add a constraint of the form 𝑋 + 𝑑𝑋 ≤ 𝐼𝑛𝑠𝑝𝑒𝑐𝑡.
• Finally, suppose there is a requirement to get the whole assembly done in 30 minutes
→ limit the domain of all variables to 𝐷𝑖 = {1, 2,3, … , 27}.
12
Why formulate a problem as a CSP?
• CSP gives better insights to the problems and their solutions.
• A CSP solver can quickly prune large swathes of the search
space that an atomic state-space searcher cannot.
14
Types of constraints in CSP
• A unary constraint restricts the value of a single variable.
• E.g., the South Australians hates green → 𝑆𝐴 , 𝑆𝐴 ≠ 𝑔𝑟𝑒𝑒𝑛
• A binary constraint relates two variables
• E.g., adjacent regions are of different colors, 𝑆𝐴, 𝑊𝐴 , 𝑆𝐴 ≠ 𝑊𝐴
• Any 𝑛-ary 𝑛 > 2 constraint can be turned into a binary one.
15
Types of constraints in CSP
• A higher-order constraint involves three or more variables.
• E.g., the ternary constraint 𝐵𝑒𝑡𝑤𝑒𝑒𝑛(𝑋, 𝑌, 𝑍) can be defined as
𝑋, 𝑌, 𝑍 , 𝑋 < 𝑌 < 𝑍 or 𝑋 > 𝑌 > 𝑍
• A global constraint relates an arbitrary number of variables.
16
Example problem: Cryptarithmetic
(Left) A cryptarithmetic problem assumes that each letter stands for a distinct digit and
find a substitution of digits for letters such that the resulting sum is arithmetically correct.
(Right) The constraint hypergraph for the cryptarithmetic problem, showing the 𝐴𝑙𝑙𝑑𝑖𝑓𝑓
constraint (square box at the top) and the column addition constraints (four square boxes
in the middle). The variables 𝐶1 , 𝐶2 , and 𝐶3 represent the carry digits for the three columns
from right to left.
17
Example problem: Cryptarithmetic
• Variables: 𝐹 𝑇 𝑈 𝑊 𝑅 𝑂 𝐶1 𝐶2 𝐶3
• Domains: {0,1,2,3,4,5,6,7,8,9}
• Constraints:
• Alldiff (𝐹, 𝑇, 𝑈, 𝑊, 𝑅, 𝑂)
• 𝑂 + 𝑂 = 𝑅 + 10 ∙ 𝐶1
• 𝐶1 + 𝑊 + 𝑊 = 𝑈 + 10 ∙ 𝐶2
Column addition constraints
• 𝐶2 + 𝑇 + 𝑇 = 𝑂 + 10 ∙ 𝐶3
• 𝐶3 = 𝐹
• 𝑇 ≠ 0, 𝐹 ≠ 0 No leading zeroes are allowed.
18
Preference constraints
• The constraints described so far have all been absolute
constraints, violation of which rules out a potential solution.
• Many real-world CSPs involve preference constraints telling
which solutions are preferred.
• E.g., Prof. R prefers teaching in the morning. A schedule that has
Prof. R’s class at 2 p.m. would still be an allowable but not optimal.
• These constraints are often encoded as costs on variable
assignments → Constrained optimization problem (COP)
• E.g., an afternoon slot for Prof. R costs 2 points, whereas a morning
slot costs only 1 points.
19
Constraint Propagation:
Inference in CSPs
Constraint propagation
• Reduce the number of legal values for a variable by using
constraints → lower the legal values for other variables, etc.
• This will leave fewer choices to consider when we make the next
choice of a variable assignment.
• It may be intertwined with search or done as a preprocessing
step, i.e., before search starts.
• The preprocessing can sometimes solve the whole problem without
any further search.
21
Node consistency
• A single variable is node-consistent if all the values in the
variable’s domain satisfy the variable’s unary constraints.
22
Arc consistency (AC)
• Given two variables, 𝑋𝑖 and 𝑋𝑗 , whose domains are 𝐷𝑖 and
𝐷𝑗 , respectively.
• 𝑋𝑖 is arc-consistent with 𝑋𝑗 if for every value in 𝐷𝑖 there is
some value in 𝐷𝑗 that satisfies the binary constraint 𝑋𝑖 , 𝑋𝑗 .
• E.g., 𝑋, 𝑌 , 𝑌 = 𝑋 2 , both domains are sets of digits → reduce 𝑋’s
domain to {0, 1, 2, 3} and 𝑌’s to {0, 1, 4, 9}
• Arc consistency may have no effect in several cases.
• E.g., in the constraint 𝑆𝐴 ≠ 𝑊𝐴, no matter what value chosen for 𝑆𝐴,
there is a valid value for 𝑊𝐴, following 𝑆𝐴 ≠ 𝑊𝐴
{(red,green), (red,blue), (green,red), (green,blue), (blue,red), (blue,green)}
23
function AC-3(csp) returns false if an inconsistency is found and true otherwise
queue ← a queue of arcs, initially all the arcs in csp
while queue is not empty do
(Xi , Xj) ← POP(queue)
if REVISE(csp, Xi , Xj) then
if size of Di = 0 then return false
for each Xk in Xi.NEIGHBORS - {Xj} do
• The worst-case complexity is 𝑂(𝑐𝑑 3 )
add (Xk , Xi) to queue
• 𝑐: number of binary constraints (arc)
return true
• Each variable has domain size 𝑑
• 𝑆𝐴 → 𝑁𝑆𝑊 is consistent if
𝑆𝐴 = 𝑏𝑙𝑢𝑒 and 𝑁𝑆𝑊 = 𝑟𝑒𝑑
• 𝑁𝑆𝑊 → 𝑆𝐴 is consistent if
𝑁𝑆𝑊 = 𝑟𝑒𝑑 and 𝑆𝐴 = 𝑏𝑙𝑢𝑒
𝑁𝑆𝑊 = 𝑏𝑙𝑢𝑒 and 𝑆𝐴 =? ? ?
• Check 𝑉 → 𝑁𝑆𝑊
• Not consistent for 𝑉 = 𝑟𝑒𝑑
→ remove 𝑟𝑒𝑑 from 𝑉
• Check 𝑆𝐴 → 𝑁𝑇
• Not consistent for 𝑆𝐴 = 𝑏𝑙𝑢𝑒
→ remove 𝑏𝑙𝑢𝑒 from 𝑆𝐴
• The domain of 𝑆𝐴 is empty.
27
Quiz 01: Timetable scheduling
• You are scheduling for computer science classes that meet on Mondays,
Wednesdays and Fridays .
• There are 5 classes and 3 professors who will be teaching these classes.
• You are constrained that each professor can only teach one class at a time.
• The classes are:
• Class 1 - Intro to Programming: meets from 8:00-9:00am
• Class 2 - Intro to Artificial Intelligence: meets from 8:30-9:30am
• Class 3 - Natural Language Processing: meets from 9:00-10:00am
• Class 4 - Computer Vision: meets from 9:00-10:00am
• Class 5 - Machine Learning: meets from 9:30-10:30am
• The professors are:
• Professor A, who is available to teach Classes 3 and 4.
• Professor B, who is available to teach Classes 2, 3, 4, and 5.
• Professor C, who is available to teach Classes 1, 2, 3, 4, and 5.
28
Quiz 01: Timetable scheduling
• Formulate this problem as a CSP in which there is one variable per class, stating
the domains (i.e., available professors) and constraints.
• Constraints should be specified formally and precisely but may be implicit rather than
explicit.
• Draw the constraint graph associated with your CSP.
• Show the domains of the variables after running arc-consistency on this initial
graph (after having already enforced any unary constraints).
• Give one solution to this CSP.
29
Quiz 02: Magic square of order 3
• A magic square of order n is an arrangement of 𝑛2 distinct positive integers, in a
square, such that the 𝑛 numbers in all rows, all columns, and both diagonals sum
to the same constant.
• For order-3 magic squares, there is a general formula devised by Édouard
Lucas.
• Formulate the above magic square construction method as a CSP over the
variables p, q and z, each of which has its initial domain in {1,...,9}.
• Let the magic constant be 15. Solve the CSP.
30
Backtracking Search
for CSPs
CSP as a Search problem
• Let a state be a partial assignment and an action extend the
assignment.
• A standard depth-limited search can be used solve CSPs.
• Initial state: empty assignment { }
• Successor function: assign a value to an unassigned variable that
agrees with the current assignment → fail if no legal assignments
• Goal test: the current assignment is complete
• All the complete assignments of 𝑛 variables of domain size 𝑑
appears at depth 𝑛 on the search tree.
• The branching factor 𝑏 = (𝑛 − 𝑙)𝑑 at depth 𝑙, there are 𝑛! ∙ 𝑑 𝑛 leaves
with only 𝑑 𝑛 complete assignments!
32
The commutativity of CSPs
• A problem is commutative if the order of application of any
given set of actions does not matter.
• Variable assignments in CPS are commutative.
• E.g., [𝑊𝐴 = 𝑟𝑒𝑑 then 𝑁𝑇 = 𝑔𝑟𝑒𝑒𝑛] [𝑁𝑇 = 𝑔𝑟𝑒𝑒𝑛 then 𝑊𝐴 = 𝑟𝑒𝑑]
• We need only consider a single variable at each node in the
search tree.
• The branching factor 𝑏 = 𝑑, and there are 𝑑 𝑛 leaves.
33
Backtracking search: An example
There is only one possible value for 𝑆𝐴 after [𝑊𝐴 = 𝑟𝑒𝑑, 𝑁𝑇 = 𝑔𝑟𝑒𝑒𝑛].
36
Degree heuristic
• DH heuristic selects the variable that involves in the largest
number of constraints on other unassigned variables.
• It attempts to reduce the branching factor on future choices.
37
Least constraining value heuristic
• LCV heuristic prefers the value that rules out the fewest
choices for neighboring variables in the constraint graph.
• It tries to leave the maximum flexibility for subsequent variable
assignments.
38
Interleaving search and inference
• Let 𝑋 be the variable just been assigned and 𝑌 be each
unassigned variable that connects 𝑋 by some constraint.
• Forward checking (FC) deletes from 𝑌’s domain any value
that is inconsistent with the value chosen for 𝑋.
39
• Assign {𝑊𝐴 = 𝑟𝑒𝑑}
• 𝑁𝑇 and 𝑆𝐴 can no longer be red
• Assign {𝑄 = 𝑔𝑟𝑒𝑒𝑛}
• 𝑁𝑇, 𝑁𝑆𝑊 and 𝑆𝐴 can no longer
be green.
• Assign 𝑉 = 𝑏𝑙𝑢𝑒
• 𝑁𝑆𝑊 can no longer be blue.
• 𝑆𝐴 is empty
40
Example: Australian map coloring
• The map coloring problem can be easily solved by using
forward checking and MRV heuristic.
41
Maintaining Arc Consistency (MAC)
• Consider the (directed) arc 𝑋𝑗 , 𝑋𝑖 , where 𝑋𝑖 is the assigned
variable and 𝑋𝑗 is each unassigned neighbor of 𝑋𝑖 .
• The MAC algorithm starts with only 𝑋𝑗 , 𝑋𝑖 and applies AC-3
for constraint propagation.
• If any variable has its domain reduced to the empty set, AC-
3 fails and backtrack occurs immediately.
42
Quiz 03: Map coloring problem
• Coloring each region either red, yellow, or blue in such a
way that no neighboring regions have the same color
43
Quiz 04: AC vs. FC
• The graph shown aside is a constraint
graph for a CSP that has only binary
constraints. Initially, no variables have
been assigned.
• For each of the given scenarios, mark all variables for which the
specified filtering might result in their domain being changed. Note that
every scenario is independent from the others.
48
Quiz 04: AC vs. FC
• A value is assigned to A. Which domains might be changed as a result of
running forward checking for A?
A B C D E F
• A value is assigned to A, and then forward checking is run for A. Then a
value is assigned to B. Which domains might be changed as a result of
running forward checking for B?
A B C D E F
• A value is assigned to A. Which domains might be changed as a result of
enforcing arc consistency after this assignment?
A B C D E F
• A value is assigned to A, and then arc consistency is enforced. Then a
value is assigned to B. Which domains might be changed as a result of
enforcing arc consistency after the assignment to B?
A B C D E F 49
Local search
for CSP
50
Local search for CSPs
• Complete-state formulation
• The initial state assigns a value to every variable → violate constraints
• The search changes the value of one variable at a time → resolve
the confliction
• Min-conflicts heuristic: the minimum number of conflicts
with other variables
• Min-conflicts is surprisingly effective for many CSPs.
• Million-queens problem can be solved ~ 50 steps
• Hubble Space Telescope: the time taken to schedule a week of
observations down from 3 weeks (!) to ~10 minutes
51
MIN-CONFLICTS algorithm
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)
set var = value in current
return failure
52
MIN-CONFLICTS: 8-queens
53
Local search for CSPs
• The landscape of a CSP under the min-conflicts heuristic
usually has a series of plateau.
• There are millions of variable assignments that are only one conflict
away from a solution.
• Plateau search: allow sideways moves to another state with
the same score
• Tabu search: keep a small list of recently visited states and
forbid the algorithm to return to those states
• Simulated annealing can also be used
54
Constraint weighting
• Concentrate the search on the important constraints
• Each constraint is given a numeric weight, 𝑊𝑖 , initially all 1.
• At each step, choose a variable/value pair to change that
has the lowest total weight of all violated constraints
• Increase the weight of each constraint that is violated by the
current assignment
55
Local search in online setting
• Scheduling problems: online setting
• A weekly airline schedule may involve thousands of flights and tens
of thousands of personnel assignments
• The bad weather at one airport can render the schedule infeasible.
• The schedule should be repaired with a minimum number of
changes.
• Done easily with a local search starting from the current schedule
• A backtracking search with the new set of constraints usually
requires much more time and might find a solution with many
changes from the current schedule
56
The
structure
of
problems
57
Independent subproblems
• If assignment 𝑆𝑖 is a solution of 𝐶𝑆𝑃𝑖 , then 𝑖𝑆 𝑖ڂis a solution
of 𝑖𝑃𝑆𝐶 𝑖ڂ.
• For example, the Australia map coloring: Tasmania and the mainland
58
Tree-structured CSP
• A constraint graph is a tree when any two variables are
connected by only one path.
• Any tree-structured CSP can be solved in time linear in the
number of variables
• Directed arc consistency (DAC): A CSP is directed arc-
consistent under an ordering of variables 𝑋1 , 𝑋2 , … , 𝑋𝑛 iff
every 𝑋𝑖 is arc-consistent with each 𝑋𝑗 for 𝑗 > 𝑖.
59
Tree-structured CSP
• Topological sort: first pick any variable to be the root of the
tree and choose an ordering of the variables such that each
variable appears after its parent in the tree.
60
Reducing graphs to trees
• Assign values to some variables so that the remaining
variables form a tree
• E.g., fix a value for 𝑆𝐴 and delete from other variables’ domains any
values that are inconsistent with the value chosen for 𝑆𝐴
61
Reducing graphs to trees
• Construct a tree decomposition of the constraint graph into a
set of connected subproblems.
• Each subproblem is solved independently and the resulting
solutions are then combined.
62
The structure of values
• Consider the map-coloring problem with 𝑛 colors.
• For every consistent solution, there is a set of 𝑛! solutions
formed by permuting the color names.
• E.g., 𝑊𝐴, 𝑁𝑇, and 𝑆𝐴 must all have different colors, but there are 3!
ways to assign the three colors to these three regions.
• Symmetry-breaking constraint: Impose an arbitrary ordering
constraint that requires the values to be in alphabetical order
• E.g., 𝑁𝑇 < 𝑆𝐴 < 𝑊𝐴 → only one solution possible: {𝑁𝑇 = 𝑏𝑙𝑢𝑒,
𝑆𝐴 = 𝑔𝑟𝑒𝑒𝑛, 𝑊𝐴 = 𝑟𝑒𝑑}
63
64