Files
PathPlanning/Search-based Planning/bfs.py
T

82 lines
2.7 KiB
Python
Raw Normal View History

2020-06-16 14:04:55 -07:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
2020-06-18 10:56:12 -07:00
@author: huiming zhou
2020-06-16 14:04:55 -07:00
"""
2020-06-18 10:56:12 -07:00
import queue
2020-06-20 12:46:49 -07:00
import plotting
2020-06-18 14:57:03 -07:00
import env
2020-06-16 14:04:55 -07:00
2020-06-18 10:56:12 -07:00
2020-06-19 14:18:34 -07:00
class BFS:
2020-06-18 17:08:10 -07:00
def __init__(self, x_start, x_goal):
2020-06-18 14:57:03 -07:00
self.xI, self.xG = x_start, x_goal
2020-06-18 15:52:17 -07:00
2020-06-20 12:46:49 -07:00
self.Env = env.Env()
self.u_set = self.Env.motions # feasible input set
self.obs = self.Env.obs # position of obstacles
[self.path, self.policy, self.visited] = self.searching(self.xI, self.xG)
2020-06-19 14:18:34 -07:00
2020-06-20 13:04:36 -07:00
self.fig_name = "Breadth-first Searching"
2020-06-20 12:46:49 -07:00
plotting.animation(self.xI, self.xG, self.obs,
self.path, self.visited, self.fig_name) # animation generate
2020-06-18 10:56:12 -07:00
2020-06-20 12:46:49 -07:00
def searching(self, xI, xG):
2020-06-18 10:56:12 -07:00
"""
Searching using BFS.
:return: planning path, action in each node, visited nodes in the planning process
"""
q_bfs = queue.QueueFIFO() # first-in-first-out queue
2020-06-20 12:46:49 -07:00
q_bfs.put(xI)
parent = {xI: xI} # record parents of nodes
action = {xI: (0, 0)} # record actions of nodes
visited = []
2020-06-18 14:57:03 -07:00
2020-06-18 10:56:12 -07:00
while not q_bfs.empty():
x_current = q_bfs.get()
2020-06-20 12:46:49 -07:00
if x_current == xG:
2020-06-18 10:56:12 -07:00
break
2020-06-20 12:46:49 -07:00
visited.append(x_current)
2020-06-18 10:56:12 -07:00
for u_next in self.u_set: # explore neighborhoods of current node
2020-06-18 14:57:03 -07:00
x_next = tuple([x_current[i] + u_next[i] for i in range(len(x_current))])
if x_next not in parent and x_next not in self.obs: # node not visited and not in obstacles
2020-06-18 10:56:12 -07:00
q_bfs.put(x_next)
2020-06-20 12:46:49 -07:00
parent[x_next], action[x_next] = x_current, u_next
[path, policy] = self.extract_path(xI, xG, parent, action) # extract path
return path, policy, visited
def extract_path(self, xI, xG, parent, policy):
"""
Extract the path based on the relationship of nodes.
:param xI: Starting node
:param xG: Goal node
:param parent: Relationship between nodes
:param policy: Action needed for transfer between two nodes
:return: The planning path
"""
path_back = [xG]
acts_back = [policy[xG]]
x_current = xG
while True:
x_current = parent[x_current]
path_back.append(x_current)
acts_back.append(policy[x_current])
if x_current == xI: break
2020-06-19 14:18:34 -07:00
2020-06-20 12:46:49 -07:00
return list(path_back), list(acts_back)
2020-06-18 10:56:12 -07:00
if __name__ == '__main__':
2020-06-18 14:57:03 -07:00
x_Start = (5, 5) # Starting node
x_Goal = (49, 5) # Goal node
2020-06-18 17:08:10 -07:00
bfs = BFS(x_Start, x_Goal)