一文教你用python編寫Dijkstra算法進行機器人路徑規(guī)劃
前言
為了機器人在尋路的過程中避障并且找到最短距離,我們需要使用一些算法進行路徑規(guī)劃(Path Planning),常用的算法有Djikstra算法、A*算法等等,在github上有一個非常好的項目叫做PythonRobotics,其中給出了源代碼,參考代碼,可以對Djikstra算法有更深的了解。
一、算法原理

如圖所示,Dijkstra算法要解決的是一個有向權(quán)重圖中最短路徑的尋找問題,圖中紅色節(jié)點1代表起始節(jié)點,藍色節(jié)點6代表目標結(jié)點。箭頭上的數(shù)字代表兩個結(jié)點中的的距離,也就是模型中所謂的代價(cost)。
貪心算法需要設(shè)立兩個集合,open_set(開集)和closed_set(閉集),然后根據(jù)以下程序進行操作:
- 把初始結(jié)點放入到open_set中;
- 把open_set中代價最小的節(jié)點取出來放入到closed_set中,并且作為當前節(jié)點;
- 把與當前節(jié)點相鄰的節(jié)點放入到open_set中,如果代價更小更新代價
- 重復2-3過程,直到找到終點。
注意open_set中的代價是可變的,而closed_set中的代價已經(jīng)是最小的代價了,這也是為什么叫做open和close的原因。
至于為什么closed_set中的代價是最小的,是因為我們使用了貪心算法,既然已經(jīng)把節(jié)點加入到了close中,那么初始點到close節(jié)點中的距離就比到open中的距離小了,無論如何也不可能找到比它更小的了。
二、程序代碼
"""
Grid based Dijkstra planning
author: Atsushi Sakai(@Atsushi_twi)
"""
import matplotlib.pyplot as plt
import math
show_animation = True
class Dijkstra:
def __init__(self, ox, oy, resolution, robot_radius):
"""
Initialize map for a star planning
ox: x position list of Obstacles [m]
oy: y position list of Obstacles [m]
resolution: grid resolution [m]
rr: robot radius[m]
"""
self.min_x = None
self.min_y = None
self.max_x = None
self.max_y = None
self.x_width = None
self.y_width = None
self.obstacle_map = None
self.resolution = resolution
self.robot_radius = robot_radius
self.calc_obstacle_map(ox, oy)
self.motion = self.get_motion_model()
class Node:
def __init__(self, x, y, cost, parent_index):
self.x = x # index of grid
self.y = y # index of grid
self.cost = cost
self.parent_index = parent_index # index of previous Node
def __str__(self):
return str(self.x) + "," + str(self.y) + "," + str(
self.cost) + "," + str(self.parent_index)
def planning(self, sx, sy, gx, gy):
"""
dijkstra path search
input:
s_x: start x position [m]
s_y: start y position [m]
gx: goal x position [m]
gx: goal x position [m]
output:
rx: x position list of the final path
ry: y position list of the final path
"""
start_node = self.Node(self.calc_xy_index(sx, self.min_x),
self.calc_xy_index(sy, self.min_y), 0.0, -1)
goal_node = self.Node(self.calc_xy_index(gx, self.min_x),
self.calc_xy_index(gy, self.min_y), 0.0, -1)
open_set, closed_set = dict(), dict()
open_set[self.calc_index(start_node)] = start_node
while 1:
c_id = min(open_set, key=lambda o: open_set[o].cost)
current = open_set[c_id]
# show graph
if show_animation: # pragma: no cover
plt.plot(self.calc_position(current.x, self.min_x),
self.calc_position(current.y, self.min_y), "xc")
# for stopping simulation with the esc key.
plt.gcf().canvas.mpl_connect(
'key_release_event',
lambda event: [exit(0) if event.key == 'escape' else None])
if len(closed_set.keys()) % 10 == 0:
plt.pause(0.001)
if current.x == goal_node.x and current.y == goal_node.y:
print("Find goal")
goal_node.parent_index = current.parent_index
goal_node.cost = current.cost
break
# Remove the item from the open set
del open_set[c_id]
# Add it to the closed set
closed_set[c_id] = current
# expand search grid based on motion model
for move_x, move_y, move_cost in self.motion:
node = self.Node(current.x + move_x,
current.y + move_y,
current.cost + move_cost, c_id)
n_id = self.calc_index(node)
if n_id in closed_set:
continue
if not self.verify_node(node):
continue
if n_id not in open_set:
open_set[n_id] = node # Discover a new node
else:
if open_set[n_id].cost >= node.cost:
# This path is the best until now. record it!
open_set[n_id] = node
rx, ry = self.calc_final_path(goal_node, closed_set)
return rx, ry
def calc_final_path(self, goal_node, closed_set):
# generate final course
rx, ry = [self.calc_position(goal_node.x, self.min_x)], [
self.calc_position(goal_node.y, self.min_y)]
parent_index = goal_node.parent_index
while parent_index != -1:
n = closed_set[parent_index]
rx.append(self.calc_position(n.x, self.min_x))
ry.append(self.calc_position(n.y, self.min_y))
parent_index = n.parent_index
return rx, ry
def calc_position(self, index, minp):
pos = index * self.resolution + minp
return pos
def calc_xy_index(self, position, minp):
return round((position - minp) / self.resolution)
def calc_index(self, node):
return (node.y - self.min_y) * self.x_width + (node.x - self.min_x)
def verify_node(self, node):
px = self.calc_position(node.x, self.min_x)
py = self.calc_position(node.y, self.min_y)
if px < self.min_x:
return False
if py < self.min_y:
return False
if px >= self.max_x:
return False
if py >= self.max_y:
return False
if self.obstacle_map[node.x][node.y]:
return False
return True
def calc_obstacle_map(self, ox, oy):
self.min_x = round(min(ox))
self.min_y = round(min(oy))
self.max_x = round(max(ox))
self.max_y = round(max(oy))
print("min_x:", self.min_x)
print("min_y:", self.min_y)
print("max_x:", self.max_x)
print("max_y:", self.max_y)
self.x_width = round((self.max_x - self.min_x) / self.resolution)
self.y_width = round((self.max_y - self.min_y) / self.resolution)
print("x_width:", self.x_width)
print("y_width:", self.y_width)
# obstacle map generation
self.obstacle_map = [[False for _ in range(self.y_width)]
for _ in range(self.x_width)]
for ix in range(self.x_width):
x = self.calc_position(ix, self.min_x)
for iy in range(self.y_width):
y = self.calc_position(iy, self.min_y)
for iox, ioy in zip(ox, oy):
d = math.hypot(iox - x, ioy - y)
if d <= self.robot_radius:
self.obstacle_map[ix][iy] = True
break
@staticmethod
def get_motion_model():
# dx, dy, cost
motion = [[1, 0, 1],
[0, 1, 1],
[-1, 0, 1],
[0, -1, 1],
[-1, -1, math.sqrt(2)],
[-1, 1, math.sqrt(2)],
[1, -1, math.sqrt(2)],
[1, 1, math.sqrt(2)]]
return motion
def main():
print(__file__ + " start!!")
# start and goal position
sx = -5.0 # [m]
sy = -5.0 # [m]
gx = 50.0 # [m]
gy = 50.0 # [m]
grid_size = 2.0 # [m]
robot_radius = 1.0 # [m]
# set obstacle positions
ox, oy = [], []
for i in range(-10, 60):
ox.append(i)
oy.append(-10.0)
for i in range(-10, 60):
ox.append(60.0)
oy.append(i)
for i in range(-10, 61):
ox.append(i)
oy.append(60.0)
for i in range(-10, 61):
ox.append(-10.0)
oy.append(i)
for i in range(-10, 40):
ox.append(20.0)
oy.append(i)
for i in range(0, 40):
ox.append(40.0)
oy.append(60.0 - i)
if show_animation: # pragma: no cover
plt.plot(ox, oy, ".k")
plt.plot(sx, sy, "og")
plt.plot(gx, gy, "xb")
plt.grid(True)
plt.axis("equal")
dijkstra = Dijkstra(ox, oy, grid_size, robot_radius)
rx, ry = dijkstra.planning(sx, sy, gx, gy)
if show_animation: # pragma: no cover
plt.plot(rx, ry, "-r")
plt.pause(0.01)
plt.show()
if __name__ == '__main__':
main()
三、運行結(jié)果

四、 A*算法:Djikstra算法的改進
Dijkstra算法實際上是貪心搜索算法,算法復雜度為O( n 2 n^2 n2),為了減少無效搜索的次數(shù),我們可以增加一個啟發(fā)式函數(shù)(heuristic),比如搜索點到終點目標的距離,在選擇open_set元素的時候,我們將cost變成cost+heuristic,就可以給出搜索的方向性,這樣就可以減少南轅北轍的情況。我們可以run一下PythonRobotics中的Astar代碼,得到以下結(jié)果:

總結(jié)
到此這篇關(guān)于python編寫Dijkstra算法進行機器人路徑規(guī)劃的文章就介紹到這了,更多相關(guān)python寫Dijkstra算法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python數(shù)據(jù)結(jié)構(gòu)與算法之圖的最短路徑(Dijkstra算法)完整實例
- Python使用Dijkstra算法實現(xiàn)求解圖中最短路徑距離問題詳解
- Python實現(xiàn)Dijkstra算法
- python實現(xiàn)dijkstra最短路由算法
- python實現(xiàn)Dijkstra靜態(tài)尋路算法
- python實現(xiàn)Dijkstra算法的最短路徑問題
- python Dijkstra算法實現(xiàn)最短路徑問題的方法
- python3實現(xiàn)Dijkstra算法最短路徑的實現(xiàn)
- python最短路徑的求解Dijkstra算法示例代碼
相關(guān)文章
Python3實現(xiàn)將文件樹中所有文件和子目錄歸檔到tar壓縮文件的方法
這篇文章主要介紹了Python3實現(xiàn)將文件樹中所有文件和子目錄歸檔到tar壓縮文件的方法,涉及Python3使用tarfile模塊實現(xiàn)tar壓縮文件的技巧,需要的朋友可以參考下2015-05-05
Django使用channels + websocket打造在線聊天室
本文將教你如何使用channels + websocket打造個在線聊天室。一共只有四步,你可以輕松上手并學會。項目中大部分代碼是基于channels的官方文檔的,加入了些自己的理解,以便新手學習使用。2021-05-05
利用Python中的Xpath實現(xiàn)一個在線匯率轉(zhuǎn)換器
這篇文章主要給大家介紹了關(guān)于如何利用Python中的Xpath實現(xiàn)一個在線匯率轉(zhuǎn)換器的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
基于Python的圖像數(shù)據(jù)增強Data Augmentation解析
這篇文章主要介紹了基于Python的圖像數(shù)據(jù)增強Data Augmentation解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08
Pycharm?2020最新永久激活碼(附最新激活碼和插件)
最近很多朋友的Pycharm激活時間又過期了,今天小編再把激活的方法匯總和工具分享一下,文中給大家分享兩種方式,需要的朋友直接拿去用吧2020-01-01

