Files
PathPlanning/Search_based_Planning/Search_2D/bfs.py
T

121 lines
3.1 KiB
Python
Raw Normal View History

2020-06-27 00:38:38 -07:00
"""
2020-08-05 12:53:19 -07:00
Breadth-first Searching_2D (BFS)
2020-06-27 00:38:38 -07:00
@author: huiming zhou
"""
import os
import sys
2020-08-05 12:53:19 -07:00
from collections import deque
2020-06-27 00:38:38 -07:00
sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
2020-07-30 10:02:26 -07:00
"/../../Search_based_Planning/")
2020-06-27 00:38:38 -07:00
2020-08-05 12:53:19 -07:00
from Search_based_Planning.Search_2D import plotting, env
2020-06-16 14:04:55 -07:00
2020-06-21 21:50:11 -07:00
2020-06-19 14:18:34 -07:00
class BFS:
2020-07-02 21:23:38 -07:00
def __init__(self, s_start, s_goal):
2020-08-05 12:53:19 -07:00
self.s_start = s_start
self.s_goal = s_goal
2020-06-18 15:52:17 -07:00
2020-06-20 12:46:49 -07:00
self.Env = env.Env()
2020-07-02 21:23:38 -07:00
self.plotting = plotting.Plotting(self.s_start, self.s_goal)
2020-06-20 19:12:34 -07:00
2020-08-10 12:22:06 -07:00
self.u_set = self.Env.motions # feasible input set
self.obs = self.Env.obs # position of obstacles
2020-06-20 19:12:34 -07:00
2020-08-10 12:22:06 -07:00
self.OPEN = deque() # OPEN set: visited nodes
self.PARENT = dict() # recorded parent
self.CLOSED = [] # CLOSED set: explored nodes
2020-06-19 14:18:34 -07:00
2020-06-28 20:41:44 -07:00
def searching(self):
2020-06-18 10:56:12 -07:00
"""
2020-07-02 21:23:38 -07:00
Breadth-first Searching.
:return: path, visited order
2020-06-18 10:56:12 -07:00
"""
2020-08-05 12:53:19 -07:00
self.PARENT[self.s_start] = self.s_start
self.OPEN.append(self.s_start)
2020-07-02 21:23:38 -07:00
while self.OPEN:
2020-08-05 12:53:19 -07:00
s = self.OPEN.popleft()
2020-07-02 21:23:38 -07:00
if s == self.s_goal:
2020-06-18 10:56:12 -07:00
break
2020-06-28 20:41:44 -07:00
self.CLOSED.append(s)
2020-06-20 12:46:49 -07:00
2020-07-02 21:23:38 -07:00
for s_n in self.get_neighbor(s):
2020-08-05 12:53:19 -07:00
if self.is_collision(s, s_n):
continue
2020-08-10 12:22:06 -07:00
if s_n not in self.PARENT: # node not explored
2020-08-05 12:53:19 -07:00
self.OPEN.append(s_n)
2020-07-02 21:23:38 -07:00
self.PARENT[s_n] = s
2020-06-20 12:46:49 -07:00
2020-06-28 20:41:44 -07:00
return self.extract_path(), self.CLOSED
2020-06-20 12:46:49 -07:00
2020-07-02 21:23:38 -07:00
def get_neighbor(self, s):
"""
find neighbors of state s that not in obstacles.
:param s: state
2020-08-05 12:53:19 -07:00
:return: neighbors : [nodes]
2020-07-02 21:23:38 -07:00
"""
2020-08-05 12:53:19 -07:00
return [(s[0] + u[0], s[1] + u[1]) for u in self.u_set]
2020-07-02 21:23:38 -07:00
2020-07-04 18:23:30 -07:00
def is_collision(self, s_start, s_end):
2020-08-05 12:53:19 -07:00
"""
check if the line segment (s_start, s_end) is collision.
:param s_start: start node
:param s_end: end node
:return: True: is collision / False: not collision
"""
2020-07-04 18:23:30 -07:00
if s_start in self.obs or s_end in self.obs:
return True
if s_start[0] != s_end[0] and s_start[1] != s_end[1]:
if s_end[0] - s_start[0] == s_start[1] - s_end[1]:
s1 = (min(s_start[0], s_end[0]), min(s_start[1], s_end[1]))
s2 = (max(s_start[0], s_end[0]), max(s_start[1], s_end[1]))
else:
s1 = (min(s_start[0], s_end[0]), max(s_start[1], s_end[1]))
s2 = (max(s_start[0], s_end[0]), min(s_start[1], s_end[1]))
if s1 in self.obs or s2 in self.obs:
return True
return False
2020-06-28 20:41:44 -07:00
def extract_path(self):
2020-06-20 12:46:49 -07:00
"""
2020-07-02 21:23:38 -07:00
Extract the path based on the PARENT set.
2020-08-05 12:53:19 -07:00
:return: The planning path : [nodes]
2020-06-20 12:46:49 -07:00
"""
2020-07-02 21:23:38 -07:00
path = [self.s_goal]
s = self.s_goal
2020-06-25 14:21:36 -07:00
2020-06-28 20:41:44 -07:00
while True:
s = self.PARENT[s]
path.append(s)
2020-08-05 12:53:19 -07:00
2020-07-02 21:23:38 -07:00
if s == self.s_start:
2020-06-25 14:21:36 -07:00
break
2020-06-19 14:18:34 -07:00
2020-06-28 20:41:44 -07:00
return list(path)
def main():
2020-07-02 21:23:38 -07:00
s_start = (5, 5)
s_goal = (45, 25)
2020-06-28 20:41:44 -07:00
2020-07-02 21:23:38 -07:00
bfs = BFS(s_start, s_goal)
plot = plotting.Plotting(s_start, s_goal)
2020-06-28 20:41:44 -07:00
path, visited = bfs.searching()
2020-07-02 21:23:38 -07:00
plot.animation(path, visited, "Breadth-first Searching (BFS)")
2020-06-18 10:56:12 -07:00
if __name__ == '__main__':
2020-06-28 20:41:44 -07:00
main()