Lecture06 CSP
Lecture06 CSP
CONSTRAINT SATISFACTION
PROBLEMS
2
Constraint satisfaction problem
3
State-space search problems
State-space
Formalize
Problem
…
4
Constraint satisfaction problems
Value +
Variable Constraints
𝑋2
…
Each state is Value +
Variable
factored Constraints
represented
𝑋𝑛
5
Constraint satisfaction problem
• State = a set of variables and each of which has a value
• Solution = each variable has a value that satisfies all the
constraints on that variable
𝑿 = 𝑋1 , . . , 𝑋𝑛 : a set of variables
7
Solutions for CSPs
• Each state is defined by an assignment of values to some or
all the variables, 𝑋𝑖 = 𝑣𝑖 , 𝑋𝑗 = 𝑣𝑗 , … .
• A solution to a CSP is a consistent – complete assignment.
• A consistent assignment does not violate any constraints.
• A complete assignment has every variable assigned, while a partial
assignment assigns values to only some variables.
Constraint graph
• Nodes are variables
• Arcs are constraints
• Color each region either red, green, or blue in such a way that no
neighboring regions have the same color
9
Example problem: Map coloring
• Variables: 𝑋 = {𝑊𝐴, 𝑁𝑇, 𝑄, 𝑁𝑆𝑊, 𝑉, 𝑆𝐴, 𝑇}
• Domains: 𝐷𝑖 = {𝑟𝑒𝑑, 𝑔𝑟𝑒𝑒𝑛, 𝑏𝑙𝑢𝑒}
• Constraints: Adjacent regions must have different colors
𝑆𝐴 ≠ 𝑊𝐴, 𝑆𝐴 ≠ 𝑁𝑇, 𝑆𝐴 ≠ 𝑄, 𝑆𝐴 ≠ 𝑁𝑆𝑊 , 𝑆𝐴 ≠ 𝑉,
𝐶=
𝑊𝐴 ≠ 𝑁𝑇, 𝑁𝑇 ≠ 𝑄, 𝑄 ≠ 𝑁𝑆𝑊 , 𝑁𝑆𝑊 ≠ 𝑉
• where 𝑆𝐴 ≠ 𝑊𝐴 is a shortcut of 𝑆𝐴, 𝑊𝐴 , 𝑆𝐴 ≠ 𝑊𝐴
• 𝑆𝐴 ≠ 𝑊𝐴 can be fully enumerated as {(red,green), (red,blue), (green,red),
(green,blue), (blue,red), (blue,green)}
11
Example problem: Job-shop scheduling
15 tasks
• Install axles (front and back)
• Affix all four wheels (right and left, front
and back)
• Tighten nuts for each wheel
• Affix hubcaps, and
• Inspect the final assembly
• 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.
12
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 𝑻𝟐 + 𝒅𝟐 ≤ 𝑻𝟏
13
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}.
14
Why formulate a problem as a CSP?
16
Real-world CSPs
• Operations research (scheduling, timetabling)
• Scheduling the time of observations on the Hubble Space Telescope
• Linear programming
• Constraints must be linear equalities or inequalities → solved in time
polynomial in the number of variables.
17
Types of constraints
• Unary constraint: restrict the value of a single variable
• E.g., the South Australians do not like green → 𝑆𝐴 , 𝑆𝐴 ≠ 𝑔𝑟𝑒𝑒𝑛
• Binary constraint: relate two variables
• E.g., adjacent regions are of different colors, 𝑆𝐴, 𝑊𝐴 , 𝑆𝐴 ≠ 𝑊𝐴
18
Preference constraints
• Which solutions are preferred → soft constraints
• E.g., 𝑟𝑒𝑑 is better than 𝑔𝑟𝑒𝑒𝑛 → this can be represented by a cost
for each variable assignment
• Constraint optimization problem (COP): a combination of
optimization with CSPs → linear programming
19
Examples of toy problems in CSP
4-Queens Problem
• Variables: 𝑄1, 𝑄2, 𝑄3, 𝑄4
• Domains: D = {1,2,3,4}
• Constraints
• 𝑄𝑖 ≠ 𝑄𝑗 (cannot be in the same row)
• 𝑄𝑖 − 𝑄𝑗 ≠ 𝑖 − 𝑗 (cannot be in the same diagonal)
The Cryptarithmetic
• Variables: 𝐹 𝑇 𝑈 𝑊 𝑅 𝑂 𝐶1 𝐶2 𝐶3
• Domains: {0,1,2,3,4,5,6,7,8,9}
• Constraints:
• Alldiff (𝐹, 𝑇, 𝑈, 𝑊, 𝑅, 𝑂)
• 𝐶3 = 𝐹, 𝑇 ≠ 0, 𝐹 ≠ 0
• … 20
Constraint propagation
• Node consistency
• Arc consistency
• Path consistency
• K-consistency
• Global constraints
21
Constraint propagation
• Constraints help to reduce the number of legal values for a
variable → legal values for another variable are also reduced
• Intertwined with search, or done as a preprocessing step
• Sometimes the preprocessing can solve the whole problem!
• Enforcing local consistency in each part of a graph causes
inconsistent values to be eliminated throughout the graph
22
Node consistency
• A single variable is node-consistent if all the values in the
variable’s domain satisfy the variable’s unary constraints.
23
Arc consistency
• A variable in a CSP is arc-consistent if every value in its
domain satisfies the variable’s binary constraints.
• E.g., (𝑋, 𝑌), { 0, 0 , 1, 1 , 2, 4 , 3, 9 } , both domains are sets of
digits → reduce X’s domain to {0, 1, 2, 3} and Y’s to {0, 1, 4, 9}
• Arc consistency may have no effect in several cases.
• E.g., the Australia map, no matter what value chosen for 𝑆𝐴 (or for
𝑊𝐴), there is a valid value for the other variable.
24
Consider state of search after 𝑊𝐴
and 𝑄 are assigned
• 𝑆𝐴 → 𝑁𝑆𝑊 is consistent if 𝑆𝐴 =
𝑏𝑙𝑢𝑒 and 𝑁𝑆𝑊 = 𝑟𝑒𝑑
• 𝑁𝑆𝑊 → 𝑆𝐴 is consistent if
𝑁𝑆𝑊 = 𝑟𝑒𝑑 and 𝑆𝐴 = 𝑏𝑙𝑢𝑒
𝑁𝑆𝑊 = 𝑏𝑙𝑢𝑒 and 𝑆𝐴 =? ? ?
25
If 𝑋 loses a value, neighbors of 𝑋
need to be rechecked
26
Arc consistency
• Run as a preprocessor before the search starts or after each
assignment
• AC must be run repeatedly until no inconsistency remains.
• Trade-off
• Eliminate large (inconsistent) parts of the state-space,
• Require some overhead to do
• Generally, more effective than direct search
• Need a systematic method for arc-checking
• If 𝑋 loses a value, neighbors of 𝑋 need to be rechecked.
• Incoming arcs can become inconsistent, while outgoing arcs stay still.
27
The AC-3 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 all the arcs in csp
while queue is not empty do
(Xi , Xj) ← REMOVE-FIRST(queue)
if REVISE(csp, Xi , Xj) then
if size of Di = 0 then return false
for each Xk in Xi.NEIGHBORS - {Xj} do
add (Xk , Xi) to queue
return true
29
Backtracking search
• Backtracking search
• Variable and value ordering
• Interleaving search and inference: Forward checking
30
CSP as a Search problem
• Let's start with the straightforward approach, then fix it.
• States are defined by the values assigned so far
• 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
• This is the same for all CSPs
• Every solution appears at depth 𝑛 with 𝑛 variables → use depth-first
(or depth-limited) search
• Given 𝑑 is the domain size, the branching factor 𝑏 = (𝑛 − 𝑙)𝑑 at
depth 𝑙, 𝑛! ∙ 𝑑 𝑛 leaves with only 𝑑 𝑛 complete assignments!
31
Backtracking search
• Variable assignments are commutative.
• E.g., [𝑊𝐴 = 𝑟𝑒𝑑 then 𝑁𝑇 = 𝑔𝑟𝑒𝑒𝑛] = [𝑁𝑇 = 𝑔𝑟𝑒𝑒𝑛 then 𝑊𝐴 = 𝑟𝑒𝑑]
• Only need to consider assignments to a single variable at
each node → branching factor 𝑏 = 𝑑, 𝑑𝑛 leaves
• Depth-first search: choose values for one variable at a time
and backtrack when a variable has no legal values left
32
Backtracking search
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 Which variable should
be assigned next?
inferences ← INFERENCE(csp, var, value)
if inferences failure then In what order should
add inferences to assignment its values be tried?
result ← BACKTRACK(assignment, csp)
What inferences
if result failure then should be performed?
return result
remove {var = value} and inferences from assignment
return failure 33
Backtracking search: An example
34
Variable and value ordering
• Minimum-remaining-values (MRV) heuristic: choose the
variable with the fewest legal values
• E.g., after [𝑊𝐴 = 𝑟𝑒𝑑, 𝑁𝑇 = 𝑔𝑟𝑒𝑒𝑛] only one possible value for 𝑆𝐴
35
Variable and value ordering
• Degree heuristic (DH): choose the variable that involves in
the largest number of constraints on other unassigned
variables
• E.g., 𝑆𝐴 has a highest degree of 5, other variables except 𝑇 have
degrees of 2 or 3.
36
Variable and value ordering
• Least constraining value (LCV) heuristic: given a variable,
choose the value that leaves the maximum flexibility for
subsequent variable assignments
38
✓ Assign {𝑊𝐴 = 𝑟𝑒𝑑}
✓ Effects on other variables
connected by constraints to WA
– 𝑁𝑇 can no longer be red
– 𝑆𝐴 can no longer be red
✓ Assign {𝑄 = 𝑔𝑟𝑒𝑒𝑛}
✓ Effects on other variables
connected by constraints to 𝑄
FC has detected that partial assignment is can
• 𝑁𝑇 inconsistent
no longer be green
• 𝑆𝐴 can no longer be green
with the constraints and backtracking can occur.
• 𝑁𝑆𝑊 can no longer be green
✓ Assign {𝑉 = 𝑏𝑙𝑢𝑒}
✓ Effects on other variables
connected by constraints to 𝑉
• 𝑁𝑆𝑊 can no longer be blue
• 𝑆𝐴 is empty
39
Forward checking vs. Arc consistency
40
𝑴𝑹𝑽 + 𝑫𝑯
41
𝑴𝑹𝑽 + 𝑫𝑯
42
Quiz 01: 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 02: AC vs. Forward checking
• 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.
44
Quiz 02: AC vs. Forward checking
• 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
45
Quiz 03: 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.
46
Quiz 03: 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.
47
Local search for CSP
48
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
49
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
50
MIN-CONFLICTS: 8-queens
51
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
52
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
53
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
54
The structure of problems
55
Independent subproblems
• If assignment 𝑆𝑖 is a solution of 𝐶𝑆𝑃𝑖 , then 𝑖𝑆 𝑖ڂis a solution
of 𝑖𝑃𝑆𝐶 𝑖ڂ.
• For example, the Australia map coloring: Tasmania and the mainland
56
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 𝑗 > 𝑖.
57
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.
58
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 𝑆𝐴
59
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.
60
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: {𝑁𝑇 = 𝑏𝑙𝑢𝑒,
𝑆𝐴 = 𝑔𝑟𝑒𝑒𝑛, 𝑊𝐴 = 𝑟𝑒𝑑}
61
THE END
62