0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Dijkstra和A*算法及其Matlab實現(xiàn)

3D視覺工坊 ? 來源:古月居 ? 作者:古月居 ? 2022-12-07 15:04 ? 次閱讀

寫在前面的話:只是對兩種路徑優(yōu)化算法進行簡單的理解和嘗試,為后續(xù)使用做準備。如果用到,請再次好好理解原理和Matlab源碼。

首先給出Matlab下的三個腳本文件:

TestScript.m

%
% TestScript for Assignment 1
%


%% Define a small map
% map = false(10);
map = ans;
% Add an obstacle
% map (1:5, 6) = true;
map = logical(map);
start_coords = [1, 1];
dest_coords = [40, 20];


%%
close all;
%  [route, numExpanded] = DijkstraGrid (map, start_coords, dest_coords);
% Uncomment following line to run Astar
 [route, numExpanded] = AStarGrid (map, start_coords, dest_coords);


%HINT: With default start and destination coordinates defined above, numExpanded for Dijkstras should be 76, numExpanded for Astar should be 23.

AStarGrid.m

function [route,numExpanded] = AStarGrid (input_map, start_coords, dest_coords)
% Run A* algorithm on a grid.
% Inputs : 
%  input_map : a logical array where the freespace cells are false or 0 and
%  the obstacles are true or 1
%  start_coords and dest_coords : Coordinates of the start and end cell
%  respectively, the first entry is the row and the second the column.
% Output :
%  route : An array containing the linear indices of the cells along the
%  shortest route from start to dest or an empty array if there is no
%  route. This is a single dimensional vector
%  numExpanded: Remember to also return the total number of nodes
%  expanded during your search. Do not count the goal node as an expanded node. 


% set up color map for display用一個map矩陣來表示每個點的狀態(tài)
% 1 - white - clear cell
% 2 - black - obstacle
% 3 - red = visited 相當于CLOSED列表的作用
% 4 - blue - on list 相當于OPEN列表的作用
% 5 - green - start
% 6 - yellow - destination


cmap = [1 1 1; ...
  0 0 0; ...
  1 0 0; ...
  0 0 1; ...
  0 1 0; ...
  1 1 0; ...
  0.5 0.5 0.5];


colormap(cmap);


% variable to control if the map is being visualized on every
% iteration
drawMapEveryTime = true;


[nrows, ncols] = size(input_map);


% map - a table that keeps track of the state of each grid cell用來上色的
map = zeros(nrows,ncols);


map(~input_map) = 1;  % Mark free cells
map(input_map) = 2;  % Mark obstacle cells


% Generate linear indices of start and dest nodes將下標轉(zhuǎn)換為線性的索引值
start_node = sub2ind(size(map), start_coords(1), start_coords(2));
dest_node = sub2ind(size(map), dest_coords(1), dest_coords(2));


map(start_node) = 5;
map(dest_node) = 6;


% meshgrid will `replicate grid vectors' nrows and ncols to produce
% a full grid
% type `help meshgrid' in the Matlab command prompt for more information
parent = zeros(nrows,ncols);%用來記錄每個節(jié)點的父節(jié)點


% 
[X, Y] = meshgrid (1:ncols, 1:nrows);


xd = dest_coords(1);
yd = dest_coords(2);


% Evaluate Heuristic function, H, for each grid cell
% Manhattan distance用曼哈頓距離作為啟發(fā)式函數(shù)
H = abs(X - xd) + abs(Y - yd);
H = H';
% Initialize cost arrays
f = Inf(nrows,ncols);
g = Inf(nrows,ncols);


g(start_node) = 0;
f(start_node) = H(start_node);


% keep track of the number of nodes that are expanded
numExpanded = 0;


% Main Loop


while true
  
  % Draw current map
  map(start_node) = 5;
  map(dest_node) = 6;
  
  % make drawMapEveryTime = true if you want to see how the 
  % nodes are expanded on the grid. 
  if (drawMapEveryTime)
    image(1.5, 1.5, map);
    grid on;
    axis image;
    drawnow;
  end
  
  % Find the node with the minimum f value,其中的current是index值,需要轉(zhuǎn)換
  [min_f, current] = min(f(:));
  
  if ((current == dest_node) || isinf(min_f))
    break;
  end;
  
  % Update input_map
  map(current) = 3;
  f(current) = Inf; % remove this node from further consideration
  numExpanded=numExpanded+1;
  % Compute row, column coordinates of current node
  [i, j] = ind2sub(size(f), current);
  
  % *********************************************************************
  % ALL YOUR CODE BETWEEN THESE LINES OF STARS
  % Visit all of the neighbors around the current node and update the
  % entries in the map, f, g and parent arrays
  %
  action=[-1 0; 1 0; 0 -1; 0 1];%上,下,左,右
  for a=1:4
    expand=[i,j]+action(a,:);
    expand1=expand(1,1);
    expand2=expand(1,2);
    %不超出邊界,不穿越障礙,不在CLOSED列表里,也不是起點,則進行擴展
    if ( expand1>=1 && expand1<=nrows && expand2>=1 && expand2<=nrows && map(expand1,expand2)~=2 && map(expand1,expand2)~=3 && map(expand1,expand2)~=5)
 ? ? ? ? ? ?if ( g(expand1,expand2)> g(i,j)+1 )
        g(expand1,expand2)= g(i,j)+1;
        f(expand1,expand2)= g(expand1,expand2)+H(expand1,expand2);
        parent(expand1,expand2)=current;
        map(expand1,expand2)=4;
      end
    end
  end
  %*********************************************************************
  
  
end


%% Construct route from start to dest by following the parent links
if (isinf(f(dest_node)))
  route = [];
else
  route = [dest_node];
  
  while (parent(route(1)) ~= 0)
    route = [parent(route(1)), route];
  end


  % Snippet of code used to visualize the map and the path
  for k = 2:length(route) - 1    
    map(route(k)) = 7;
    pause(0.1);
    image(1.5, 1.5, map);
    grid on;
    axis image;
  end
end
end

DijkstraGrid.m

function [route,numExpanded] = DijkstraGrid (input_map, start_coords, dest_coords)
% Run Dijkstra's algorithm on a grid.
% Inputs : 
%  input_map : a logical array where the freespace cells are false or 0 and
%  the obstacles are true or 1
%  start_coords and dest_coords : Coordinates of the start and end cell
%  respectively, the first entry is the row and the second the column.
% Output :
%  route : An array containing the linear indices of the cells along the
%  shortest route from start to dest or an empty array if there is no
%  route. This is a single dimensional vector
%  numExpanded: Remember to also return the total number of nodes
%  expanded during your search. Do not count the goal node as an expanded node.




% set up color map for display
% 1 - white - clear cell
% 2 - black - obstacle
% 3 - red = visited
% 4 - blue - on list
% 5 - green - start
% 6 - yellow - destination


cmap = [1 1 1; ...
    0 0 0; ...
    1 0 0; ...
    0 0 1; ...
    0 1 0; ...
    1 1 0; ...
 0.5 0.5 0.5];


colormap(cmap);


% variable to control if the map is being visualized on every
% iteration
drawMapEveryTime = true;


[nrows, ncols] = size(input_map);


% map - a table that keeps track of the state of each grid cell
map = zeros(nrows,ncols);


map(~input_map) = 1;  % Mark free cells
map(input_map) = 2;  % Mark obstacle cells


% Generate linear indices of start and dest nodes
start_node = sub2ind(size(map), start_coords(1), start_coords(2));
dest_node = sub2ind(size(map), dest_coords(1), dest_coords(2));


map(start_node) = 5;
map(dest_node) = 6;


% Initialize distance array
distanceFromStart = Inf(nrows,ncols);


% For each grid cell this array holds the index of its parent
parent = zeros(nrows,ncols);


distanceFromStart(start_node) = 0;


% keep track of number of nodes expanded 
numExpanded = 0;


% Main Loop
while true
  
  % Draw current map
  map(start_node) = 5;
  map(dest_node) = 6;
  
  % make drawMapEveryTime = true if you want to see how the 
  % nodes are expanded on the grid. 
  if (drawMapEveryTime)
    image(1.5, 1.5, map);
    grid on;
    axis image;
    drawnow;
  end
  
  % Find the node with the minimum distance
  [min_dist, current] = min(distanceFromStart(:));
  
  if ((current == dest_node) || isinf(min_dist))
    break;
  end;
  
  % Update map
  map(current) = 3;     % mark current node as visited
  numExpanded=numExpanded+1;
  % Compute row, column coordinates of current node
  [i, j] = ind2sub(size(distanceFromStart), current);
  
  % ********************************************************************* 
  % YOUR CODE BETWEEN THESE LINES OF STARS
  
  % Visit each neighbor of the current node and update the map, distances
  % and parent tables appropriately.
  action=[-1 0; 1 0; 0 -1; 0 1];%上,下,左,右
  for a=1:4
    expand=[i,j]+action(a,:);
    expand1=expand(1,1);
    expand2=expand(1,2);
    %不超出邊界,不穿越障礙,不在CLOSED列表里,則進行擴展
    if ( expand1>=1 && expand1<=nrows && expand2>=1 && expand2<=ncols && map(expand1,expand2)~=2 && map(expand1,expand2)~=3 && map(expand1,expand2)~=5 )
% ? ? ? ? ? if ( expand1>=1 && expand1<=nrows && expand2>=1 && expand2<=ncols && map(expand1,expand2)~=2 && map(expand1,expand2)~=3 && map(expand1,expand2)~=5)
      if ( distanceFromStart(expand1,expand2)> distanceFromStart(i,j)+1 )
        distanceFromStart(expand1,expand2)= distanceFromStart(i,j)+1;
        parent(expand1,expand2)=current;
        map(expand1,expand2)=4;
      end
    end
  end
  distanceFromStart(current) = Inf; % remove this node from further consideration
  %*********************************************************************


end


%% Construct route from start to dest by following the parent links
if (isinf(distanceFromStart(dest_node)))
  route = [];
else
  route = [dest_node];
  
  while (parent(route(1)) ~= 0)
    route = [parent(route(1)), route];
  end
  
    % Snippet of code used to visualize the map and the path
  for k = 2:length(route) - 1    
    map(route(k)) = 7;
    pause(0.1);
    image(1.5, 1.5, map);
    grid on;
    axis image;
  end
end
end

注:運行環(huán)境,Matlab 2019a 版本,安裝 RTB(Robotic Tool Box)工具包,鏈接地址為,RTB安裝鏈接。

該工具包中可以運行作者大佬寫到的matlab/simulink四軸歷程,只需要使用指令 sl_quadrotor 即可。

b0b40faa-725c-11ed-8abf-dac502259ad0.png

b0cd2cec-725c-11ed-8abf-dac502259ad0.png

使用方法

在Matlab 中,使用 makemap(30) 來生成地圖,通過鼠標來設(shè)置障礙形狀。該例子生成了一個30*30的方陣,然后直接運行TestScript.m即可。

其中要在TestScript.m中選擇是采用A算法,還是Dijkstra算法。同時設(shè)置起始點和終點在哪。下圖顯示得到的A算法路徑優(yōu)化結(jié)果。

其中綠色點為起點,黃色點為終點,黑色表示障礙,白色表示空閑,紅色表示搜尋過,灰色表示最后規(guī)劃的路徑。

b0e4161e-725c-11ed-8abf-dac502259ad0.png

下圖顯示Dijkstra算法的路徑優(yōu)化結(jié)果:

b0fb2e62-725c-11ed-8abf-dac502259ad0.png

對應(yīng)的動態(tài)效果已經(jīng)錄屏,下面給出傳送門(錄屏水印廣告請忽略):

通過對比可以看出:A* 算法搜索速度較快,畢竟里面有貪心算法。

這在地圖較大的場景應(yīng)用較好。但是A*算法只能得到局部最優(yōu)解,并不能保證全局最優(yōu)解。

相比之下,Dijkstra算法盡管搜索速度慢,但是是全局最優(yōu)解。不知道兩種方法結(jié)合gmapping,hector或者cartographer生成的柵格地圖會是什么樣的效果。后面期待嘗試一下。

審核編輯:湯梓紅

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • matlab
    +關(guān)注

    關(guān)注

    181

    文章

    2960

    瀏覽量

    230024
  • 算法
    +關(guān)注

    關(guān)注

    23

    文章

    4587

    瀏覽量

    92501
  • 源碼
    +關(guān)注

    關(guān)注

    8

    文章

    632

    瀏覽量

    29110
  • Dijkstra
    +關(guān)注

    關(guān)注

    0

    文章

    13

    瀏覽量

    8423

原文標題:Dijkstra和A*算法及其Matlab實現(xiàn)

文章出處:【微信號:3D視覺工坊,微信公眾號:3D視覺工坊】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。

收藏 人收藏

    評論

    相關(guān)推薦

    使用dijkstra算法的準備工作

    使用dijkstra算法,dijkstra算法是特別經(jīng)典的路徑分析算法,文章中的算法也確實很容易
    發(fā)表于 05-23 08:13

    數(shù)字信號處理及其MATLAB實現(xiàn)

    數(shù)字信號處理及其MATLAB實現(xiàn)
    發(fā)表于 03-25 15:05 ?20次下載

    數(shù)字信號處理及其MATLAB實現(xiàn)

    數(shù)字信號處理及其MATLAB實現(xiàn)
    發(fā)表于 03-26 14:13 ?373次下載

    SOFM網(wǎng)絡(luò)及其MATLAB中的實現(xiàn)

    本文詳細敘述了自組織映射網(wǎng)絡(luò)的原理、算法及其Matlab實現(xiàn)的工具箱,并結(jié)合實例給出了SOFM 在Matlab 上的
    發(fā)表于 09-18 11:04 ?14次下載

    模糊推理的Mamdani算法及其Matlab實現(xiàn)

    模糊濾波的mamdani算法及其Matlab實現(xiàn)
    發(fā)表于 11-17 18:23 ?40次下載

    基于有向非負極圖數(shù)據(jù)DIJKSTRA算法

    法相結(jié)合的方法。對Dijkstra算法改進,并求解關(guān)鍵節(jié)點(起點,終點和必經(jīng)節(jié)點)間的最短路徑,進而從關(guān)鍵節(jié)點所構(gòu)成的矩陣中采用回溯法得到目標路徑。通過實際的算法實現(xiàn),測試大量的有向非
    發(fā)表于 11-03 15:22 ?8次下載
    基于有向非負極圖數(shù)據(jù)<b class='flag-5'>DIJKSTRA</b><b class='flag-5'>算法</b>

    BP算法及其matlab實現(xiàn)

    高級自動控制算法:BP算法及其matlab實現(xiàn)
    發(fā)表于 12-02 11:45 ?2次下載

    基于Dijkstra最短路徑的抽樣算法

    針對社交網(wǎng)絡(luò)中隨機抽樣算法抽樣結(jié)果不能很好地代表原始網(wǎng)絡(luò)的問題,設(shè)計了一種基于Dijkstra最短路徑的抽樣算法。首先,利用Dijkstra算法
    發(fā)表于 12-17 11:40 ?1次下載
    基于<b class='flag-5'>Dijkstra</b>最短路徑的抽樣<b class='flag-5'>算法</b>

    《圖論算法及其MATLAB實現(xiàn)》電子教材和函數(shù)及函數(shù)功能列表概述

    全書分為相對獨立的9章每章都是解決-類問題的算法思想及其MATLAB實現(xiàn),首先介紹有關(guān)基礎(chǔ)知識,然后給出相關(guān)著名實際問題及解決此向題的算法
    發(fā)表于 10-22 08:00 ?0次下載

    遺傳算法原理及其MATLAB實現(xiàn)的詳細資料說明

    本文檔的主要內(nèi)容詳細介紹的是遺傳算法原理及其MATLAB實現(xiàn)的詳細資料說明。
    發(fā)表于 12-18 08:00 ?4次下載
    遺傳<b class='flag-5'>算法</b>原理<b class='flag-5'>及其</b><b class='flag-5'>MATLAB</b><b class='flag-5'>實現(xiàn)</b>的詳細資料說明

    系統(tǒng)仿真及其Matlab實現(xiàn)

    系統(tǒng)仿真及其Matlab實現(xiàn)方法介紹。
    發(fā)表于 06-17 17:13 ?27次下載

    基于STM32的A*(A星)尋路算法實現(xiàn)

    STM32 + LED點陣屏 實現(xiàn)A算法尋路A算法最初發(fā)表于1968年。它可以被認為是Dijkstr
    發(fā)表于 12-27 19:15 ?14次下載
    基于STM32的<b class='flag-5'>A</b>*(<b class='flag-5'>A</b>星)尋路<b class='flag-5'>算法</b><b class='flag-5'>實現(xiàn)</b>

    Dijkstra算法A*算法

    在本文中,我們將主要介紹Dijkstra算法A*算法,從成本計算的角度出發(fā),并逐步展開討論。 我們將從廣度優(yōu)先搜索開始,然后引入Dijkstra
    的頭像 發(fā)表于 07-07 10:56 ?1391次閱讀
    <b class='flag-5'>Dijkstra</b><b class='flag-5'>算法</b>和<b class='flag-5'>A</b>*<b class='flag-5'>算法</b>

    路徑規(guī)劃算法實現(xiàn)原理

    本文會用matlab實現(xiàn)Dijkstra算法,并且會分享一些函數(shù)用法的鏈接,也是本人學習得來,供大家參考,批評指正。
    的頭像 發(fā)表于 09-06 15:36 ?736次閱讀

    中國鐵路網(wǎng)的Dijkstra算法實現(xiàn)案例

    該項目分別在DE1-SOC開發(fā)板的FPGA和HPS上實現(xiàn)Dijkstra算法,能在中國鐵路網(wǎng)中找到兩站之間的最短距離和路線。
    的頭像 發(fā)表于 04-09 11:10 ?486次閱讀
    中國鐵路網(wǎng)的<b class='flag-5'>Dijkstra</b><b class='flag-5'>算法</b><b class='flag-5'>實現(xiàn)</b>案例