Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space...

50
CS 343H: Honors Artificial Intelligence Search Prof. Peter Stone University of Texas at Austin [These slides were created by Dan Klein and Pieter Abbeel for CS188 Intro to AI at UC Berkeley. All CS188 materials are available at http://ai.berkeley.edu.]

Transcript of Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space...

Page 1: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

CS343H:HonorsArtificialIntelligenceSearch

Prof.PeterStone

UniversityofTexasatAustin[TheseslideswerecreatedbyDanKleinandPieterAbbeelforCS188IntrotoAIatUCBerkeley.AllCS188materialsareavailableathttp://ai.berkeley.edu.]

Page 2: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

Today

▪ AgentsthatPlanAhead

▪ SearchProblems

▪ UninformedSearchMethods

▪ Depth-FirstSearch▪ Breadth-FirstSearch▪ Uniform-CostSearch

Page 3: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

AgentsthatPlan

Page 4: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

ReflexAgents

▪ Reflexagents:▪ Chooseactionbasedoncurrentpercept(and

maybememory)▪ Mayhavememoryoramodeloftheworld’s

currentstate▪ Donotconsiderthefutureconsequencesof

theiractions▪ ConsiderhowtheworldIS

▪ Canareflexagentberational?

Page 5: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

VideoofDemoReflex—Success

Page 6: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

VideoofDemoReflex—Stuck

Page 7: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

PlanningAgents

▪ Planningagents:▪ Ask“whatif”▪ Decisionsbasedon(hypothesized)consequences

ofactions▪ Musthaveamodelofhowtheworldevolvesin

responsetoactions▪ Mustformulateagoal(test)▪ ConsiderhowtheworldWOULDBE

▪ Optimalvs.completeplanning

▪ Planningvs.replanning

Page 8: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

VideoofDemo—Suboptimallocalreplanning

Page 9: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

VideoofDemo—Globallyoptimalplanning

Page 10: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

SearchProblems

Page 11: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

SearchProblems

▪ Asearchproblemconsistsof:

▪ Astatespace

▪ Asuccessorfunction (withactions,costs)

▪ Astartstateandagoaltest

▪ Asolutionisasequenceofactions(aplan)whichtransformsthestartstatetoagoalstate

“N”,1.0

“E”,1.0

Page 12: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

SearchProblemsAreModels

Page 13: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

Example:TravelinginRomania

▪ Statespace:▪ Cities

▪ Successorfunction:▪ Roads:Gotoadjacentcitywith

cost=distance▪ Startstate:

▪ Arad▪ Goaltest:

▪ Isstate==Bucharest?

▪ Solution?

Page 14: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

What’sinaStateSpace?

▪ Problem:Pathing▪ States:(x,y)location▪ Actions:NSEW▪ Successor:updatelocation

only▪ Goaltest:is(x,y)=END

▪ Problem:Eat-All-Dots▪ States:{(x,y),dotbooleans}▪ Actions:NSEW▪ Successor:updatelocation

andpossiblyadotboolean▪ Goaltest:dotsallfalse

Theworldstateincludeseverylastdetailoftheenvironment

Asearchstatekeepsonlythedetailsneededforplanning(abstraction)

Page 15: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

StateSpaceSizes?

▪ Worldstate:▪ Agentpositions:120▪ Foodcount:30▪ Ghostpositions:12▪ Agentfacing:NSEW

▪ Howmany▪ Worldstates? 120x(230)x(122)x4(>74trillion!)▪ Statesforpathing? 120▪ Statesforeat-all-dots? 120x(230)(>128billion)

Page 16: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

Quiz:SafePassage

▪ Problem:eatalldotswhilekeepingtheghostsperma-scared▪ Whatdoesthestatespacehavetospecify?▪ (agentposition,dotbooleans,powerpelletbooleans,remainingscaredtime)

Page 17: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

StateSpaceGraphsandSearchTrees

Page 18: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

StateSpaceGraphs

▪ Statespacegraph:Amathematicalrepresentationofasearchproblem▪ Nodesare(abstracted)worldconfigurations▪ Arcsrepresentsuccessors(actionresults)▪ Thegoaltestisasetofgoalnodes(maybeonlyone)

▪ Inastatespacegraph,eachstateoccursonlyonce!

▪ Wecanrarelybuildthisfullgraphinmemory(it’stoobig),butit’sausefulidea

Page 19: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

StateSpaceGraphs

S

G

d

b

p q

c

e

h

a

f

r

Tinysearchgraphforatinysearchproblem

▪ Statespacegraph:Amathematicalrepresentationofasearchproblem▪ Nodesare(abstracted)worldconfigurations▪ Arcsrepresentsuccessors(actionresults)▪ Thegoaltestisasetofgoalnodes(maybeonlyone)

▪ Inastatespacegraph,eachstateoccursonlyonce!

▪ Wecanrarelybuildthisfullgraphinmemory(it’stoobig),butit’sausefulidea

Page 20: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

SearchTrees

▪ Asearchtree:▪ A“whatif”treeofplansandtheiroutcomes▪ Thestartstateistherootnode▪ Childrencorrespondtosuccessors▪ Nodesshowstates,butcorrespondtoPLANSthatachievethosestates▪ Formostproblems,wecanneveractuallybuildthewholetree

“E”,1.0“N”,1.0

Thisisnow/start

Possiblefutures

Page 21: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

StateSpaceGraphsvs.SearchTrees

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 Ga

S

G

d

b

p q

c

e

h

a

f

r

Weconstructbothondemand–andweconstructaslittleas

possible.

EachNODEinthesearchtreeisanentirePATHinthestatespacegraph.

SearchTreeStateSpaceGraph

Page 22: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

Quiz:StateSpaceGraphsvs.SearchTrees

S G

b

a

Considerthis4-stategraph:

Important:Lotsofrepeatedstructureinthesearchtree!

Howbigisitssearchtree(fromS)?

Sowhywouldweeveruseasearchtree?

1)Cannotstore“closedlist”(previouslyvisitednodes)2)Graphhappenstobeatree,sonoreasontostoreclosedlist

Page 23: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

TreeSearch

Page 24: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

SearchExample:Romania

Page 25: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

SearchingwithaSearchTree

▪ Search:▪ Expandoutpotentialplans(treenodes)▪ Maintainafringeofpartialplansunderconsideration▪ Trytoexpandasfewtreenodesaspossible

Page 26: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

GeneralTreeSearch

▪ Importantideas:▪ Fringe▪ Expansion▪ Explorationstrategy

▪ Mainquestion:whichfringenodestoexplore?

Page 27: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

Depth-FirstSearch

Page 28: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

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 29: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

SearchAlgorithmProperties

Page 30: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

SearchAlgorithmProperties

▪ Complete:Guaranteedtofindasolutionifoneexists?▪ Optimal:Guaranteedtofindtheleastcostpath?▪ Timecomplexity?▪ Spacecomplexity?

▪ Cartoonofsearchtree:▪ bisthebranchingfactor▪ misthemaximumdepth▪ solutionsatvariousdepths

▪ Numberofnodesinentiretree?▪ 1+b+b2+….bm=O(bm)

…b

1 nodeb nodes

b2 nodes

bm nodes

m tiers

Page 31: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

Depth-FirstSearch(DFS)Properties

…b

1 nodeb nodes

b2 nodes

bm nodes

m tiers

▪ WhatnodesDFSexpand?▪ Someleftprefixofthetree.▪ Couldprocessthewholetree!▪ Ifmisfinite,takestimeO(bm)

▪ Howmuchspacedoesthefringetake?▪ Onlyhassiblingsonpathtoroot,soO(bm)

▪ Isitcomplete?▪ mcouldbeinfinite,soonlyifwepreventcycles

(morelater)

▪ Isitoptimal?▪ No,itfindsthe“leftmost”solution,regardlessof

depthorcost

Page 32: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

Breadth-FirstSearch

Page 33: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

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 34: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

Breadth-FirstSearch(BFS)Properties

▪ WhatnodesdoesBFSexpand?▪ Processesallnodesaboveshallowestsolution▪ Letdepthofshallowestsolutionbes▪ SearchtakestimeO(bs)

▪ Howmuchspacedoesthefringetake?▪ Hasroughlythelasttier,soO(bs)

▪ Isitcomplete?▪ smustbefiniteifasolutionexists,soyes!

▪ Isitoptimal?▪ Onlyifcostsareall1(moreoncostslater)

…b

1 nodeb nodes

b2 nodes

bm nodes

s tiers

bs nodes

Page 35: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

Quiz:DFSvsBFS

Page 36: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

Quiz:DFSvsBFS

▪ WhenwillBFSoutperformDFS?

▪ WhenwillDFSoutperformBFS?

▪ Whatistheworstcaseforeach?

Page 37: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

VideoofDemoMazeWaterDFS/BFS(part1)

Page 38: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

VideoofDemoMazeWaterDFS/BFS(part2)

Page 39: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

IterativeDeepening

…b

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

▪ Isn’tthatwastefullyredundant?▪ Generallymostworkhappensinthelowestlevelsearched,sonotsobad!

Page 40: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

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 41: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

UniformCostSearch

Page 42: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

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: expand a cheapest node first:

Fringe is a priority queue (priority: cumulative cost) 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

Cost contours

2

Page 43: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

UniformCostSearch(UCS)Properties

▪ WhatnodesdoesUCSexpand?▪ Processesallnodeswithcostlessthancheapestsolution!▪ IfthatsolutioncostsC* andarcscostatleastε , thenthe“effective

depth”isroughlyC*/ε▪ TakestimeO(bC*/ε)(exponentialineffectivedepth)

▪ Howmuchspacedoesthefringetake?▪ Hasroughlythelasttier,soO(bC*/ε)

▪ Isitcomplete?▪ Assumingbestsolutionhasafinitecostandminimumarccostispositive,

yes!

▪ Isitoptimal?▪ Yes!(ProofnextlectureviaA*)

b

C*/ε “tiers”c ≤ 3

c ≤ 2

c ≤ 1

Page 44: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

UniformCostIssues

▪ Remember:UCSexploresincreasingcostcontours

▪ Thegood:UCSiscompleteandoptimal!

▪ Thebad:▪ Exploresoptionsinevery“direction”▪ Noinformationaboutgoallocation

▪ We’llfixthatsoon!Start Goal

c ≤ 3c ≤ 2

c ≤ 1

Page 45: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

VideoofDemoEmptyUCS

Page 46: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

VideoofDemoMazewithDeep/ShallowWater---DFS,BFS,orUCS?(part1)

Page 47: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

VideoofDemoMazewithDeep/ShallowWater---DFS,BFS,orUCS?(part2)

Page 48: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

VideoofDemoMazewithDeep/ShallowWater---DFS,BFS,orUCS?(part3)

Page 49: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

TheOneQueue

▪ Allthesesearchalgorithmsarethesameexceptforfringestrategies▪ Conceptually,allfringesarepriorityqueues(i.e.collectionsofnodeswithattachedpriorities)

▪ Practically,forDFSandBFS,youcanavoidthelog(n)overheadfromanactualpriorityqueue,byusingstacksandqueues

▪ Canevencodeoneimplementationthattakesavariablequeuingobject

Page 50: Search - University of Texas at Austinpstone/Courses/343Hfall17/... · Idea: get DFS’s space advantage with BFS’s time / shallow-solution advantages Run a DFS with depth limit

SearchandModels

▪ Searchoperatesovermodelsoftheworld▪ Theagentdoesn’tactuallytryalltheplansoutintherealworld!

▪ Planningisall“insimulation”

▪ Yoursearchisonlyasgoodasyourmodels…