Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Python Programming
Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).
Python Programming
Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).
Python Programming
Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).
Python Programming
Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).
Python Programming
Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).
Python Programming
Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).
Python Programming
Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).
Python Programming
Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).
Python Programming
Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).
Python Programming
Source: Beazley,David;Jones,BrianK.(2013). PythonCookbook (3rded.).
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
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
DFSvsBFS
IterativeDeepening
…b
• Idea:getDFS’sspaceadvantagewithBFS’stime/shallow-solutionadvantages• RunaDFSwithdepthlimit1.Ifnosolution…• RunaDFSwithdepthlimit2.Ifnosolution…• RunaDFSwithdepthlimit3.…..
• Isn’tthatwastefullyredundant?• Generallymostworkhappensinthelowestlevelsearched,sonotsobad!
Python Code for IterativeDeepening
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
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
…
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
UniformCostIssues
• Remember:UCSexploresincreasingcostcontours
• Thegood:UCSiscompleteandoptimal!
• Thebad:• Exploresoptionsinevery“direction”• Noinformationaboutgoallocation
• We’llfixthatsoon!Start Goal
…
c£ 3c£ 2
c£ 1
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!
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)
Searchexample:PancakeProblem
Rule: a spatula canbeinsertedatanyinterval andflipallpancakesaboveit.Cost:Numberofpancakes flipped.
PancakeBFS
Draw it by yourself!
PancakeUCS
Draw it by yourself!
PancakeDFS
3
2
4
3
3
2
2
2
4
Statespacegraphwithcostsasweights
34
3
4
2
Start
Goal
3Cost:16#Steps:6
PancakeOptimal
3
2
4
3
3
2
2
2
4
Statespacegraphwithcostsasweights
34
3
4
2
Start
Goal
3Cost:7#Steps:2
Project 1: Search (due 09/13)
Incorporatinggoalinformation
Howtoefficiently solvesearchproblemswithvariable-costactions,usinginformation
aboutthegoalstate?
Ø HeuristicsØ GreedyapproachØ A*search
SearchHeuristics
§ Aheuristicis:§ Afunctionthatestimates howcloseastateistoagoal§ Designedforaparticularsearchproblem§ Examples:Manhattandistance,Euclideandistancefor
pathing
10
5
11.2
Note thattheheuristicisapropertyofthestate,nottheactiontakentogettothestate!
PancakeHeuristics
Heuristic1:thenumberofpancakes thatare outofplace
43
0
2
3
3
3
4
4
3
4
4
4
h(x)Start
Goal
PancakeHeuristics
Heuristic2:howmanypancakesareontopofasmallerpancake?
12
0
1
1
1
1
2
2
1
1
2
1
h(x)Start
Goal
PancakeHeuristics
Heuristic3:Allzeros(akanullheuristic,or”Ilikewafflesbetteranyway”)
00
0
0
0
0
0
0
0
0
0
0
0
h(x)Start
Goal
Straight-lineHeuristicinRomania
h(x)
GreedySearch
GreedyStraight-LineSearchinRomania
h(x)
Greedyo Cost:450
Optimalo Cost:418
• Expandthenodethatseemsclosest…
GreedySearch
• Strategy:expandanodethatyouthinkisclosesttoagoalstate• Heuristic:estimateofdistancetonearestgoalforeachstate
• Acommoncase:• Best-firsttakesyoustraighttothe(non-optimal)goal
• Worst-case:likeabadly-guidedDFS
• Whatgoeswrong?• Doesn’ttakereal pathcostintoaccount
…b
…b
Next class
A*search