a_star.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @author: huiming zhou
  5. """
  6. import queue
  7. import tools
  8. import env
  9. import motion_model
  10. class Astar:
  11. def __init__(self, x_start, x_goal, x_range, y_range, heuristic_type):
  12. self.u_set = motion_model.motions # feasible input set
  13. self.xI, self.xG = x_start, x_goal
  14. self.x_range, self.y_range = x_range, y_range
  15. self.obs = env.obs_map() # position of obstacles
  16. self.heuristic_type = heuristic_type
  17. env.show_map(self.xI, self.xG, self.obs, "a_star searching")
  18. def searching(self):
  19. """
  20. Searching using A_star.
  21. :return: planning path, action in each node, visited nodes in the planning process
  22. """
  23. q_astar = queue.QueuePrior() # priority queue
  24. q_astar.put(self.xI, 0)
  25. parent = {self.xI: self.xI} # record parents of nodes
  26. action = {self.xI: (0, 0)} # record actions of nodes
  27. cost = {self.xI: 0}
  28. while not q_astar.empty():
  29. x_current = q_astar.get()
  30. if x_current == self.xG: # stop condition
  31. break
  32. if x_current != self.xI:
  33. tools.plot_dots(x_current, len(parent))
  34. for u_next in self.u_set: # explore neighborhoods of current node
  35. x_next = tuple([x_current[i] + u_next[i] for i in range(len(x_current))])
  36. if x_next not in self.obs:
  37. new_cost = cost[x_current] + self.get_cost(x_current, u_next)
  38. if x_next not in cost or new_cost < cost[x_next]: # conditions for updating cost
  39. cost[x_next] = new_cost
  40. priority = new_cost + self.Heuristic(x_next, self.xG, self.heuristic_type)
  41. q_astar.put(x_next, priority) # put node into queue using priority "f+h"
  42. parent[x_next] = x_current
  43. action[x_next] = u_next
  44. [path_astar, actions_astar] = tools.extract_path(self.xI, self.xG, parent, action)
  45. return path_astar, actions_astar
  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 Heuristic(self, state, goal, heuristic_type):
  56. """
  57. Calculate heuristic.
  58. :param state: current node (state)
  59. :param goal: goal node (state)
  60. :param heuristic_type: choosing different heuristic functions
  61. :return: heuristic
  62. """
  63. if heuristic_type == "manhattan":
  64. return abs(goal[0] - state[0]) + abs(goal[1] - state[1])
  65. elif heuristic_type == "euclidean":
  66. return ((goal[0] - state[0]) ** 2 + (goal[1] - state[1]) ** 2) ** (1 / 2)
  67. else:
  68. print("Please choose right heuristic type!")
  69. if __name__ == '__main__':
  70. x_Start = (5, 5) # Starting node
  71. x_Goal = (49, 5) # Goal node
  72. astar = Astar(x_Start, x_Goal, env.x_range, env.y_range, "manhattan")
  73. [path_astar, actions_astar] = astar.searching()
  74. tools.showPath(x_Start, x_Goal, path_astar) # Plot path and visited nodes