dijkstra.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """
  2. Dijkstra 2D
  3. @author: huiming zhou
  4. """
  5. import os
  6. import sys
  7. sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
  8. "/../../Search-based Planning/")
  9. from Search_2D import queue
  10. from Search_2D import plotting
  11. from Search_2D import env
  12. class Dijkstra:
  13. def __init__(self, x_start, x_goal):
  14. self.xI, self.xG = x_start, x_goal
  15. self.Env = env.Env()
  16. self.plotting = plotting.Plotting(self.xI, self.xG)
  17. self.u_set = self.Env.motions # feasible input set
  18. self.obs = self.Env.obs # position of obstacles
  19. [self.path, self.policy, self.visited] = self.searching(self.xI, self.xG)
  20. self.fig_name = "Dijkstra's Algorithm"
  21. self.plotting.animation(self.path, self.visited, self.fig_name) # animation generate
  22. def searching(self, xI, xG):
  23. """
  24. Searching using Dijkstra.
  25. :return: planning path, action in each node, visited nodes in the planning process
  26. """
  27. q_dijk = queue.QueuePrior() # priority queue
  28. q_dijk.put(xI, 0)
  29. parent = {xI: xI} # record parents of nodes
  30. action = {xI: (0, 0)} # record actions of nodes
  31. visited = [] # record visited nodes
  32. cost = {xI: 0}
  33. while not q_dijk.empty():
  34. x_current = q_dijk.get()
  35. if x_current == xG: # stop condition
  36. break
  37. visited.append(x_current)
  38. for u_next in self.u_set: # explore neighborhoods of current node
  39. x_next = tuple([x_current[i] + u_next[i] for i in range(len(x_current))])
  40. if x_next not in self.obs: # node not visited and not in obstacles
  41. new_cost = cost[x_current] + self.get_cost(x_current, u_next)
  42. if x_next not in cost or new_cost < cost[x_next]:
  43. cost[x_next] = new_cost
  44. priority = new_cost
  45. q_dijk.put(x_next, priority) # put node into queue using cost to come as priority
  46. parent[x_next], action[x_next] = x_current, u_next
  47. [path, policy] = self.extract_path(xI, xG, parent, action)
  48. return path, policy, visited
  49. @staticmethod
  50. def get_cost(x, u):
  51. """
  52. Calculate cost for this motion
  53. :param x: current node
  54. :param u: input
  55. :return: cost for this motion
  56. :note: cost function could be more complicate!
  57. """
  58. return 1
  59. @staticmethod
  60. def extract_path(xI, xG, parent, policy):
  61. """
  62. Extract the path based on the relationship of nodes.
  63. :param xI: Starting node
  64. :param xG: Goal node
  65. :param parent: Relationship between nodes
  66. :param policy: Action needed for transfer between two nodes
  67. :return: The planning path
  68. """
  69. path_back = [xG]
  70. acts_back = [policy[xG]]
  71. x_current = xG
  72. while True:
  73. x_current = parent[x_current]
  74. path_back.append(x_current)
  75. acts_back.append(policy[x_current])
  76. if x_current == xI: break
  77. return list(path_back), list(acts_back)
  78. if __name__ == '__main__':
  79. x_Start = (5, 5) # Starting node
  80. x_Goal = (49, 25) # Goal node
  81. dijkstra = Dijkstra(x_Start, x_Goal)