bfs.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """
  2. Breadth-first Searching_2D (BFS)
  3. @author: huiming zhou
  4. """
  5. import os
  6. import sys
  7. from collections import deque
  8. sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
  9. "/../../Search_based_Planning/")
  10. from Search_based_Planning.Search_2D import plotting, env
  11. class BFS:
  12. def __init__(self, s_start, s_goal):
  13. self.s_start = s_start
  14. self.s_goal = s_goal
  15. self.Env = env.Env()
  16. self.plotting = plotting.Plotting(self.s_start, self.s_goal)
  17. self.u_set = self.Env.motions # feasible input set
  18. self.obs = self.Env.obs # position of obstacles
  19. self.OPEN = deque() # OPEN set: visited nodes
  20. self.PARENT = dict() # recorded parent
  21. self.CLOSED = [] # CLOSED set: explored nodes
  22. def searching(self):
  23. """
  24. Breadth-first Searching.
  25. :return: path, visited order
  26. """
  27. self.PARENT[self.s_start] = self.s_start
  28. self.OPEN.append(self.s_start)
  29. while self.OPEN:
  30. s = self.OPEN.popleft()
  31. if s == self.s_goal:
  32. break
  33. self.CLOSED.append(s)
  34. for s_n in self.get_neighbor(s):
  35. if self.is_collision(s, s_n):
  36. continue
  37. if s_n not in self.PARENT: # node not explored
  38. self.OPEN.append(s_n)
  39. self.PARENT[s_n] = s
  40. return self.extract_path(), self.CLOSED
  41. def get_neighbor(self, s):
  42. """
  43. find neighbors of state s that not in obstacles.
  44. :param s: state
  45. :return: neighbors : [nodes]
  46. """
  47. return [(s[0] + u[0], s[1] + u[1]) for u in self.u_set]
  48. def is_collision(self, s_start, s_end):
  49. """
  50. check if the line segment (s_start, s_end) is collision.
  51. :param s_start: start node
  52. :param s_end: end node
  53. :return: True: is collision / False: not collision
  54. """
  55. if s_start in self.obs or s_end in self.obs:
  56. return True
  57. if s_start[0] != s_end[0] and s_start[1] != s_end[1]:
  58. if s_end[0] - s_start[0] == s_start[1] - s_end[1]:
  59. s1 = (min(s_start[0], s_end[0]), min(s_start[1], s_end[1]))
  60. s2 = (max(s_start[0], s_end[0]), max(s_start[1], s_end[1]))
  61. else:
  62. s1 = (min(s_start[0], s_end[0]), max(s_start[1], s_end[1]))
  63. s2 = (max(s_start[0], s_end[0]), min(s_start[1], s_end[1]))
  64. if s1 in self.obs or s2 in self.obs:
  65. return True
  66. return False
  67. def extract_path(self):
  68. """
  69. Extract the path based on the PARENT set.
  70. :return: The planning path : [nodes]
  71. """
  72. path = [self.s_goal]
  73. s = self.s_goal
  74. while True:
  75. s = self.PARENT[s]
  76. path.append(s)
  77. if s == self.s_start:
  78. break
  79. return list(path)
  80. def main():
  81. s_start = (5, 5)
  82. s_goal = (45, 25)
  83. bfs = BFS(s_start, s_goal)
  84. plot = plotting.Plotting(s_start, s_goal)
  85. path, visited = bfs.searching()
  86. plot.animation(path, visited, "Breadth-first Searching (BFS)")
  87. if __name__ == '__main__':
  88. main()