博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A*算法 (MATLAB) -路径搜索
阅读量:7071 次
发布时间:2019-06-28

本文共 3244 字,大约阅读时间需要 10 分钟。

A* 算法跟 很像,只是在下一步搜索中心的选择的方法不一样。 没有任何干预,找离起点 “最近”的邻居作为备选点,如果有若干个邻居都是相同距离的话,纯粹就是按照找到的顺序取第一个。A*算法,找与终点最近的邻居,作为下一个搜索中心。(不过,如果若干个邻居与终点的距离一样呢?)

下面的代码是从 拷贝来的,四个黄色的部分是修改的。

 

第二个黄色部分: 找与终点最近的邻居作为下一个搜索点。

[~, current] = min(f(:));
[min_dist, ~] = min(distanceFromStart(:));

 

里,这部分是:[min_dist, current] = min(distanceFromStart(:));

 

第三个黄色部分: 排除已经作为搜索点的邻居再次被 min 到的可能性f(current) = Inf;

 

里,这部分是:distanceFromStart(current) = Inf;

 

第四个黄色部分是新增加的:就是为了计算每一个邻居与终点的距离权值。只不过这里的距离权值预先计算好了放在H矩阵里,所以直需要从 H 里取值就好了。

 

所以第一黄色部分:定义f,计算H

[X, Y] = meshgrid (1:ncols, 1:nrows);
H = abs(Y - 4) + abs(X - 8);
f = Inf(nrows,ncols);
f(start_node) = H(start_node);
 
H 有很多种计算方法,可以直接算两点距离之类。
 
 
%% % set up color map for display cmap = [1 1 1; ...% 1 - white - clear cell         0 0 0; ...% 2 - black - obstacle                1 0 0; ...% 3 - red = visited                0 0 1; ...% 4 - blue - on list                0 1 0; ...% 5 - green - start                1 1 0];% 6 - yellow - destination colormap(cmap); map = zeros(10); % Add an obstacle map (1:5, 7) = 2; map(6, 2) = 5; % start_coords map(4, 8) = 6; % dest_coords image(1.5,1.5,map); grid on; axis image; %% nrows = 10; ncols = 10; start_node = sub2ind(size(map), 6, 2); dest_node = sub2ind(size(map), 4, 8); % Initialize distance array distanceFromStart = Inf(nrows,ncols); distanceFromStart(start_node) = 0; %====================[X, Y] = meshgrid (1:ncols, 1:nrows);H = abs(Y - 4) + abs(X - 8);f = Inf(nrows,ncols); f(start_node) = H(start_node); %=======================% For each grid cell this array holds the index of its parent parent = zeros(nrows,ncols); % Main Loop while true  % Draw current map  map(start_node) = 5;  map(dest_node) = 6;  image(1.5, 1.5, map);  grid on;  axis image;  drawnow;  %====================  % Find the node with the minimum distance  [
~, current] =
min(f(:)); [min_dist, ~] =
min(distanceFromStart(:)); %===================  if ((current == dest_node) || isinf(min_dist))        break;   end;  map(current) = 3; %============ f(current)
=
Inf; %============ [i, j] = ind2sub(size(distanceFromStart), current);  neighbor = [i-1,j;...             i+1,j;...             i,j+1;...             i,j-1] outRangetest = (neighbor(:,1)<1) + (neighbor(:,1)>nrows) +...                   (neighbor(:,2)<1) + (neighbor(:,2)>ncols ) locate = find(outRangetest>0); neighbor(locate,:)=[] neighborIndex = sub2ind(size(map),neighbor(:,1),neighbor(:,2)) for i=1:length(neighborIndex) if (map(neighborIndex(i))~=2) && (map(neighborIndex(i))~=3 && map(neighborIndex(i))~= 5)     map(neighborIndex(i)) = 4;   if distanceFromStart(neighborIndex(i))> min_dist + 1            distanceFromStart(neighborIndex(i)) = min_dist+1;         parent(neighborIndex(i)) = current;         f(neighborIndex(i))
=
H(neighborIndex(i));   end  end end end%%if (isinf(distanceFromStart(dest_node)))     route = []; else     %提取路线坐标   route = [dest_node];       while (parent(route(1)) ~= 0)               route = [parent(route(1)), route];        end   % 动态显示出路线             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

转载于:https://www.cnblogs.com/simulink/p/5219781.html

你可能感兴趣的文章
ZOJ 1649 Rescue(有敌人迷宫BFS)
查看>>
XV Open Cup named after E.V. Pankratiev. GP of Three Capitals
查看>>
ORA-01012: not logged on
查看>>
[svc]logstash和filebeat之间ssl加密
查看>>
初识btrace
查看>>
Mybatis-plus之RowBounds实现分页查询
查看>>
AjaxPro.Net的使用
查看>>
分享一些经典资源
查看>>
HDU-1723 Distribute Message
查看>>
6200 sdboot 测试版分析(一)
查看>>
WayOs扩展WAN口工具1.4隆重发布,同时发布BCM内置三天智能重启超级终端调试图...
查看>>
Java反编译插件Jdclipse导致Eclipse 3.7.2启动崩溃的解决方法
查看>>
关于Installshield中Ie8\Ie9\SQL Server 2008 R2 Native Client等Prq文件在线下载地址
查看>>
SQL Server 2008中增强的汇总技巧
查看>>
they're hiring
查看>>
几个 HTML 标签的用法
查看>>
《老罗Android开发视频教程-安卓巴士》(Android 开发)
查看>>
asp.net 伪静态 IIS设置后 直正HTML无法显示
查看>>
垃圾代码评析——关于《C程序设计伴侣》6.2(一)
查看>>
为 iPhone 和 iPad 自定义网站的主屏幕图标
查看>>