bidirectional_Astar3D.py 4.8 KB

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