37
Python Programming Source: Beazley, David; Jones, Brian K. (2013). Python Cookbook (3rd ed.).

Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Python Programming

Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).

Page 2: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Python Programming

Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).

Page 3: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Python Programming

Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).

Page 4: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Python Programming

Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).

Page 5: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Python Programming

Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).

Page 6: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Python Programming

Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).

Page 7: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Python Programming

Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).

Page 8: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Python Programming

Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).

Page 9: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Python Programming

Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).

Page 10: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Python Programming

Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).

Page 11: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Breadth-FirstSearch

S

a

b

d p

a

c

e

p

h

f

r

q

q c G

a

qe

p

h

f

r

q

q c G

a

S

G

d

b

p q

ce

h

a

f

r

Search

Tiers

Strategy:expandashallowestnodefirst

Implementation:FringeisaFIFOqueue

Page 12: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Depth-FirstSearch

S

a

b

d p

a

c

e

p

h

f

r

q

q c G

a

qe

p

h

f

r

q

q c G

a

S

G

d

b

p q

c

e

h

a

f

rqph

fd

ba

c

e

r

Strategy:expandadeepestnodefirst

Implementation:FringeisaLIFOstack

Page 13: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

DFSvsBFS

Page 14: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

IterativeDeepening

…b

• Idea:getDFS’sspaceadvantagewithBFS’stime/shallow-solutionadvantages• RunaDFSwithdepthlimit1.Ifnosolution…• RunaDFSwithdepthlimit2.Ifnosolution…• RunaDFSwithdepthlimit3.…..

• Isn’tthatwastefullyredundant?• Generallymostworkhappensinthelowestlevelsearched,sonotsobad!

Page 15: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Python Code for IterativeDeepening

Page 16: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Cost-SensitiveSearch

BFSfindstheshortestpathintermsofnumberofactions.Itdoesnotfindtheleast-costpath.Wewillnowcoverasimilaralgorithmwhichdoesfindtheleast-costpath.

START

GOAL

d

b

pq

c

e

h

a

f

r

2

9 2

81

8

2

3

2

4

4

15

1

32

2

Page 17: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

UniformCostSearch

S

a

b

d p

a

c

e

p

h

f

r

q

q c G

a

qe

p

h

f

r

q

q c G

a

Strategy:expandacheapestnodefirst:

Fringeisapriorityqueue(priority:cumulativecost) S

G

d

b

p q

c

e

h

a

f

r

3 9 1

16411

5

713

8

1011

17 11

0

6

39

1

1

2

8

8 2

15

1

2

Costcontours

2

Page 18: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

UniformCostSearch(UCS)Properties

• WhatnodesdoesUCSexpand?• Processesallnodeswithcostlessthancheapestsolution!• IfthatsolutioncostsC* andarcscostatleaste , thenthe“effectivedepth”isroughlyC*/e

• TakestimeO(bC*/e)(exponentialineffectivedepth)

• Howmuchspacedoesthefringetake?• Hasroughlythelasttier,soO(bC*/e)

• Isitcomplete?• Assumingbestsolutionhasafinitecostandminimumarccostispositive,yes!

• Isitoptimal?• Yes!(skippingtheprooffornow)

b

C*/e “tiers”c£ 3

c£ 2c£ 1

Page 19: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

UniformCostIssues

• Remember:UCSexploresincreasingcostcontours

• Thegood:UCSiscompleteandoptimal!

• Thebad:• Exploresoptionsinevery“direction”• Noinformationaboutgoallocation

• We’llfixthatsoon!Start Goal

c£ 3c£ 2

c£ 1

Page 20: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

BFS/DFS/UCS

• Breadth-firstsearch• Good:optimal,workswellwhenmanyoptions,butnotmanyactionsrequired• Bad:assumesallactionshaveequalcost

• Depth-firstsearch• Good:memory-efficient,workswellwhenfewoptions,butlotsofactionsrequired• Bad:notoptimal,canruninfinitely,assumesallactionshaveequalcost

• Uniform-costsearch• Good:optimal,handlesvariable-costactions• Bad:exploresalloptions,noinformationaboutgoallocation BasicallyDijkstra’s

Algorithm!

Page 21: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

S

a

b

d p

a

c

e

p

h

f

r

q

q c G

a

qe

p

h

f

r

q

q c G

a

Strategy:expandacheapestnodefirst:

Fringeisapriorityqueue(priority:cumulativecost) S

G

d

b

p q

c

e

h

a

f

r

3 9 1

16411

5

713

8

1011

17 11

0

6

39

1

1

2

8

8 2

15

1

2

Costcontours

2

Dijkstra‘salgorithm (Uniform-cost search)

Page 22: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Searchexample:PancakeProblem

Rule: a spatula canbeinsertedatanyinterval andflipallpancakesaboveit.Cost:Numberofpancakes flipped.

Page 23: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

PancakeBFS

Draw it by yourself!

Page 24: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

PancakeUCS

Draw it by yourself!

Page 25: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

PancakeDFS

3

2

4

3

3

2

2

2

4

Statespacegraphwithcostsasweights

34

3

4

2

Start

Goal

3Cost:16#Steps:6

Page 26: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

PancakeOptimal

3

2

4

3

3

2

2

2

4

Statespacegraphwithcostsasweights

34

3

4

2

Start

Goal

3Cost:7#Steps:2

Page 27: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Project 1: Search (due 09/13)

Page 28: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Incorporatinggoalinformation

Howtoefficiently solvesearchproblemswithvariable-costactions,usinginformation

aboutthegoalstate?

Ø HeuristicsØ GreedyapproachØ A*search

Page 29: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

SearchHeuristics

§ Aheuristicis:§ Afunctionthatestimates howcloseastateistoagoal§ Designedforaparticularsearchproblem§ Examples:Manhattandistance,Euclideandistancefor

pathing

10

5

11.2

Note thattheheuristicisapropertyofthestate,nottheactiontakentogettothestate!

Page 30: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

PancakeHeuristics

Heuristic1:thenumberofpancakes thatare outofplace

43

0

2

3

3

3

4

4

3

4

4

4

h(x)Start

Goal

Page 31: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

PancakeHeuristics

Heuristic2:howmanypancakesareontopofasmallerpancake?

12

0

1

1

1

1

2

2

1

1

2

1

h(x)Start

Goal

Page 32: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

PancakeHeuristics

Heuristic3:Allzeros(akanullheuristic,or”Ilikewafflesbetteranyway”)

00

0

0

0

0

0

0

0

0

0

0

0

h(x)Start

Goal

Page 33: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Straight-lineHeuristicinRomania

h(x)

Page 34: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

GreedySearch

Page 35: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

GreedyStraight-LineSearchinRomania

h(x)

Greedyo Cost:450

Optimalo Cost:418

• Expandthenodethatseemsclosest…

Page 36: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

GreedySearch

• Strategy:expandanodethatyouthinkisclosesttoagoalstate• Heuristic:estimateofdistancetonearestgoalforeachstate

• Acommoncase:• Best-firsttakesyoustraighttothe(non-optimal)goal

• Worst-case:likeabadly-guidedDFS

• Whatgoeswrong?• Doesn’ttakereal pathcostintoaccount

…b

…b

Page 37: Python Programming · 2020-06-09 · • Run a DFS with depth limit 1. ... Project1: Search (due 09/13) Incorporating goal information How to efficientlysolve search problems with

Next class

A*search