bidirectional_Astar3D.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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
  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 children(self,x):
  38. allchild = []
  39. for j in self.Alldirec:
  40. collide,child = isCollide(self,x,j)
  41. if not collide:
  42. allchild.append(child)
  43. return allchild
  44. def run(self):
  45. x0, xt = self.start, self.goal
  46. self.OPEN1.put(x0, self.g[x0] + self.h1[x0]) # item, priority = g + h
  47. self.OPEN2.put(xt, self.g[xt] + self.h2[xt]) # item, priority = g + h
  48. self.ind = 0
  49. while not self.CLOSED1.intersection(self.CLOSED2): # while xt not reached and open is not empty
  50. xi1, xi2 = self.OPEN1.get(), self.OPEN2.get()
  51. self.CLOSED1.add(xi1) # add the point in CLOSED set
  52. self.CLOSED2.add(xi2)
  53. self.V.append(xi1)
  54. self.V.append(xi2)
  55. visualization(self)
  56. allchild1, allchild2 = self.children(xi1), self.children(xi2)
  57. self.evaluation(allchild1,xi1,conf=1)
  58. self.evaluation(allchild2,xi2,conf=2)
  59. if self.ind % 100 == 0: print('iteration number = '+ str(self.ind))
  60. self.ind += 1
  61. self.common = self.CLOSED1.intersection(self.CLOSED2)
  62. self.done = True
  63. self.Path = self.path()
  64. visualization(self)
  65. plt.show()
  66. def evaluation(self, allchild, xi, conf):
  67. for xj in allchild:
  68. if conf == 1:
  69. if xj not in self.CLOSED1:
  70. gi, gj = self.g[xi], self.g[xj]
  71. a = gi + cost(xi,xj)
  72. if a < gj:
  73. self.g[xj] = a
  74. self.Parent1[xj] = xi
  75. if (a, xj) in self.OPEN1.enumerate():
  76. self.OPEN1.put(xj, a+1*self.h1[xj])
  77. else:
  78. self.OPEN1.put(xj, a+1*self.h1[xj])
  79. if conf == 2:
  80. if xj not in self.CLOSED2:
  81. gi, gj = self.g[xi], self.g[xj]
  82. a = gi + cost(xi,xj)
  83. if a < gj:
  84. self.g[xj] = a
  85. self.Parent2[xj] = xi
  86. if (a, xj) in self.OPEN2.enumerate():
  87. self.OPEN2.put(xj, a+1*self.h2[xj])
  88. else:
  89. self.OPEN2.put(xj, a+1*self.h2[xj])
  90. def path(self):
  91. # TODO: fix path
  92. path = []
  93. goal = self.goal
  94. start = self.start
  95. x = list(self.common)[0]
  96. while x != start:
  97. path.append([x,self.Parent1[x]])
  98. x = self.Parent1[x]
  99. x = list(self.common)[0]
  100. while x != goal:
  101. path.append([x,self.Parent2[x]])
  102. x = self.Parent2[x]
  103. path = np.flip(path,axis=0)
  104. return path
  105. if __name__ == '__main__':
  106. Astar = Weighted_A_star(0.5)
  107. Astar.run()