1. 最佳優(yōu)先搜索(Best-First Search)
最佳優(yōu)先搜索(BFS),又稱A算法,是一種啟發(fā)式搜索算法(Heuristic Algorithm)。[不是廣度優(yōu)先搜索算法( Breadth First Search , BFS )]BFS算法在廣度優(yōu)先搜索的基礎(chǔ)上,用啟發(fā)估價函數(shù)對將要被遍歷到的點進行估價,然后選擇代價小的進行遍歷,直到找到目標節(jié)點或者遍歷完所有點,算法結(jié)束。要實現(xiàn)最佳優(yōu)先搜索我們必須使用一個優(yōu)先隊列(priority queue)來實現(xiàn),通常采用一個open優(yōu)先隊列和一個closed集,open優(yōu)先隊列用來儲存還沒有遍歷將要遍歷的節(jié)點,而closed集用來儲存已經(jīng)被遍歷過的節(jié)點。1.1 最佳優(yōu)先搜索的過程最佳優(yōu)先搜索的過程可以被描述為:1、將根節(jié)點放入優(yōu)先隊列open中。2、從優(yōu)先隊列中取出優(yōu)先級最高的節(jié)點X。3、根據(jù)節(jié)點X生成子節(jié)點Y:X的子節(jié)點Y不在open隊列或者closed中,由估價函數(shù)計算出估價值,放入open隊列中。X的子節(jié)點Y在open隊列中,且估價值優(yōu)于open隊列中的子節(jié)點Y,將open隊列中的子節(jié)點Y的估價值替換成新的估價值并按優(yōu)先值排序。X的子節(jié)點Y在closed集中,且估價值優(yōu)于closed集中的子節(jié)點Y,將closed集中的子節(jié)點Y移除,并將子節(jié)點Y加入open優(yōu)先隊列。4、將節(jié)點X放入closed集中。5、重復(fù)過程2,3,4直到目標節(jié)點找到,或者open為空,程序結(jié)束。

2. A-Star算法
1968年發(fā)明的A*算法就是把啟發(fā)式方法(heuristic approaches)如BFS,和常規(guī)方如Dijsktra算法結(jié)合在一起的算法。A-Star算法是一種靜態(tài)路網(wǎng)中求解最短路徑最有效的直接搜索方法,也是解決許多搜索問題的有效算法。?和Dijkstra一樣,A*能用于搜索最短路徑。?和BFS一樣,A*能用啟發(fā)式函數(shù)引導(dǎo)它自己。左圖為Astar算法效果圖,右圖為Dijkstra算法效果圖


(x1,y1)——起始節(jié)點的坐標;
(x2,y2)——目標節(jié)點的坐標;


(xi,yi)——當前節(jié)點的坐標;
(xk,yk)——目標節(jié)點的坐標;











3. 其他Astar算法
3.1 Astar——三維地圖規(guī)劃 3.1.1 3D-Astar原理三維柵格地圖,顧名思義不是簡單的二維平面,它必須得有三維方向,也就是高度方向上的拓展。柵格地圖在XY水平面上的柵格的投影顏色不盡相同,柵格黃色程度越高,表明此處的高度越高。












4. MATLAB實現(xiàn)Astar算法
4.1 defColorMap.m 用于初始化地圖參數(shù)
function [field,cmap] = defColorMap(rows, cols)
cmap = [1 1 1; ... % 1-白色-空地
0 0 0; ... % 2-黑色-靜態(tài)障礙
1 0 0; ... % 3-紅色-動態(tài)障礙
1 1 0;... % 4-黃色-起始點
1 0 1;... % 5-品紅-目標點
0 1 0; ... % 6-綠色-到目標點的規(guī)劃路徑
0 1 1]; % 7-青色-動態(tài)規(guī)劃的路徑
% 構(gòu)建顏色MAP圖
colormap(cmap);
% 定義柵格地圖全域,并初始化空白區(qū)域
field = ones(rows, cols);
% 障礙物區(qū)域
obsRate = 0.3;
obsNum = floor(rows*cols*obsRate);
obsIndex = randi([1,rows*cols],obsNum,1);
field(obsIndex) = 2;
4.2 getChildNode.m
用于獲取子節(jié)點信息
function childNodes = getChildNode(field,closeList, parentNode)
% 選取父節(jié)點周邊8個節(jié)點作為備選子節(jié)點,線性化坐標
% 排除超過邊界之外的、位于障礙區(qū)的、位于closeList中的
[rows, cols] = size(field);
[row_parentNode, col_parentNode] = ind2sub([rows, cols], parentNode);
childNodes = [];
closeList = closeList(:,1);
% 第1個子節(jié)點(右節(jié)點)
childNode = [row_parentNode, col_parentNode+1];
if ~(childNode(1) < 1 || childNode(1) > rows ||...
childNode(2) < 1 || childNode(2) > cols)
if field(childNode(1), childNode(2)) ~= 2
childNode_LineIdx = sub2ind([rows, cols], childNode(1), childNode(2));
if ~ismember(childNode_LineIdx, closeList)
childNodes(end+1) = childNode_LineIdx;
end
end
end
% 第2個子節(jié)點(右上節(jié)點)
childNode = [row_parentNode-1, col_parentNode+1];
child_brother_node_sub1 = [row_parentNode-1, col_parentNode];
child_brother_node_sub2 = [row_parentNode, col_parentNode+1];
if ~(childNode(1) < 1 || childNode(1) > rows ||...
childNode(2) < 1 || childNode(2) > cols)
if field(childNode(1), childNode(2)) ~= 2
if ~(field(child_brother_node_sub1(1), child_brother_node_sub1(2)) == 2 & field(child_brother_node_sub2(1), child_brother_node_sub2(2)) == 2)
childNode_LineIdx = sub2ind([rows, cols], childNode(1), childNode(2));
if ~ismember(childNode_LineIdx, closeList)
childNodes(end+1) = childNode_LineIdx;
end
end
end
end
% 第3個子節(jié)點(上節(jié)點)
childNode = [row_parentNode-1, col_parentNode];
if ~(childNode(1) < 1 || childNode(1) > rows ||...
childNode(2) < 1 || childNode(2) > cols)
if field(childNode(1), childNode(2)) ~= 2
childNode_LineIdx = sub2ind([rows, cols], childNode(1), childNode(2));
if ~ismember(childNode_LineIdx, closeList)
childNodes(end+1) = childNode_LineIdx;
end
end
end
% 第4個子節(jié)點(左上)
childNode = [row_parentNode-1, col_parentNode-1];
child_brother_node_sub1 = [row_parentNode-1, col_parentNode];
child_brother_node_sub2 = [row_parentNode, col_parentNode-1];
if ~(childNode(1) < 1 || childNode(1) > rows ||...
childNode(2) < 1 || childNode(2) > cols)
if field(childNode(1), childNode(2)) ~= 2
if ~(field(child_brother_node_sub1(1), child_brother_node_sub1(2)) == 2 & field(child_brother_node_sub2(1), child_brother_node_sub2(2)) == 2)
childNode_LineIdx = sub2ind([rows, cols], childNode(1), childNode(2));
if ~ismember(childNode_LineIdx, closeList)
childNodes(end+1) = childNode_LineIdx;
end
end
end
end
% 第5個子節(jié)點(左節(jié)點)
childNode = [row_parentNode, col_parentNode-1];
if ~(childNode(1) < 1 || childNode(1) > rows ||...
childNode(2) < 1 || childNode(2) > cols)
if field(childNode(1), childNode(2)) ~= 2
childNode_LineIdx = sub2ind([rows, cols], childNode(1), childNode(2));
if ~ismember(childNode_LineIdx, closeList)
childNodes(end+1) = childNode_LineIdx;
end
end
end
% 第6個子節(jié)點(左下)
childNode = [row_parentNode+1, col_parentNode-1];
child_brother_node_sub1 = [row_parentNode, col_parentNode-1];
child_brother_node_sub2 = [row_parentNode+1, col_parentNode];
if ~(childNode(1) < 1 || childNode(1) > rows ||...
childNode(2) < 1 || childNode(2) > cols)
if field(childNode(1), childNode(2)) ~= 2
if ~(field(child_brother_node_sub1(1), child_brother_node_sub1(2)) == 2 & field(child_brother_node_sub2(1), child_brother_node_sub2(2)) == 2)
childNode_LineIdx = sub2ind([rows, cols], childNode(1), childNode(2));
if ~ismember(childNode_LineIdx, closeList)
childNodes(end+1) = childNode_LineIdx;
end
end
end
end
% 第7個子節(jié)點(下)
childNode = [row_parentNode+1, col_parentNode];
if ~(childNode(1) < 1 || childNode(1) > rows ||...
childNode(2) < 1 || childNode(2) > cols)
if field(childNode(1), childNode(2)) ~= 2
childNode_LineIdx = sub2ind([rows, cols], childNode(1), childNode(2));
if ~ismember(childNode_LineIdx, closeList)
childNodes(end+1) = childNode_LineIdx;
end
end
end
% 第8個子節(jié)點(右下)
childNode = [row_parentNode+1, col_parentNode+1];
child_brother_node_sub1 = [row_parentNode, col_parentNode+1];
child_brother_node_sub2 = [row_parentNode+1, col_parentNode];
if ~(childNode(1) < 1 || childNode(1) > rows ||...
childNode(2) < 1 || childNode(2) > cols)
if field(childNode(1), childNode(2)) ~= 2
if ~(field(child_brother_node_sub1(1), child_brother_node_sub1(2)) == 2 & field(child_brother_node_sub2(1), child_brother_node_sub2(2)) == 2)
childNode_LineIdx = sub2ind([rows, cols], childNode(1), childNode(2));
if ~ismember(childNode_LineIdx, closeList)
childNodes(end+1) = childNode_LineIdx;
end
end
end
end
4.3 Astar.m
主程序
% 基于柵格地圖的機器人路徑規(guī)劃算法
% A*算法
clc
clear
close all
%% 柵格界面、場景定義
% 行數(shù)和列數(shù)
rows = 20;
cols = 20;
[field,cmap] = defColorMap(rows, cols);
% 起點、終點、障礙物區(qū)域
startPos = 2;
goalPos = rows*cols-2;
field(startPos) = 4;
field(goalPos) = 5;
%% 預(yù)處理
% 初始化closeList
parentNode = startPos;
closeList = [startPos,0];
% 初始化openList
openList = struct;
childNodes = getChildNode(field,closeList,parentNode);
for i = 1:length(childNodes)
[row_startPos,col_startPos] = ind2sub([rows, cols],startPos);
[row_goalPos,col_goalPos] = ind2sub([rows, cols],goalPos);
[row,col] = ind2sub([rows, cols],childNodes(i));
% 存入openList結(jié)構(gòu)體
openList(i).node = childNodes(i);
openList(i).g = norm([row_startPos,col_startPos] - [row,col]); % 實際代價采用歐式距離
openList(i).h = abs(row_goalPos - row) + abs(col_goalPos - col); % 估計代價采用曼哈頓距離
openList(i).f = openList(i).g + openList(i).h;
end
% 初始化path
for i = 1:rows*cols
path{i,1} = i; % 線性索引值
end
for i = 1:length(openList)
node = openList(i).node;
path{node,2} = [startPos,node];
end
%% 開始搜索
% 從openList開始搜索移動代價最小的節(jié)點
[~, idx_min] = min([openList.f]);
parentNode = openList(idx_min).node;
% 進入循環(huán)
while true
% 找出父節(jié)點的8個子節(jié)點,障礙物節(jié)點用inf,
childNodes = getChildNode(field, closeList,parentNode);
% 判斷這些子節(jié)點是否在openList中,若在,則比較更新;沒在則追加到openList中
for i = 1:length(childNodes)
% 需要判斷的子節(jié)點
childNode = childNodes(i);
[in_flag,idx] = ismember(childNode, [openList.node]);
% 計算代價函數(shù)
[row_parentNode,col_parentNode] = ind2sub([rows, cols], parentNode);
[row_childNode,col_childNode] = ind2sub([rows, cols], childNode);
[row_goalPos,col_goalPos] = ind2sub([rows, cols],goalPos);
g = openList(idx_min).g + norm( [row_parentNode,col_parentNode] -...
[row_childNode,col_childNode]);
h = abs(row_goalPos - row_childNode) + abs(col_goalPos - col_childNode); % 采用曼哈頓距離進行代價估計
f = g + h;
if in_flag % 若在,比較更新g和f
if f < openList(idx).f
openList(idx).g = g;
openList(idx).h = h;
openList(idx).f = f;
path{childNode,2} = [path{parentNode,2}, childNode];
end
else % 若不在,追加到openList
openList(end+1).node = childNode;
openList(end).g = g;
openList(end).h = h;
openList(end).f = f;
path{childNode,2} = [path{parentNode,2}, childNode];
end
end
% 從openList移出移動代價最小的節(jié)點到closeList
closeList(end+1,: ) = [openList(idx_min).node, openList(idx_min).f];
openList(idx_min)= [];
% 重新搜索:從openList搜索移動代價最小的節(jié)點
[~, idx_min] = min([openList.f]);
parentNode = openList(idx_min).node;
% 判斷是否搜索到終點
if parentNode == goalPos
closeList(end+1,: ) = [openList(idx_min).node, openList(idx_min).f];
break
end
end
%% 畫路徑
% 找出目標最優(yōu)路徑
path_target = path{goalPos,2};
for i = 1:rows*cols
if ~isempty(path{i,2})
field((path{i,1})) = 7;
end
end
field(startPos) = 4;
field(goalPos) = 5;
field(path_target(2:end-1)) = 6;
% 畫柵格圖
image(1.5,1.5,field);
grid on;
set(gca,'gridline','-','gridcolor','k','linewidth',2,'GridAlpha',0.5);
set(gca,'xtick',1:cols+1,'ytick',1:rows+1);
axis image;
hold on;
[y0,x0] = ind2sub([rows,cols], path_target);
y = y0 + 0.5;
x = x0 + 0.5;
plot(x,y,'-','Color','r','LineWidth',2.5);
hold on;
points = [x',y'];
M = 10;
[x1,y1] = bezir_n(points, M);
plot(x1,y1,'-','Color','y','LineWidth',2.5);
hold on;
values = spcrv([[x(1) x x(end)];[y(1) y y(end)]],3);
plot(values(1,:),values(2,:),'b','LineWidth',2.5);
4.4 算法效果




5. python實現(xiàn)Astar算法
可以參考這篇文章https://www.jianshu.com/p/5704e67f40aa這篇文章介紹了Astar以及后續(xù)的變種算法python 版本的偽代碼(來源:https://brilliant.org/wiki/a-star-search/)如下:
make an openlist containing only the starting node
make an empty closed list
while (the destination node has not been reached):
consider the node with the lowest f score in the open list
if (this node is our destination node) :
we are finished
if not:
put the current node in the closed list and look at all of its neighbors
for (each neighbor of the current node):
if (neighbor has lower g value than current and is in the closed list) :
replace the neighbor with the new, lower, g value
current node is now the neighbor's parent
else if (current g value is lower and this neighbor is in the open list ) :
replace the neighbor with the new, lower, g value
change the neighbor's parent to our current node
else if this neighbor is not in both lists:
add it to the open list and set its g
6. Java實現(xiàn)Astar算法
可以參考這篇文章:https://www.jianshu.com/p/950233af52df7. 實踐案例—基于ROS實現(xiàn)Astar與DWA算法
本項目以Astar算法作為全局路徑規(guī)劃算法,DWA作為局部路徑規(guī)劃算法,實現(xiàn)效果如下。(具體原理與算法代碼解釋與說明會在之后的文章附上)-
算法
+關(guān)注
關(guān)注
23文章
4671瀏覽量
94183 -
Open
+關(guān)注
關(guān)注
0文章
21瀏覽量
11161 -
自動駕駛
+關(guān)注
關(guān)注
788文章
14080瀏覽量
168481
原文標題:自動駕駛路徑規(guī)劃:A*(Astar)算法
文章出處:【微信號:3D視覺工坊,微信公眾號:3D視覺工坊】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
自動駕駛路徑規(guī)劃技術(shù)之A-Star算法
FPGA在自動駕駛領(lǐng)域有哪些應(yīng)用?
自動駕駛的到來
即插即用的自動駕駛LiDAR感知算法盒子 RS-Box
UWB主動定位系統(tǒng)在自動駕駛中的應(yīng)用實踐
如何讓自動駕駛更加安全?
你知道有哪幾種常見的車輛路徑規(guī)劃算法嗎?
自動駕駛系統(tǒng)設(shè)計及應(yīng)用的相關(guān)資料分享
自動駕駛技術(shù)的實現(xiàn)
自動駕駛汽車四種常用的路徑規(guī)劃算法解析
解析自動駕駛汽車路徑規(guī)劃算法
自動駕駛之路徑規(guī)劃

自動駕駛軌跡規(guī)劃之路徑規(guī)劃總結(jié)

評論