Constraint Satisfaction Problems Chapter 5 Ch. 05 p.1/44 Outline - - PowerPoint PPT Presentation

constraint satisfaction problems
SMART_READER_LITE
LIVE PREVIEW

Constraint Satisfaction Problems Chapter 5 Ch. 05 p.1/44 Outline - - PowerPoint PPT Presentation

Constraint Satisfaction Problems Chapter 5 Ch. 05 p.1/44 Outline CSP examples Backtracking search for CSPs Problem structure and problem decomposition Local search for CSPs Ch. 05 p.2/44 Constraint satisfaction problems


slide-1
SLIDE 1

Constraint Satisfaction Problems

Chapter 5

  • Ch. 05 – p.1/44
slide-2
SLIDE 2

Outline

CSP examples Backtracking search for CSPs Problem structure and problem decomposition Local search for CSPs

  • Ch. 05 – p.2/44
slide-3
SLIDE 3

Constraint satisfaction problems (CSPs)

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

✂✁

with values from domain

✄ ✁

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

  • Ch. 05 – p.3/44
slide-4
SLIDE 4

Example: Map-Coloring

Queensland Western Australia Northern Territory South Australia New South Wales Victoria Tasmania

Variables

,

✂ ✄

,

,

✂ ✆
  • ,

,

✆ ✁

,

Domains

✞✠✟ ✡ ☛✌☞✍ ✎✑✏ ✒ ☞ ✍ ✍ ✓ ✏ ✔✕✗✖ ✍ ✘

Constraints: adjacent regions must have different colors e.g.,

  • ✁✙
✡ ✂ ✄

(if the language allows this), or

✏ ✂ ✄ ✛✢✜ ☛ ✚ ☞✍ ✎✑✏ ✒ ☞ ✍ ✍ ✓ ✛ ✏ ✚ ☞✍ ✎✑✏ ✔✕✗✖ ✍ ✛ ✏ ✚ ✒ ☞✍ ✍ ✓ ✏ ☞ ✍ ✎ ✛ ✏ ✚ ✒ ☞✍ ✍ ✓ ✏ ✔✕ ✖ ✍ ✛ ✏✣ ✣ ✣ ✘
  • Ch. 05 – p.4/44
slide-5
SLIDE 5

Example: Map-Coloring (cont’d)

Queensland Western Australia Northern Territory South Australia New South Wales Victoria Tasmania

Solutions are assignments satisfying all constraints, e.g.,

✂ ✄☎ ✆✞✝ ✟ ✠ ✂ ✡ ✄☎ ☎ ☛ ✝ ☞ ✂ ✄ ☎ ✆✞✝ ✟ ✌ ✂ ✡ ✄☎ ☎ ☛ ✝ ✍ ✂ ✄☎ ✆✞✝ ✌ ✁ ✂ ✎✏✒✑ ☎ ✝ ✠ ✂ ✡ ✄☎ ☎ ☛ ✓
  • Ch. 05 – p.5/44
slide-6
SLIDE 6

Constraint graph

Binary CSP: each constraint relates at most two variables Constraint graph: nodes are variables, arcs show constraints WA NT SA Q

NSW

V T General-purpose CSP algorithms use the graph structure to speed up search. E.g., Tasmania is an independent subproblem!

  • Ch. 05 – p.6/44
slide-7
SLIDE 7

Varieties of CSPs

Discrete variables finite domains; size

✆ ✂
✂ ✆ ✄ ☎

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

✌ ✆✞✝ ✄ ✆ ✟✡✠ ✎☞☛ ✌ ✍ ✎ ✌ ✆ ✝ ✄ ✆ ✟ ✠ ✎☞✏

linear constraints solvable, nonlinear undecidable Continuous variables e.g., start/end times for Hubble Telescope

  • bservations
  • Ch. 05 – p.7/44
slide-8
SLIDE 8

Varieties of constraints

Unary constraints involve a single variable, e.g.,

✌ ✁
✡ ✄☎ ☎ ☛

Binary constraints involve pairs of variables, e.g.,

✌ ✁

Higher-order constraints involve 3 or more variables, e.g., cryptarithmetic column constraints Preferences (soft constraints), e.g.,

✄ ☎ ✆

is better than

✡ ✄ ☎ ☎ ☛
  • ften representable by a cost for each variable

assignment

constrained optimization problems

  • Ch. 05 – p.8/44
slide-9
SLIDE 9

Example: Cryptarithmetic

(a)

O W T F U R

(b)

+ F T T O W W U O O R X3 X1 X2

Variables:

✁ ✂ ✁
☎✄

Domains:

✝ ✝ ✝ ✞ ✝ ✟ ✝ ✠ ✝ ✍ ✝ ✡ ✝ ☛ ✝ ☞ ✝ ✌ ✓

Constraints alldiff

✠ ✝ ✁ ✝ ✝ ✂ ✝ ✁ ☎ ✁ ✌ ✁ ✂ ✂ ✌ ✝ ✆ ✍

, etc.

  • Ch. 05 – p.9/44
slide-10
SLIDE 10

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 Notice that many real-world problems involve real-valued variables

  • Ch. 05 – p.10/44
slide-11
SLIDE 11

Standard search formulation (incremental)

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

  • Ch. 05 – p.11/44
slide-12
SLIDE 12

Standard search formulation (incremental)

This is the same for all CSPs! Every solution appears at depth

with

variables

  • use depth-first search

Path is irrelevant, so can also use complete-state formulation

✎ ✂ ✂ ☛
☎ ✆

at depth

, hence

☛ ✂ ✆ ✄

leaves!!!!

  • Ch. 05 – p.12/44
slide-13
SLIDE 13

Backtracking search

Variable assignments are commutative, i.e., [

✁ ✂ ✄ ☎ ✆

then

✟ ✠ ✂ ✡ ✄ ☎ ☎ ☛

] same as [

✟ ✠ ✂ ✡ ✄ ☎ ☎ ☛

then

✁ ✂ ✄ ☎ ✆

] Only need to consider assignments to a single variable at each node

✂ ✆

and there are

✆ ✄

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

  • queens for
  • Ch. 05 – p.13/44
slide-14
SLIDE 14

Backtracking search

function BACKTRACKING-SEARCH (csp) returns a solution, or failure return RECURSIVE-BACKTRACKING({ }, csp) function RECURSIVE-BACKTRACKING (assignment, csp) returns a solution, or failure if assignment is complete then return assignment var

  • SELECT-UNASSIGNED-VAR(VARIABLES[csp], assignment,csp)

for each value in ORDER-DOMAIN-VARS(var, assignment, csp) do add { var = value } to assignment result

  • RECURSIVE-BACKTRACKING (assignment, csp)

if result

✙ ✡

failure then return result remove { var = value } from assignment return failure

  • Ch. 05 – p.14/44
slide-15
SLIDE 15

Backtracking example

  • Ch. 05 – p.15/44
slide-16
SLIDE 16

Backtracking example

  • Ch. 05 – p.16/44
slide-17
SLIDE 17

Backtracking example

  • Ch. 05 – p.17/44
slide-18
SLIDE 18

Backtracking example

  • Ch. 05 – p.18/44
slide-19
SLIDE 19

Improving backtracking efficiency

General-purpose methods can give huge gains in speed:

  • 1. Which variable should be assigned next?
  • 2. In what order should its values be tried?
  • 3. Can we detect inevitable failure early?
  • 4. Can we take advantage of problem structure?
  • Ch. 05 – p.19/44
slide-20
SLIDE 20

Most constrained variable

Most constrained variable: choose the variable with the fewest legal values

  • Ch. 05 – p.20/44
slide-21
SLIDE 21

Most constraining variable

Tie-breaker among most constrained variables Most constraining variable: choose the variable with the most constraints on remaining variables

  • Ch. 05 – p.21/44
slide-22
SLIDE 22

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 value for SA

Combining these heuristics makes 1000 queens feasible

  • Ch. 05 – p.22/44
slide-23
SLIDE 23

Forward checking

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

WA NT Q NSW V SA T

  • Ch. 05 – p.23/44
slide-24
SLIDE 24

Forward checking

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

WA NT Q NSW V SA T

  • Ch. 05 – p.24/44
slide-25
SLIDE 25

Forward checking

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

WA NT Q NSW V SA T

  • Ch. 05 – p.25/44
slide-26
SLIDE 26

Forward checking

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

WA NT Q NSW V SA T

  • Ch. 05 – p.26/44
slide-27
SLIDE 27

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

✟ ✠

and

✌ ✁

cannot both be blue! Constraint propagation repeatedly enforces constraints locally

  • Ch. 05 – p.27/44
slide-28
SLIDE 28

Arc consistency

Simplest form of propagation makes each arc consistent

  • is consistent iff

for every value

  • f
  • there is some allowed

WA NT Q NSW V SA T

  • Ch. 05 – p.28/44
slide-29
SLIDE 29

Arc consistency

Simplest form of propagation makes each arc consistent

  • is consistent iff

for every value

  • f
  • there is some allowed

WA NT Q NSW V SA T

  • Ch. 05 – p.29/44
slide-30
SLIDE 30

Arc consistency

Simplest form of propagation makes each arc consistent

  • is consistent iff

for every value

  • f
  • there is some allowed

WA NT Q NSW V SA T

If

  • loses a value, neighbors of
  • need to be rechecked
  • Ch. 05 – p.30/44
slide-31
SLIDE 31

Arc consistency

Simplest form of propagation makes each arc consistent

  • is consistent iff

for every value

  • f
  • there is some allowed

WA NT Q NSW V SA T

If

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

Arc consistency detects failure earlier than forward checking Can be run as a preprocessor or after each assignment

  • Ch. 05 – p.31/44
slide-32
SLIDE 32

Arc consistency algorithm

function AC-3 (csp) returns the CSP , with possibly reduced domains inputs: csp, a binary CSP with variables {

✂✁ ✏ ✂✄ ✏✣ ✣ ✣ ✏ ✆☎

} local variables: queue, a queue of arcs, initally all the arcs in csp while queue is not empty do (

✏ ✞✝

)

  • REMOVEFIRST(queue)

if REMOVE-INCONSISTENT-VALUES(

✏ ✞✝

) then for each

✠✟

in NEIGHBORS[

] do add (

✡✟ ✏

) to queue

  • Ch. 05 – p.32/44
slide-33
SLIDE 33

Arc consistency algorithm

function REMOVE-INCONSISTENT-VALUES (

✏ ✞✝

) returns true iff we remove a value removed

  • false

for each x in DOMAIN[

] do if no value in DOMAIN[

] allows (

) to satisfy the constraint between

and

✞✝

then delete

  • from DOMAIN[

]; then removed

  • true

return removed

✂ ✚ ✓ ✄ ✎ ✄ ✛

, can be reduced to

✂ ✚ ✓ ✄ ✎ ✄ ✛

but cannot detect all failures in poly time!

  • Ch. 05 – p.33/44
slide-34
SLIDE 34

Problem structure

WA NT SA Q

NSW

V T

Tasmania and mainland are independent subproblems Identifiable as connected components of constraint graph

  • Ch. 05 – p.34/44
slide-35
SLIDE 35

Problem structure contd.

Suppose each subproblem has

  • variables out of

total Worst-case solution cost is

☛ ✁
✆ ✂

, linear in

E.g.,

☛ ✂ ☞ ✆

,

✆ ✂ ✞

,

✞ ✆ ✞ ✄☎

= 4 billion years at 10 million nodes/sec

✠ ✍ ✞ ✄ ☎

= 0.4 seconds at 10 million nodes/sec

  • Ch. 05 – p.35/44
slide-36
SLIDE 36

Tree-structured CSPs

A B C D E F

Theorem: if the constraint graph has no loops, the CSP can be solved in

✁ ✂ ☛ ✆ ✄ ☎

time Compare to general CSPs, where worst-case time is

✁ ✂ ✆ ✄ ☎

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

  • Ch. 05 – p.36/44
slide-37
SLIDE 37

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

  • rdering

A A B C D E F B C D E F

  • 2. For
  • from

down to 2, apply REMOVEINCONSISTENT

✂ ✁ ✝ ✄ ☎ ☛ ✆ ✂ ✄✂ ☎ ✝
  • 3. For
  • from 1 to

, assign

✄✂

consistently with

✁ ✝ ✄☎ ☛ ✆ ✂
  • Ch. 05 – p.37/44
slide-38
SLIDE 38

Nearly tree-structured CSPs

Conditioning: instantiate a variable, prune its neighbors’ domains

NT NSW WA Q SA V T NT NSW WA Q V T

  • Ch. 05 – p.38/44
slide-39
SLIDE 39

Nearly tree-structured CSPs

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

  • runtime
✁ ✂ ✆ ✂ ✍ ✂ ☛
✆ ✄ ☎

, very fast for small

  • Ch. 05 – p.39/44
slide-40
SLIDE 40

Iterative algorithms 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

  • perators 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., hillclimb with

☛ ☎

= total number of violated constraints

  • Ch. 05 – p.40/44
slide-41
SLIDE 41

Example: 4-Queens

States: 4 queens in 4 columns (

✞ ✍ ✡

states) Operators: move queen in column Goal test: no attacks Evaluation:

☛ ☎

= number of attacks h=0 h=2 h=5

  • Ch. 05 – p.41/44
slide-42
SLIDE 42

Performance of min-conflicts

Given random initial state, can solve

  • queens in almost

constant time for arbitrary

with high probability (e.g.,

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

✂ ✂

number of constraints number of variables

critical ratio CPU time R

  • Ch. 05 – p.42/44
slide-43
SLIDE 43

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

  • Ch. 05 – p.43/44
slide-44
SLIDE 44

Summary

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

  • Ch. 05 – p.44/44