0% found this document useful (0 votes)
11 views

Lecture06 CSP

Uploaded by

Thanh Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lecture06 CSP

Uploaded by

Thanh Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Artificial Intelligence

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
• The structure of problems

2
Constraint satisfaction problem

• Defining the Constraint satisfaction problems


• Example problem: Map coloring and Job-shop scheduling
• Variations on the CSP formalism

3
State-space search problems

State-space

Formalize
Problem

Each state is atomic


and indivisible

4
Constraint satisfaction problems

Problem Variable Value +


Constraints
𝑋1

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

• A CSP consists of the following three 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.
6
Constraints in CSPs
• Each 𝐶𝑖 consists of a pair 𝑠𝑐𝑜𝑝𝑒, 𝑟𝑒𝑙
• 𝑠𝑐𝑜𝑝𝑒: a tuple of variables that participate in the constraint
• A relation 𝑟𝑒𝑙 defines the values that participated variables can take

• Assume that both 𝑋1 and 𝑋2 have the domain {𝐴, 𝐵}


• “Two variables must have different values”
• A relation can explicitly list all tuples satisfying the constraint.
• E.g., 𝑋1 , 𝑋2 , 𝐴, 𝐵 , (𝐵, 𝐴)
• It can be implicitly an abstract relation that supports two operations
• Test whether a tuple is a member of the relation
• Enumerate the members of the relation
• E.g., 𝑋1 , 𝑋2 , 𝑋1 ≠ 𝑋2

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.

Incomplete, Complete, Complete,


consistent inconsistent consistent
assignment assignment assignment
8
Example problem: Map coloring

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)}

• There are many possible solutions

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


𝑁𝑆𝑊 = 𝑔𝑟𝑒𝑒𝑛, 𝑉 = 𝑟𝑒𝑑, 𝑆𝐴 = 𝑏𝑙𝑢𝑒, 𝑇 = 𝑟𝑒𝑑}
10
Aside: The Graph Coloring Problem
• More general problem than map coloring
• Planar graph = graph in the 2D plane with no edge crossings

• Guthrie’s conjecture (1852): Every planar graph can


be colored with 4 colors or less.
• Proved (using a computer) in 1977 (Appel and Haken)

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?

• Many problems intractable in regular state-space search can


be solved quickly with CSP formulation.
• E.g., the Australian problem
Search: 35 = 243 assignments
CSP: 25 = 32 assignments  87%
𝒃𝒍𝒖𝒆

• Better insights to the problem and its solution


• General-purpose rather than problem-specific heuristics
• Identify combinations of variable-value that violate the constraints
→ eliminate large portions of the search space all at once
• Solutions to complex problems
15
Variations on the CSP formalism
• Discrete and finite variables
• 𝑛 variables, domain size 𝑑 → 𝑂(𝑑 𝑛 ) complete assignments
• E.g., map coloring, scheduling with time limits, 8-queens, etc.
• Discrete, infinite domains
• Sets of integers, strings, etc. E.g., job scheduling without deadlines
• Constraint language: understand constraints without enumeration,
e.g., 𝑆𝑡𝑎𝑟𝑡𝐽𝑜𝑏1 + 5 ≤ 𝑆𝑡𝑎𝑟𝑡𝐽𝑜𝑏3
• Continuous domains
• Real-world problems often involve continuous domains and even
real-valued variables.

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.

• Bioinformatics (DNA sequencing)


• Electrical engineering (circuit layout-ing)
• Airline schedules
• Cryptography
• Computer vision: image interpretation
• …

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, 𝑆𝐴, 𝑊𝐴 , 𝑆𝐴 ≠ 𝑊𝐴

• Higher-order constraints: involve three or more variables


• E.g., Professors A, B, and C cannot be on a committee together
• Always possible to be represented by multiple binary constraints
• Global constraints: involving an arbitrary number of variables
• 𝐴𝑙𝑙𝑑𝑖𝑓𝑓 = all variables involved must have different values
• E.g., Sudoku: all variables in a row/column must satisfy an 𝐴𝑙𝑙𝑑𝑖𝑓𝑓

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.

The South Australians dislike


green, the domain of {𝑆𝐴} will be
{𝑟𝑒𝑑, 𝑔𝑟𝑒𝑒𝑛, 𝑏𝑙𝑢𝑒}

• Eliminate all the unary constraints in a CSP

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.

{(red,green), (red,blue), (green,red), (green,blue), (blue,red), (blue,green)}

24
Consider state of search after 𝑊𝐴
and 𝑄 are 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

Continue to propagate constraints


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

Arc consistency detects failure


earlier than forward checking

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

The worst-case complexity is 𝑂(𝑐𝑑 3 )


𝑛: number of variables, each has domain size 𝑑, 𝑐 binary constraints (arc)
28
The AC-3 algorithm

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
then
delete x from Di
revised ← true
return revised

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 𝑆𝐴

• Failure will be detected immediately, avoiding pointless searches


• MRV usually performs better than a random/static ordering,
sometimes by a factor of 1,000 or more.

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.

• DH is the tie-breaker among most constrained variables

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

• Combining the three heuristics makes 1000 queens feasible

Why should variable selection be fail-first,


but value selection be fail-last?
37
Inference: Forward checking
• Supervise remaining legal values for unassigned variables
• Terminate search when any variable has no legal values
• MRV heuristic + forward checking → more effective search
• It can detect many inconsistencies but not all of them.
• Make only the current variable arc-consistent, but do not look ahead
and make all the other variables arc-consistent

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

• Given a constraint 𝐶𝑋𝑌 between two variables 𝑋 and 𝑌.


• For any value of 𝑋, there is a consistent value that can be
chosen for 𝑌 such that 𝐶𝑋𝑌 is satisfied, and visa versa.
• Arc consistency is directed, which is checked in both
directions for two connected variables.
• Forward checking only checks variables that directly connect
to the variable being considered.
• Arc consistency is stronger than forward checking

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

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.

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

• 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 𝑂(𝑑 𝑛 ).

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.

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

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

You might also like