路径规划A*算法_asasasaababab的博客-程序员宅基地

技术标签: 算法  a-算法  学习笔记  搜索  Astar  

A*算法是在Dijkstra算法上进行改进,毕竟,我们是知道终点和起点的位置信息的,Dijkstra算法完全是四面八方全都找,然而我们既然已经知道,比方说重点在起点的北方,那么完全可以直接往北方搜索。

所以算法综合了Best-First Search和Dijkstra算法的优点:在进行启发式搜索提高算法效率的同时,可以保证找到一条最优路径(基于评估函数)。

在此算法中,如果以g(n)表示从起点到任意顶点n的实际距离,h(n)表示任意顶点n到目标顶点的估算距离(根据所采用的评估函数的不同而变化),那么A*算法的估算函数为:

f(x)=g(x)+h(x)

  • 如果g(n)为0,即只计算任意顶点 n到目标的评估函数 h(n),而不计算起点到顶点n的距离,则算法转化为使用贪心策略的Best-First Search,速度最快,但可能得不出最优解;
  • 如果h(n)不高于实际到目标顶点的距离,则一定可以求出最优解,而且h(n)越小,需要计算的节点越多,算法效率越低,常见的评估函数有——欧几里得距离、曼哈顿距离、切比雪夫距离;
  • 如果h(n)为0,即只需求出起点到任意顶点n的最短路径 g(n),而不计算任何评估函数h(n),则转化为单源最短路径问题,即Dijkstra算法,此时需要计算最多的定点;

  • 4方向用 L1 距离

  • 8方向用 L 距离,也就是max
  • 任意方向用 L2 距离

heuristic function

这个评估函数,其实就是给搜索算法一个知识,告诉搜索算法往哪个方向比较对。
评估函数有这么几个特性:

  • 将每一个节点映射到非负实数 H:node{ x|x0,xR}
  • H(goal)=0
  • 对任意两个相邻的节点 x,y
    • H(x)H(y)+d(x,y)
    • d(x,y)=weightheight of edge from x to y

这些属性可以令:

n,H(n) shortest path from n to goal

关于H函数:
http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html

伪代码

  • 对于每一个node n
    • n.f = Infinity, n.g = Infinity
  • 创建一个空的List
  • start.g = 0, start.f = H(start),将start加入到list里边
  • While(List非空)
    • current = list中最小f值的那个node
    • 如果current == goal,那么就完成啦
    • 对每一个node, n是这个node的临近的节点
    • if(n.g > (current.g + cost of edge from n to current))
      • n.g = current.g + cost of edge from n to current
      • n.f = n.g + H(n)
      • n.parent = current
      • add n to list if it’s not there already

matlab代码

function [route,numExpanded] = AStarGrid (input_map, start_coords, dest_coords, drawMapEveryTime)
% 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
% 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;

% 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);

% 
[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
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
    [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

    % 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
    %
    numExpanded = numExpanded + 1;
    if(i-1>=1) %upper
        id = sub2ind(size(map), i-1, j);    
        if((map(id) ~= 2) ... %if not obst
            && (map(id) ~= 3) ... % if not visited
            && (map(id) ~= 5)) ... % if not start
            if(g(id) >= g(current) + 1)
                g(id) = g(current) + 1;
                f(id) = g(id) + H(id);
                parent(id) = current;
                map(id) = 4;

            end            
        end
    end

    if(i+1 <= nrows) %lower
        id = sub2ind(size(map), i+1, j);
        if((map(id) ~= 2) ... %if not obst
            && (map(id) ~= 3) ... % if not visited
            && (map(id) ~= 5)) ... % if not start
            if(g(id) >= g(current) + 1)
                g(id) = g(current) + 1;
                f(id) = g(id) + H(id);
                parent(id) = current;
                map(id) = 4;

            end                     
        end
    end

    if(j-1 >= 1) %left
        id = sub2ind(size(map), i, j-1);
        if((map(id) ~= 2) ... %if not obst
            && (map(id) ~= 3) ... % if not visited
            && (map(id) ~= 5)) ... % if not start
            if(g(id) >= g(current) + 1)
                g(id) = g(current) + 1;
                f(id) = g(id) + H(id);
                parent(id) = current;
                map(id) = 4;

            end                    
        end
    end

    if(j+1 <= ncols) %left
        id = sub2ind(size(map), i, j+1);
        if((map(id) ~= 2) ... %if not obst
            && (map(id) ~= 3) ... % if not visited
            && (map(id) ~= 5)) ... % if not start
            if(g(id) >= g(current) + 1)
                g(id) = g(current) + 1;
                f(id) = g(id) + H(id);
                parent(id) = current;
                map(id) = 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

测试代码

map = false(10); %Input map parameters
map (2:10, 6) = true; %Obstacle Declaration
start_coords = [6, 2]; %Starting coordinates
dest_coords  = [8, 9]; %Destination Coordinates
drawMapEveryTime = false; %Display Outputs
[route, numExpanded] = AStarGrid(map, start_coords, dest_coords,drawMapEveryTime) %Implementatio

结果:
结果

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/asasasaababab/article/details/78763751

智能推荐

速度可调流水灯控制_如何在程序里改灯的速度_yydtz的博客-程序员宅基地

一、目标假设单片机晶振频率为12MHz,单片机P0口外接8只LED,P3.2按键外接K1,P3.3外接K2。编写程序,每次按下按键K1时,加快8只LED的流水显示速度;每次按下K2时,降低8只LED的流水显示速度。(该实验要求用到定时器)二、布线三、实现1、设置全局变量,定义管脚。sbit K1=P3^2;sbit K2=P3^3; uint speed_control=10;uint counter=0;uint p0=0xFE;2、两个外部中断和一个定时器中断函数。void i_如何在程序里改灯的速度

ESP8266开发中无法启动的问题_esp8266 不启动-程序员宅基地

编译没有任何问题,下载到8266中时,出现以下问题:ets Jan 8 2013,rst cause:2, boot mode:(3,7)load 0x40100000, len 25964, room 16tail 12chksum 0x2cho 0 tail 12 room 4load 0x3ffe8000, len 1296, room 12tail 4chksum 0xf5load 0x3ffe8510, len 1860, room 4tail 0chksum 0x99c_esp8266 不启动

sap-错误多收了库存的跨期调整实例(转)-程序员宅基地

实例: 期初无库存,某炼化企业上月采购原油10500吨,已收货,单价为4000元/吨,合计4200万元,发票校验已完成,付完款项4200万且已清账,上月消耗5000吨;本月发现收货时库存错误,采购收货应为10000吨,即多..._sap收货收多了如何调账

Oracle升级中的参数补充-程序员宅基地

数据库升级的时候有一个很重要的环节就是数据库参数审核,对于数据库参数还是有很多的门道,其中一种就是对于过期参数的处理。我们可以使用如下的SQL来得到一个基本的过期参数列表。 SELECT name FROM...

redhat 10 下载地址(官方网站转的 图个方便 可以多线程下载)_redhat10镜像-程序员宅基地

Mirror AccessThe following mirrors carry Fedora Core: North America South America Europe Africa Asia Asia/Pacific North America USA East ftp://ftp.linux.ncsu.edu/pub/fedora/linux/core/ http://mirror.l_redhat10镜像

python 创建txt文件并写入字符串-python创建txt文件-程序员宅基地

1.自己写入txt直接上核心代码:with open("douban.txt","w") as f:f.write("这是个测试!")1212这句话自带文件关闭功能,所以和那些先open再write再close的方式来说,更加pythontic!结果就是这样:2.将文件输入(print)的内容写入txt#分模块测试,txt写入测试# -*..._python创建test·txt文件,将字符串分两行写入文件中

随便推点

js动态生成表格逐行添加、删除、遍历取值保存-程序员宅基地

  关于js对表格进行逐行添加,今天抽空整理了一下:新建一个html文件(没有编辑器的可以新建一个demo.txt文件,然后改后缀名为demo.html),把下面代码全部贴进去即可。功能包括:表格添加一行,表格删除一行,表格遍历取值等。点击说明:点击添加按钮,则表格添加一行,可进行录入,删除按钮,可删除当前行,其他行不影响。删除或者添加,每行的的编号都会自动变化,套餐和价格是<input...

Web前端-Html2--a链接-程序员宅基地

标签 a链接4大功能a标签有四大功能: 1.超链接功能 能够点击跳转到指定页面 2.回到顶部功能 <a href="#">文本</a&g..._a链接

读书笔记之《深入理解计算机系统(中文版)》_雷迎春计算机-程序员宅基地

《深入理解计算机系统》关于这本书译序 2004.2.15中科院,雷迎春现实中需要程序员更好地理解计算机系统,知道程序如何在计算机上被执行。这本书最大优点是为程序员描述计算机系统用的实现细节,帮助其在大脑中构造一个层次性的计算机系统,从最底层的数据在内存中的表示,到流水线指令的构成,到虚拟存储器。到编译系统,到动态加载库,到最后的用户态应用。使程序员在设计程序时,能充分意识到计算机系统的重要性,建立起被所写程序可能被执行的数据或指令流图,明白当程序被执行时,到底发生了什么事,从而能设_雷迎春计算机

Docker镜像篇(10)- 综合练习 - springboot项目制作镜像_/app#/addperson/d745a10381d3453a8e083aecaf5b7ed0-程序员宅基地

mysql镜像制作[root@docker ~]# docker run --rm --name first-mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.64bcd97f8848534b3a6ff461c49ab3b9b49347b05e5bfc68e87d0ecfdbb491d71[root@docker ~]# docker ps -aCONTAINER ID IMAGE COMMAND _/app#/addperson/d745a10381d3453a8e083aecaf5b7ed0

carsim路径设置_carsim不沿着线走-程序员宅基地

carsim----miscellaneous-------roads------3D surface:这里选的是3Dgrid里的1200m的单车道,点进去就是第二个图了,几何机构一栏1,从上到下:第一个,可以设置路径的形状,比如,直线,圆等,点进去可以自己根据坐标进行更改,第二个更改道路坡度;可以使连续值,也可以自己编辑坡度随位移的变化第三个:设定道路,两侧的高度,更改车..._carsim不沿着线走

vue-自定义组件传-程序员宅基地

项目中,我们经常会遇到自定义组件传值的问题,方法很多种,但是原理很简单,下述文档总结实际项目中使用的传值方式。父组件传递给子组件某一值,子组件内会修改该值,然后父组件需要获取新值​ 在 Vue 中,父子组件的关系可以总结为prop 向下传递,事件向上传递。父组件通过prop给子组件下发数据,子组件通过事件给父组件发送消息。常规prop-event父组件<p...