27
Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Embed Size (px)

Citation preview

Page 1: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Computer Science CPSC 322

Lecture 13

Arc Consistency

(4.5, 4.6 )

Slide 1

Page 2: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Announcements• Assignment 2 out later today – Due Friday Feb. 13

• Can already start doing the first 2 questions after today’s class

• Remember that I have posted in Connect:

• Learning goals for the whole course

• Short questions on material for the whole course.Both midterm and final will have questions very similar, or even

verbatim, from this set ( about 40-60% of overall mark)

You can answer these questions by studying the slides and texbook

– Cover questions as we proceed through the material, come to see us if you have doubts on how to answer

– BUT DO STUDY THE REVELANT MATERIAL FIRST!

Do not wait until just before the midterm to review the questions, if you start now it will be much easier to cover all the relevant ones before the exam

2

Page 3: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Lecture Overview

• Recap of Lecture 12• Arc Consistency for CSP• Domain Splitting

3

Page 4: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Search-Based Approach• Constraint Satisfaction (Problems):

• State: assignments of values to a subset of the variables• Successor function: assign values to a “free” variable• Goal test: all variables assigned a value and all constraints satisfied?• Solution: possible world that satisfies the constraints• Heuristic function: none (all solutions at the same distance from start)

• Planning : • State• Successor function• Goal test• Solution• Heuristic function

• Inference• State• Successor function• Goal test• Solution• Heuristic function

V1 = v1

V2 = v1

V1 = v1

V2 = v2

V1 = v1

V2 = vk

V1 = v1

V2 = v1

V3 = v2

V1 = v1

V2 = v1

V3 = v1

{}

V1 = v1 V1 = vk

4

Page 5: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

• Explore search space via DFS but evaluate each constraint as soon as all its variables are bound.

• Any partial assignment that doesn’t satisfy the constraint can be pruned.

Backtracking algorithms

Check unary constraints on V1

If not satisfied = PRUNE

Check constraints on V1

and V2 If not satisfied = PRUNEV1 = v1

V2 = v1

V1 = v1

V2 = v2

V1 = v1

V2 = vk

V1 = v1

V2 = v1

V3 = v2

V1 = v1

V2 = v1

V3 = v1

{}

V1 = v1 V1 = vk

5

Page 6: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

• Backtracking relies on one or more heuristics to select which variables to consider next.- E.g, variable involved in the largest number of constraints:

“If you are going to fail on this branch, fail early!”

• But we will look at an alternative approach that can do much better

• Arc Consistency: • Key idea: prune the domains as much as possible before

searching for a solution.

Selecting variables in a smart way

6

Page 7: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Constraint NetworksDef. A constraint network is defined by a graph, with

- one node for every variable (drawn as circle)

- one node for every constraint (drawn as rectangle)- undirected edges running between variable nodes and constraint nodes

whenever a given variable is involved in a given constraint.

{2,3} {3}A B

A< B

A>C

{1,2}

C

7

- Three variables: A, B, C- Two constraints: A<B,

A>C

Page 8: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Arc Consistency

Definition:An arc <x, r(x,y)> is arc consistent if for each value x in dom(X) there is some value y in dom(Y) such that r(x,y) is satisfied.A network is arc consistent if all its arcs are arc consistent.

{1,2,3} {2,3}A B

A< B

Not arc consistent: No value in domain of B that satisfies A<B if A=3

Arc consistent: Both B=2 and B=3 have ok values for A (e.g. A=1)

8

Page 9: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Arc Consistency

Definition (arc consistency)An arc is arc consistent if for each value in

there is some value in such that is satisfied.),(, YXrX x Xdom

y )(Ydom ),( yxr

A. Both arcs are consistentB. Left consistent, right inconsistentC. Left inconsistent, right consistentD. Both arcs are inconsistent

Left = <X, (X < Y/2)> Right = <Y, (X < Y/2)>

2, 5 3, 6, 12

X Y

X< Y/2

9

Page 10: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Arc Consistency

Definition:An arc <x, r(x,y)> is arc consistent if for each value x in dom(X) there is some value y in dom(Y) such that r(x,y) is satisfied.A network is arc consistent if all its arcs are arc consistent.

2,5, 3,5,12

X Y

X< Y/2

Not arc consistent: No value in domain of X that satisfies X< Y/2 if

Arc consistent: Both X=2 and X=5 have ok values for Y (e.g. Y= )

10

Page 11: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Arc Consistency Algorithm

How can we enforce Arc Consistency?

• If an arc <X, r(X,Y)> is not arc consistent- Delete all values x in dom(X) for which there is no corresponding value

in dom(Y)- This deletion makes the arc <X, r(X,Y)> arc consistent.- This removal can never rule out any models/solutions

WHY?

11

2,3,4 1,2,3X Y

X< Y

Download this example (SimpleCSP) from Schedule page Save to a local file and the open file in

Page 12: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Lecture Overview

• Recap of Lecture 12• Arc Consistency for CSP• Domain Splitting

12

Page 13: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Arc Consistency Algorithm

How can we enforce Arc Consistency?

• If an arc <X, r(X,Y)> is not arc consistent- Delete all values x in dom(X) for which there is no corresponding value

in dom(Y)- This deletion makes the arc <X, r(X,Y)> arc consistent.- This removal can never rule out any models/solutions

Algorithm: general idea

• Go through all the arcs in the network• Make each arc consistent by pruning the appropriate domain,

when needed• Reconsider arcs that could be turned back to being inconsistent

by this pruning• Eventually reach a ‘fixed point’: all arcs consistent 13

Page 14: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Arc Consistency

{2,3} {3}A

B

A< B

Not arc consistent: No value in domain of B that satisfies A<B if A=3

A>C

{1,2}

C

Arc consistent: For each value in dom(C), there is one in dom(A) that satisfies A>C (namely A=3)

Try to build this simple network in AISpace

14

Page 15: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Arc Consistency

{2,3} {3}A

B

A< B

Not arc consistent: No value in domain of B that satisfies A<B if A=3

A>C

{1,2}

C

Arc consistent: For each value in dom(C), there is one in dom(A) that satisfies A>C (namely A=3)

{2,3} {3}

A B

A< B

A>C

{1,2}

C

After pruning A= 3: Not arc consistent anymore: For C=2, there is no value in dom(A) that satisfies A>C: prune!

15

Page 16: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

• When we reduce the domain of a variable X to make an arc X,c arc consistent, which arcs do we need to reconsider?

Which arcs need to be reconsidered?

every arc Z,c' where c’ c involves Z and X:

Z1 c1

Z2 c2

Z3 c3

Yc

THESE

X

Ac4 16

Page 17: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

• When we reduce the domain of a variable X to make an arc X,c arc consistent, which arcs do we need to reconsider?

Which arcs need to be reconsidered?

every arc Z,c' where c’ c involves Z and X:

Z1 c1

Z2 c2

Z3 c3

Yc

THESE

X

Ac4

Pruning elements of Dom(x) may remove values that made one or more of Z,c' consistent

17

Page 18: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

• When we reduce the domain of a variable X to make an arc X,c arc consistent, which arcs do we need to reconsider?

You do not need to reconsider other arcs- If arc Y,c was arc consistent before, it will still be arc consistent “Consistent before” means each element yi in Y must have an element xi in X that

satisfies the constraint. Those xi would not be pruned from Dom(X), so arc Y,c stays consistent

- If an arc X,ci was arc consistent before, it will still be arc consistent

The domains of Zi have not been touched

- Nothing changes for arcs of constraints not involving X

Which arcs need to be reconsidered?

Z1 c1

Z2 c2

Z3 c3

Yc

THESE

X

Ac4

18

Page 19: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

• Consider the arcs in turn, making each arc consistent• Reconsider arcs that could be made inconsistent again by

this pruning

• DO trace on ‘simple problem 1’ and on ‘scheduling problem 1’, trying to predict

- which arcs are not consistent and - which arcs need to be reconsidered after each removal

in

Which arcs need to be reconsidered?

19

Page 20: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Arc consistency algorithm (for binary constraints)

Procedure GAC(V,dom,C)           Inputs                     V: a set of variables                     dom: a function such that dom(X) is the domain of variable X                     C: set of constraints to be satisfied           Output                     arc-consistent domains for each variable           Local                     DX is a set of values for each variable X                    TDA is a set of arcs

1:           for each variable X do2:                     DX ←dom(X) 3:           TDA ←{ X,c | X ∈ V, c ∈ C  and X ∈ scope(c)} ⟨ ⟩         4:           while (TDA {}) 5:                     select X,c ∈TDA⟨ ⟩6:                     TDA ←TDA  \ { X,c }⟨ ⟩7:                     NDX ←{x| x ∈ DX and y ∈ DY s.t. (x, y) satisfies c}8:                     if (NDX DX) then 9:                               TDA ←TDA ∪ { Z,c' | X ∈ scope(c'), c' ⟨ ⟩ c, Z ∈ scope(c') \ {X} } 10:                             DX ←NDX                     11:           return {DX| X  is a variable}

Scope of constraint c is the set of variables involved in that constraint

NDX: values x for X for which there is a value for y supporting x

X’s domain changed: arcs (Z,c’) for variables Z sharing a constraint c’ with X could become inconsistent, thus are added to TDA

TDA: ToDoArcs,blue arcsin AIspace

If arc was inconsistent

Domain is reduced20

Page 21: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

A. O(n*d)

B. O(dn)

C. O(nd)

D. O(n * d2)

Arc Consistency Algorithm: Complexity• Let’s determine Worst-case complexity of this procedure

(compare with DFS)• let the max size of a variable domain be d• let the number of variables be n• Worst-case time complexity of Backtracking (DFS with

pruning?)

Check unary constraints on V1

If not satisfied = PRUNE

Check constraints on V1

and V2 If not satisfied = PRUNE

V1 = v1

V2 = v1

V1 = v1

V2 = v2

V1 = v1

V2 = vk

V1 = v1

V2 = v1

V3 = v2

V1 = v1

V2 = v1

V3 = v1

{}

V1 = v1 V1 = vk

21

Page 22: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Arc Consistency Algorithm: Complexity• Let’s determine Worst-case complexity of this procedure

(compare with DFS……O(dn)……..)• let the max size of a variable domain be d• let the number of variables be n• The max number of binary constraints is ?

A. n * d

B. d * d

C. (n * (n-1)) / 2

D. (n * d) / 222

Page 23: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

B. O(d)

C. O(n*d)A. O(n)

Arc Consistency Algorithm: Complexity• Let’s determine Worst-case complexity of this procedure

(compare with DFS O(dn) )• let the max size of a variable domain be d• let the number of variables be n• The max number of binary constraints is ? (n * (n-1)) / 2

• How many times, at worst, the same arc can be inserted in the ToDoArc list?

D. O(d2)

23

Page 24: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Arc Consistency Algorithm: Complexity• Let’s determine Worst-case complexity of this

procedure (compare with DFS O(dn) )• let the max size of a variable domain be d• let the number of variables be n• The max number of binary constraints is ?

(n * (n-1)) / 2• How many times, at worst, the same arc

can be inserted in the ToDoArc list? O(d)

• How many steps are involved in checking the consistency of an arc?

A. O(n2) B. O(d) C. O(n * d) D. O(d2)

24

Page 25: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Arc Consistency Algorithm: Complexity• Let’s determine Worst-case complexity of this

procedure (compare with DFS O(dn) )• let the max size of a variable domain be d• let the number of variables be n• The max number of binary constraints is ?

(n * (n-1)) / 2• How many times, at worst, the same arc

can be inserted in the ToDoArc list? O(d)• How many steps are involved in checking

the consistency of an arc? O(d2)

• Overall complexity: O(n2d3) • Compare to O(dN) of DFS!! Arc consistency is MUCH faster

25

Page 26: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Can we have an arc consistent network with non-empty domains

that has no solution?

YES

• Example: vars A, B, C with domain {1, 2} and constraints A ≠ B, B ≠ C, A ≠ C

• Or see AIspace CSP applet Simple Problem 2

26

Page 27: Computer Science CPSC 322 Lecture 13 Arc Consistency (4.5, 4.6 ) Slide 1

Learning Goals for CSP• Define/read/write/trace/debug the arc consistency algorithm. Compute its

complexity and assess its possible outcomes

------------------------------------

To Do

Start Assignment 2

Do Practice Exercises 4a and 4b

Read Stocastic Local Search (4.8.1)

27