Constraint Satisfaction Problems - Sharif

103
Soleymani Constraint Satisfaction Problems CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring 2019 โ€œArtificial Intelligence: A Modern Approachโ€, 3 rd Edition, Chapter 6 Some slides have been adopted from Klein and Abdeel, CS188, UC Berkeley.

Transcript of Constraint Satisfaction Problems - Sharif

Page 1: Constraint Satisfaction Problems - Sharif

Soleymani

Constraint Satisfaction ProblemsCE417: Introduction to Artificial IntelligenceSharif University of TechnologySpring 2019

โ€œArtificial Intelligence: A Modern Approachโ€, 3rd Edition, Chapter 6Some slides have been adopted from Klein and Abdeel, CS188, UC Berkeley.

Page 2: Constraint Satisfaction Problems - Sharif

Constraint Satisfaction Problems

2

Page 3: Constraint Satisfaction Problems - Sharif

Outline} Constraint Satisfaction Problems (CSP)

} Representation for wide variety of problems} CSP solvers can be faster than general state-space searchers

} Backtracking search for CSPs} Inference in CSPs} Problem Structure} Local search for CSPs

3

Page 4: Constraint Satisfaction Problems - Sharif

What is CSPs?} In CSPs, the problem is to search for a set of values for

the variables (features) so that the assigned values satisfyconstraints.

} CSPs yield a natural representation for a widevariety of problems} CSP search algorithms use general-purpose heuristics

based on the structure of states

4

Page 5: Constraint Satisfaction Problems - Sharif

What is CSPs?} Components of a CSP

} ๐‘‹ is a set of variables {๐‘‹1, ๐‘‹2, โ€ฆ , ๐‘‹๐‘›}} ๐ท is the set of domains {๐ท1, ๐ท2, โ€ฆ , ๐ท๐‘›} where ๐ท๐‘– is the domain of ๐‘‹๐‘–} ๐ถ is a set of constraints {๐ถ1, ๐ถ2, โ€ฆ , ๐ถ๐‘š}

} Each constraint limits the values that variables can take (e.g.,๐‘‹- โ‰  ๐‘‹2)

} Solving a CSP

} State:An assignment of values to some or all of the variables

} Solution (goal):A complete and consistent assignment

} Consistent:An assignment that does not violate any constraint

} Complete:All variables are assigned.

5

Page 6: Constraint Satisfaction Problems - Sharif

CSP Examples

6

Page 7: Constraint Satisfaction Problems - Sharif

CSP: Map coloring example} Coloring regions with tree colors such that no neighboring

regions have the same color} Variables corresponding to regions:๐‘‹ = {๐‘Š๐ด,๐‘๐‘‡, ๐‘„, ๐‘๐‘†๐‘Š, ๐‘‰, ๐‘†๐ด, ๐‘‡}} The domain of all variables is {๐‘Ÿ๐‘’๐‘‘, ๐‘”๐‘Ÿ๐‘’๐‘’๐‘›, ๐‘๐‘™๐‘ข๐‘’}} Constraints: ๐ถ = {๐‘†๐ด โ‰  ๐‘Š๐ด, ๐‘†๐ด โ‰  ๐‘๐‘‡, ๐‘†๐ด โ‰  ๐‘„, ๐‘†๐ด โ‰  ๐‘๐‘†๐‘Š, ๐‘† โ‰  ๐‘‰,

๐‘Š๐ด โ‰  ๐‘๐‘‡,๐‘๐‘‡ โ‰  ๐‘„, ๐‘„ โ‰  ๐‘๐‘†๐‘Š,๐‘๐‘†๐‘Š โ‰  ๐‘‰}

} A solution:{๐‘Š๐ด = ๐‘Ÿ๐‘’๐‘‘, ๐‘๐‘‡ = ๐‘”๐‘Ÿ๐‘’๐‘’๐‘›, ๐‘„ = ๐‘Ÿ๐‘’๐‘‘,๐‘๐‘†๐‘Š = ๐‘”๐‘Ÿ๐‘’๐‘’๐‘›, ๐‘‰ = ๐‘Ÿ๐‘’๐‘‘, ๐‘‡ = ๐‘”๐‘Ÿ๐‘’๐‘’๐‘›}

7

Page 8: Constraint Satisfaction Problems - Sharif

Example: N-Queens} Variables: {๐‘„1, ๐‘„2, โ€ฆ ,

๐‘„?}} Domains: {1,2, โ€ฆ , ๐‘}} Constraints:

} Implicit:โˆ€๐‘–, ๐‘— โ‰  ๐‘–๐‘›๐‘œ๐‘›_๐‘กโ„Ž๐‘Ÿ๐‘’๐‘Ž๐‘ก๐‘’๐‘›๐‘–๐‘›๐‘”(๐‘„H, ๐‘„I)} Explicit: ๐‘„H, ๐‘„I โˆˆ { 1,3 , 1,4 , โ€ฆ , (8,6)}

8

Page 9: Constraint Satisfaction Problems - Sharif

Boolean satisfiability example} Given a Boolean expression, is it satisfiable?

} ๐‘’. ๐‘”. , ((๐‘-โˆง ๐‘S) โ†’ ๐‘U) โˆจ (ยฌ๐‘-โˆง ๐‘U)} Representing Boolean expression in 3-CNF} 3-SAT: find a satisfying truth assignment for 3-CNF

} Variables: ๐‘-,๐‘S, โ€ฆ , ๐‘X} Domains: {๐‘ก๐‘Ÿ๐‘ข๐‘’, ๐‘“๐‘Ž๐‘™๐‘ ๐‘’}} Constraints: all clauses must be satisfied

9

Page 10: Constraint Satisfaction Problems - Sharif

Sudoku example} Variables: Each (open) square} Domains: {1,2, โ€ฆ , 9}} Constraints:

} 9-way ๐‘Ž๐‘™๐‘™๐‘‘๐‘–๐‘“๐‘“ for each row} 9-way ๐‘Ž๐‘™๐‘™๐‘‘๐‘–๐‘“๐‘“ for each column} 9-way ๐‘Ž๐‘™๐‘™๐‘‘๐‘–๐‘“๐‘“ for each region

10

Page 11: Constraint Satisfaction Problems - Sharif

Varieties of CSPs and Constraints

11

Page 12: Constraint Satisfaction Problems - Sharif

Varieties of CSPs} DiscreteVariables

} Finite domains} Size d means O(dn) complete assignments} E.g., Boolean CSPs, including Boolean satisfiability (NP-complete)

} Infinite domains (integers, strings, etc.)} E.g., job scheduling, variables are start/end times for each job} Linear constraints solvable, nonlinear undecidable

} Continuous variables} E.g., start/end times for Hubble Telescope observations} Linear constraints solvable in polynomial time by LP methods (see

cs170 for a bit of this theory)

12

Page 13: Constraint Satisfaction Problems - Sharif

Varieties of Constraints} Varieties of Constraints

} Unary constraints involve a single variable (equivalent toreducing domains), 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., red is better than green} Often representable by a cost for each variable

assignment} Gives constrained optimization problems} (Weโ€™ll ignore these until we get to Bayesโ€™ nets)

13

Page 14: Constraint Satisfaction Problems - Sharif

Cryptarithmetic example

Hypergraph

Global constraint

} Variables:๐‘‹ = {๐น, ๐‘‡, ๐‘ˆ,๐‘Š, ๐‘…, ๐‘‚, ๐‘‹1, ๐‘‹2, ๐‘‹3}} Domains:{0,1, . . , 9}} Constraints:

} ๐‘Ž๐‘™๐‘™๐‘‘๐‘–๐‘“๐‘“(๐น, ๐‘‡, ๐‘ˆ,๐‘Š, ๐‘…, ๐‘‚)} ๐‘‚ + ๐‘‚ = ๐‘… + 10ร—๐‘‹1} ๐‘‹1 +๐‘Š +๐‘Š = ๐‘ˆ + 10ร—๐‘‹2} ๐‘‹2 + ๐‘‡ + ๐‘‡ = ๐‘‚ + 10ร—๐‘‹3} ๐‘‹3 = ๐น, ๐‘‡ โ‰  0, ๐น โ‰  0

14

Page 15: Constraint Satisfaction Problems - Sharif

Example: Sudoku

ยง Variables:ยง Each(open)square

ยง Domains:ยง {1,2,โ€ฆ,9}

ยง Constraints:

9-wayalldiff foreachrow

9-wayalldiffforeachcolumn

9-wayalldiffforeachregion

(orcanhaveabunchofpairwiseinequalityconstraints)

15

Page 16: Constraint Satisfaction Problems - Sharif

Real-World CSPs} Assignment problems: e.g., who teaches what class} Timetabling problems: e.g., which class is offered when and where?} Hardware configuration} Transportation scheduling} Factory scheduling} Circuit layout} Fault diagnosis} โ€ฆ lots more!

} Many real-world problems involve real-valued variablesโ€ฆ

16

Page 17: Constraint Satisfaction Problems - Sharif

Solving CSPs

17

Page 18: Constraint Satisfaction Problems - Sharif

Solving CSPs as a standard search problemIncremental} Initial State: { }

} Actions: assign a value to an unassigned variable thatdoes not conflict with current assignment

} Goal test: Consistent & complete assignment} Path cost: not important

Weโ€™ll start with the straightforward, naรฏve approach, then improve it

18

Page 19: Constraint Satisfaction Problems - Sharif

Properties of CSPs as a standard search problem

} Generic problem formulation: same formulation for all CSPs

} Every solution appears at depth ๐‘› with ๐‘› variables

} Which search algorithm is proper?} Depth-limited search

} Branching factor is ๐‘›๐‘‘ at the top level, ๐‘ = (๐‘› โˆ’ ๐‘™)๐‘‘ at depth๐‘™, hence there are ๐‘›! ๐‘‘๐‘› leaves.} However, there are only ๐‘‘X complete assignments.

19

Page 20: Constraint Satisfaction Problems - Sharif

Assignment community} When assigning values to variables, we reach the same partial

assignment regardless of the order of variables

๐‘›!ร—๐‘‘๐‘›

WA NT TWA WA

WANT

WANT

WANT

(๐‘›ร—๐‘‘)ร—((๐‘› โˆ’ 1)ร—๐‘‘)NTWA

Equal!There are ๐‘›!ร—๐‘‘๐‘› leaves in the tree but only ๐‘‘๐‘› distinct states!

โ€ฆ.TT

โ€ฆ.

NT

โ€ฆ

๐‘›ร—๐‘‘

20

Page 21: Constraint Satisfaction Problems - Sharif

Backtracking Search

21

Page 22: Constraint Satisfaction Problems - Sharif

Backtracking Search} Backtracking search is the basic uninformed algorithm for solving CSPs

} Idea 1: One variable at a time} Variable assignments are commutative, so fix ordering} 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 step

} Idea 2: Check constraints as you go} I.e. consider only values which do not conflict previous assignments} Might have to do some computation to check the constraints} โ€œIncremental goal testโ€

} Depth-first search with these two improvementsis called backtracking search (not the best name)

} Can solve n-queens for n ยป 25

22

Page 23: Constraint Satisfaction Problems - Sharif

Backtracking search} Depth-first search for CSPs with single-variable assignments is

called backtracking search} assigns one variable at each level (eventually they all have to be

assigned.)

} Naรฏve backtracking is not generally efficient for solving CSPs.} More heuristics will be introduced later to speedup it.

23

Page 24: Constraint Satisfaction Problems - Sharif

Backtracking search} Nodes are partial assignments

} Incremental completion} Each partial candidate is the parent of all candidates that differ from it by

a single extension step.

} Traverses the search tree in depth first order.

} At each node c} If it cannot be completed to a valid solution, the whole sub-tree rooted

at c is skipped (not promising branches are pruned).} Otherwise, the algorithm (1) checks whether c itself is a valid solution,

returns it; and (2) recursively enumerates all sub-trees of c.

24

Page 25: Constraint Satisfaction Problems - Sharif

Search tree} Variable assignments in the order:๐‘Š๐ด,๐‘๐‘‡, ๐‘„,โ€ฆ

WANTSA

QNSWVT

25

Page 26: Constraint Satisfaction Problems - Sharif

General backtracking searchfunction ๐ต๐ด๐ถ๐พ๐‘‡๐‘…๐ด๐ถ๐พ(๐‘ฃ) returns a solution, or failure

if there is a solution at ๐‘ฃ then return solutionfor each child ๐‘ข of ๐‘ฃ do

if ๐‘ƒ๐‘Ÿ๐‘œ๐‘š๐‘–๐‘ ๐‘–๐‘›๐‘”(๐‘ข) then ๐‘Ÿ๐‘’๐‘ ๐‘ข๐‘™๐‘ก โ† ๐ต๐ด๐ถ๐พ๐‘‡๐‘…๐ด๐ถ๐พ(๐‘ข)if ๐‘Ÿ๐‘’๐‘ ๐‘ข๐‘™๐‘ก โ‰  failure return ๐‘Ÿ๐‘’๐‘ ๐‘ข๐‘™๐‘ก

return failure

function ๐ต๐ด๐ถ๐พ๐‘‡๐‘…๐ด๐ถ๐พ(๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก, ๐‘๐‘ ๐‘) returns an assignment, or failureIf ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก is complete then return ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก๐‘ฃ๐‘Ž๐‘Ÿ โ†select an unassigned variablefor each ๐‘ฃ๐‘Ž๐‘™ in ๐ท๐‘œ๐‘š๐‘Ž๐‘–๐‘›(๐‘ฃ๐‘Ž๐‘Ÿ) do

if ๐ถ๐‘œ๐‘›๐‘ ๐‘–๐‘ ๐‘ก๐‘’๐‘›๐‘ก(๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก โˆช {๐‘ฃ๐‘Ž๐‘Ÿ โ† ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’}, ๐‘๐‘ ๐‘) then๐‘Ÿ๐‘’๐‘ ๐‘ข๐‘™๐‘ก โ† ๐ต๐ด๐ถ๐พ๐‘‡๐‘…๐ด๐ถ๐พ(๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก โˆช {๐‘ฃ๐‘Ž๐‘Ÿ โ† ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’}, ๐‘๐‘ ๐‘)if ๐‘Ÿ๐‘’๐‘ ๐‘ข๐‘™๐‘ก โ‰  failure return ๐‘Ÿ๐‘’๐‘ ๐‘ข๐‘™๐‘ก

return failure26

Page 27: Constraint Satisfaction Problems - Sharif

4-Queens

27

Page 28: Constraint Satisfaction Problems - Sharif

4-Queens

28

Page 29: Constraint Satisfaction Problems - Sharif

4-Queens

29

Page 30: Constraint Satisfaction Problems - Sharif

4-Queens

30

Page 31: Constraint Satisfaction Problems - Sharif

4-Queens

31

Page 32: Constraint Satisfaction Problems - Sharif

4-Queens

32

Page 33: Constraint Satisfaction Problems - Sharif

4-Queens

Solution33

Page 34: Constraint Satisfaction Problems - Sharif

Naรฏve backtracking (late failure)} Map coloring with three colors

} {๐‘Š๐ด = ๐‘Ÿ๐‘’๐‘‘, ๐‘„ = ๐‘๐‘™๐‘ข๐‘’} can not be completed.} However, the backtracking search does not detect this before

selecting but ๐‘๐‘‡ and ๐‘†๐ด variables

WANT

SAQ

NSWVT

Variable NT has nopossible value. Butit is not detecteduntil tying to assignit a value.

๐‘Š๐ด = ๐‘Ÿ๐‘’๐‘‘

๐‘„ = ๐‘๐‘™๐‘ข๐‘’๐‘๐‘†๐‘Š๐‘‰๐‘‡

๐‘†๐ด๐‘๐‘‡

34

Page 35: Constraint Satisfaction Problems - Sharif

Backtracking Search

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

} What are the choice points?

35

Page 36: Constraint Satisfaction Problems - Sharif

Improving Backtracking

} General-purpose ideas give huge gains in speed

} Ordering:} Which variable should be assigned next?} In what order should its values be tried?

} Filtering: Can we detect inevitable failure early?

} Structure: Can we exploit the problem structure?

36

Page 37: Constraint Satisfaction Problems - Sharif

Constraint Graphs

38

Page 38: Constraint Satisfaction Problems - Sharif

Constraint Graphs

} Binary CSP: each constraint relates (at most) twovariables

} Binary constraint graph: nodes are variables, arcsshow constraints

} General-purpose CSP algorithms use the graphstructure to speed up search. E.g., Tasmania is anindependent subproblem!

39

Page 39: Constraint Satisfaction Problems - Sharif

Filtering} Filtering: Keep track of domains for unassigned variables

and cross off bad options} Filtering by inference (looking ahead) in solving CSPs

40

Page 40: Constraint Satisfaction Problems - Sharif

Filtering by inference (looking ahead) in solving CSPs} Filtering as a preprocessing stage

} AC-3 (arc consistency) algorithm} Removes values from domains of variables (and propagates constraints)

to provide all constrained pairs of variables arc consistent.

} Filtering intertwined with search} Forward checking

} When selecting a value for a variable, infers new domain reductions onneighboring unassigned variables

} Maintaining Arc Consistency (MAC) - Constraint propagation} Forward checking + recursively propagating constraints when changes

are made to the domains

} โ€ฆ

41

Page 41: Constraint Satisfaction Problems - Sharif

Forward Checking (FC)} When selecting a value for a variable, infer new domain

reductions on neighboring unassigned variables.} Terminate search when a variable has no legal value

} When ๐‘‹ is assigned, FC establishes arc-consistency for it.

WANTSA

QNSWVT

42

Page 42: Constraint Satisfaction Problems - Sharif

Forward Checking (FC)} When selecting a value for a variable, infer new domain

reductions on neighboring unassigned variables.} Terminate search when a variable has no legal value

} When ๐‘‹ is assigned, FC establishes arc-consistency for it.

WANTSA

QNSWVT

๐‘Š๐ด = ๐‘Ÿ๐‘’๐‘‘

43

Page 43: Constraint Satisfaction Problems - Sharif

Forward Checking (FC)} When selecting a value for a variable, infer new domain

reductions on neighboring unassigned variables.} Terminate search when a variable has no legal value

} When ๐‘‹ is assigned, FC establishes arc-consistency for it.

WANTSA

QNSWVT

๐‘Š๐ด = ๐‘Ÿ๐‘’๐‘‘๐‘„ = ๐‘”๐‘Ÿ๐‘’๐‘’๐‘›

Combination of MRV heuristic and FC is more effective.FC incrementally computes the information that MRV needs.

44

Page 44: Constraint Satisfaction Problems - Sharif

Forward Checking (FC)} When selecting a value for a variable, infer new domain

reductions on neighboring unassigned variables.} Terminate search when a variable has no legal value

} When ๐‘‹ is assigned, FC establishes arc-consistency for it.

WANTSA

QNSWVT

๐‘Š๐ด = ๐‘Ÿ๐‘’๐‘‘๐‘„ = ๐‘”๐‘Ÿ๐‘’๐‘’๐‘›๐‘‰ = ๐‘๐‘™๐‘ข๐‘’

โŸน {๐‘Š๐ด = ๐‘Ÿ๐‘’๐‘‘, ๐‘„ = ๐‘”๐‘Ÿ๐‘’๐‘’๐‘›, ๐‘‰ = ๐‘๐‘™๐‘ข๐‘’} is an inconsistent partial assignment

45

Page 45: Constraint Satisfaction Problems - Sharif

Example: 4-Queens

1

32

4

32 41

X1{1,2,3,4}

X3{1,2,3,4}

X4{1,2,3,4}

X2{1,2,3,4}

4-Qeens slides have been adopted from Dorrโ€™s slides on CMSC-421 course46

Page 46: Constraint Satisfaction Problems - Sharif

Example: 4-Queens

1

32

4

32 41

X1{1,2,3,4}

X3{1,2,3,4}

X4{1,2,3,4}

X2{1,2,3,4}

47

Page 47: Constraint Satisfaction Problems - Sharif

Example: 4-Queens

1

32

4

32 41

X1{1,2,3,4}

X3{ ,2, ,4}

X4{ ,2,3, }

X2{ , ,3,4}

48

Page 48: Constraint Satisfaction Problems - Sharif

Example: 4-Queens

1

32

4

32 41

X1{1,2,3,4}

X3{ ,2, ,4}

X4{ ,2,3, }

X2{ , ,3,4}

49

Page 49: Constraint Satisfaction Problems - Sharif

Example: 4-Queens

1

32

4

32 41

X1{1,2,3,4}

X3{ , , , }

X4{ , ,3, }

X2{ , ,3,4}

50

Page 50: Constraint Satisfaction Problems - Sharif

Example: 4-Queens

1

32

4

32 41

X1{1,2,3,4}

X3{ ,2, ,4}

X4{ ,2,3, }

X2{ , , ,4}

51

Page 51: Constraint Satisfaction Problems - Sharif

Example: 4-Queens

1

32

4

32 41

X1{1,2,3,4}

X3{ ,2, ,4}

X4{ ,2,3, }

X2{ , , ,4}

52

Page 52: Constraint Satisfaction Problems - Sharif

Example: 4-Queens

1

32

4

32 41

X1{1,2,3,4}

X3{ ,2, , }

X4{ , ,3, }

X2{ , , ,4}

53

Page 53: Constraint Satisfaction Problems - Sharif

Example: 4-Queens

1

32

4

32 41

X1{1,2,3,4}

X3{ ,2, , }

X4{ , ,3, }

X2{ , , ,4}

54

Page 54: Constraint Satisfaction Problems - Sharif

Example: 4-Queens

1

32

4

32 41

X1{1,2,3,4}

X3{ ,2, , }

X4{ , , , }

X2{ , ,3,4}

55

Page 55: Constraint Satisfaction Problems - Sharif

Filtering: shortcoming} Forward checking propagates information from assigned to

unassigned variables, but doesn't provide early detection for allfailures:

} NT and SA cannot both be blue!} Why didnโ€™t we detect this yet?} Constraint propagation: reason from constraint to constraint

WA SA

NT Q

NSW

V

56

Page 56: Constraint Satisfaction Problems - Sharif

Consistency of A Single Arc

} An arc X ยฎ Y is consistent iff for every x there is some y which could beassigned without violating a constraint

}

} Forward checking: Enforcing consistency of arcs pointing to each newassignment

Deletefromthetail!

WA SA

NT Q

NSW

V

57

Page 57: Constraint Satisfaction Problems - Sharif

Arc consistency} ๐‘‹H is arc-consistent with respect to ๐‘‹I

if for every value in ๐ทH there is a consistent value in ๐ทI

} Example} Variables:๐‘‹ = ๐‘‹-, ๐‘‹S} Domain: {0,1,2, โ€ฆ , 9}} Constraint:๐‘‹- = ๐‘‹SS

} Is ๐‘‹- is arc-consistent w.r.t.๐‘‹S?} No, to be arc-consistent ๐ท๐‘œ๐‘š๐‘Ž๐‘–๐‘›(๐‘‹-) = {0,1,4,9}

} Is ๐‘‹S is arc-consistent w.r.t.๐‘‹-?} No, to be arc-consistent ๐ท๐‘œ๐‘š๐‘Ž๐‘–๐‘›(๐‘‹S) = {0,1,2,3}

58

Page 58: Constraint Satisfaction Problems - Sharif

Arc-consistency: cryptarithmetic example

(๐‘…, ๐‘‚)

makearc-consistent

It is not arc-consistent๐ท๐‘œ๐‘š๐‘Ž๐‘–๐‘›(๐‘…) = {0,2,4,6,8}

59

Page 59: Constraint Satisfaction Problems - Sharif

Arc Consistency of an Entire CSP

} A simple form of propagation makes sure all arcs are consistent:

} Important: 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} Whatโ€™s the downside of enforcing arc consistency?

WA SANT Q

NSW

V

60

Page 60: Constraint Satisfaction Problems - Sharif

Arc consistency algorithm (AC-3)function ๐ด๐ถ_3(๐‘๐‘ ๐‘) returns false if an inconsistency is found and true otherwise

inputs: ๐‘๐‘ ๐‘, a binary CSP with components ๐‘‹, ๐ท, ๐ถlocal variables: ๐‘ž๐‘ข๐‘’๐‘ข๐‘’, a queue of arcs, initially all the arcs in ๐‘๐‘ ๐‘

while ๐‘ž๐‘ข๐‘’๐‘ข๐‘’ is not empty do(๐‘‹H, ๐‘‹I) โ† ๐‘…๐ธ๐‘€๐‘‚๐‘‰๐ธ_๐น๐ผ๐‘…๐‘†๐‘‡(๐‘ž๐‘ข๐‘’๐‘ข๐‘’)if ๐‘…๐ธ๐‘‰๐ผ๐‘†๐ธ(๐‘๐‘ ๐‘, ๐‘‹H, ๐‘‹I) then

If size of ๐ทH = 0 then return ๐‘“๐‘Ž๐‘™๐‘ ๐‘’for each ๐‘‹q in ๐‘‹H. ๐‘๐ธ๐ผ๐บ๐ป๐ต๐‘‚๐‘…๐‘† โˆ’ {๐‘‹I} do

add (๐‘‹q, ๐‘‹H) to ๐‘ž๐‘ข๐‘’๐‘ข๐‘’

function ๐‘…๐ธ๐‘‰๐ผ๐‘†๐ธ(๐‘๐‘ ๐‘, ๐‘‹H, ๐‘‹I) returns true iff we revise the domain of ๐‘‹H๐‘Ÿ๐‘’๐‘ฃ๐‘–๐‘ ๐‘’๐‘‘ โ† ๐‘“๐‘Ž๐‘™๐‘ ๐‘’for each ๐‘ฅ in ๐ทH do

if no value ๐‘ฆ in ๐ทI allows (๐‘ฅ, ๐‘ฆ) to satisfy the constraint between ๐‘‹H and ๐‘‹I thendelete ๐‘ฅ from ๐ทH๐‘Ÿ๐‘’๐‘ฃ๐‘–๐‘ ๐‘’๐‘‘ โ† ๐‘ก๐‘Ÿ๐‘ข๐‘’

return ๐‘Ÿ๐‘’๐‘ฃ๐‘–๐‘ ๐‘’๐‘‘

Makes ๐‘‹H arc-consistent with respect to ๐‘‹I61

Page 61: Constraint Satisfaction Problems - Sharif

AC-3For each arc ๐‘‹H, ๐‘‹I in the queue

Remove it from queueMakes ๐‘‹H arc-consistent with respect to ๐‘‹I

1) If ๐ทH remains unchanged then continue2) If |๐ทH| = 0 then return false3) For each neighbor ๐‘‹q of ๐‘‹H except to ๐‘‹I do

add ๐‘‹q, ๐‘‹H to queueIf domain of ๐‘‹H loses a value, neighbors of ๐‘‹H must be rechecked

} Removing a value from a domain may cause further inconsistency, sowe have to repeat the procedure until everything is consistent.

} When queue is empty, resulted CSP is equivalent to the original CSP.} Same solution (usually reduced domains speed up the search)

62

Page 62: Constraint Satisfaction Problems - Sharif

AC-3: time complexity} Time complexity (๐‘› variables, ๐‘ binary constraints, ๐‘‘

domain size): ๐‘‚(๐‘๐‘‘U)} Each arc (๐‘‹q, ๐‘‹H) is inserted in the queue at most ๐‘‘ times.

} At most all values in domain ๐‘‹H can be deleted.

} Checking consistency of an arc:๐‘‚(๐‘‘S)

63

Page 63: Constraint Satisfaction Problems - Sharif

Constraint propagation} FC makes the current variable arc-consistent but does not

make all the other variables arc-consistent

} NT and SA cannot both be blue!} FC does not look far enough ahead to find this inconsistency

} Maintaining Arc Consistency (MAC) - Constraint propagation} Forward checking + recursively propagating constraints when

changing domains (similar to AC-3 but only arcs related to thecurrent variable are put in the queue at start)

WANTSA

QNSWVT

๐‘Š๐ด = ๐‘Ÿ๐‘’๐‘‘๐‘„ = ๐‘”๐‘Ÿ๐‘’๐‘’๐‘›

64

Page 64: Constraint Satisfaction Problems - Sharif

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 still runs inside abacktracking search! Whatwent

wronghere?

65

Page 65: Constraint Satisfaction Problems - Sharif

Arc consistency: map coloring example} For general map coloring problem all pairs of variables are arc-

consistent if ๐ทH โ‰ฅ 2(๐‘– = 1,โ€ฆ , ๐‘›)

} Arc consistency can do nothing.} Fails to make enough inference

} We need stronger notion of consistency to detect failure atstart.} 3-consistency (path consistency): for any consistent assignment to each

set of two variables, a consistent value can be assigned to any othervariable.

} Both of the possible assignments to set {๐‘Š๐ด, ๐‘†๐ด} are inconsistent with๐‘๐‘‡.

WANT

SAQ

NSWVT

66

Page 66: Constraint Satisfaction Problems - Sharif

Local consistency} Node consistency (1-consistency)

} Unary constraints are satisfied.

} Arc consistency (2-consistency)} Binary constraints are satisfied.

} Path consistency (3-consistency)

67

Page 67: Constraint Satisfaction Problems - Sharif

k-consistency} Arc consistency does not detect all inconsistencies

} Partial assignment {๐‘Š๐ด = ๐‘Ÿ๐‘’๐‘‘, ๐‘๐‘†๐‘Š = ๐‘Ÿ๐‘’๐‘‘} is inconsistent.

} A CSP is k-consistent if for any set of ๐‘˜ โˆ’ 1 variables and forany consistent assignment to those variables, a consistent valuecan always be assigned to any ๐‘˜th variable.} E.g. 1-consistency = node-consistency} E.g. 2-consistency = arc-consistency} E.g. 3-consistency = path-consistency

WANTSA

QNSWVT

68

Page 68: Constraint Satisfaction Problems - Sharif

Strong K-Consistency} Strong k-consistency: also k-1, k-2, โ€ฆ 1 consistent

} Claim: strong n-consistency means we can solve without backtracking!

} Why?} Choose any assignment to any variable} Choose a new variable} By 2-consistency, there is a choice consistent with the first} Choose a new variable} By 3-consistency, there is a choice consistent with the first 2} โ€ฆ

} Lots of middle ground between arc consistency and n-consistency! (e.g.k=3, called path consistency)

69

Page 69: Constraint Satisfaction Problems - Sharif

Which level of consistency?} Trade off between the required time to establish k-

consistency and amount of the eliminated search space.} If establishing consistency is slow, this can slow the search

down to the point where no propagation is better.

} Establishing k-consistency need exponential time andspace in ๐‘˜ (in the worst case)

} Commonly computing 2-consistency and less commonly3-consistency

70

Page 70: Constraint Satisfaction Problems - Sharif

Ordering

71

Page 71: Constraint Satisfaction Problems - Sharif

Ordering: Minimum Remaining Values

} Variable Ordering: Minimum remaining values (MRV):} Choose the variable with the fewest legal left values in its domain

} Why min rather than max?} Also called โ€œmost constrained variableโ€} โ€œFail-fastโ€ ordering

72

Page 72: Constraint Satisfaction Problems - Sharif

Minimum Remaining Values (MRV)} Chooses the variable with the fewest legal values

} Fail first

} Also known as Most ConstrainedVariable (MCS)

} Most likely to cause a failure soon and so pruning thesearch tree

73

Page 73: Constraint Satisfaction Problems - Sharif

Degree heuristic} Tie-breaker among MRV variables

} Degree heuristic: choose the variable with the mostconstraints on remaining variables} To choose one who interferes the others most!} reduction in branching factor WA

NT

SAQ

NSWVT

74

Page 74: Constraint Satisfaction Problems - Sharif

Least constraining value} Given a variable, choose the least constraining value:

} one that rules out the fewest values in the remaining variables} leaving maximum flexibility for subsequent variable assignments

} Fail last (the most likely values first)

} Assumption: we only need one solution WANTSA

QNSWVT

75

Page 75: Constraint Satisfaction Problems - Sharif

Ordering: Least Constraining Value

} Value Ordering: Least ConstrainingValue} Note that it may take some computation to

determine this! (E.g., rerunning filtering)

} Why least rather than most?

} Combining these ordering ideas makes1000 queens feasible

76

Page 76: Constraint Satisfaction Problems - Sharif

Inference during the search process

} It can be more powerful than inference in thepreprocessing stage.

} Interleaving search and inference

77

Page 77: Constraint Satisfaction Problems - Sharif

Solving CSP efficiently} Which variable should be assigned next?

} ๐‘†๐ธ๐ฟ๐ธ๐ถ๐‘‡_๐‘ˆ๐‘๐ด๐‘†๐‘†๐ผ๐บ๐‘๐ธ๐ท_๐‘‰๐ด๐‘…๐ผ๐ด๐ต๐ฟ๐ธ

} In what order should values of the selected variable betried?} ๐‘‚๐‘…๐ท๐ธ๐‘…_๐ท๐‘‚๐‘€๐ด๐ผ๐‘_๐‘‰๐ด๐ฟ๐‘ˆ๐ธ๐‘†

} What inferences should be performed at each step in thesearch?} ๐ผ๐‘๐น๐ธ๐‘…๐ธ๐‘๐ถ๐ธ

78

Page 78: Constraint Satisfaction Problems - Sharif

CSP backtracking search function ๐ต๐ด๐ถ๐พ๐‘‡๐‘…๐ด๐ถ๐พ๐ผ๐‘_๐‘†๐ธ๐ด๐‘…๐ถ๐ป(๐‘๐‘ ๐‘) returns a solution, or failure

return ๐ต๐ด๐ถ๐พ๐‘‡๐‘…๐ด๐ถ๐พ({}, ๐‘๐‘ ๐‘)

function ๐ต๐ด๐ถ๐พ๐‘‡๐‘…๐ด๐ถ๐พ(๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก, ๐‘๐‘ ๐‘) returns a solution, or failureif ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก is complete then return ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก๐‘ฃ๐‘Ž๐‘Ÿ โ† ๐‘†๐ธ๐ฟ๐ธ๐ถ๐‘‡_๐‘ˆ๐‘๐ด๐‘†๐‘†๐ผ๐บ๐‘๐ธ๐ท_๐‘‰๐ด๐‘…๐ผ๐ด๐ต๐ฟ๐ธ(๐‘๐‘ ๐‘)for each ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’ in ๐‘‚๐‘…๐ท๐ธ๐‘…_๐ท๐‘‚๐‘€๐ด๐ผ๐‘_๐‘‰๐ด๐ฟ๐‘ˆ๐ธ๐‘†(๐‘ฃ๐‘Ž๐‘Ÿ, ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก, ๐‘๐‘ ๐‘) do

if ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’ is consistent with ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก thenadd {๐‘ฃ๐‘Ž๐‘Ÿ = ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’} to ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก๐‘–๐‘›๐‘“๐‘’๐‘Ÿ๐‘’๐‘›๐‘๐‘’๐‘  โ† ๐ผ๐‘๐น๐ธ๐‘…๐ธ๐‘๐ถ๐ธ(๐‘๐‘ ๐‘, ๐‘ฃ๐‘Ž๐‘Ÿ, ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’)if ๐‘–๐‘›๐‘“๐‘’๐‘Ÿ๐‘’๐‘›๐‘๐‘’๐‘  โ‰  ๐‘“๐‘Ž๐‘–๐‘™๐‘ข๐‘Ÿ๐‘’ then

add ๐‘–๐‘›๐‘“๐‘’๐‘Ÿ๐‘’๐‘›๐‘๐‘’๐‘  to ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก๐‘Ÿ๐‘’๐‘ ๐‘ข๐‘™๐‘ก โ† ๐ต๐ด๐ถ๐พ๐‘‡๐‘…๐ด๐ถ๐พ(๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘’๐‘š๐‘›๐‘ก, ๐‘๐‘ ๐‘)if ๐‘Ÿ๐‘’๐‘ ๐‘ข๐‘™๐‘ก โ‰  ๐‘“๐‘Ž๐‘–๐‘™๐‘ข๐‘Ÿ๐‘’ then return ๐‘Ÿ๐‘’๐‘ ๐‘ข๐‘™๐‘ก

remove {๐‘ฃ๐‘Ž๐‘Ÿ = ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’} and ๐‘–๐‘›๐‘“๐‘’๐‘Ÿ๐‘’๐‘›๐‘๐‘’๐‘  from ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘กreturn ๐‘“๐‘Ž๐‘–๐‘™๐‘ข๐‘Ÿ๐‘’

79

Page 79: Constraint Satisfaction Problems - Sharif

CSP backtracking search function ๐ต๐ด๐ถ๐พ๐‘‡๐‘…๐ด๐ถ๐พ๐ผ๐‘_๐‘†๐ธ๐ด๐‘…๐ถ๐ป(๐‘๐‘ ๐‘) returns a solution, or failure

return ๐ต๐ด๐ถ๐พ๐‘‡๐‘…๐ด๐ถ๐พ({}, ๐‘๐‘ ๐‘)

function ๐ต๐ด๐ถ๐พ๐‘‡๐‘…๐ด๐ถ๐พ(๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก, ๐‘๐‘ ๐‘) returns a solution, or failureif ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก is complete then return ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก๐‘ฃ๐‘Ž๐‘Ÿ โ† ๐‘†๐ธ๐ฟ๐ธ๐ถ๐‘‡_๐‘ˆ๐‘๐ด๐‘†๐‘†๐ผ๐บ๐‘๐ธ๐ท_๐‘‰๐ด๐‘…๐ผ๐ด๐ต๐ฟ๐ธ(๐‘๐‘ ๐‘)for each ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’ in ๐‘‚๐‘…๐ท๐ธ๐‘…_๐ท๐‘‚๐‘€๐ด๐ผ๐‘_๐‘‰๐ด๐ฟ๐‘ˆ๐ธ๐‘†(๐‘ฃ๐‘Ž๐‘Ÿ, ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก, ๐‘๐‘ ๐‘) do

if ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’ is consistent with ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก thenadd {๐‘ฃ๐‘Ž๐‘Ÿ = ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’} to ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก๐‘–๐‘›๐‘“๐‘’๐‘Ÿ๐‘’๐‘›๐‘๐‘’๐‘  โ† ๐ผ๐‘๐น๐ธ๐‘…๐ธ๐‘๐ถ๐ธ(๐‘๐‘ ๐‘, ๐‘ฃ๐‘Ž๐‘Ÿ, ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’)if ๐‘–๐‘›๐‘“๐‘’๐‘Ÿ๐‘’๐‘›๐‘๐‘’๐‘  โ‰  ๐‘“๐‘Ž๐‘–๐‘™๐‘ข๐‘Ÿ๐‘’ then

add ๐‘–๐‘›๐‘“๐‘’๐‘Ÿ๐‘’๐‘›๐‘๐‘’๐‘  to ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก๐‘Ÿ๐‘’๐‘ ๐‘ข๐‘™๐‘ก โ† ๐ต๐ด๐ถ๐พ๐‘‡๐‘…๐ด๐ถ๐พ(๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘’๐‘š๐‘›๐‘ก, ๐‘๐‘ ๐‘)if ๐‘Ÿ๐‘’๐‘ ๐‘ข๐‘™๐‘ก โ‰  ๐‘“๐‘Ž๐‘–๐‘™๐‘ข๐‘Ÿ๐‘’ then return ๐‘Ÿ๐‘’๐‘ ๐‘ข๐‘™๐‘ก

remove {๐‘ฃ๐‘Ž๐‘Ÿ = ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’} and ๐‘–๐‘›๐‘“๐‘’๐‘Ÿ๐‘’๐‘›๐‘๐‘’๐‘  from ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘กreturn ๐‘“๐‘Ž๐‘–๐‘™๐‘ข๐‘Ÿ๐‘’

80

Page 80: Constraint Satisfaction Problems - Sharif

Constraint graph} Nodes are variables, arcs are constraints

} Enforcing local consistency in each part of the graph can causeinconsistent values to be eliminated

WANT

SAQ

NSWVT

81

Page 81: Constraint Satisfaction Problems - Sharif

Problem Structure

} Independent subproblems are identifiable asconnected components of constraint graph

} Suppose a graph of n variables can be broken intosubproblems of only c variables:} Worst-case solution cost is O((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

Page 82: Constraint Satisfaction Problems - Sharif

Graph Structure} Extreme case: independent subproblems

} Example:Tasmania and mainland do not interact

} Connected components as independent sub-problems} The color of ๐‘‡ is independent of those of other regions

} Suppose each sub-problem has โ„Ž variables out of ๐‘›} Worst-case solution cost is ๐‘‚((๐‘›/โ„Ž)(๐‘‘{)) that is linear in ๐‘›

} Example: ๐‘› = 80, ๐‘‘ = 2, โ„Ž = 20 (processing: 10| nodes/sec)} 40 billion years} 4 seconds

83

Page 83: Constraint Satisfaction Problems - Sharif

Tree structured CSPs} Any two variables are connected by only one path

} Theorem: if the constraint graph has no loops, the CSP can besolved in O(n d2) time} Compare to general CSPs, where worst-case time is O(dn)

84

Page 84: Constraint Satisfaction Problems - Sharif

Tree structured CSPs: topological ordering } Construct a rooted tree (picking any variable to be root, โ€ฆ)

} Order variables from root to leaves such that every nodeโ€™sparent precedes it in the ordering (topological ordering)

85

Page 85: Constraint Satisfaction Problems - Sharif

Tree-Structured CSPs

} Algorithm for tree-structured CSPs:} Order: Choose a root variable, order variables so that parents precede

children

} Remove backward: For i=n:2, apply ArcConsistent(Parent(Xi),Xi)} Assign forward: For i=1:n, assign Xi consistently with Parent(Xi)

Page 86: Constraint Satisfaction Problems - Sharif

Tree structured CSP Solver

} After running loop1, any arc from a parent to its child is arc-consistent.

} โ‡’ if the constraint graph has no loops, the CSP can be solvedin ๐‘‚(๐‘›๐‘‘S) time.

๐‘‹ โ†Topological Sortfor ๐‘– = ๐‘›downto2 do

Make-Arc-Consistent(Parent(XH), XH)for ๐‘– = 1to๐‘› do

XH โ† any consistent value (with its parent) in ๐ทH

remove all values fromdomain of Parent(๐‘‹H) whichmay violate arc-consistency.

87

Page 87: Constraint Satisfaction Problems - Sharif

function ๐‘‡๐‘…๐ธ๐ธ_๐ถ๐‘†๐‘ƒ_๐‘†๐‘‚๐ฟ๐‘‰๐ธ๐‘…(๐‘๐‘ ๐‘) returns a solution or failureinput: ๐‘๐‘ ๐‘, a CSP with components ๐‘‹,๐ท, ๐ถ

๐‘› โ† number of variables in ๐‘‹๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก โ† an empty assignment๐‘Ÿ๐‘œ๐‘œ๐‘ก โ† any variable in ๐‘‹๐‘‹ โ† ๐‘‡๐‘‚๐‘ƒ๐‘‚๐ฟ๐‘‚๐บ๐ผ๐ถ๐ด๐ฟ(๐‘‹, ๐‘Ÿ๐‘œ๐‘œ๐‘ก)for ๐‘— = ๐‘› down to 2 do๐‘€๐ด๐พ๐ธ_๐ด๐‘…๐ถ_๐ถ๐‘‚๐‘๐‘†๐ผ๐‘†๐‘‡๐ธ๐‘๐‘‡(๐‘ƒ๐ด๐‘…๐ธ๐‘๐‘‡(๐‘‹I), ๐‘‹I))if it cannot be made consistent then return ๐‘“๐‘Ž๐‘–๐‘™๐‘ข๐‘Ÿ๐‘’

for ๐‘– = 1 to ๐‘› do๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก ๐‘‹H โ† ๐‘Ž๐‘›๐‘ฆ๐‘๐‘œ๐‘›๐‘ ๐‘–๐‘ ๐‘ก๐‘’๐‘›๐‘ก๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’๐‘“๐‘Ÿ๐‘œ๐‘š๐ทHif there is no consistent value then return ๐‘“๐‘Ž๐‘–๐‘™๐‘ข๐‘Ÿ๐‘’

return ๐‘Ž๐‘ ๐‘ ๐‘–๐‘”๐‘›๐‘š๐‘’๐‘›๐‘ก

Runtime: O(n d2) (why?)

88

Page 88: Constraint Satisfaction Problems - Sharif

Tree-Structured CSPs} Claim 1:After backward pass, all root-to-leaf arcs are consistent} Proof: Each XยฎY was made consistent at one point and Yโ€™s domain could

not have been reduced thereafter (because Yโ€™s children were processedbeforeY)

} Claim 2: If root-to-leaf arcs are consistent, forward assignment will notbacktrack

} Proof: Induction on position

} Why doesnโ€™t this algorithm work with cycles in the constraint graph?

} Note: weโ€™ll see this basic idea again with Bayesโ€™ nets

Page 89: Constraint Satisfaction Problems - Sharif

Reduction of general graphs into trees} Removing nodes

} Collapsing nodes together

90

Page 90: Constraint Satisfaction Problems - Sharif

Nearly Tree-Structured CSPs

} Conditioning: instantiate a variable, prune its neighbors' domains

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

Page 91: Constraint Satisfaction Problems - Sharif

Cutset Conditioning

SA

SA SA SA

Instantiatethecutset(allpossibleways)

ComputeresidualCSPforeachassignment

SolvetheresidualCSPs(treestructured)

Chooseacutset

Page 92: Constraint Satisfaction Problems - Sharif

Cut-set conditioning

} Cutset size ๐‘ gives runtime ๐‘‚((๐‘‘๏ฟฝ)(๐‘› โˆ’ ๐‘)๐‘‘S)} very fast for small ๐‘} ๐‘ can be as large as ๐‘› โˆ’ 2

1) Find a subset S such that the remaining graph becomes a tree2) For each possible consistent assignment to S

a) remove inconsistent values from domains of remaining variablesb) solve the remaining CSP which has a tree structure

93

Page 93: Constraint Satisfaction Problems - Sharif

Tree Decomposition} Create a tree-structured graph of overlapping sub-

problems (each sub-problem as a mega-variable)

} Solve each sub-problem (enforcing local constraints)

} Solve the tree-structured CSP over mega-variables

94

Page 94: Constraint Satisfaction Problems - Sharif

Tree Decomposition} Include all variables} Each constraint must be in at least one sub problem.} If a variable is in two sub-probs, it must be in all sub-

probs along the path.

95

Page 95: Constraint Satisfaction Problems - Sharif

Tree Decomposition*

ยง Idea:createatree-structuredgraphofmega-variablesยง Eachmega-variableencodespartoftheoriginalCSPยง Subproblems overlaptoensureconsistentsolutions

M1 M2 M3 M4

{(WA=r,SA=g,NT=b), (WA=b,SA=r,NT=g),โ€ฆ}

{(NT=r,SA=g,Q=b),(NT=b,SA=g,Q=r),โ€ฆ}

Agree: (M1,M2) รŽ{((WA=g,SA=g,NT=g), (NT=g,SA=g,Q=g)), โ€ฆ}

Agree on shared vars

NT

SA

ยนWA

ยน ยน

Q

SA

ยนNT

ยน ยน

Agree on shared vars

NSW

SA

ยนQ

ยน ยนAgree on shared vars

V

SA

ยนNSW

ยน ยน

Page 96: Constraint Satisfaction Problems - Sharif

Solving CSPs by local search algorithms} In the CSP formulation as a search problem, path is

irrelevant, so we can use complete-state formulation

} State: an assignment of values to variables} Successors(s): all states resulted from ๐‘  by choosing

a new value for a variable} Cost function โ„Ž(s): Number of violated constraints} Global minimum: โ„Ž ๐‘  = 0

97

Page 97: Constraint Satisfaction Problems - Sharif

Iterative Algorithms for CSPs} Local search methods typically work with โ€œcompleteโ€ states, i.e., all variables

assigned

} To apply to CSPs:} Take an assignment with unsatisfied constraints} Operators reassign variable values

} Algorithm:While not solved,} Variable selection: randomly select any conflicted variable} Value selection: min-conflicts heuristic:

} Choose a value that violates the fewest constraints} I.e., hill climb with h(n) = -total number of violated constraints

98

Page 98: Constraint Satisfaction Problems - Sharif

function ๐‘€๐ผ๐‘_๐ถ๐‘‚๐‘๐น๐ฟ๐ผ๐ถ๐‘‡๐‘†(๐‘๐‘ ๐‘,max_๐‘ ๐‘ก๐‘’๐‘๐‘ ) returns a solution or failure

inputs: ๐‘๐‘ ๐‘, a constraint satisfaction problem

m๐‘Ž๐‘ฅ_๐‘ ๐‘ก๐‘’๐‘๐‘ , the number of steps allowed before giving up

๐‘๐‘ข๐‘Ÿ๐‘Ÿ๐‘’๐‘›๐‘ก โ† an initial complete assignment for ๐‘๐‘ ๐‘for ๐‘– = 1 to max_๐‘ ๐‘ก๐‘’๐‘๐‘  do

if ๐‘๐‘ข๐‘Ÿ๐‘Ÿ๐‘’๐‘›๐‘ก is a solution for ๐‘๐‘ ๐‘ then return current

๐‘ฃ๐‘Ž๐‘Ÿ โ† a randomly chosen conflicted variable from ๐‘๐‘ ๐‘. ๐‘‰๐ด๐‘…๐ผ๐ด๐ต๐ฟ๐ธ๐‘†๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’ โ† the value ๐‘ฃ for ๐‘ฃ๐‘Ž๐‘Ÿ that minimizes ๐ถ๐‘‚๐‘๐น๐ฟ๐ผ๐ถ๐‘‡๐‘†(๐‘ฃ๐‘Ž๐‘Ÿ, ๐‘ฃ, ๐‘๐‘ข๐‘Ÿ๐‘Ÿ๐‘’๐‘›๐‘ก, ๐‘๐‘ ๐‘)set ๐‘ฃ๐‘Ž๐‘Ÿ = ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’ in ๐‘๐‘ข๐‘Ÿ๐‘Ÿ๐‘’๐‘›๐‘ก

return ๐‘“๐‘Ž๐‘–๐‘™๐‘ข๐‘Ÿ๐‘’

if current state is consistent thenreturn it

elsechoose a random variable v, and change assignment of v to a value that causes minimum conflict.

99

Page 99: Constraint Satisfaction Problems - Sharif

Local search for CSPs} Variable selection: randomly select any conflicted variable

} Value selection by min-conflicts heuristic} choose value that violates the fewest constraints

} i.e., hill-climbing

} Given random initial state, it can solve n-queens in almostconstant time for arbitrary n with high probability} ๐‘› = 1000000 in an average of 50 steps

} N-queens is easy for local search methods (while quite trickyfor backtracking)} Solutions are very densely distributed in the space and any initial

assignment is guaranteed to have a solution nearby.

100

Page 100: Constraint Satisfaction Problems - Sharif

8-Queens example

๐‘„๏ฟฝ = 3 ๐‘„| = 8

101

Page 101: Constraint Satisfaction Problems - Sharif

4-Queens example

102

Page 102: Constraint Satisfaction Problems - Sharif

Performance of Min-Conflicts

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

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

103

Page 103: Constraint Satisfaction Problems - Sharif

Summary} CSP benefits

} Standard representation of many problems} Generic heuristics (no domain specific expertise)

} CSPs solvers (based on systematic search)} Basic solution: backtracking search} Speed-ups:

} Ordering} Filtering} Structure

} Graph structure may be useful in solving CSPs efficiently.

} Local search methods for CSPs: Iterative min-conflicts is usually effective insolving CSPs.

104