constraint satisfaction problems russell and norvig: chapter 3, section 3.7 chapter 4, pages 104-105...

57
Constraint Constraint Satisfaction Problems Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/hom e.htm

Post on 21-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Constraint Satisfaction Constraint Satisfaction ProblemsProblems

Russell and Norvig: Chapter 3, Section 3.7Chapter 4, Pages 104-105

Slides adapted from:robotics.stanford.edu/~latombe/cs121/2003/home.htm

Page 2: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Intro Example: 8-QueensIntro Example: 8-Queens

Generate-and-test, with noredundancies “only” 88 combinations

Page 3: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Intro Example: 8-QueensIntro Example: 8-Queens

Page 4: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Example: Map Coloring

s0 = ???????

successors(s0) = {R??????, G??????, B??????}

What search algorithms could we use?BFS? DFS?

Page 12: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 13: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Map ColoringMap Coloring

{}

WA=red WA=green WA=blue

WA=redNT=green

WA=redNT=blue

WA=redNT=greenQ=red

WA=redNT=greenQ=blue

WA

NT

SA

Q

NSWV

T

Page 14: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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

Return failure

CSP-BACKTRACKING({})

Page 15: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

QuestionsQuestions

1. Which variable X should be assigned a value next?

2. In which order should its domain D be sorted?

3. What are the implications of a partial assignment for yet unassigned variables?

Page 16: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Choice of VariableChoice of Variable

Map coloring

WA

NT

SA

Q

NSWV

T

WA

NT

SA

Page 17: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Choice of VariableChoice of Variable

8-queen

Page 18: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Choice of VariableChoice of Variable

Most-constrained-variable heuristic: Select a variable with the fewest

remaining values

Page 19: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Choice of VariableChoice of Variable

Most-constraining-variable heuristic: Select the variable that is involved in the largest

number of constraints on other unassigned variables

WA

NT

SA

Q

NSWV

T

SA

Page 20: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

{}

Choice of ValueChoice of Value

WA

NT

SA

Q

NSWV

T

WA

NT

Page 21: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Choice of ValueChoice of Value

Least-constraining-value heuristic: Prefer the value that leaves the largest subset

of legal values for other unassigned variables

{blue}

WA

NT

SA

Q

NSWV

T

WA

NT

Page 22: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Constraint Propagation …Constraint Propagation …

… is the process of determining how the possible values of one variable affect the possible values of other variables

Page 23: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 24: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Map ColoringMap Coloring

WA NT Q NSW V SA T

RGB RGB RGB RGB RGB RGB RGB

TWA

NT

SA

Q

NSW

V

Page 25: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Map ColoringMap Coloring

WA NT Q NSW V SA T

RGB RGB RGB RGB RGB RGB RGB

R GB RGB RGB RGB GB RGB

TWA

NT

SA

Q

NSW

V

Page 26: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

WA NT Q NSW V SA T

RGB RGB RGB RGB RGB RGB RGB

R GB RGB RGB RGB GB RGB

R B G RB RGB B RGB

Map ColoringMap Coloring

TWA

NT

SA

Q

NSW

V

Page 27: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Map ColoringMap Coloring

WA NT Q NSW V SA T

RGB RGB RGB RGB RGB RGB RGB

R GB RGB RGB RGB GB RGB

R B G RB RGB B RGB

R B G R B RGB

Impossible assignments that forward checking do not detect

TWA

NT

SA

Q

NSW

V

Page 28: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Edge Labeling in Computer Edge Labeling in Computer VisionVision

Russell and Norvig: Chapter 24, pages 745-749

Page 29: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Edge LabelingEdge Labeling

Page 30: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Edge LabelingEdge Labeling

Page 31: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Labels of Edges

Convex edge: two surfaces intersecting at an angle greater than

180°

Concave edge two surfaces intersecting at an angle less than 180°

+ convex edge, both surfaces visible− concave edge, both surfaces visible convex edge, only one surface is visible and it is on the right side of

Page 32: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Edge LabelingEdge Labeling

Page 33: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Edge LabelingEdge Labeling

+

++

+

+

+

+

+

++

--

Page 34: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Junction Label SetsJunction Label Sets

+ + --

-- - + +

++ +

+

+

--

--

-+

(Waltz, 1975; Mackworth, 1977)

Page 35: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Edge Labeling as a CSPEdge Labeling as a CSP

A variable is associated with each junctionThe domain of a variable is the label set of the corresponding junctionEach constraint imposes that the values given to two adjacent junctions give the same label to the joining edge

Page 36: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Edge LabelingEdge Labeling

+ -

+

-+- -

++

Page 37: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Edge LabelingEdge Labeling +

+

+

+---

-- -

+

Page 38: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Edge LabelingEdge Labeling

+

+

+

++

+

-- - + +

++

Page 39: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Edge LabelingEdge Labeling

++

+

- -++

+ + --

Page 40: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Removal of Arc Removal of Arc InconsistenciesInconsistencies

REMOVE-ARC-INCONSISTENCIES(J,K)removed falseX label set of JY label set of KFor every label y in Y do If there exists no label x in X such that the

constraint (x,y) is satisfied then Remove y from Y If Y is empty then contradiction true removed true

Label set of K YReturn removed

Page 41: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

CP Algorithm for Edge CP Algorithm for Edge LabelingLabeling

Associate with every junction its label set Q stack of all junctions while Q is not empty do J UNSTACK(Q) For every junction K adjacent to J do

If REMOVE-ARC-INCONSISTENCIES(J,K) then If K’s domain is non-empty then

STACK(K,Q) Else return false

(Waltz, 1975; Mackworth, 1977)

Page 42: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 43: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Complexity Analysis of Complexity Analysis of AC3AC3

e = number of constraints (edges) d = number of values per variable Each variables is inserted in Q up to d times REMOVE-ARC-INCONSISTENCY takes O(d2) time CP takes O(ed3) time

Page 44: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Is AC3 All What is Is AC3 All What is Needed?Needed?

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 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

Solving a CSPSolving a CSP

Search: can find good 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 46: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

1

3

2

4

32 41

1

3

2

4

32 41

1

3

2

4

32 41

1

3

2

4

32 41

Page 47: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 48: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 49: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 50: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 51: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 52: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 53: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 54: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 55: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

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 56: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm

SummarySummary

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

Forward checkingConstraint propagationEdge labeling in Computer VisionInterweaving CP and backtracking

Page 57: Constraint Satisfaction Problems Russell and Norvig: Chapter 3, Section 3.7 Chapter 4, Pages 104-105 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm