0% found this document useful (0 votes)
11 views60 pages

2024 Lecture06 ConstraintSatisfactionProblems

The document discusses Constraint Satisfaction Problems (CSPs), outlining their formulation, structure, and types of variables and constraints involved. It provides examples such as map coloring and job-shop scheduling to illustrate the concepts, and emphasizes the benefits of CSPs in problem-solving. Additionally, it touches on constraint propagation and its role in reducing the search space for solutions.

Uploaded by

pvmtue22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views60 pages

2024 Lecture06 ConstraintSatisfactionProblems

The document discusses Constraint Satisfaction Problems (CSPs), outlining their formulation, structure, and types of variables and constraints involved. It provides examples such as map coloring and job-shop scheduling to illustrate the concepts, and emphasizes the benefits of CSPs in problem-solving. Additionally, it touches on constraint propagation and its role in reducing the search space for solutions.

Uploaded by

pvmtue22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

CONSTRAINT

SATISFACTION
PROBLEMS

Nguyễn Ngọc Thảo – Nguyễn Hải Minh


{nnthao, nhminh}@fit.hcmus.edu.vn
Outline
• Constraint satisfaction problems (CSPs)
• Constraint propagation: Inference in CSPs
• Backtracking search for CSPs
• Local search for CSPs (Self-study)
• The structure of problems (Self-study)

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

𝑫 = 𝐷1 , . . , 𝐷𝑛 : a set of domains, one for each variable.


• 𝐷𝑖 = 𝑣1 , . . , 𝑣𝑘 : set of allowable values for variable 𝑋𝑖

𝑪: a set of constraints, 𝐶𝑗 = 𝑠𝑐𝑜𝑝𝑒, 𝑟𝑒𝑙, that state allowable


combinations of values.
• 𝑠𝑐𝑜𝑝𝑒: a tuple of variables that participate in the constraint
• A relation 𝑟𝑒𝑙 defines the values that participated variables can take

• A CSP is an NP-complete problem in general.


6
Assignments in CSP
• CSPs assigns values to variables, 𝑋𝑖 = 𝑣𝑖 , 𝑋𝑗 = 𝑣𝑗 , … .
• A solution is a consistent and complete assignment.
• A consistent assignment does not violate any constraints.
• A complete assignment has every variable assigned a value.
• A partial solution is a partial assignment that is consistent.
• A partial assignment is one that leaves some variables unassigned.

Incomplete, Complete, Complete,


consistent inconsistent consistent
assignment assignment assignment 7
Example problem: Map coloring
• Objective: Color each region either red, green, or blue in such a way
that no neighboring regions have the same color

• Nodes → variables
• Arcs → constraints

(a) The principal states and territories of Australia.


(b) The map-coloring problem represented as a constraint graph.
8
Example problem: Map coloring
• Variables: 𝑋 = {𝑊𝐴, 𝑁𝑇, 𝑄, 𝑁𝑆𝑊, 𝑉, 𝑆𝐴, 𝑇}
• Domains: 𝐷𝑖 = {𝑟𝑒𝑑, 𝑔𝑟𝑒𝑒𝑛, 𝑏𝑙𝑢𝑒}
• Constraints: Neighboring regions have distinct colors.
𝑆𝐴 ≠ 𝑊𝐴, 𝑆𝐴 ≠ 𝑁𝑇, 𝑆𝐴 ≠ 𝑄, 𝑆𝐴 ≠ 𝑁𝑆𝑊 , 𝑆𝐴 ≠ 𝑉,
𝐶=
𝑊𝐴 ≠ 𝑁𝑇, 𝑁𝑇 ≠ 𝑄, 𝑄 ≠ 𝑁𝑆𝑊 , 𝑁𝑆𝑊 ≠ 𝑉

• 𝑆𝐴 ≠ 𝑊𝐴 is a shortcut of 𝑆𝐴, 𝑊𝐴 , 𝑆𝐴 ≠ 𝑊𝐴

• There are many possible solutions.

{𝑊𝐴 = 𝑟𝑒𝑑, 𝑁𝑇 = 𝑔𝑟𝑒𝑒𝑛, 𝑄 = 𝑟𝑒𝑑,


𝑁𝑆𝑊 = 𝑔𝑟𝑒𝑒𝑛, 𝑉 = 𝑟𝑒𝑑, 𝑆𝐴 = 𝑏𝑙𝑢𝑒, 𝑇 = 𝑟𝑒𝑑} 9
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.

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.

Assume that we have chosen {SA = blue}.


SA
Search: 35 = 243 assignments
CSP: 25 = 32 assignments  87%

• It is more general than problem-specific heuristics.


13
Types of variables in CSP
• The simplest CSP involves variables that have discrete and
finite domains.
• E.g., map coloring problem, job-shop scheduling with time limits
• A discrete domain can be infinite.
• E.g., set of integers problem, job-shop scheduling without time limits
• CSPs with continuous domains are common in practice
• E.g., the scheduling of experiments on the Hubble Space Telescope
uses real-values variables for observation timings.
• They are widely studied in the field of operations research, e.g.,
linear programming.

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.

• A binary CSP is one with only unary and binary constraints,


which can be represented as a constraint graph.

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.

𝐴𝑙𝑙𝑑𝑖𝑓𝑓: all variables involved


must have different values

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.

The South Australians dislike green,

The domain of {𝑆𝐴} will be X

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 𝑑

function REVISE(csp, Xi , Xj) returns true iff we revise the domain of Xi


revised ← false
for each x in Di do
if no value y in Dj allows (x ,y) to satisfy the constraint between Xi and Xj
delete x from Di
revised ← true
return revised 24
Consider state of search after 𝑊𝐴 and 𝑄 are already assigned.

• 𝑆𝐴 → 𝑁𝑆𝑊 is consistent if
𝑆𝐴 = 𝑏𝑙𝑢𝑒 and 𝑁𝑆𝑊 = 𝑟𝑒𝑑

• 𝑁𝑆𝑊 → 𝑆𝐴 is consistent if
𝑁𝑆𝑊 = 𝑟𝑒𝑑 and 𝑆𝐴 = 𝑏𝑙𝑢𝑒
𝑁𝑆𝑊 = 𝑏𝑙𝑢𝑒 and 𝑆𝐴 =? ? ?

Arc-consistency can be made by removing 𝑏𝑙𝑢𝑒 from 𝑁𝑆𝑊


25
If 𝑋 loses a value, neighbors of 𝑋 need to be rechecked

• Check 𝑉 → 𝑁𝑆𝑊
• Not consistent for 𝑉 = 𝑟𝑒𝑑
→ remove 𝑟𝑒𝑑 from 𝑉

• Check 𝑆𝐴 → 𝑁𝑇
• Not consistent for 𝑆𝐴 = 𝑏𝑙𝑢𝑒
→ remove 𝑏𝑙𝑢𝑒 from 𝑆𝐴
• The domain of 𝑆𝐴 is empty.

Arc consistency detects failure earlier than forward checking.


26
Arc consistency: An evaluation
• Arc consistency may run before the search starts or after
each assignment, repeatedly until no inconsistency remains.
• Trade-off: Eliminate large inconsistent parts of the state space while
requiring some overhead to do
• AC performs a systematic method for arc-checking.
• Incoming arcs can become inconsistent, while outgoing arcs stay still.

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.

The aside table is made of positive


integers, p, q, and z.
These nine numbers will form a magic
square with the magic constant 3(p+q)
so long as 0 < q < z < p and z  2q.

• 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

Part of the search tree for the map-


coloring problem. The variables are
assigned in the order WA, NT, Q,…
34
function BACKTRACKING-SEARCH(csp) returns a solution or failure
return BACKTRACK(csp, { })

function BACKTRACK(csp, assignment) returns a solution or failure


if assignment is complete then return assignment
var ← SELECT-UNASSIGNED-VARIABLE(csp, assignment)
for each value in ORDER-DOMAIN-VALUES(csp, var, assignment) do
if value is consistent with assignment then
add {var = value} to assignment Which variable should
be assigned next?
inferences ← INFERENCE(csp, var, assignment)
if inferences  failure then In what order should
add inferences to csp its values be tried?
result ← BACKTRACK(csp, var, assignment)
What inferences
if result  failure then return result should be performed?
remove inferences from csp
remove {var = value} from assignment
return failure
35
Minimum-remaining-values heuristic
• MRV heuristic takes the variable with the fewest legal values.
• It picks a variable that is most likely to cause a failure soon, thereby
pruning the search tree.

There is only one possible value for 𝑆𝐴 after [𝑊𝐴 = 𝑟𝑒𝑑, 𝑁𝑇 = 𝑔𝑟𝑒𝑒𝑛].

• It usually performs better than a random or static ordering.


• The advantage is sometimes by orders of magnitude, yet the results
vary depending on the problem.

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.

𝑆𝐴 has a highest degree of 5, other variables except 𝑇 have degrees of 2 or 3.

• DH is the tie-breaker among most constrained variables.

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.

• In conclusion, variable selection should be fail-first, while


value choice should be fail-last. Why?

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 𝑋.

• Combining MRV heuristic with forward checking effectively


improves the search in many problems.
• FC detects many inconsistencies, but not all of them.
• It makes the current variable arc-consistent but does not look ahead.

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

A two-step solution using min-conflicts for an 8-queens problem.


At each stage, a queen is chosen for reassignment in its column.
The number attacking queens (i.e., conflicts) is shown in each square.
The algorithm moves the queen to the min-conflicts square, breaking ties randomly.

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

• Suppose each 𝐶𝑆𝑃𝑖 has 𝑐 variables from 𝑛 variables.


• Then there are 𝑛/𝑐 subproblems, each of which takes at
most 𝑑𝑐 work to solve.
• where 𝑐 is a constant and 𝑑 is the size of the domain.
• Hence, the total work is 𝑂(𝑑𝑐 𝑛/𝑐), which is linear in 𝑛.
• Without the decomposition, the total work is 𝑂(𝑑 𝑛 ).

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.

(a) The constraint graph of a tree-structured CSP.


(b) A linear ordering of the variables consistent with the tree with A as the root.

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

You might also like