A*算法例子

作者: 学习编程王同学 | 来源:发表于2019-05-20 18:31 被阅读23次

A*算法代码下载本文涉及到的代码。

A*算法程序代码

function [distance, path] = a_star(map_size, neighbors, start, goal)
% A* 算法, 找不到路径时返回 error
% map_size      输入  地图尺寸 == size(mymap)
% neighbors     输入  邻居 (cell)
% start         输入  起点
% goal          输入  目标点
% distance      输出  距离
% path          输出  路径

nodes_num = map_size(1) * map_size(2);

open_set = start;
close_set = [];
came_from = inf(nodes_num, 1);
gscore = inf(nodes_num, 1);
fscore = inf(nodes_num, 1);

gscore(start) = 0;
fscore(start) = gscore(start) + h_manhattan(map_size, start, goal);

while ~isempty(open_set)
    % 取出 open_set 中 f 最小的点 current
    min_f = inf;
    current = 0;
    for i = 1:length(open_set)
        node = open_set(i);
        if fscore(node) < min_f
            min_f = fscore(node);
            current = node;
        end
    end
    
    if current == goal
        distance  = gscore(current);
        path = reconstruct_path(came_from, current);
        return;
    end
    
    open_set = setdiff(open_set, current);
    close_set = [close_set current];
    
    for i = 1:length(neighbors{current})
        neighbor = neighbors{current}(i);
        if any(ismember(close_set, neighbor))
            continue;
        end
        
        tentative_gscore = gscore(current) + get_distance(map_size, current, neighbor);
        if ~any(ismember(open_set, neighbor))
            open_set = [open_set neighbor];
        elseif tentative_gscore >= gscore(neighbor)
            continue;
        end
        
        came_from(neighbor) = current;
        gscore(neighbor) = tentative_gscore;
        fscore(neighbor) = gscore(neighbor) + h_manhattan(map_size, neighbor, goal);
    end
end
error("Failure!");
end

% 返回两点曼哈顿距离
function hscore = h_manhattan(map_size, current, goal)
[current_row, current_col] = ind2sub(map_size, current);
[goal_row, goal_col] = ind2sub(map_size, goal);
hscore = abs(current_row - goal_row) + abs(current_col - goal_col);
end

% 构造路径
function path = reconstruct_path(came_from, current)
path = [current];
while came_from(current) <= length(came_from)
    path = [came_from(current) path];
    current = came_from(current);
end
end

找栅格地图中两点间最短距离

如下图所示栅格地图,指定起始点和目标点,智能体(或机器人)只能在“上、下、左、右”四个方向移动,找出最短路径:

栅格地图

结果如下:

A*得到的路径

相关文章

  • 算法例子

    11.手写二叉树层序遍历。 12.手写在数组指定位置插入元素,后续元素向后顺延,考虑数组长度和数组容量间的关系,长...

  • A*算法例子

    在A*算法代码下载本文涉及到的代码。 A*算法程序代码 找栅格地图中两点间最短距离 如下图所示栅格地图,指定起始点...

  • 算法优化例子

    最大连续子序列和问题:给定(可能是负的)整数序列,寻找(并标识)的值为最大的序列。如果所有的整数都是负的,那么最大...

  • Python机器学习基本分类算法脑图

    算法实例子证明 TO BE CONTINUED...

  • 第一篇 算法与时间复杂度

    算法的几个例子 算法的简单定义:将一定的输入转化为输出的过程 几个简单的例子了解哪些内容都是算法: 在Excel表...

  • 8月21日

    发刊词━给你的人生明确算法 举例子什么是算法呢?举个例子按钮AB,不同的思维有不同的按法! 通过算法的迭代,成功的...

  • Dijkstra算法例子

    在Dijkstra算法代码下载本文涉及到的代码。 程序代码 Dijkstra算法的程序如下: 找图中顶点间最短距离...

  • 隐马尔可夫模型(HMM) - 4 - 预测算法(维特比算法)

    这里介绍隐马尔可夫模型预测的两种算法:近似算法与维特比算法。近似算法 维特比算法 算法(维特比算法): 例子

  • python学习资料

    算法:Python中数据结构和算法的最小例子https://github.com/keon/algorithms?...

  • Paxos算法

    Basic-Paxos算法(可以先看后面的实际例子再看前面的具体介绍部分) Paxos算法的目的 Paxos算法的...

网友评论

    本文标题:A*算法例子

    本文链接:https://www.haomeiwen.com/subject/kydlzqtx.html