1. def aStarAlgo(start_node, stop_node):
  2. open_set = set(start_node)
  3. closed_set = set()
  4. g = {} #store distance from starting node
  5. parents = {}# parents contains an adjacency map of all nodes
  6. #ditance of starting node from itself is zero
  7. g[start_node] = 0
  8. #start_node is root node i.e it has no parent nodes
  9. #so start_node is set to its own parent node
  10. parents[start_node] = start_node
  11. while len(open_set) > 0:
  12. n = None
  13. #node with lowest f() is found
  14. for v in open_set:
  15. if n == None or g[v] + heuristic(v) < g[n] + heuristic(n):
  16. n = v
  17. if n == stop_node or Graph_nodes[n] == None:
  18. pass
  19. else:
  20. for (m, weight) in get_neighbors(n):
  21. #nodes 'm' not in first and last set are added to first
  22. #n is set its parent
  23. if m not in open_set and m not in closed_set:
  24. open_set.add(m)
  25. parents[m] = n
  26. g[m] = g[n] + weight
  27. #for each node m,compare its distance from start i.e g(m) to the
  28. #from start through n node
  29. else:
  30. if g[m] > g[n] + weight:
  31. #update g(m)
  32. g[m] = g[n] + weight
  33. #change parent of m to n
  34. parents[m] = n
  35. #if m in closed set,remove and add to open
  36. if m in closed_set:
  37. closed_set.remove(m)
  38. open_set.add(m)
  39. if n == None:
  40. print('Path does not exist!')
  41. return None
  42. # if the current node is the stop_node
  43. # then we begin reconstructin the path from it to the start_node
  44. if n == stop_node:
  45. path = []
  46. while parents[n] != n:
  47. path.append(n)
  48. n = parents[n]
  49. path.append(start_node)
  50. path.reverse()
  51. print('Path found: {}'.format(path))
  52. return path
  53. # remove n from the open_list, and add it to closed_list
  54. # because all of his neighbors were inspected
  55. open_set.remove(n)
  56. closed_set.add(n)
  57. print('Path does not exist!')
  58. return None
  59. #define fuction to return neighbor and its distance
  60. #from the passed node
  61. def get_neighbors(v):
  62. if v in Graph_nodes:
  63. return Graph_nodes[v]
  64. else:
  65. return None
  66. #for simplicity we ll consider heuristic distances given
  67. #and this function returns heuristic distance for all nodes
  68. def heuristic(n):
  69. H_dist = {
  70. 'START': 550,
  71. 'A': 550,
  72. 'B': 450,
  73. 'C': 510,
  74. 'D': 325,
  75. 'E': 415,
  76. 'F': 235,
  77. 'G': 455,
  78. 'H': 400,
  79. 'I': 325,
  80. 'J': 240,
  81. 'K': 170,
  82. 'L': 205,
  83. 'GOAL': 0,
  84. }
  85. return H_dist[n]
  86. #Input goes here
  87. Graph_nodes = {
  88. 'START': [('A', 120), ('I', 142), ('G', 77)],
  89. 'A': [('B', 113)],
  90. 'B': [('C', 72)],
  91. 'C': [('D', 77)],
  92. 'D': [('E', 122)],
  93. 'E': [('F', 126)],
  94. 'F': [('L', 148), ('K', 140)],
  95. 'G': [('H', 71)],
  96. 'H': [('I', 122)],
  97. 'I': [('J', 111), ('L', 99)],
  98. 'J': [('GOAL', 213)],
  99. 'K': [('GOAL', 105)],
  100. 'L': [('K', 99)],
  101. }
  102. aStarAlgo('START', 'GOAL')