bidirectional_Astar3D.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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
  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 = np.array([[1 ,0,0],[0,1 ,0],[0,0, 1],[1 ,1 ,0],[1 ,0,1 ],[0, 1, 1],[ 1, 1, 1],\
  20. [-1,0,0],[0,-1,0],[0,0,-1],[-1,-1,0],[-1,0,-1],[0,-1,-1],[-1,-1,-1],\
  21. [1,-1,0],[-1,1,0],[1,0,-1],[-1,0, 1],[0,1, -1],[0, -1,1],\
  22. [1,-1,-1],[-1,1,-1],[-1,-1,1],[1,1,-1],[1,-1,1],[-1,1,1]])
  23. self.env = env(resolution = resolution)
  24. self.g = g_Space(self) # key is the point, store g value
  25. self.start, self.goal = getNearest(self.g,self.env.start), getNearest(self.g,self.env.goal)
  26. self.g[self.start] = 0 # set g(x0) = 0
  27. self.g[self.goal] = 0 # set g(x0) = 0
  28. self.OPEN1 = queue.QueuePrior() # store [point,priority]
  29. self.OPEN2 = queue.QueuePrior()
  30. self.h1 = Heuristic(self.g,self.goal) # tree NO.1
  31. self.h2 = Heuristic(self.g,self.start) # tree NO.2
  32. self.Parent1, self.Parent2 = {}, {}
  33. self.CLOSED1, self.CLOSED2 = set(), set()
  34. self.V = []
  35. self.done = False
  36. self.Path = []
  37. def run(self):
  38. x0, xt = self.start, self.goal
  39. self.OPEN1.put(x0, self.g[x0] + self.h1[x0]) # item, priority = g + h
  40. self.OPEN2.put(xt, self.g[xt] + self.h2[xt]) # item, priority = g + h
  41. self.ind = 0
  42. while not self.CLOSED1.intersection(self.CLOSED2): # while xt not reached and open is not empty
  43. xi1, xi2 = self.OPEN1.get(), self.OPEN2.get()
  44. self.CLOSED1.add(xi1) # add the point in CLOSED set
  45. self.CLOSED2.add(xi2)
  46. self.V.append(xi1)
  47. self.V.append(xi2)
  48. visualization(self)
  49. allchild1, allchild2 = children(self,xi1), children(self,xi2)
  50. self.evaluation(allchild1,xi1,conf=1)
  51. self.evaluation(allchild2,xi2,conf=2)
  52. if self.ind % 100 == 0: print('iteration number = '+ str(self.ind))
  53. self.ind += 1
  54. self.common = self.CLOSED1.intersection(self.CLOSED2)
  55. self.done = True
  56. self.Path = self.path()
  57. visualization(self)
  58. plt.show()
  59. def evaluation(self, allchild, xi, conf):
  60. for xj in allchild:
  61. if conf == 1:
  62. if xj not in self.CLOSED1:
  63. gi, gj = self.g[xi], self.g[xj]
  64. a = gi + cost(self,xi,xj)
  65. if a < gj:
  66. self.g[xj] = a
  67. self.Parent1[xj] = xi
  68. if (a, xj) in self.OPEN1.enumerate():
  69. self.OPEN1.put(xj, a+1*self.h1[xj])
  70. else:
  71. self.OPEN1.put(xj, a+1*self.h1[xj])
  72. if conf == 2:
  73. if xj not in self.CLOSED2:
  74. gi, gj = self.g[xi], self.g[xj]
  75. a = gi + cost(self,xi,xj)
  76. if a < gj:
  77. self.g[xj] = a
  78. self.Parent2[xj] = xi
  79. if (a, xj) in self.OPEN2.enumerate():
  80. self.OPEN2.put(xj, a+1*self.h2[xj])
  81. else:
  82. self.OPEN2.put(xj, a+1*self.h2[xj])
  83. def path(self):
  84. # TODO: fix path
  85. path = []
  86. goal = self.goal
  87. start = self.start
  88. x = list(self.common)[0]
  89. while x != start:
  90. path.append([x,self.Parent1[x]])
  91. x = self.Parent1[x]
  92. x = list(self.common)[0]
  93. while x != goal:
  94. path.append([x,self.Parent2[x]])
  95. x = self.Parent2[x]
  96. path = np.flip(path,axis=0)
  97. return path
  98. if __name__ == '__main__':
  99. Astar = Weighted_A_star(0.5)
  100. Astar.run()