dijkstra.py 3.4 KB

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