0% found this document useful (0 votes)
25 views71 pages

Lec4 CSP

The document discusses Constraint Satisfaction Problems (CSPs), highlighting their structure, representation, and various algorithms for solving them, such as backtracking search and the AC-3 algorithm. It explains the importance of state representation and the role of constraints in defining the problem space. Additionally, it covers techniques for improving search efficiency, including variable ordering and constraint propagation methods.

Uploaded by

春苗 陈
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)
25 views71 pages

Lec4 CSP

The document discusses Constraint Satisfaction Problems (CSPs), highlighting their structure, representation, and various algorithms for solving them, such as backtracking search and the AC-3 algorithm. It explains the importance of state representation and the role of constraints in defining the problem space. Additionally, it covers techniques for improving search efficiency, including variable ordering and constraint propagation methods.

Uploaded by

春苗 陈
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/ 71

Constraint Satisfaction Problems

In which we see how treating states as more than just little black
boxes leads to the invention of a range of powerful new search
methods and a deeper understanding of problem structure and
complexity.

1
Outline
• Constraint Satisfaction Problems (CSP)
• Backtracking search for CSPs
• AC-3 algorithm
• Local search for CSPs

2
What is Search For?
• Planning: sequences of actions
– The path to the goal is the important thing
– Paths have various costs, depths
– Heuristics give problem-specific guidance
• Identification: assignments to variables
– The goal itself is important, not the path
– All paths at the same depth (for some formulations)
• CSP is a special class of search problems
– Mostly identification problems
– Have specialized algorithms for them
3
State Representation

B C

B C
(a) Atomic (b) Factored (c) Structured

a) Atomic representation: a state is a black box with no internal structure;


b) Factored representation: a state consists of a vector of attribute values;
values can be Boolean, real-valued, or one of a fixed set of symbols.
c) Structured representation: a state includes objects, each of which may
have attributes of its own as well as relationships to other objects.

4
Constraint Satisfaction Problems (CSPs)
• Standard search problem:
– state is a “black box” – any data structure that supports successor function,
heuristic function, and goal test

• CSP:
– state is defined by variables Xi (i=1,…,n) with values from domain Di
– goal test is a set of constraints Ci (i=1,…,m) 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

5
Example: Map-Coloring

• Variables WA, NT, Q, NSW, V, SA, T

• Domains Di = {red, green, blue}

• Constraints: adjacent regions must have different colors


– e.g., WA ≠ NT, or (WA,NT) in {(red ,green), (red, blue), (green, red), (green, blue), (blue,
red), (blue, green)}

6
Example: Map-Coloring

Solutions are complete and consistent assignments, e.g.,


WA = red, NT = green,Q = red,NSW = green,V = red,SA
= blue,T = green

7
Constraint graph
• Binary CSP: each constraint relates two variables

• Constraint graph: nodes are variables, arcs are constraints

8
CSP: Incremental Formulation
• Initial state: the empty assignment {}, in which all variables are
unassigned.
• Successor function: a value can be assigned to any unassigned
variable, provided that it does not conflict with previously
assigned variables.
• Goal test: the current assignment is complete.
• Path cost: a constant cost (e.g., 1) for every step.

9
Example: N-Queens
• Formulation 1:
– Variables:
– Domains:
– Constraints

10
Example: N-Queens

• Formulation 2:
– Variables:

– Domains:

– Constraints: Implicit:
Explicit:

11
Example: Cryptarithmetic

• Variables: F T U W R O X1 X2 X3

• Domains: {0,1,2,3,4,5,6,7,8,9}

• Constraints: Alldiff (F,T,U,W,R,O)


– O + O = R + 10 ·X1
– X1 + W + W = U + 10 ·X2
– X2 + T + T = O + 10 ·X3
– X3 = F, T ≠ 0, F ≠ 0

12
Example: Sudoku

• Variables: Each (open)


square
• Domains: {1,2,…,9}
• Constraints:
9-way alldiff for each column
9-way alldiff for each row
9-way alldiff for each region
(or can have a bunch
of pairwise inequality
constraints)

13
Varieties of CSPs
• Discrete variables
– finite domains:
• n variables, domain size d → O(dn) complete assignments
– 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

• Continuous variables
– e.g., start/end times for Hubble Space Telescope observations
– linear constraints solvable in polynomial time by linear
programming

14
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

15
Real-world CSPs
• Assignment problems
– e.g., who teaches what class

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

• Transportation scheduling

• Factory scheduling

• Notice that many real-world problems involve real-


valued variables

16
Standard search formulation (incremental)

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
• 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. Path is irrelevant, so can also use complete-state formulation
4. b = (n - l )d at depth l, hence n! ·dn leaves
17
Backtracking search
• Depth-first search for CSPs with single-variable assignments is
called backtracking search

• Backtracking search is the basic uninformed algorithm for


CSPs

• Only need to consider assignments to a single variable at each


node ( b = d )
– There are n! ·dn leaves, dn possible complete assignments

18
Backtracking Search
• Backtracking search = DFS + two improvements

• Idea 1: One variable at a time


– Variable assignments are commutative
• [WA = red then NT = green] same as [NT = green then
WA = red]
– Only need to consider assign value to a single variable at each
step

• Idea 2: Check constraints as you go


– Consider only values which do not conflict previous
assignments
– May need some computation to check the constraints
– “Incremental goal test”

19
Backtracking Search

https://inst.eecs.berkeley.edu/~cs188/fa19/assets/demos/csp/csp_demos.html

20
Backtracking Search

No need to check consistency for a complete assignment

21
Backtracking Search

Checks consistency at each assignment

22
Backtracking Search

• Backtracking = DFS + variable-ordering + fail-on-violation

23
Backtracking example

24
Backtracking example

25
Backtracking example

26
Backtracking example

27
Improving backtracking efficiency
• Consider the order of variables to be assigned
and their values to be tried
– Which variable should be assigned next?
– In what order should its values be tried?
– Can we detect inevitable failure early?

• Interleaving search and inference

28
Which variable should be
assigned next?

29
Which variable should be assigned next?
• Most constrained variable:
choose the variable with the fewest legal values
– a.k.a. minimum remaining values (MRV) heuristic
• Most constraining variable (Tie-breaker):
– choose the variable with the most constraints on
remaining variables
– the degree heuristic

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

31
Interleaving search and inference
• Inference in CSPs
• In regular state-space search, algorithms only
do search. In CSPs, algorithms can search and
inference
– Search: choose a new variable assignment from
several possibilities
– Inference (a specific type): constraint propagation
• Constraint propagation may be intertwined
with search, or it may be done as a
preprocessing step, before search starts.
32
Forward checking
(Interleaving search and inference)
• Idea:
– Keep track of remaining legal values for unassigned
variables
– Terminate search when any variable has no legal values

NT
Q
WA
SA
NSW

V
T

33
Forward checking
• Idea:
– Keep track of remaining legal values for unassigned
variables
– Terminate search when any variable has no legal values

34
Forward checking
• Idea:
– Keep track of remaining legal values for unassigned
variables
– Terminate search when any variable has no legal values

35
Forward checking
• Idea:
– Keep track of remaining legal values for unassigned
variables
– Terminate search when any variable has no legal values

36
Constraint propagation
• Forward checking propagates information from assigned to
unassigned variables, but doesn't provide early detection for all
failures:

• NT and SA cannot both be blue!


• Constraint propagation repeatedly enforces constraints locally

37
Limitation of forward checking

• Although forward checking detects many


inconsistencies, it does not detect all of them.
– The problem is that it makes the current variable
arc-consistent, but doesn’t look ahead and make all
the other variables arc-consistent.

38
Arc consistency
• Simplest form of propagation: making each arc consistent
• X →Y is consistent iff
for every value x of X there is some allowed y

39
Consistency of A Single Arc

(Remove values from the tail!)

Recall: Binary constraint graph for a binary CSP (i.e., each


constraint has most two variables): nodes are variables, edges
show constraints

40
Consistency of A Single Arc

41
Arc consistency
• Simplest form of propagation: making each arc consistent
• X →Y is consistent iff
for every value x of X there is some allowed y

NSW → SA

42
Arc consistency
• Simplest form of propagation: making each arc consistent
• X →Y is consistent iff
for every value x of X there is some allowed y

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

V → NSW

43
Arc consistency
• Simplest form of propagation: making each arc consistent
• X →Y is consistent iff
for every value x of X there is some allowed y
• 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

SA → NT

44
How to Enforce Arc Consistency
of Entire CSP
• A simplistic algorithm: Cycle over the pairs of variables,
enforcing arc-consistency, repeating the cycle until no domains
change for a whole cycle
• AC-3 (short for Arc Consistency Algorithm #3): A more efficient
algorithm ignoring constraints that have not been modified since
they were last analyzed

NT
Q
WA
SA
NSW

V
T

45
AC-3: Enforce Arc Consistency
of Entire CSP

Constraint Propagation!

46
AC-3: Enforce Arc Consistency
of Entire CSP

Queue:
NT
Q
SA->WA
WA
SA
NT->WA
NSW

V
T

Remember: Delete from the tail!

47
AC-3: Enforce Arc Consistency of
Entire CSP

Queue:
NT
Q
NT->WA
WA
SA
WA->SA
NSW NT->SA
V Q->SA
T
NSW->SA
V->SA

Remember: Delete from the tail!

48
AC-3: Enforce Arc Consistency of
Entire CSP

Queue:
NT
Q
WA->SA
WA
SA
NT->SA
NSW Q->SA
V NSW->SA
T
V->SA
WA->NT
SA->NT
Q->NT

Remember: Delete from the tail!

49
AC-3: Enforce Arc Consistency of
Entire CSP

Queue:
NT
Q
WA->SA
WA
SA
NT->SA
NSW Q->SA
V NSW->SA
T
V->SA
WA->NT
SA->NT
Q->NT

Remember: Delete from the tail!

50
AC-3: Enforce Arc Consistency of
Entire CSP

Queue:
NT
Q
NT->SA
WA
SA
Q->SA
NSW NSW->SA
V V->SA
T
WA->NT
SA->NT
Q->NT

Remember: Delete from the tail!

51
AC-3: Enforce Arc Consistency of
Entire CSP

Queue:
NT
Q
Q->SA
WA
SA
NSW->SA
NSW V->SA
V WA->NT
T
SA->NT
Q->NT

Remember: Delete from the tail!

52
AC-3: Enforce Arc Consistency of
Entire CSP

Queue:
NT
Q
NSW->SA
WA
SA
V->SA
NSW WA->NT
V SA->NT
T
Q->NT

Remember: Delete from the tail!

53
AC-3: Enforce Arc Consistency of
Entire CSP

Queue:
NT
Q
WA
SA
NSW

V
T

Remember: Delete from the tail!

54
After assigning Q to Green,
what gets added to the Queue?

Queue:

NT
Q
WA
SA
NSW

V
T

A: NSW->Q, SA->Q, NT->Q


B: Q->NSW, Q->SA, Q->NT

55
AC-3: Enforce Arc Consistency
of Entire CSP

Queue:
NT
Q
NT->Q
WA
SA
SA->Q
NSW NSW->Q
V
T

Remember: Delete from the tail!

56
AC-3: Enforce Arc Consistency
of Entire CSP

Queue:
NT
Q
SA->Q
WA
SA
NSW->Q
NSW WA->NT
V SA->NT
T
Q->NT

Remember: Delete from the tail!

57
AC-3: Enforce Arc Consistency
of Entire CSP

Queue:
NT
Q
NSW->Q
WA
SA
WA->NT
NSW SA->NT
V Q->NT
T
WA->SA
NT->SA
Q->SA
NSW->SA
V->SA

Remember: Delete from the tail!

58
AC-3: Enforce Arc Consistency
of Entire CSP
Queue:
WA->NT
NT
SA->NT
WA
Q Q->NT
SA
NSW WA->SA
V NT->SA
T Q->SA
NSW->SA
V->SA
V->NSW
Q->NSW
SA->NSW

Remember: Delete from the tail!

59
AC-3: Enforce Arc Consistency
of Entire CSP
Queue:
WA->NT
NT
SA->NT
WA
Q Q->NT
SA
NSW WA->SA
V NT->SA
T Q->SA
NSW->SA
V->SA
V->NSW
Q->NSW
SA->NSW

Remember: Delete from the tail!

60
AC-3: Enforce Arc Consistency
of Entire CSP

Queue:
SA->NT
NT
Q->NT
WA
Q WA->SA
SA
NSW NT->SA
V Q->SA
NSW->SA
!!!
T
V->SA
V->NSW
Q->NSW
SA->NSW

Remember: Delete from the tail!

61
AC-3: Enforce Arc Consistency
of Entire CSP
Queue:
SA->NT
Q->NT
NT
Q
WA->SA
WA
SA
NT->SA
NSW
Q->SA
V
NSW->SA
V->SA
!!!
T

V->NSW
Q->NSW
SA->NSW
• Backtrack on the assignment of Q
• Arc consistency detects failure earlier than forward checking
• Can be run as a preprocessor or after each assignment
• What’s the downside of enforcing arc consistency?

62
Limitations of Arc Consistency
• After enforcing arc consistency:
– Can have one solution left
– Can have multiple solutions left
– Can have no solutions left (and
not know it)
• Arc consistency only checks local
consistency conditions
• Arc consistency still runs inside a What went
wrong here?
backtracking search!

63
Backtracking Search with AC-3

• Where do you run AC-3?


64
Complexity of a single run of AC-3

Recall that the whole backtracking algorithm with AC-3 will call AC-3 many times

65
Complexity of a single run of AC-3

• An arc is added after a removal


of value at a node
• 𝑛 node in total, each has ≤ 𝑑
values
• Total times of removal: 𝑂 𝑛𝑑

66
Complexity of a single run of AC-3

• An arc is added after a removal


of value at a node
• 𝑛 node in total, each has ≤ 𝑑
values
• Total times of removal: 𝑂 𝑛𝑑
• After a removal, ≤ 𝑛 arcs added
• Total times of adding arcs:
𝑂(𝑛2 𝑑)

67
Complexity of a single run of AC-3

• An arc is added after a removal


of value at a node
• 𝑛 node in total, each has ≤ 𝑑
values
• Total times of removal: 𝑂 𝑛𝑑
• After a removal, ≤ 𝑛 arcs added
• Total times of adding arcs: 𝑂(𝑛2 𝑑)

• Check arc consistency per arc:


𝑂(𝑑 2 )

Complexity of a single run of AC-3 is at most 𝑂(𝑛2 𝑑 3 )

68
Local search for CSPs
• Hill-climbing, simulated annealing typically work
with "complete" states, i.e., all variables assigned
• To apply to CSPs:
– allow states with unsatisfied constraints
– operators reassign variable values
• Variable selection: randomly select any conflicted
variable
• Value selection by min-conflicts heuristic:
– choose value that violates the fewest constraints
– i.e., hill-climb with h(n) = total number of violated
constraints
69
Example: 4-Queens
• States: 4 queens in 4 columns (44 = 256 states)
• Actions: move queen in column
• Goal test: no attacks
• Evaluation: h(n) = number of attacks

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

70
Summary
• 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
• Iterative min-conflicts is usually effective in practice

Credit:
15-281, Artificial Intelligence: Representation and Problem Solving, CMU

71

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy