bidirectional_Astar3D.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # this is the three dimensional bidirectional A* algo
  2. # !/usr/bin/env python3
  3. # -*- coding: utf-8 -*-
  4. """
  5. @author: yue qi
  6. """
  7. import numpy as np
  8. import matplotlib.pyplot as plt
  9. from collections import defaultdict
  10. import os
  11. import sys
  12. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../Search-based Planning/")
  13. from Search_3D.env3D import env
  14. from Search_3D.utils3D import getDist, getRay, g_Space, Heuristic, getNearest, isCollide, cost, children, heuristic_fun
  15. from Search_3D.plot_util3D import visualization
  16. import queue
  17. class Weighted_A_star(object):
  18. def __init__(self,resolution=0.5):
  19. self.Alldirec = {(1, 0, 0): 1, (0, 1, 0): 1, (0, 0, 1): 1, \
  20. (-1, 0, 0): 1, (0, -1, 0): 1, (0, 0, -1): 1, \
  21. (1, 1, 0): np.sqrt(2), (1, 0, 1): np.sqrt(2), (0, 1, 1): np.sqrt(2), \
  22. (-1, -1, 0): np.sqrt(2), (-1, 0, -1): np.sqrt(2), (0, -1, -1): np.sqrt(2), \
  23. (1, -1, 0): np.sqrt(2), (-1, 1, 0): np.sqrt(2), (1, 0, -1): np.sqrt(2), \
  24. (-1, 0, 1): np.sqrt(2), (0, 1, -1): np.sqrt(2), (0, -1, 1): np.sqrt(2), \
  25. (1, 1, 1): np.sqrt(3), (-1, -1, -1) : np.sqrt(3), \
  26. (1, -1, -1): np.sqrt(3), (-1, 1, -1): np.sqrt(3), (-1, -1, 1): np.sqrt(3), \
  27. (1, 1, -1): np.sqrt(3), (1, -1, 1): np.sqrt(3), (-1, 1, 1): np.sqrt(3)}
  28. self.env = env(resolution = resolution)
  29. self.start, self.goal = tuple(self.env.start), tuple(self.env.goal)
  30. self.g = {self.start:0,self.goal:0}
  31. self.OPEN1 = queue.QueuePrior() # store [point,priority]
  32. self.OPEN2 = queue.QueuePrior()
  33. self.Parent1, self.Parent2 = {}, {}
  34. self.CLOSED1, self.CLOSED2 = set(), set()
  35. self.V = []
  36. self.done = False
  37. self.Path = []
  38. def run(self):
  39. x0, xt = self.start, self.goal
  40. self.OPEN1.put(x0, self.g[x0] + heuristic_fun(self,x0,xt)) # item, priority = g + h
  41. self.OPEN2.put(xt, self.g[xt] + heuristic_fun(self,xt,x0)) # item, priority = g + h
  42. self.ind = 0
  43. while not self.CLOSED1.intersection(self.CLOSED2): # while xt not reached and open is not empty
  44. xi1, xi2 = self.OPEN1.get(), self.OPEN2.get()
  45. self.CLOSED1.add(xi1) # add the point in CLOSED set
  46. self.CLOSED2.add(xi2)
  47. self.V.append(xi1)
  48. self.V.append(xi2)
  49. # visualization(self)
  50. allchild1, allchild2 = children(self,xi1), children(self,xi2)
  51. self.evaluation(allchild1,xi1,conf=1)
  52. self.evaluation(allchild2,xi2,conf=2)
  53. if self.ind % 100 == 0: print('iteration number = '+ str(self.ind))
  54. self.ind += 1
  55. self.common = self.CLOSED1.intersection(self.CLOSED2)
  56. self.done = True
  57. self.Path = self.path()
  58. visualization(self)
  59. plt.show()
  60. def evaluation(self, allchild, xi, conf):
  61. for xj in allchild:
  62. if conf == 1:
  63. if xj not in self.CLOSED1:
  64. if xj not in self.g:
  65. self.g[xj] = np.inf
  66. else:
  67. pass
  68. gi = self.g[xi]
  69. a = gi + cost(self,xi,xj)
  70. if a < self.g[xj]:
  71. self.g[xj] = a
  72. self.Parent1[xj] = xi
  73. if (a, xj) in self.OPEN1.enumerate():
  74. self.OPEN1.put(xj, a+1*heuristic_fun(self,xj,self.goal))
  75. else:
  76. self.OPEN1.put(xj, a+1*heuristic_fun(self,xj,self.goal))
  77. if conf == 2:
  78. if xj not in self.CLOSED2:
  79. if xj not in self.g:
  80. self.g[xj] = np.inf
  81. else:
  82. pass
  83. gi = self.g[xi]
  84. a = gi + cost(self,xi,xj)
  85. if a < self.g[xj]:
  86. self.g[xj] = a
  87. self.Parent2[xj] = xi
  88. if (a, xj) in self.OPEN2.enumerate():
  89. self.OPEN2.put(xj, a+1*heuristic_fun(self,xj,self.start))
  90. else:
  91. self.OPEN2.put(xj, a+1*heuristic_fun(self,xj,self.start))
  92. def path(self):
  93. # TODO: fix path
  94. path = []
  95. goal = self.goal
  96. start = self.start
  97. x = list(self.common)[0]
  98. while x != start:
  99. path.append([x,self.Parent1[x]])
  100. x = self.Parent1[x]
  101. x = list(self.common)[0]
  102. while x != goal:
  103. path.append([x,self.Parent2[x]])
  104. x = self.Parent2[x]
  105. path = np.flip(path,axis=0)
  106. return path
  107. if __name__ == '__main__':
  108. Astar = Weighted_A_star(0.5)
  109. Astar.run()