a star 算法 matlab,A-star Search Algorithm(A*优化算法)

BColorImageLinkQuoteCodeSmilies

验证问答

换一个        验证码

换一个

帮助本版积分规则参与/回复主题

请认真回复帖子,拒绝恶意灌水、垃圾广告、反动言论!

打包下载每日签到积分充值有偿担保函数帮助设为首页收藏本站开启辅助访问切换到窄版切换风格

消息

新听众(1)

《量化投资:以MATLAB为工具》

MATLAB技术论坛

hi好还ijaj |马甲 |我的 |设置 |消息 |提醒(1) |退出

积分: 11 |用户组: 幼儿园

快捷导航

首页论坛培训资源数模威客我的

资讯动态技术论坛仿真论坛资源中心读书频道数学建模数学软件有偿编程技术开发信息共享站务管理

请输入搜索内容

帖子

搜索

热搜: 电机预测单相逆变器定位无刷直流电机变压器太阳能数据挖掘激光器跟踪轨迹HSV轨迹跟踪simulinksvm怎么训练路径跟踪目标跟踪数据处理小波变换信号处理封装

MATLAB技术论坛»论坛›〓 MATLAB 资源共享 〓›MATLAB/Simulink 程序源码›A-star Search Algorithm(A*优化算法)

12345678910... 67

1

/ 67 页下一页

返回列表发新帖回复

查看: 62774|回复: 664

打印 上一主题 下一主题

收起左侧 [其它] A-star Search Algorithm(A*优化算法)     [复制链接]

dynamic

签到天数: 12 天

[LV.3]偶尔看看II

1519

主题

5588

帖子

7万

积分

管理员

风雪夜归人

Rank: 30Rank: 30Rank: 30Rank: 30

积分72761

管理团队技术小组原创先锋宣传大使

QQ

发消息

电梯直达

跳转到指定楼层 楼主

发表于 2008-12-13 15:54:50 | 只看该作者 回帖奖励

图论工具箱:http://www.matlabsky.com/thread-295-1-1.html

基本图论函数库:http://www.matlabsky.com/thread-299-1-1.html

Dijkstra最短路径:http://www.matlabsky.com/thread-297-1-1.html

Kruskal最小生成树:http://www.matlabsky.com/thread-296-1-1.html

Prims算法:http://www.matlabsky.com/thread-300-1-1.html

A星优化算法:http://www.matlabsky.com/thread-298-1-1.html

MATLAB/C++ mixed implementation for Astar search algorithm

Usage:

1. Extract the zip file

2. Type "Mex Astar.cpp" in MATLAB command window to generate Astar.dll (you must choose to have such ability when installing MATLAB)

"

以下是A*算法Matlab代码示例: ``` function [path, cost] = astar(start, goal, map) % A* algorithm implementation in Matlab % Input arguments: % start - starting node coordinates [x, y] % goal - goal node coordinates [x, y] % map - binary matrix representing the environment, where 0 is free and 1 is occupied % Output arguments: % path - array of nodes representing the path from start to goal % cost - total cost of the path % Define the heuristic function (Euclidean distance) heuristic = @(a,b) sqrt((a(1)-b(1))^2 + (a(2)-b(2))^2); % Define the cost function (Euclidean distance) costfunc = @(a,b) sqrt((a(1)-b(1))^2 + (a(2)-b(2))^2); % Define the open and closed lists openList = start; closedList = []; % Define the gScore and fScore of the starting node gScore = inf(size(map)); gScore(start(2), start(1)) = 0; fScore = inf(size(map)); fScore(start(2), start(1)) = heuristic(start, goal); % Start the search loop while ~isempty(openList) % Select the node with the lowest fScore in the openList [~, current] = min(fScore(openList(:,2), openList(:,1))); current = openList(current,:); % Check if the current node is the goal node if isequal(current, goal) % Reconstruct the path and compute the cost path = [current]; cost = 0; while ~isequal(current, start) [~, prev] = min(gScore(current(2)-1:current(2)+1, current(1)-1:current(1)+1) + ... costfunc(current, [current(1)-1:current(1)+1; current(2)-1:current(2)+1]')); current = [current(1)-1:current(1)+1; current(2)-1:current(2)+1](:,prev); path = [current path]; cost = cost + costfunc(path(:,1), path(:,2)); end return end % Move the current node from the openList to the closedList openList(ismember(openList, current, 'rows'),:) = []; closedList(end+1,:) = current; % Generate the neighbors of the current node neighbors = [current(1)-1 current(2); current(1)+1 current(2); current(1) current(2)-1; current(1) current(2)+1]; neighbors(neighbors(:,1)<1 | neighbors(:,1)>size(map,2) | ... neighbors(:,2)<1 | neighbors(:,2)>size(map,1) | ... map(sub2ind(size(map),neighbors(:,2),neighbors(:,1))), :) = []; % Iterate over the neighbors of the current node for i = 1:size(neighbors,1) neighbor = neighbors(i,:); % Check if the neighbor is already in the closedList if any(ismember(closedList, neighbor, 'rows')) continue end % Compute the tentative gScore of the neighbor tentative_gScore = gScore(current(2), current(1)) + costfunc(current, neighbor); % Check if the neighbor is already in the openList if ~any(ismember(openList, neighbor, 'rows')) openList(end+1,:) = neighbor; % Check if the tentative gScore is greater than the current gScore of the neighbor elseif tentative_gScore >= gScore(neighbor(2), neighbor(1)) continue end % Update the gScore and fScore of the neighbor gScore(neighbor(2), neighbor(1)) = tentative_gScore; fScore(neighbor(2), neighbor(1)) = tentative_gScore + heuristic(neighbor, goal); end end % If the goal node is not reachable, return an empty path and infinite cost path = []; cost = inf; end ``` 这个代码中的输入参数 `map` 是一个二元矩阵,其中 0 表示空闲,1 表示障碍物。输出参数 `path` 是一个节点数组,表示从起点到终点的路径;`cost` 是该路径的总成本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值