当前位置: 首页 > news >正文

路径规划算法大对决:A星、改进A星与新A星

A星 改进A星 新A星算法 路径规划 放在一张图上 对比 三天对比线在一张图 避障

在路径规划领域,A星算法就像一位老将,一直以来都备受瞩目。而随着研究的深入,改进A星和新A星算法也相继登场,今天咱们就把这几位“选手”放在一张图上,来一场精彩的对比,看看它们在避障场景下到底谁更胜一筹。

A星算法:经典的智慧

A星算法是一种启发式搜索算法,它结合了Dijkstra算法的广度优先搜索和贪心算法的最佳优先搜索特点。其核心思想在于通过一个评估函数$f(n) = g(n) + h(n)$来选择下一个要扩展的节点。

这里的$g(n)$表示从起点到节点$n$的实际代价,$h(n)$则是从节点$n$到目标点的估计代价。下面是一段简化的Python代码示例:

import heapq def heuristic(a, b): return abs(a[0] - b[0]) + abs(a[1] - b[1]) def astar(graph, start, goal): open_set = [] heapq.heappush(open_set, (0, start)) came_from = {} g_score = {node: float('inf') for node in graph.keys()} g_score[start] = 0 f_score = {node: float('inf') for node in graph.keys()} f_score[start] = heuristic(start, goal) while open_set: _, current = heapq.heappop(open_set) if current == goal: path = [] while current in came_from: path.append(current) current = came_from[current] path.append(start) path.reverse() return path for neighbor in graph[current]: tentative_g_score = g_score[current] + 1 if tentative_g_score < g_score[neighbor]: came_from[neighbor] = current g_score[neighbor] = tentative_g_score f_score[neighbor] = tentative_g_score + heuristic(neighbor, goal) if neighbor not in [i[1] for i in open_set]: heapq.heappush(open_set, (f_score[neighbor], neighbor)) return None

这段代码中,heuristic函数就是用来计算估计代价$h(n)$的,采用的是曼哈顿距离。astar函数则实现了整个A星搜索的过程,open_set使用优先队列来存储待扩展节点,按照$f$值从小到大排序。通过不断扩展节点,直到找到目标节点或者遍历完所有可到达节点。

改进A星算法:升级的策略

改进A星算法往往针对A星算法的某些不足进行优化。比如在传统A星算法中,$h(n)$的估计可能不够精准,导致搜索效率不高。改进的方法可能是对启发函数进行优化,让它更加贴近实际情况。

A星 改进A星 新A星算法 路径规划 放在一张图上 对比 三天对比线在一张图 避障

假设我们采用一种动态权重的启发函数:

def improved_heuristic(a, b, dynamic_weight): return dynamic_weight * (abs(a[0] - b[0]) + abs(a[1] - b[1])) def improved_astar(graph, start, goal, dynamic_weight): open_set = [] heapq.heappush(open_set, (0, start)) came_from = {} g_score = {node: float('inf') for node in graph.keys()} g_score[start] = 0 f_score = {node: float('inf') for node in graph.keys()} f_score[start] = improved_heuristic(start, goal, dynamic_weight) while open_set: _, current = heapq.heappop(open_set) if current == goal: path = [] while current in came_from: path.append(current) current = came_from[current] path.append(start) path.reverse() return path for neighbor in graph[current]: tentative_g_score = g_score[current] + 1 if tentative_g_score < g_score[neighbor]: came_from[neighbor] = current g_score[neighbor] = tentative_g_score f_score[neighbor] = tentative_g_score + improved_heuristic(neighbor, goal, dynamic_weight) if neighbor not in [i[1] for i in open_set]: heapq.heappush(open_set, (f_score[neighbor], neighbor)) return None

这里的improvedheuristic函数通过引入一个动态权重dynamicweight,可以根据实际场景调整启发函数的影响程度,使得搜索过程更加灵活高效,在避障场景下可能更快地找到路径。

新A星算法:崭露头角的新秀

新A星算法可能是基于一些全新的理念或者结合其他技术产生的。例如结合机器学习的方法来预学习地图的特征,从而优化路径搜索。

# 简单模拟新A星结合机器学习预学习的情况 class NewAStar: def __init__(self, learned_model): self.learned_model = learned_model def new_heuristic(self, a, b): # 根据预学习模型得到启发值 learned_value = self.learned_model.predict([a, b]) return learned_value def new_astar(self, graph, start, goal): open_set = [] heapq.heappush(open_set, (0, start)) came_from = {} g_score = {node: float('inf') for node in graph.keys()} g_score[start] = 0 f_score = {node: float('inf') for node in graph.keys()} f_score[start] = self.new_heuristic(start, goal) while open_set: _, current = heapq.heappop(open_set) if current == goal: path = [] while current in came_from: path.append(current) current = came_from[current] path.append(start) path.reverse() return path for neighbor in graph[current]: tentative_g_score = g_score[current] + 1 if tentative_g_score < g_score[neighbor]: came_from[neighbor] = current g_score[neighbor] = tentative_g_score f_score[neighbor] = tentative_g_score + self.new_heuristic(neighbor, goal) if neighbor not in [i[1] for i in open_set]: heapq.heappush(open_set, (f_score[neighbor], neighbor)) return None

这里假设learnedmodel是一个已经预学习好的模型,通过newheuristic函数来给出更符合实际情况的启发值,进而引导搜索过程。

对比:三天的“较量”

为了直观地对比这三种算法,我们在一张带有障碍物的地图上进行测试,并连续测试三天,记录它们每天找到路径的情况。

通过绘图工具(比如Python的Matplotlib库),我们可以将三种算法每天找到的路径绘制在同一张图上。

import matplotlib.pyplot as plt # 假设已经得到三种算法每天的路径结果 astar_paths = [astar(graph, start, goal) for _ in range(3)] improved_astar_paths = [improved_astar(graph, start, goal, 1.5) for _ in range(3)] new_astar_paths = [NewAStar(learned_model).new_astar(graph, start, goal) for _ in range(3)] colors = ['r', 'g', 'b'] labels = ['A星算法', '改进A星算法', '新A星算法'] for i, paths in enumerate([astar_paths, improved_astar_paths, new_astar_paths]): for j, path in enumerate(paths): x = [node[0] for node in path] y = [node[1] for node in path] plt.plot(x, y, color=colors[i], label=labels[i] if j == 0 else "") plt.legend() plt.show()

从这张图中可以明显看出,A星算法找到的路径相对比较“规矩”,按照传统的评估方式进行搜索。改进A星算法由于调整了启发函数,路径可能更加“直接”一些,避开障碍物的同时更高效地接近目标。而新A星算法因为结合了预学习等新技术,它的路径有可能在某些情况下展现出独特的优势,比如能够更好地利用地图的隐藏特征来规划路径。

在避障场景下,三种算法各有千秋。A星算法作为经典算法,稳定性强;改进A星算法通过优化启发函数提升了效率;新A星算法借助新技术带来了更多可能性。在实际应用中,我们可以根据具体的场景和需求来选择最合适的路径规划算法。

http://www.cnnetsun.cn/news/1554276.html

相关文章:

  • MrDoc最佳实践案例分享:成功企业的文档管理经验
  • ComfyUI-KJNodes:重构AI创作工作流的效率革命
  • Clawdbot代码管理:GitHub协作开发流程
  • CPython 3.15原生AOT启动耗时下降73%?深度还原2026面试高频质疑点:JIT禁用后如何保障热路径性能?
  • 每天3分钟搞定淘宝日常:淘金币自动化脚本的智能解决方案
  • 次元画室安装避坑指南:解决Anaconda环境冲突与依赖问题
  • RTX4090D优化版Qwen3-32B+OpenClaw:长文本处理自动化实战
  • 思源宋体终极指南:7款免费商用字体完整使用宝典
  • CentOS7虚拟机网络配置全攻略:从ifconfig不显示ens33到FinalShell成功连接
  • 通义千问1.5-1.8B-Chat-GPTQ-Int4 WebUI轻量化优势:对比传统方案在边缘计算场景下的潜力
  • MAA_Punish:战双帕弥什的智能解放方案
  • GLM-Image创新应用:基于算法的艺术风格探索
  • 避坑指南:STM32F411 USB声卡开发中的时钟同步与中文显示难题
  • 从代码生成到多平台部署:手把手用C#预处理器指令搭建你的项目脚手架
  • 基于二阶自抗扰控制器的双惯量伺服系统机械谐振抑制Matlab/Simulink仿真模型
  • Win11Debloat:3步轻松告别Windows 11臃肿,让电脑重获新生
  • BiliTools哔哩哔哩工具箱:5分钟搞定B站资源高效下载的完整解决方案
  • SQL调优实战手册:索引、并行、参数调优一站式解决方案
  • 毕设程序java基于Java的商铺租赁管理系统 基于Spring Boot的商业门面出租管理平台设计与实现 Java Web驱动的临街旺铺招租信息化系统开发
  • 智简魔方业务系统安装全攻略:从伪静态配置到ionCube扩展安装的保姆级教程
  • LFM2.5-1.2B-Thinking-GGUF快速上手:使用Ollama本地化部署与管理
  • 3个关键技巧优化华硕笔记本性能:GHelper完全指南
  • C++的std--ranges路径优化
  • Creo报错代码-9?手把手教你重新生成许可证文件(基于Creo 10.0)
  • 极简纯净音乐体验:铜钟音乐平台的高效使用指南
  • OpenRocket火箭设计与仿真全攻略:从理论到实践的开源解决方案
  • 终极指南:如何用开源固件拯救你的戴森吸尘器电池免于“死亡“
  • MT5 Zero-Shot中文文本增强效果展示:法律合同关键条款同义替换合规性验证
  • 探索开源词典引擎:ECDICT英汉词典数据库的开发者工具实践指南
  • 实战对比:ext4 vs NTFS vs XFS vs Btrfs vs ZFS - 哪个文件系统最适合你的SSD?