IT代写 COMP30024 – Artificial Intelligence Chapter 5

Week 7: Constraint Satisfaction Problems – COMP30024 – Artificial Intelligence Chapter 5

Week 7: Constraint Satisfaction Problems
COMP30024 – Artificial Intelligence

Copyright By PowCoder代写 加微信 powcoder

Dr Wafa Johal

University of Melbourne

Introduction

• CSP examples
• General search applied to CSPs
• Backtracking search for CSPs
• Problem structure and problem decomposition
• Local search for CSPs

AIMA Slides © and ; Dr Wafa Johal 3

Constraint satisfaction problems (CSPs)

Standard search problem:
state is a “black box”—any old data structure that supports goal test, eval,

state is defined by variables Vi with values from domain Di
goal test is a set of constraints specifying allowable combinations of values for
subsets of variables

Simple example of a formal representation language

Allows useful general-purpose algorithms with more power than standard
search algorithms

AIMA Slides © and ; Dr Wafa Johal 4

Example: Map-Coloring

Queensland

Variables WA, NT, Q, NSW, V, SA, T
Domains Di = {red, green, blue}
Constraints: adjacent regions must have different colors

e.g., WA ̸= NT (if the language allows this), or
(WA,NT) ∈ {(red, green), (red, blue), (green, red), (green, blue), . . .}

AIMA Slides © and ; Dr Wafa Johal 5

Example: Map-Coloring

Queensland

Variables WA, NT, Q, NSW, V, SA, T
Domains Di = {red, green, blue}
Constraints: adjacent regions must have different colors

e.g., WA ̸= NT (if the language allows this), or
(WA,NT) ∈ {(red, green), (red, blue), (green, red), (green, blue), . . .}

AIMA Slides © and ; Dr Wafa Johal 6

Example: Map-Coloring contd.

Queensland

Solutions are assignments satisfying all constraints, e.g.,
{WA = red,NT = green,Q = red,NSW = green,V = red, SA = blue,T =
AIMA Slides © and ; Dr Wafa Johal 7

Constraint graph

Binary CSP: each constraint relates at most two variables

Constraint graph: nodes are variables, arcs show constraints

General-purpose CSP algorithms use the graph structure
to speed up search. E.g., Tasmania is an independent subproblem!
AIMA Slides © and ; Dr Wafa Johal 8

Varieties of CSPs

Discrete variables

finite domains; size d
where n is the number of variables in the CSP
=⇒ O(dn) complete assignments

e.g., Boolean CSPs, incl. Boolean satisfiability (NP-complete)

infinite domains (integers, strings, etc.)
• e.g., job scheduling, variables are start/end days for each job
• need a constraint language, e.g., StartJob1 + 5 ≤ StartJob3
• linear constraints solvable, nonlinear undecidable

Continuous variables

• e.g., start/end times for observations
• linear constraints solvable in polynomial time by linear programming (LP)

AIMA Slides © and ; Dr Wafa Johal 9

Varieties of constraints

• Unary constraints involve a single variable,
e.g., SA ̸= green

• Binary constraints involve pairs of variables,
e.g., SA ̸= WA

• Higher-order constraints involve 3 or more variables, e.g., cryptarithmetic
column constraints

• Preferences (soft constraints), e.g., red is better than green often
representable by a cost for each variable assignment
→ constrained optimization problems

AIMA Slides © and ; Dr Wafa Johal 10

Example: Cryptarithmetic

Variables: F T U W R O C1 C2 C3
Domains: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Constraints

alldiff(F,T,U,W,R,O)
O + O = R + 10 · C1, etc.

AIMA Slides © and ; Dr Wafa Johal 11

Real-world CSPs

Assignment problems e.g., who teaches what class

Timetabling problems e.g., which class is offered when and where?

Hardware configuration

Spreadsheets

Transportation scheduling

Factory scheduling

Floorplanning

Many real-world problems involve real-valued variables

AIMA Slides © and ; Dr Wafa Johal 12

Applying standard search

Let’s start with the straightforward, dumb approach, then fix it

States are defined by the values assigned so far

• Initial state: the empty assignment, ∅
• Successor function: assign a value to an unassigned variable that does

not conflict with current assignment.
=⇒ fail if no legal assignments (not fixable!)

• Goal test: the current assignment is complete

1. This is the same for all CSPs!
2. Every solution appears at depth n with n variables

=⇒ use depth-first search
3. b = (n− ℓ)d at depth ℓ, hence n!dn leaves!!!!
4. Path is irrelevant, so can also use complete-state formulation

AIMA Slides © and ; Dr Wafa Johal 13

Applying standard search

Let’s start with the straightforward, dumb approach, then fix it

States are defined by the values assigned so far

• Initial state: the empty assignment, ∅
• Successor function: assign a value to an unassigned variable that does

not conflict with current assignment.
=⇒ fail if no legal assignments (not fixable!)

• Goal test: the current assignment is complete

1. This is the same for all CSPs!
2. Every solution appears at depth n with n variables

=⇒ use depth-first search
3. b = (n− ℓ)d at depth ℓ, hence n!dn leaves!!!!
4. Path is irrelevant, so can also use complete-state formulation

AIMA Slides © and ; Dr Wafa Johal 14

Backtracking search

Variable assignments are commutative, i.e.,
[WA = red then NT = green] same as [NT = green then WA = red]

Only need to consider assignments to a single variable at each node
=⇒ b = d and there are dn leaves

Depth-first search for CSPs with single-variable assignments is called
backtracking search

Backtracking search is the basic uninformed algorithm for CSPs

Can solve n-queens for n ≈ 25

AIMA Slides © and ; Dr Wafa Johal 15

Backtracking search: Implementation

function Backtracking-Search(csp) return solution/failure
Recursive-Backtracking({}, csp)

end function

function Recursive-Backtracking(assignment, csp)
if assignment is complete then

return assignment
var← Select-Unassigned-Variable(Variables[csp],assignment, csp)
foreach value ∈ Order-Domain-Values(var, assignment, csp) do
if value is consistent with assignment given Constraints[csp] then

add{var = value} to assignment
result← Recursive-Backtracking(assignment, csp)
if result ̸= failure then

return result
remove{var = value} from assignment

return failure
end function

AIMA Slides © and ; Dr Wafa Johal 16

Backtracking example

AIMA Slides © and ; Dr Wafa Johal 17

Backtracking example

AIMA Slides © and ; Dr Wafa Johal 18

Backtracking example

AIMA Slides © and ; Dr Wafa Johal 19

Backtracking example

AIMA Slides © and ; Dr Wafa Johal 20

Improving backtracking efficiency

General-purpose methods can give huge gains in speed:

1. Which variable should be assigned next?
var← Select-Unassigned-Variable(Variables[csp],assignment, csp)

2. In what order should its values be tried?
Order-Domain-Values(var, assignment, csp)

3. Can we detect inevitable failure early?
4. Can we take advantage of problem structure?

AIMA Slides © and ; Dr Wafa Johal 21

Variable Ordering

Which variable should be assigned next?
var← Select-Unassigned-Variable(Variables[csp],assignment, csp)

Minimum remaining values (MRV)
choose the variable with the fewest legal values

AIMA Slides © and ; Dr Wafa Johal 22

Variable Ordering

Which variable should be assigned next?
var← Select-Unassigned-Variable(Variables[csp],assignment, csp)

Minimum remaining values (MRV)
choose the variable with the fewest legal values

AIMA Slides © and ; Dr Wafa Johal 23

Variable Ordering

Tie-breaker among MRV variables or choice of first variable

Degree heuristic
choose the variable with the most constraints on remaining variables

AIMA Slides © and ; Dr Wafa Johal 24

Value Ordering

In what order should its values be tried?
Order-Domain-Values(var, assignment, csp)

Least constraining value
Given a variable, choose the least constraining value:
the one that rules out the fewest values in the remaining variables

Allows 1 value for SA

Allows 0 values for SA

Combining these heuristics makes 1000 queens feasible
AIMA Slides © and ; Dr Wafa Johal 25

Forward checking

Forward Checking
Keep track of remaining legal values for unassigned variables
Terminate search when any variable has no legal values

WA NT Q NSW V SA T

AIMA Slides © and ; Dr Wafa Johal 26

Forward checking

Forward Checking
Keep track of remaining legal values for unassigned variables
Terminate search when any variable has no legal values

WA NT Q NSW V SA T

AIMA Slides © and ; Dr Wafa Johal 27

Forward checking

Forward Checking
Keep track of remaining legal values for unassigned variables
Terminate search when any variable has no legal values

WA NT Q NSW V SA T

AIMA Slides © and ; Dr Wafa Johal 28

Forward checking

Forward Checking
Keep track of remaining legal values for unassigned variables
Terminate search when any variable has no legal values

WA NT Q NSW V SA T

AIMA Slides © and ; Dr Wafa Johal 29

Constraint propagation

Forward checking propagates information from assigned to unassigned
variables, but doesn’t provide early detection for all failures:

WA NT Q NSW V SA T

NT and SA cannot both be blue!

Constraint propagation repeatedly enforces constraints locally

AIMA Slides © and ; Dr Wafa Johal 30

Arc consistency – Illustration

AIMA Slides © and ; Dr Wafa Johal 31

Arc consistency – Illustration

AIMA Slides © and ; Dr Wafa Johal 32

Arc consistency – Illustration

AIMA Slides © and ; Dr Wafa Johal 33

Arc consistency – Illustration

AIMA Slides © and ; Dr Wafa Johal 34

Arc consistency – Illustration

AIMA Slides © and ; Dr Wafa Johal 35

Arc consistency – Illustration

AIMA Slides © and ; Dr Wafa Johal 36

Arc consistency

Simplest form of propagation makes each arc consistent

X→ Y is arc consistent iff
for every value x of X there is at least one value y of Y
that satisfies the constraint between X and Y

WA NT Q NSW V SA T

AIMA Slides © and ; Dr Wafa Johal 37

Arc consistency

Simplest form of propagation makes each arc consistent

X→ Y is arc consistent iff
for every value x of X there is at least one value y of Y
that satisfies the constraint between X and Y

WA NT Q NSW V SA T

AIMA Slides © and ; Dr Wafa Johal 38

Arc consistency

Simplest form of propagation makes each arc consistent

X→ Y is arc consistent iff
for every value x of X there is at least one value y of Y
that satisfies the constraint between X and Y

WA NT Q NSW V SA T

If X loses a value, neighbors of X need to be rechecked

AIMA Slides © and ; Dr Wafa Johal 39

Arc consistency

Simplest form of propagation makes each arc consistent

X→ Y is arc consistent iff
for every value x of X there is at least one value y of Y
that satisfies the constraint between X and Y

WA NT Q NSW V SA T

If X loses a value, neighbors of X need to be rechecked

Arc consistency detects failure earlier than forward checking

Can be run as a preprocessor or after each assignment
AIMA Slides © and ; Dr Wafa Johal 40

Arc consistency algorithm

function AC-3(csp) returns the CSP, possibly with reduced domains
input csp a binary CSP with variables {X1,X2, . . . ,Xn}

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 Remove-Inconsistent-Values(Xi,Xj) then

foreach Xk in Neighbors(Xi) do
add (Xk,Xi) to queue

end function
——————————–
function Remove-Inconsistent-Values(Xi, Xj) true iff succeeds

removed← false
foreach x in Domain(Xi) do

if no value y Domain(Xi) allows (x, y) to satisfy the constraint Xi↔ Xj then
delete x from Domain(Xi);
removed← true

return removed
end function

O(n2d3), can be reduced to O(n2d2) (but detecting all is NP-hard)

Alternative approach: exploit structure of the constraint graph to find
independent subproblems …
AIMA Slides © and ; Dr Wafa Johal 41

Problem structure

Tasmania and mainland are independent subproblems

Identifiable as connected components of constraint graph

AIMA Slides © and ; Dr Wafa Johal 42

Problem structure contd.

Suppose each subproblem has c variables out of n total

Worst-case solution cost is n/c · dc, linear in n

E.g., n = 80, d = 2, c = 20
280 = 4 billion years at 10 million nodes/sec
4 · 220 = 0.4 seconds at 10 million nodes/sec

Unfortunately, completely independent subproblems are rare in practice

However, there are other graph structures that are easy to solve
e.g. when the constraint graph is a tree

AIMA Slides © and ; Dr Wafa Johal 43

Problem structure contd.

Suppose each subproblem has c variables out of n total

Worst-case solution cost is n/c · dc, linear in n

E.g., n = 80, d = 2, c = 20
280 = 4 billion years at 10 million nodes/sec
4 · 220 = 0.4 seconds at 10 million nodes/sec

Unfortunately, completely independent subproblems are rare in practice

However, there are other graph structures that are easy to solve
e.g. when the constraint graph is a tree

AIMA Slides © and ; Dr Wafa Johal 44

Tree-structured CSPs

Theorem: if the constraint graph has no loops, the CSP can be solved in
O(n d2) time

Compare to general CSPs, where worst-case time is O(dn)

This property also applies to logical and probabilistic reasoning:
an important example of the relation between syntactic restrictions
and the complexity of reasoning.

AIMA Slides © and ; Dr Wafa Johal 45

Algorithm for tree-structured CSPs

1. Choose a variable as root, order variables from root to leaves
such that every node’s parent precedes it in the ordering

A B C D E F

2. For j from n down to 2, apply MakeArcConsistent(Parent(Xj),Xj)

3. For j from 1 to n, assign Xj consistently with Parent(Xj)

AIMA Slides © and ; Dr Wafa Johal 46

Nearly tree-structured CSPs

Conditioning: instantiate a variable, prune its neighbors’ domains

Cutset conditioning: instantiate (in all ways) a set of variables
such that the remaining constraint graph is a tree

Cutset: set of variables that can be deleted so constraint graph forms a tree

Cutset size c =⇒ runtime O(dc · (n− c)d2), very fast for small c

AIMA Slides © and ; Dr Wafa Johal 47

Iterative algorithms for CSPs – Local search

Recall hill-climbing search from Week 3

Hill-climbing typically works with
“complete” states, i.e., all variables assigned

Local search then tries to change one variable assignment at a time

To apply to CSPs:
allow states with unsatisfied constraints(variable selection)
operators reassign variable values (value selection)

Variable selection: randomly select any conflicted variable

Value selection by min-conflicts heuristic:
choose value that violates the fewest constraints
i.e., hillclimb with h(n) = total number of violated constraints

AIMA Slides © and ; Dr Wafa Johal 48

Example: 4-Queens

States: 4 queens in 4 columns (44 = 256 states)

Operators: move queen in column

Goal test: no attacks

Evaluation: h(n) = number of attacks

h = 5 h = 2 h = 0

AIMA Slides © and ; Dr Wafa Johal 49

Performance of min-conflicts

Given random initial state, can solve n-queens in almost constant time for
arbitrary n with high probability (e.g., n = 10,000,000)

The same appears to be true for any randomly-generated CSP
except in a narrow range of the ratio

R = number of constraints
number of variables

AIMA Slides © and ; Dr Wafa Johal 50

CSPs are a special kind of problem:
states defined by values of a fixed set of variables
goal test defined by constraints on variable values

Backtracking = depth-first search with one variable assigned per node

Variable ordering and value selection heuristics help significantly

Forward checking prevents assignments that guarantee later failure

Constraint propagation (e.g., arc consistency) does additional work
to constrain values and detect inconsistencies

The CSP representation allows analysis of problem structure

Tree-structured CSPs can be solved in linear time

Iterative min-conflicts is usually effective in practice

AIMA Slides © and ; Dr Wafa Johal 51

Examples of skills expected:

• Model a given problem as a CSP
• Demonstrate operation of CSP search algorithms
• Discuss and evaluate the properties of different constraint satisfaction

techniques

AIMA Slides © and ; Dr Wafa Johal 52

Introduction

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com