Question: I am getting this result everytime: Using A * to find path from 1 3 to 3 A * Path is - 1 long in
I am getting this result everytime: Using A to find path from to
A Path is long in edges; found in pops
def aStarG s t debugFalse:
given a weighted graph G find the shortest path by edge weights from s to t
This time we use an Informed Search
the debug flag can be used to print optional output while testing your program
You will need data structures to track both prior and pathcost I suggest you use
a dictionary where the key is the node vertex id and the value is either
the previous node id for prior or the current lowest cost to reach the node
for pathcost Your frontier can take advantage of the provided IndexMinPQ
return: path a list of the nodes from s to t; should include both s & t
distance the distance from s to t
popcount the number of pops from the search frontier
def Huv:
given two vertices u & v calculate the optimistic length between them.
Each vertex has an attribute, tags, a dictionary of various values associated with
the vertex. The Graph.getTagsvertex method provides access to them.
Use the 'lat' and 'lon' values associated with each vertex
to calculate the euclidian distance between the two vertices.
#TODO Your code goes here
ulat, ulon GgetTagsulat GgetTagsulon
vlat, vlon GgetTagsvlat GgetTagsvlon
distance math.sqrtulatvlatulonvlon
return distance # You will need to update this return statement returing makes A & Dijkstra equivelant, do you see why?
popcount # increment this every time you pop a node from the search frontier
printf
Using A to find path from s to t
# TODO your code goes here. Using A find the path from st; return that
# path as a python list. Be sure to increment popcount
#
prior
pathcost
path
distance
frontier IndexMinPQGsizeV
frontier.pushs Hst
while not frontier.isEmpty:
cvertex, ccost frontier.pop
popcount
if cvertex t:
path t
while priorcvertex is not None:
path.appendpriorcvertex
cvertex priorcvertex
path.reverse
return path, pathcostt popcount
for edge in GedgesOfcvertex:
neighbor edge.t
ncost pathcost.getcvertex floatinf edge.weight
if ncost pathcost.getneighbor floatinf:
priorneighbor cvertex
pathcostneighbor ncost
frontier.pushneighbor ncost Hneighbort
return path, distance, popcount
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
