用Matlab实现超酷的路径规划动画:A星与PSO算法的碰撞

张开发
2026/4/11 4:36:20 15 分钟阅读

分享文章

用Matlab实现超酷的路径规划动画:A星与PSO算法的碰撞
1-149matlabA星算法PSO算法实现路径规划动画演示 基于matlab的A星算法和PSO算法实现路径规划动画演示具有GUI界面可自主生成障碍物 移动靶路径规划 程序已调通可直接运行嘿各位程序猿小伙伴们今天来聊聊在Matlab里用A星算法和PSO算法实现超炫的路径规划动画而且还有个超方便的GUI界面障碍物都能自己生成还涉及到移动靶路径规划哦关键是程序已经调通直接就能跑起来是不是很心动A星算法A星算法在路径规划里可是明星算法它结合了Dijkstra算法的广度优先搜索和贪心算法的最佳优先搜索特点通过一个评估函数$f(n)g(n)h(n)$ 来寻找最佳路径。$g(n)$ 是从起点到节点$n$ 的实际代价$h(n)$ 是从节点$n$ 到目标点的估计代价。下面咱来看点Matlab代码实现A星算法的关键部分function [path, cost] aStar(start, goal, map) openSet [start]; cameFrom containers.Map; gScore containers.Map; fScore containers.Map; gScore(start) 0; fScore(start) heuristic(start, goal); while ~isempty(openSet) [~, currentIndex] min([fScore.values{:}]); current openSet(currentIndex); openSet(currentIndex) []; if current goal path reconstructPath(cameFrom, current); cost gScore(current); return; end neighbors getNeighbors(current, map); for i 1:numel(neighbors) neighbor neighbors(i); tentativeGScore gScore(current) 1; if ~gScore.isKey(neighbor) || tentativeGScore gScore(neighbor) cameFrom(neighbor) current; gScore(neighbor) tentativeGScore; fScore(neighbor) tentativeGScore heuristic(neighbor, goal); if ~ismember(neighbor, openSet) openSet [openSet, neighbor]; end end end end path []; cost Inf; end function h heuristic(node, goal) h abs(node(1) - goal(1)) abs(node(2) - goal(2)); end function neighbors getNeighbors(node, map) neighbors []; x node(1); y node(2); if x 1 map(x - 1, y) ~ 1 neighbors [neighbors; x - 1, y]; end if x size(map, 1) map(x 1, y) ~ 1 neighbors [neighbors; x 1, y]; end if y 1 map(x, y - 1) ~ 1 neighbors [neighbors; x, y - 1]; end if y size(map, 2) map(x, y 1) ~ 1 neighbors [neighbors; x, y 1]; end end function path reconstructPath(cameFrom, current) path [current]; while cameFrom.isKey(current) current cameFrom(current); path [current; path]; end end在这段代码里aStar函数就是整个A星算法的核心。一开始初始化了openSet等关键数据结构openSet用来存放待探索的节点。gScore和fScore分别记录实际代价和评估代价。在循环里每次从openSet里取出fScore最小的节点进行探索找到它的邻居节点后更新gScore和fScore如果邻居节点不在openSet里就添加进去。最后通过reconstructPath函数根据记录的路径信息恢复出最终路径。PSO算法PSO算法粒子群优化算法模拟鸟群觅食行为每个粒子代表解空间中的一个潜在解通过追踪个体极值和全局极值来更新自己的位置和速度。1-149matlabA星算法PSO算法实现路径规划动画演示 基于matlab的A星算法和PSO算法实现路径规划动画演示具有GUI界面可自主生成障碍物 移动靶路径规划 程序已调通可直接运行Matlab代码实现PSO算法路径规划的关键片段function [bestPath, bestCost] psoPathPlanning(numParticles, numIterations, map, start, goal) dim 2; % 二维空间 c1 1.5; % 学习因子1 c2 1.5; % 学习因子2 w 0.7; % 惯性权重 positions zeros(numParticles, dim); velocities zeros(numParticles, dim); pbestPositions zeros(numParticles, dim); pbestCosts Inf(numParticles, 1); gbestPosition zeros(1, dim); gbestCost Inf; for i 1:numParticles positions(i, :) start rand(1, dim).* (size(map) - start); velocities(i, :) randn(1, dim); end for t 1:numIterations for i 1:numParticles cost pathCost(positions(i, :), map, goal); if cost pbestCosts(i) pbestCosts(i) cost; pbestPositions(i, :) positions(i, :); end if cost gbestCost gbestCost cost; gbestPosition positions(i, :); end end for i 1:numParticles r1 rand(1, dim); r2 rand(1, dim); velocities(i, :) w * velocities(i, :) c1 * r1.* (pbestPositions(i, :) - positions(i, :)) c2 * r2.* (gbestPosition - positions(i, :)); positions(i, :) positions(i, :) velocities(i, :); % 边界处理 positions(i, 1) max(1, min(size(map, 1), positions(i, 1))); positions(i, 2) max(1, min(size(map, 2), positions(i, 2))); end end bestPath gbestPosition; bestCost gbestCost; end function cost pathCost(position, map, goal) % 这里简单计算一个到目标点的距离和障碍物代价的和 dist norm(position - goal); if map(round(position(1)), round(position(2))) 1 dist dist 10; % 障碍物惩罚 end cost dist; end在psoPathPlanning函数里初始化了粒子的位置、速度等信息。每次迭代时计算每个粒子的路径代价更新个体最优和全局最优。然后根据速度更新公式更新粒子的速度和位置同时做边界处理保证粒子在合理的范围内。pathCost函数用来计算粒子位置对应的路径代价这里简单地结合了到目标点的距离和碰到障碍物的惩罚。GUI界面与动画演示Matlab的GUI界面设计让用户操作超方便。可以通过uicontrol等函数创建按钮、文本框等组件。比如创建一个生成障碍物的按钮uicontrol(Style, pushbutton, String, 生成障碍物,... Position, [100 100 100 30], Callback, generateObstaclesCallback); function generateObstaclesCallback(hObject, eventdata) % 在这里实现生成障碍物的逻辑比如随机在地图上设置一些点为障碍物 numObstacles randi([10, 20]); for i 1:numObstacles x randi(size(map, 1)); y randi(size(map, 2)); map(x, y) 1; end % 更新地图显示等操作 end动画演示部分可以利用Matlab的animatedline等函数。假设已经计算出路径pathfigure; hold on; grid on; mapImage imagesc(map); set(mapImage, AlphaData, 0.5); for i 1:size(path, 1) a animatedline(path(i, 1), path(i, 2)); set(a, Color,red, Marker, o); drawnow; pause(0.1); end这段代码先创建地图显示然后通过animatedline逐点绘制路径每次绘制暂停0.1秒就形成了动画效果。总之通过结合A星算法、PSO算法再配上方便的GUI界面和酷炫的动画演示咱们在Matlab里实现了一个超实用又好玩的路径规划小工具感兴趣的小伙伴赶紧自己试试说不定还能玩出更多花样呢

更多文章