a_star_search.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import networkx as nx
  2. import matplotlib.pyplot as plt
  3. import Queue as Q
  4. def getPriorityQueue(G, v):
  5. q = Q.PriorityQueue()
  6. for node in G[v]:
  7. q.put(Ordered_Node(float(heuristics[node])+float(G[node][v]['length']),node))
  8. return q,len(G[v])
  9. def aStarSearchUtil(G, v, visited, final_path, dest, goal):
  10. if goal == 1:
  11. return goal
  12. visited[v] = True
  13. final_path.append(v)
  14. if v == dest:
  15. goal = 1
  16. else:
  17. pq_list = []
  18. pq,size = getPriorityQueue(G, v)
  19. for i in range(size):
  20. pq_list.append(pq.get().description)
  21. for i in pq_list:
  22. if goal != 1:
  23. #print "current city:", i
  24. if visited[i] == False :
  25. goal = aStarSearchUtil(G, i, visited, final_path, dest, goal)
  26. return goal
  27. def aStarSearch(G, source, dest, heuristics, pos):
  28. visited = {}
  29. for node in G.nodes():
  30. visited[node] = False
  31. final_path = []
  32. goal = aStarSearchUtil(G, source, visited, final_path, dest, 0)
  33. prev = -1
  34. for var in final_path:
  35. if prev != -1:
  36. curr = var
  37. nx.draw_networkx_edges(G, pos, edgelist = [(prev,curr)], width = 2.5, alpha = 0.8, edge_color = 'black')
  38. prev = curr
  39. else:
  40. prev = var
  41. return
  42. class Ordered_Node(object):
  43. def __init__(self, priority, description):
  44. self.priority = priority
  45. self.description = description
  46. return
  47. def __cmp__(self, other):
  48. return cmp(self.priority, other.priority)
  49. def getHeuristics(G):
  50. heuristics = {}
  51. f = open('heuristics.txt')
  52. for i in G.nodes():
  53. node_heuristic_val = f.readline().split()
  54. heuristics[node_heuristic_val[0]] = node_heuristic_val[1]
  55. return heuristics
  56. #takes input from the file and creates a weighted graph
  57. def CreateGraph():
  58. G = nx.Graph()
  59. f = open('input.txt')
  60. n = int(f.readline())
  61. for i in range(n):
  62. graph_edge_list = f.readline().split()
  63. G.add_edge(graph_edge_list[0], graph_edge_list[1], length = graph_edge_list[2])
  64. source, dest= f.read().splitlines()
  65. return G, source, dest
  66. def DrawPath(G, source, dest):
  67. pos = nx.spring_layout(G)
  68. val_map = {}
  69. val_map[source] = 'green'
  70. val_map[dest] = 'red'
  71. values = [val_map.get(node, 'blue') for node in G.nodes()]
  72. nx.draw(G, pos, with_labels = True, node_color = values, edge_color = 'b' ,width = 1, alpha = 0.7) #with_labels=true is to show the node number in the output graph
  73. edge_labels = dict([((u, v,), d['length']) for u, v, d in G.edges(data = True)])
  74. nx.draw_networkx_edge_labels(G, pos, edge_labels = edge_labels, label_pos = 0.5, font_size = 11) #prints weight on all the edges
  75. return pos
  76. #main function
  77. if __name__ == "__main__":
  78. G, source,dest = CreateGraph()
  79. heuristics = getHeuristics(G)
  80. pos = DrawPath(G, source, dest)
  81. aStarSearch(G, source, dest, heuristics, pos)
  82. plt.show()