36
Backward Checking Search BT, BM, BJ, CBJ

Backward Checking Search BT, BM, BJ, CBJ

  • Upload
    tress

  • View
    31

  • Download
    0

Embed Size (px)

DESCRIPTION

Backward Checking Search BT, BM, BJ, CBJ. An example problem. 1. 2. 3. 4. 5. Colour each of the 5 nodes, such that if they are adjacent, they take different colours. Representation (csp1). 1. 2. 3. 4. 5. variables v1, v2, v3, v4, and v5 domains d1, d2, d3, d4, and d5 - PowerPoint PPT Presentation

Citation preview

Page 1: Backward Checking Search BT, BM, BJ, CBJ

Backward Checking SearchBT, BM, BJ, CBJ

Page 2: Backward Checking Search BT, BM, BJ, CBJ
Page 3: Backward Checking Search BT, BM, BJ, CBJ
Page 4: Backward Checking Search BT, BM, BJ, CBJ

An example problem

Colour each of the 5 nodes, such that if they are adjacent, they take different colours

1 2

3

4

5

Page 5: Backward Checking Search BT, BM, BJ, CBJ

Representation (csp1)

• variables v1, v2, v3, v4, and v5• domains d1, d2, d3, d4, and d5• domains are the three colours {R,B,G}• constraints

• v1 v2• v1 v3• v2 v3• v2 v5• v3 v5• v4 v5

Assign a value to each variable, from its domain, such that the constraints are satisfied

CSP = (V,D,C)

1 2

3

4

5

Page 6: Backward Checking Search BT, BM, BJ, CBJ

Chronological Backtracking (BT)

1 2

3

4

5

bt-label(i,v,d,cd,n)begin if i > n then return “solution” else begin consistent := false; for v[i] cd[i] while ¬consistent do begin consistent := true; for h := 1 to i-1 while consistent do consistent := check(v,i,h); if ¬consistent then cd[i] := cd[i] \ v[i]; end if consistent then bt-label(i+1,v,d,cd,n) else bt-unlabel(i,v,d,cd,n)end

i the index of the current variableh the index of a past variable (local)v an array of variables to be instantiatedd an array of domainscd an array of current domainsn the number of variables

constraints are binary.consistent is boolean (local)bt(v,d,n) = for i := 1 to n cd[i] := d[i]; return bt-label(1,v,d,cd,n)

As a pair of mutually recursive functions

bt-label

Page 7: Backward Checking Search BT, BM, BJ, CBJ

Chronological Backtracking (BT)

1 2

3

4

5

bt-label(i,v,d,cd,n)begin if i > n then return “solution” else begin consistent := false; for v[i] cd[i] while ¬consistent do begin consistent := true; for h := 1 to i-1 while consistent do consistent := check(v,i,h); if ¬consistent then cd[i] := cd[i] \ v[i]; end if consistent then bt-label(i+1,v,d,cd,n) else bt-unlabel(i,v,d,cd,n)end

Find a consistent instantiation for v[i]

bt-label

Page 8: Backward Checking Search BT, BM, BJ, CBJ

Chronological Backtracking (BT)

1 2

3

4

5

bt-label(i,v,d,cd,n)begin if i > n then return “solution” else begin consistent := false; for v[i] cd[i] while ¬consistent do begin consistent := true; for h := 1 to i-1 while consistent do consistent := check(v,i,h); if ¬consistent then cd[i] := cd[i] \ v[i]; end if consistent then bt-label(i+1,v,d,cd,n) else bt-unlabel(i,v,d,cd,n)end

check backwards, from current to past

bt-label

Page 9: Backward Checking Search BT, BM, BJ, CBJ

Chronological Backtracking (BT)

1 2

3

4

5

bt-label(i,v,d,cd,n)begin if i > n then return “solution” else begin consistent := false; for v[i] cd[i] while ¬consistent do begin consistent := true; for h := 1 to i-1 while consistent do consistent := check(v,i,h); if ¬consistent then cd[i] := cd[i] \ v[i]; end if consistent then bt-label(i+1,v,d,cd,n) else bt-unlabel(i,v,d,cd,n)end

recurse

bt-label

Page 10: Backward Checking Search BT, BM, BJ, CBJ

Chronological Backtracking (BT)

1 2

3

4

5

bt-unlabel(i,v,d,cd,n)begin if i > 0 then return “fail” else begin h := i – 1; cd[h] := cd[h] \ v[h]; cd[i] := d[i]; if cd[h] ≠ nil then bt-label(h,v,d,cd,n) else bt-unlabel(h,v,d,cd,n) endend

bt-unlabel

Page 11: Backward Checking Search BT, BM, BJ, CBJ

Chronological Backtracking (BT)

1 2

3

4

5

bt-unlabel(i,v,d,cd,n)begin if i > 0 then return “fail” else begin h := i – 1; cd[h] := cd[h] \ v[h]; cd[i] := d[i]; if cd[h] ≠ nil then bt-label(h,v,d,cd,n) else bt-unlabel(h,v,d,cd,n) endend

Past variable

bt-unlabel

Page 12: Backward Checking Search BT, BM, BJ, CBJ

Chronological Backtracking (BT)

1 2

3

4

5

bt-unlabel(i,v,d,cd,n)begin if i > 0 then return “fail” else begin h := i – 1; cd[h] := cd[h] \ v[h]; cd[i] := d[i]; if cd[h] ≠ nil then bt-label(h,v,d,cd,n) else bt-unlabel(h,v,d,cd,n) endend

Reset domain (bactrackable/reversible variable)

bt-unlabel

Page 13: Backward Checking Search BT, BM, BJ, CBJ

Chronological Backtracking (BT)

1 2

3

4

5

bt-unlabel(i,v,d,cd,n)begin if i > 0 then return “fail” else begin h := i – 1; cd[h] := cd[h] \ v[h]; cd[i] := d[i]; if cd[h] ≠ nil then bt-label(h,v,d,cd,n) else bt-unlabel(h,v,d,cd,n) endend

recurse

bt-unlabel

Page 14: Backward Checking Search BT, BM, BJ, CBJ

search(n,status)begin consistent := true; status := “unknown”; i := 1; while status = “unknown” do begin if consistent then i := label(i,consistent) else i := unlabel(i,consistent); if i > n then status = “solution” else if i = 0 then status := “impossible” endend

bt-label(i,consistent)begin consistent := false; for v[i] in cd[i] while ¬consitent do begin consistent := true; for h := 1 to i-1 while consistent do consistent := check(v,i,h); if ¬consistent then cd[i] := cd[i] \ v[i]; end; if consistent then return i+1 else return Iend

bt-unlabel(i,consistent)begin h := i-1; cd[i] := d[i]; cd[h] := cd[h] \ v[h]; consistent := cd[h] ≠ nil return h;end

Iterative implementation of BT

This is more realistic. Why?

Page 15: Backward Checking Search BT, BM, BJ, CBJ

Chronological Backtracking (BT)

1 2

3

4

5

Page 16: Backward Checking Search BT, BM, BJ, CBJ

Three Different Views of Search

a tracea tree

past, current, future

Page 17: Backward Checking Search BT, BM, BJ, CBJ

• V[1] := R• V[2] := R

• check(v[1],v[2]) fails• V[2] := B

• check(v[1],v[2]) good• V[3] := R

• check(v[1],v[3]) fails• V[3] := B

• check(v[1],v[3]) good• check(v[2],v[3]) fails

• V[3] := G• check(v[1],v[3]) good• check(v[2],v[3]) good

• V[4] := R• V[5] := R

• check(v[2],v[5]) good• check(v[3],v[5]) good• check(v[4],v[5]) fails

• V[5] := B• check(v[2],v[5]) fails

• V[5] := G• check(v[2],v[5]) good• check(v[3],v[5]) fails

• backtrack!

• V[4] := B• V[5] := R

• check(v[2],v[5]) good• check(v[3],v[5]) good• check(v[4],v[5]) good

• solution found

A Trace of BT (assume domain ordered {R,B,G})

1 2

3

4

5

3

1 2

4

5

16 checks and 12 nodes

3

1 2

4

5

Page 18: Backward Checking Search BT, BM, BJ, CBJ

A Tree Trace of BT (assume domain ordered {R,B,G})

1 2

3

4

5v1

v2

v3

v4

v5

Page 19: Backward Checking Search BT, BM, BJ, CBJ

A Tree Trace of BT (assume domain ordered {R,B,G})

1 2

3

4

5v1

v2

v3

v4

v5

Page 20: Backward Checking Search BT, BM, BJ, CBJ

A Tree Trace of BT (assume domain ordered {R,B,G})

1 2

3

4

5v1

v2

v3

v4

v5

Page 21: Backward Checking Search BT, BM, BJ, CBJ

A Tree Trace of BT (assume domain ordered {R,B,G})

1 2

3

4

5v1

v2

v3

v4

v5

Page 22: Backward Checking Search BT, BM, BJ, CBJ

A Tree Trace of BT (assume domain ordered {R,B,G})

v1

v2

v3

v4

v5

1 2

3

4

5

Page 23: Backward Checking Search BT, BM, BJ, CBJ

A Tree Trace of BT (assume domain ordered {R,B,G})

v1

v2

v3

v4

v5

1 2

3

4

5

Page 24: Backward Checking Search BT, BM, BJ, CBJ

A Tree Trace of BT (assume domain ordered {R,B,G})

v1

v2

v3

v4

v5

1 2

3

4

5

Page 25: Backward Checking Search BT, BM, BJ, CBJ

A Tree Trace of BT (assume domain ordered {R,B,G})

v1

v2

v3

v4

v5

1 2

3

4

5

Page 26: Backward Checking Search BT, BM, BJ, CBJ

A Tree Trace of BT (assume domain ordered {R,B,G})

v1

v2

v3

v4

v5

1 2

3

4

5

Page 27: Backward Checking Search BT, BM, BJ, CBJ

A Tree Trace of BT (assume domain ordered {R,B,G})

v1

v2

v3

v4

v5

1 2

3

4

5

Page 28: Backward Checking Search BT, BM, BJ, CBJ

A Tree Trace of BT (assume domain ordered {R,B,G})

v1

v2

v3

v4

v5

1 2

3

4

5

Page 29: Backward Checking Search BT, BM, BJ, CBJ

A Tree Trace of BT (assume domain ordered {R,B,G})

v1

v2

v3

v4

v5

1 2

3

4

5

Page 30: Backward Checking Search BT, BM, BJ, CBJ

A Tree Trace of BT (assume domain ordered {R,B,G})

v1

v2

v3

v4

v5

1 2

3

4

5

Page 31: Backward Checking Search BT, BM, BJ, CBJ

Another view

1 2

3

4

5

v1

v2

v3

v4

v5

current variable

past variables

future variables

check back

Page 32: Backward Checking Search BT, BM, BJ, CBJ

1 2

3

4

5

Can you solve this (csp2)?

Try bt1 and/or bt2 with constraints C2

- load(“bt2”) - bt(V,D,C2)

this is csp2

Page 33: Backward Checking Search BT, BM, BJ, CBJ

Thrashing? (csp3c)

1 2

39

4

5

67

8

Page 34: Backward Checking Search BT, BM, BJ, CBJ

Thrashing? (csp3c)

1 2

39

4

5

67

8

V1 = Rv2 = Bv3 = Gv4 = Rv5 = Bv6 = Rv7 = Bv8 = Rv9 = conflict

The cause of the conflict with v9is v4, but what will bt do?

Find out how it goes with bt3 and csp4

Page 35: Backward Checking Search BT, BM, BJ, CBJ

Where’s the code?

See clairExamples/bt*.cl and clairExample/csp*.cl

Page 36: Backward Checking Search BT, BM, BJ, CBJ

questions

• Why measure checks and nodes?• What is a node anyway?• Who cares about checks?

• Why instantiate variables in lex order?• Would a different order make a difference?

• How many nodes could there be?• How many checks could there be?• Are all csp’s binary?• How can we represent constraints? • Is it reasonable to separate V from D?

• Why not have variables with domains• What is a constraint graph?

• How can (V,D,C) be a graph?• What is BT “thinking about”? i.e. why is it so dumb?• What could we do to make BT smarter?• Is BT doing any inferencing/propagation?• What’s the 1st reference to BT?

• Golomb & Baumert JACM 12, 1965?