a_star.py 3.3 KB

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