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

57 lines
2.0 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
import tools
2020-06-18 14:57:03 -07:00
import env
2020-06-18 15:52:17 -07:00
import motion_model
2020-06-16 14:04:55 -07:00
2020-06-18 10:56:12 -07:00
class DFS:
"""
DFS -> Depth-first Searching
"""
2020-06-18 17:08:10 -07:00
def __init__(self, x_start, x_goal):
2020-06-18 15:52:17 -07:00
self.u_set = motion_model.motions # feasible input set
2020-06-18 14:57:03 -07:00
self.xI, self.xG = x_start, x_goal
2020-06-18 15:52:17 -07:00
self.obs = env.obs_map() # position of obstacles
env.show_map(self.xI, self.xG, self.obs, "depth-first searching")
2020-06-18 10:56:12 -07:00
def searching(self):
"""
Searching using DFS.
:return: planning path, action in each node, visited nodes in the planning process
"""
q_dfs = queue.QueueLIFO() # last-in-first-out queue
q_dfs.put(self.xI)
parent = {self.xI: self.xI} # record parents of nodes
2020-06-18 14:57:03 -07:00
action = {self.xI: (0, 0)} # record actions of nodes
2020-06-18 10:56:12 -07:00
while not q_dfs.empty():
x_current = q_dfs.get()
2020-06-18 14:57:03 -07:00
if x_current == self.xG:
2020-06-18 10:56:12 -07:00
break
2020-06-18 14:57:03 -07:00
if x_current != self.xI:
tools.plot_dots(x_current, len(parent))
for u_next in self.u_set: # explore neighborhoods of current node
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_dfs.put(x_next)
parent[x_next] = x_current
2020-06-18 14:57:03 -07:00
action[x_next] = u_next
[path_dfs, action_dfs] = tools.extract_path(self.xI, self.xG, parent, action)
return path_dfs, action_dfs
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
dfs = DFS(x_Start, x_Goal)
2020-06-18 14:57:03 -07:00
[path_dfs, action_dfs] = dfs.searching()
tools.showPath(x_Start, x_Goal, path_dfs)