48
Constraint Constraint Satisfaction Problems Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Embed Size (px)

Citation preview

Page 1: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Constraint Satisfaction Constraint Satisfaction ProblemsProblems

Russell and Norvig: Chapter 5.1-3

Page 2: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Intro Example: 8-QueensIntro Example: 8-Queens

Generate-and-test, 88 combinations

Page 3: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Intro Example: 8-QueensIntro Example: 8-Queens

Page 4: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

What is Needed?What is Needed?

Not just a successor function and goal testBut also a means to propagate the constraints imposed by one queen on the others and an early failure test Explicit representation of constraints and constraint manipulation algorithms

Page 5: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Constraint Satisfaction Constraint Satisfaction ProblemProblem

Set of variables {X1, X2, …, Xn}Each variable Xi has a domain Di of possible values Usually Di is discrete and finite

Set of constraints {C1, C2, …, Cp} Each constraint Ck involves a subset of

variables and specifies the allowable combinations of values of these variables

Assign a value to every variable such that all constraints are satisfied

Page 6: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Example: 8-Queens Example: 8-Queens ProblemProblem

8 variables Xi, i = 1 to 8 Domain for each variable {1,2,…,8} Constraints are of the forms: Xi = k Xj k for all j = 1 to 8, ji Xi = ki, Xj = kj |i-j| | ki - kj|

for all j = 1 to 8, ji

Page 7: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Example: Map ColoringExample: Map Coloring

• 7 variables {WA,NT,SA,Q,NSW,V,T}• Each variable has the same domain {red, green, blue}• No two adjacent variables have the same value: WANT, WASA, NTSA, NTQ, SAQ, SANSW, SAV,QNSW, NSWV

WA

NT

SA

Q

NSWV

T

WA

NT

SA

Q

NSWV

T

Page 8: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Example: Task SchedulingExample: Task Scheduling

T1 must be done during T3T2 must be achieved before T1 startsT2 must overlap with T3T4 must start after T1 is complete

T1

T2

T3

T4

Page 9: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Constraint GraphConstraint Graph

Binary constraints

T

WA

NT

SA

Q

NSW

V

Two variables are adjacent or neighbors if theyare connected by an edge or an arc

T1

T2

T3

T4

Page 10: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

CSP as a Search ProblemCSP as a Search Problem

Initial state: empty assignmentSuccessor function: a value is assigned to any unassigned variable, which does not conflict with the currently assigned variablesGoal test: the assignment is completePath cost: irrelevant

Page 11: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

RemarkRemark

Finite CSP include 3SAT as a special case 3SAT is known to be NP-complete So, in the worst-case, we cannot expect to solve a finite CSP in less than exponential time

Page 12: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Backtracking example

Page 13: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Backtracking example

Page 14: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Backtracking example

Page 15: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Backtracking example

Page 16: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Backtracking AlgorithmBacktracking Algorithm

CSP-BACKTRACKING(PartialAssignment a) If a is complete then return a X select an unassigned variable D select an ordering for the domain of X For each value v in D do

If v is consistent with a then Add (X= v) to a result CSP-BACKTRACKING(a) If result failure then return result Remove (X= v) from a

Return failure

Start with CSP-BACKTRACKING({})

Page 17: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Improving backtracking efficiency

Which variable should be assigned next?

In what order should its values be tried?

Can we detect inevitable failure early?

Page 18: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Most constrained variable

Most constrained variable:choose the variable with the fewest

legal values

a.k.a. minimum remaining values (MRV) heuristic

Page 19: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Most constraining variable

Tie-breaker among most constrained variablesMost constraining variable: choose the variable involved in

largest # of constraints on remaining variables

Page 20: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Least constraining value

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

in the remaining variables

Combining these heuristics makes 1000 queens feasible

Page 21: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Forward CheckingForward Checking After a variable X is assigned a value v,

look at each unassigned variable Y that is connected to X by a constraint and deletes from Y’s domain any value that is inconsistent with v

Page 22: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Forward checking

Page 23: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Forward checking

Page 24: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Forward checking

Page 25: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Forward checking

Page 26: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

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!

Page 27: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Definition (Arc consistency)

A constraint C_xy is said to be arc consistent w.r.t. x if for each value v of x

there is an allowed value of y.

Similarly, we define that C_xy is arc consistent w.r.t. y.

A binary CSP is arc consistent iff every constraint C_xy is arc consistent wrt x

as well as wrt y.

Page 28: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

When a CSP is not arc consistent, we can make it arc consistent, e.g. by using AC3.

This is also called “enforcing arc consistency”.

Page 29: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

ExampleLet domains be D_x = {1,2,3}, D_y = {3,4,5,6}A constaint C_xy = {(1,3),(1,5),(3,3),(3,6)}

C_xy is not arc consistent w.r.t. x, neither w.r.t. y. By enforcing arc consistency, we get reduced domains

D’_x = {1,3}, D’_y={3,5,6}

Page 30: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Arc consistency

Simplest form of propagation makes each arc consistentX Y is consistent ifffor every value x of X there is some allowed y

Page 31: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Arc consistency

Simplest form of propagation makes each arc consistentX Y is consistent ifffor every value x of X there is some allowed y

Page 32: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Arc consistency

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

Page 33: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Arc consistency

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

Page 34: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Example: domain reduction

Consider constraints: X > Y, Y > Z Domains: Dx = Dy = Dz = {1,2,3}

- 1 in Dx is removed by maintaining arc consistency, w.r.t. the constraint X < Y.

- You work out the rest. The resulting domains are

D’x = {1}, D’y = {2}, D’z = {3} No search is needed

Page 35: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

General CP for Binary General CP for Binary ConstraintsConstraints

Algorithm AC3

contradiction false Q stack of all variables while Q is not empty and not contradiction do X UNSTACK(Q) For every variable Y adjacent to X do

If REMOVE-ARC-INCONSISTENCIES(X,Y) then

If Y’s domain is non-empty then STACK(Y,Q)

Else return false

Page 36: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Complexity Analysis of Complexity Analysis of AC3AC3

e = number of constraints (edges) (or n2 where n is the # of variables)

d = number of values per variable Each variable is inserted in Q up to d times REMOVE-ARC-INCONSISTENCY takes O(d2) time AC3 takes O(ed3) time to run

Page 37: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

Solving a CSPSolving a CSP

Search: can find solutions, but must examine

non-solutions along the way

Constraint Propagation: can rule out non-solutions, but this is

not the same as finding solutions:

Interweave constraint propagation and search Perform constraint propagation at

each search step.

Page 38: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

4-Queens Problem4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{1,2,3,4}

X4{1,2,3,4}

X2{1,2,3,4}

Page 39: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

4-Queens Problem4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{1,2,3,4}

X4{1,2,3,4}

X2{1,2,3,4}

Page 40: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

4-Queens Problem4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{1,2,3,4}

X4{1,2,3,4}

X2{1,2,3,4}

Page 41: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

4-Queens Problem4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{1,2,3,4}

X4{1,2,3,4}

X2{1,2,3,4}

Page 42: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

4-Queens Problem4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{1,2,3,4}

X4{1,2,3,4}

X2{1,2,3,4}

Page 43: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

4-Queens Problem4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{1,2,3,4}

X4{1,2,3,4}

X2{1,2,3,4}

Page 44: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

4-Queens Problem4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{1,2,3,4}

X4{1,2,3,4}

X2{1,2,3,4}

Page 45: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

4-Queens Problem4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{1,2,3,4}

X4{1,2,3,4}

X2{1,2,3,4}

Page 46: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

4-Queens Problem4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{1,2,3,4}

X4{1,2,3,4}

X2{1,2,3,4}

Page 47: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

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

Page 48: Constraint Satisfaction Problems Russell and Norvig: Chapter 5.1-3

SummarySummary

Constraint Satisfaction Problems (CSP)CSP as a search problem Backtracking algorithm General heuristics

Forward checkingConstraint propagation (Arc consistency)Interweaving CP and backtracking