bidirectional_Astar3D.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, hash3D, dehash, 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.Space = g_Space(self) # key is the point, store g value
  25. self.start, self.goal = getNearest(self.Space,self.env.start), getNearest(self.Space,self.env.goal)
  26. self.Space[hash3D(self.start)] = 0 # set g(x0) = 0
  27. self.Space[hash3D(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.Space,self.goal) # tree NO.1
  31. self.h2 = Heuristic(self.Space,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 = hash3D(self.start), hash3D(self.goal)
  46. self.OPEN1.put(x0, self.Space[x0] + self.h1[x0]) # item, priority = g + h
  47. self.OPEN2.put(xt, self.Space[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. strxi1, strxi2 = self.OPEN1.get(), self.OPEN2.get()
  51. xi1, xi2 = dehash(strxi1), dehash(strxi2)
  52. self.CLOSED1.add(strxi1) # add the point in CLOSED set
  53. self.CLOSED2.add(strxi2)
  54. self.V.append(xi1)
  55. self.V.append(xi2)
  56. visualization(self)
  57. allchild1, allchild2 = self.children(xi1), self.children(xi2)
  58. self.evaluation(allchild1,strxi1,xi1,conf=1)
  59. self.evaluation(allchild2,strxi2,xi2,conf=2)
  60. if self.ind % 100 == 0: print('iteration number = '+ str(self.ind))
  61. self.ind += 1
  62. self.common = self.CLOSED1.intersection(self.CLOSED2)
  63. self.done = True
  64. self.Path = self.path()
  65. visualization(self)
  66. plt.show()
  67. def evaluation(self, allchild, strxi, xi, conf):
  68. for xj in allchild:
  69. strxj = hash3D(xj)
  70. if conf == 1:
  71. if strxj not in self.CLOSED1:
  72. gi, gj = self.Space[strxi], self.Space[strxj]
  73. a = gi + cost(xi,xj)
  74. if a < gj:
  75. self.Space[strxj] = a
  76. self.Parent1[strxj] = xi
  77. if (a, strxj) in self.OPEN1.enumerate():
  78. self.OPEN1.put(strxj, a+1*self.h1[strxj])
  79. else:
  80. self.OPEN1.put(strxj, a+1*self.h1[strxj])
  81. if conf == 2:
  82. if strxj not in self.CLOSED2:
  83. gi, gj = self.Space[strxi], self.Space[strxj]
  84. a = gi + cost(xi,xj)
  85. if a < gj:
  86. self.Space[strxj] = a
  87. self.Parent2[strxj] = xi
  88. if (a, strxj) in self.OPEN2.enumerate():
  89. self.OPEN2.put(strxj, a+1*self.h2[strxj])
  90. else:
  91. self.OPEN2.put(strxj, a+1*self.h2[strxj])
  92. def path(self):
  93. # TODO: fix path
  94. path = []
  95. strgoal = hash3D(self.goal)
  96. strstart = hash3D(self.start)
  97. strx = list(self.common)[0]
  98. while strx != strstart:
  99. path.append([dehash(strx),self.Parent1[strx]])
  100. strx = hash3D(self.Parent1[strx])
  101. strx = list(self.common)[0]
  102. while strx != strgoal:
  103. path.append([dehash(strx),self.Parent2[strx]])
  104. strx = hash3D(self.Parent2[strx])
  105. path = np.flip(path,axis=0)
  106. return path
  107. if __name__ == '__main__':
  108. Astar = Weighted_A_star(0.5)
  109. Astar.run()