a_star.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @author: huiming zhou
  5. """
  6. import queue
  7. import environment
  8. import tools
  9. class Astar:
  10. def __init__(self, Start_State, Goal_State, n, m, heuristic_type):
  11. self.xI = Start_State
  12. self.xG = Goal_State
  13. self.u_set = environment.motions # feasible input set
  14. self.obs_map = environment.map_obs() # position of obstacles
  15. self.n = n
  16. self.m = m
  17. self.heuristic_type = heuristic_type
  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. actions = {self.xI: (0, 0)} # record actions of nodes
  27. cost = {self.xI: 0}
  28. visited = []
  29. while not q_astar.empty():
  30. x_current = q_astar.get()
  31. visited.append(x_current) # record visited nodes
  32. if x_current == self.xG: # stop condition
  33. break
  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 neighbor node is not in obstacles -> ...
  37. if 0 <= x_next[0] < self.n and 0 <= x_next[1] < self.m \
  38. and not tools.obs_detect(x_current, u_next, self.obs_map):
  39. new_cost = cost[x_current] + int(self.get_cost(x_current, u_next))
  40. if x_next not in cost or new_cost < cost[x_next]: # conditions for updating cost
  41. cost[x_next] = new_cost
  42. priority = new_cost + self.Heuristic(x_next, self.xG, self.heuristic_type)
  43. q_astar.put(x_next, priority) # put node into queue using priority "f+h"
  44. parent[x_next] = x_current
  45. actions[x_next] = u_next
  46. [path_astar, actions_astar] = tools.extract_path(self.xI, self.xG, parent, actions)
  47. return path_astar, actions_astar, visited
  48. def get_cost(self, x, u):
  49. """
  50. Calculate cost for this motion
  51. :param x: current node
  52. :param u: input
  53. :return: cost for this motion
  54. :note: cost function could be more complicate!
  55. """
  56. return 1
  57. def Heuristic(self, state, goal, heuristic_type):
  58. """
  59. Calculate heuristic.
  60. :param state: current node (state)
  61. :param goal: goal node (state)
  62. :param heuristic_type: choosing different heuristic functions
  63. :return: heuristic
  64. """
  65. if heuristic_type == "manhattan":
  66. return abs(goal[0] - state[0]) + abs(goal[1] - state[1])
  67. elif heuristic_type == "euclidean":
  68. return ((goal[0] - state[0]) ** 2 + (goal[1] - state[1]) ** 2) ** (1 / 2)
  69. else:
  70. print("Please choose right heuristic type!")
  71. if __name__ == '__main__':
  72. x_Start = (15, 10) # Starting node
  73. x_Goal = (48, 15) # Goal node
  74. astar = Astar(x_Start, x_Goal, environment.col, environment.row, "manhattan")
  75. [path_astar, actions_astar, visited_astar] = astar.searching()
  76. tools.showPath(x_Start, x_Goal, path_astar, visited_astar, 'Astar_searching') # Plot path and visited nodes