StateSpaceRepresenta,ons
andSearchAlgorithms
CS171,Fall2016
Introduc,ontoAr,ficialIntelligence
Prof.AlexanderIhler
Reading: R&N 3.1-3.4
ArchitecturesforIntelligence
• Search?
– Determinehowtoachievesomegoal;“whattodo”
• Logic&inference?
– Reasonaboutwhattodo
– Encodeknowledge/“expert”systems
– Knowwhattodo
• Learning?
– Learnwhattodo
• Modernview:complex&mul,-faceted
Search
• Formulate“whattodo”asasearchproblem
– Solu,ontellstheagentwhattodo
• Ifnosolu,oninthecurrentsearchspace?
– Findspacethatdoescontainasolu,on(usesearch!)
– Solveoriginalprobleminnewsearchspace
• Manypowerfulextensionstotheseideas
– Constraintsa,sfac,on;planning;gameplaying;…
• Humanproblem-solvingo[enlookslikesearch
Whysearch?
• Engagedinbigger,importantproblem
– Hitasearchsubproblemweneedtosolve
– Search,solveit,getbacktooriginalproblem
• Predicttheresultsofourac,onsinthefuture
• Manysequencesofac,ons,eachwithsomeu,lity
– Maximizeperformancemeasure
• Wanttoachievesomegoalbysome(any?)means
• Or,findthebest(op,mal)waytoachieveit
Example:Romania
• OnholidayinRomania
– CurrentlyinArad
– FlightleavestomorrowfromBucharest
• Formulategoal:
– BeinBucharest
• Formulateproblem:
– States: variousci,es
– Ac,ons:drivebetweenci,es/choosenextcity
• Findasolu,on:
– Sequenceofci,es,e.g.:Arad,Sibiu,Fagaras,Bucharest
Example:Romania
Oradea
Neamt
71
75
Zerind
87
151
Iasi
140
Arad
Sibiu
92
Fagaras
99
118
Vaslui
Timisoara
80
Rimnicu Vilcea
97
111
211
Pitesti
Lugoj
70
Mehadia
75
Dobreta
120
142
146
85
101
138
Cralova
98
Urziceni
Bucharest
90
Giurgiu
Hirsova
86
Eforie
Problemtypes
Classifyingtheenvironment:
• Sta,c/Dynamic
Previousproblemwassta,c:noaaen,ontochangesinenvironment
• Determinis,c/Stochas,c
Previousproblemwasdeterminis,c:nonewpercepts
werenecessary,wecanpredictthefutureperfectly
• Observable/Par,allyObservable/Unobservable
Previousproblemwasobservable:itknewtheini,alstate,&c
• Discrete/Con,nuous
Previousproblemwasdiscrete:wecanenumerateallpossibili,es
WhynotDijkstra’sAlgorithm?
• D’salgorithminputstheen,regraph
– Wanttosearchinunknownspaces
– Combinesearchwith“explora,on”
– Ex:autonomousroveronMarsmustsearchanunknownspace
• D’salgorithmtakesconnec,onsasgiven
– Wanttosearchbasedonagent’sac,ons,w/unknownconnec,ons
– Ex:webcrawlermaynotknowwhatconnec,onsareavailableonaURL
beforevisi,ngit
– Ex:agentmaynotknowtheresultofanac,onbeforetryingit
• D’salgorithmwon’tworkoninfinitespaces
– Manyac,onsspacesareinfiniteoreffec,velyinfinite
– Ex:logicalreasoningspaceisinfinite
– Ex:realworldisessen,allyinfinitetoahuman-sizeagent
Ex:VacuumWorld
• Observable,startin#5.
• Solu,on?
[Right,Suck]
1
2
3
4
5
6
7
8
Vacuumworldstatespacegraph
R
L
R
L
S
S
R
R
L
R
L
R
L
L
S
S
S
R
L
R
L
S
S
S
Ex:VacuumWorld
• Unobservable,
startin{1,2,3,4,5,6,7,8}
• Solu,on?
[Right,Suck,Le[,Suck]
1
2
3
4
5
6
7
8
Ex:VacuumWorld
State-SpaceProblemFormula,on
Oradea
71
Neamt
Aproblemisdefinedbyfouritems:
ini5alstatee.g.,"atArad“
ac5onsAc#ons(s)=setofac,onsavail.instates
transi5onmodelResults(s,a)=statethatresultsfromac,onainstates
Alt:successorfunc,onS(x)=setofac,on–statepairs
75
Zerind 151
140
Arad
Sibiu 99
Fagaras
87
Iasi
92
118
Timisoara 80
97
111
70
Lugoj
Pitesti
Dobreta
120
211
85
Mehadia 146
75
Vaslui
Rimnicu Vilcea
101
98
Urziceni
Bucharest
138
Cralova
142
90
Giurgiu
Hirsova
86
Eforie
– e.g.,S(Arad)={<AradàZerind,Zerind>,…}
goaltest,(orgoalstate)
e.g.,x="atBucharest”,Checkmate(x)
pathcost(addi,ve)
– e.g.,sumofdistances,numberofac,onsexecuted,etc.
– c(x,a,y)isthestepcost,assumedtobe≥0
Asolu5onisasequenceofac,onsleadingfromtheini,alstatetoagoalstate
Selec,ngastatespace
• Realworldisabsurdlycomplex
à statespacemustbeabstractedforproblemsolving
• (Abstract)stateßsetofrealstates
• (Abstract)ac,onßcomplexcombina,onofrealac,ons
– e.g.,"AradàZerind"representsacomplexsetofpossibleroutes,detours,
reststops,etc.
• Forguaranteedrealizability,anyrealstate"inArad”mustgetto
somerealstate"inZerind”
• (Abstract)solu,onßsetofrealpathsthataresolu,onsintherealworld
• Eachabstractac,onshouldbe"easier"thantheoriginalproblem
Ex:VacuumWorld
• States?
– Discrete:dirt,loca,on
• Ini,alstate?
1
2
3
4
5
6
7
8
– Any
• Ac,ons?
– Le[,Right,Suck
• Goaltest?
– Nodirtatallloca,ons
• Pathcost?
– 1perac,on
Ex:8-Queens
Placeasmanyqueensaspossible
onthechessboardwithoutcapture
• states?-anyarrangementofn<=8queens
-orarrangementsofn<=8queensinle[mostn
columns,1percolumn,suchthatnoqueen
aaacksanyother.
• ini,alstate?noqueensontheboard
• ac,ons?-addqueentoanyemptysquare
-oraddqueentole[mostemptysquaresuch
thatitisnotaaackedbyotherqueens.
• goaltest?8queensontheboard,noneaaacked.
• pathcost?1permove
Ex:8-Queens
Placeasmanyqueensaspossible
onthechessboardwithoutcapture
• states?-anyarrangementofn<=8queens
-orarrangementsofn<=8queensinle[mostn
columns,1percolumn,suchthatnoqueen
aaacksanyother.
• ini,alstate?noqueensontheboard
• ac,ons?-addqueentoanyemptysquare
-oraddqueentole[mostemptysquaresuch
thatitisnotaaackedbyotherqueens.
• goaltest?8queensontheboard,noneaaacked.
• pathcost?1permove(notrelevant…)
Ex:Robo,cassembly
• states?Real-valuedcoordinatesofrobotjointangles&partsof
•
•
•
•
theobjecttobeassembled
ini,alstate?Restconfigura,on
ac,ons?Con,nuousmo,onsofrobotjoints
goaltest?Completeassembly
pathcost?Timetoexecute&energyused
Ex:Sliding,lepuzzle
•
•
•
•
•
2
8
3
1
2
3
1
6
4
4
5
6
5
7
8
7
Start State
states?
ini,alstate?
ac,ons?
goaltest?
pathcost?
Goal State
Try it yourselves…
Ex:Sliding,lepuzzle
•
•
•
•
•
2
8
3
1
2
3
1
6
4
4
5
6
5
7
8
7
Start State
Goal State
# of states: n+1! / 2
8-puzzle: 181,440 states
15-puzzle: 1.3 trillion
24-puzzle: 10^25
states?Loca,onsof,les
ini,alstate?Given
ac,ons?Moveblanksquareup/down/le[/right
goaltest?Goalstate(given)
pathcost?1permove
Optimal solution of
n-Puzzle family is NP-hard
Importanceofrepresenta,on
• Defini,onof“state”canbeveryimportant
• Agoodrepresenta,on
– Revealsimportantfeatures
– Hidesirrelevantdetail
– Exposesusefulconstraints
– Makesfrequentopera,onseasytodo
– Supportslocalinferencesfromlocalfeatures
• Called“sodastraw”principle,or“locality”principle
• Inferencefromfeatures“throughasodastraw”
– Rapidlyorefficientlycomputable
• It’snicetobefast
Reveals important features / Hides irrelevant detail
• In search: A man is traveling to market with a fox, a goose, and a
bag of oats. He comes to a river. The only way across the river is a
boat that can hold the man and exactly one of the fox, goose or bag of
oats. The fox will eat the goose if left alone with it, and the goose will
eat the oats if left alone with it.
How can the man get all his possessions safely across the river?
• A good representation makes this problem easy:
1110
1110
0010
1010
1111
0001
0101
0000
1010
MFGO
0100
0010
1101
1011
0001
0101
1111
M = man
F = fox
G = goose
O = oats
0 = starting side
1 = ending side
Exposes useful constraints
• In logic:
If the unicorn is mythical, then it is immortal, but if it is not
mythical, then it is a mortal mammal. If the unicorn is either
immortal or a mammal, then it is horned. The unicorn is magical if
it is horned.
Prove that the unicorn is both magical and horned.
• A good representation makes this problem easy (as we’ll see when
we do our unit on logic):
(¬Y˅¬R)^(Y˅R)^(Y˅M)^(R˅H)^(¬M˅H)^(¬H˅G)
Makesfrequentopera,onseasy-to-do
• Romannumerals
• M=1000,D=500,C=100,L=50,X=10,V=5,I=1
• 2000=MM;1776=MDCCLXXVI
• Longdivisionisverytedious(tryMDCCLXXVI/XVI)
• Tes,ngforN<1000isveryeasy(firstleaerisnot“M”)
• Arabicnumerals
• 0,1,2,3,4,5,6,7,8,9,“.”
• Longdivisionismucheasier(try1776/16)
• Tes,ngforN<1000isslightlyharder(havetoscanthe
string)
Localinferencesfromlocalfeatures
• Linearvectorofpixels
=highlynon-localinferenceforvision
… … 0 1 0 … … 0 1 1 … … 0 0 0 … …
• Rectangulararrayofpixels
=localinferenceforvision
0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Corner
??
Corne
r!!
Treesearchalgorithms
• Basicidea
– Explorespacebygenera,ngsuccessorsofalreadyexploredstates(“expanding”states)
– Evaluateeverygeneratedstate:isitagoalstate?
Example:Romania
Arad
Timisoara
Sibiu
Arad
Fagaras
Oradea
Arad
Rimnicu…
Lugoj
Arad
Oradea
71
75
Zerind
Timisoara
Neamt
87
Zerind 151
140
Arad
Sibiu 99
Iasi
92
Fagaras
118
Timisoara 80
97
111
70
Lugoj
Pitesti
Dobreta
120
142
211
85
Mehadia 146
75
Vaslui
Rimnicu Vilcea
101
Urziceni
Bucharest
138
Cralova
98
90
Giurgiu
Hirsova
86
Eforie
Oradea
Example:Romania
Arad
Timisoara
Sibiu
Arad
Fagaras
Oradea
Zerind
Timisoara
Rimnicu…
Arad
Lugoj
Arad
Oradea
Note: we may visit the same node
often, wasting time & work
!
function TREE-SEARCH (problem, strategy) : returns a solution or failure!
initialize the search tree using the initial state of problem!
while (true):!
if no candidates for expansion: return failure!
choose a leaf node for expansion according to strategy!
if the node contains a goal state: return the corresponding solution!
else: expand the node and add the resulting nodes to the search tree!
Repeatedstates
• Failuretodetectrepeatedstatescanturnalinearprobleminto
anexponen,alone!
• Testiso[enimplementedasahashtable.
33
Solu,onstoRepeatedStates
S
B
S
B
C
State Space
• Graphsearch
C
C
S
B
S
Example of a Search Tree
faster, but memory inefficient
– nevergenerateastategeneratedbefore
• mustkeeptrackofallpossiblestates(usesalotofmemory)
• e.g.,8-puzzleproblem,wehave9!/2=181,440states
• approxima,onforDFS/DLS:onlyavoidstatesinits(limited)memory:
avoidinfiniteloopsbycheckingpathbacktoroot.
– “visited?”testusuallyimplementedasahashtable
34
WhySearchcanbedifficult
• Atthestartofthesearch,thesearchalgorithmdoesnotknow
– thesizeofthetree
– theshapeofthetree
– thedepthofthegoalstates
• Howbigcanasearchtreebe?
– saythereisaconstantbranchingfactorb
– andonegoalexistsatdepthd
– searchtreewhichincludesagoalcanhave
bddifferentbranchesinthetree(worstcase)
• Examples:
– b=2,d=10:bd=210=1024
– b=10,d=10:bd=1010=10,000,000,000
Implementa,on:statesvs.nodes
• Astateisa(representa,onof)aphysicalconfigura,on
• Anodeisadatastructurecons,tu,ngpartofasearchtree
containsinfosuchas:state,parentnode,ac,on,pathcostg(x),
depth
• TheExpandfunc,oncreatesnewnodes,fillinginthevarious
fieldsandusingtheSuccessorFnoftheproblemtocreatethe
correspondingstates.
Searchstrategies
• Asearchstrategyisdefinedbypickingtheorderofnode
expansion
• Strategiesareevaluatedalongthefollowingdimensions:
–
–
–
–
completeness:
doesitalwaysfindasolu,onifoneexists?
,mecomplexity: numberofnodesgenerated
spacecomplexity: maximumnumberofnodesinmemory
op,mality: doesitalwaysfindaleast-costsolu,on?
• Timeandspacecomplexityaremeasuredintermsof
– b:maximumbranchingfactorofthesearchtree
– d:depthoftheleast-costsolu,on
– m:maximumdepthofthestatespace(maybe∞)
37
Summary
• Generatethesearchspacebyapplyingac,onstotheini,alstate
andallfurtherresul,ngstates.
• Problem:ini,alstate,ac,ons,transi,onmodel,goaltest,step/
pathcost
• Solu,on:sequenceofac,onstogoal
• Tree-search(don’tremembervisitednodes)vs.
Graph-search(dorememberthem)
• Searchstrategyevalua,on:b,d,m
– Complete?Time?Space?Op,mal?
© Copyright 2026 Paperzz