【算法 python A*算法的实现】

server/2024/11/28 18:51:11/

 - 算法实现:

python">import heapqclass Node:def __init__(self, position, g=0, h=0):self.position = position  # 节点的位置self.g = g  # 从起点到当前节点的成本self.h = h  # 从当前节点到终点的启发式估计成本self.f = g + h  # 总成本self.parent = None  # 父节点def __lt__(self, other):return self.f < other.fdef heuristic(a, b):# 使用曼哈顿距离作为启发式函数return abs(a[0] - b[0]) + abs(a[1] - b[1])def a_star(start, goal, grid):open_list = []closed_list = set()start_node = Node(start, 0, heuristic(start, goal))goal_node = Node(goal)heapq.heappush(open_list, start_node)while open_list:current_node = heapq.heappop(open_list)if current_node.position == goal_node.position:path = []while current_node:path.append(current_node.position)current_node = current_node.parentreturn path[::-1]  # 返回反转后的路径closed_list.add(current_node.position)# 获取相邻节点neighbors = [(0, 1), (1, 0), (0, -1), (-1, 0)]  # 右,下,左,上for new_position in neighbors:node_position = (current_node.position[0] + new_position[0],current_node.position[1] + new_position[1],)# 检查节点是否在网格内if (0 <= node_position[0] < len(grid)) and (0 <= node_position[1] < len(grid[0])):# 检查节点是否是障碍物if grid[node_position[0]][node_position[1]] == 1:continue# 创建新的节点neighbor_node = Node(node_position)if neighbor_node.position in closed_list:continue# 计算 g, h, f 值neighbor_node.g = current_node.g + 1neighbor_node.h = heuristic(neighbor_node.position, goal_node.position)neighbor_node.f = neighbor_node.g + neighbor_node.hneighbor_node.parent = current_node# 检查节点是否在开放列表中if add_to_open(open_list, neighbor_node):heapq.heappush(open_list, neighbor_node)return None  # 如果没有找到路径def add_to_open(open_list, neighbor):for node in open_list:if neighbor.position == node.position and neighbor.g >= node.g:return Falsereturn True

 - 测试:

python"># 示例使用import numpy as np
grid = np.array([[0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],[0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0],[0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1],[0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],[1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1],[0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1],[0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0],[0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1],[0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],[0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1],[0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1],[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],[0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0],]
)start = (0, 0)  # 起点
goal = (15, 15)  # 终点path = a_star(start, goal, grid)
print("找到的路径:", path)

找到的路径: [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (4, 1), (4, 2), (3, 2), (2, 2), (1, 2), (0, 2), (0, 3), (0, 4), (1, 4), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (1, 8), (0, 8), (0, 9), (0, 10), (0, 11), (0, 12), (0, 13), (0, 14), (1, 14), (2, 14), (3, 14), (4, 14), (4, 13), (4, 12), (4, 11), (4, 10), (4, 9), (4, 8), (4, 7), (4, 6), (5, 6), (6, 6), (6, 5), (6, 4), (7, 4), (8, 4), (8, 3), (8, 2), (9, 2), (10, 2), (11, 2), (12, 2), (12, 3), (12, 4), (12, 5), (12, 6), (11, 6), (10, 6), (9, 6), (8, 6), (8, 7), (8, 8), (7, 8), (6, 8), (6, 9), (6, 10), (6, 11), (6, 12), (7, 12), (8, 12), (8, 11), (8, 10), (9, 10), (10, 10), (10, 9), (10, 8), (11, 8), (12, 8), (12, 9), (12, 10), (13, 10), (14, 10), (14, 11), (14, 12), (13, 12), (12, 12), (11, 12), (10, 12), (10, 13), (10, 14), (11, 14), (12, 14), (13, 14), (13, 15), (14, 15), (15, 15)]

 - 可视化

python">def visualize_path(maze, path):fig, axis = plt.subplots(1, 2)mask = maze == 1maze[mask] = 0maze[~mask] = 1axis[0].imshow(maze, cmap="gray")axis[0].set_xticks([])axis[0].set_yticks([])axis[0].set_title("map")for position in path:maze[position[0]][position[1]] = 2axis[1].imshow(maze, cmap="gray")axis[1].set_xticks([])axis[1].set_yticks([])axis[1].set_title("shortest path")plt.show()visualize_path(grid.copy(), path)

 


http://www.ppmy.cn/server/145695.html

相关文章

开发需求总结19-vue 根据后端返回一年的数据,过滤出符合条件数据

需求描述&#xff1a; 定义时间分界点&#xff1a;每月26号8点&#xff0c;过了26号8点则过滤出data数组中符合条件数据下个月的数据&#xff0c;否则过滤出当月数据 1.假如现在是2024年11月14日&#xff0c;那么过滤出data数组中日期都是2024-11月的数据&#xff1b; 2.假如…

Python爬虫爬取数据报错

报错&#xff1a; Error fetching the URL: (Connection aborted., ConnectionResetError(10054, 远程主机强迫关闭了一个现有的连接。, None, 10054, None)) 报错原因&#xff1a; 目标服务器限制&#xff1a; 目标网站可能已经检测到你的请求来自自动化工具&#xff08;如爬虫…

Rust语言俄罗斯方块(漂亮的界面案例+详细的代码解说+完美运行)

tetris-demo A Tetris example written in Rust using Piston in under 500 lines of code 项目地址: https://gitcode.com/gh_mirrors/te/tetris-demo 项目介绍 "Tetris Example in Rust, v2" 是一个用Rust语言编写的俄罗斯方块游戏示例。这个项目不仅是一个简单…

go-zero(十一) 日志

go zero 日志 日志可以帮助我们记录应用程序的运行时信息、错误和调试信息&#xff0c;是个非常实用的工具。 一、基本介绍 1.logc和logx go zero的日志主要由两个组件组成logx和logc. logx 是go zero提供的核心日志库&#xff0c;它负责实际的日志记录工作。该组件支持多…

局域网的网络安全

网络安全 局域网基本上都采用以广播为技术基础的以太网&#xff0c;任何两个节点之间的通信数据包&#xff0c;不仅为这两个节点的网卡所接收&#xff0c;也同时为处在同一以太网上的任何一个节点的网卡所截取。因此&#xff0c;黑客只要接入以太网上的任一节点进行侦听&#…

代码随想录算法训练营第十一天(LeetCode150.逆波兰表达式求值;LeetCode239.滑动窗口最大值;LeetCode347.前K个高频元素)

LeetCode 150. 逆波兰表达式求值 题目链接&#xff1a;逆波兰表达式求值题目链接 思路 主要是要理解逆波兰表达式的定义&#xff0c;在理解了逆波兰表达式的定义后&#xff0c;使用栈就可以直接做了。 逆波兰表达式是一种后缀表达式&#xff0c;所谓后缀就是指运算符写在后面…

2024年第15届蓝桥杯C/C++组蓝桥杯JAVA实现

目录 第一题握手&#xff0c;这个直接从49累加到7即可&#xff0c;没啥难度&#xff0c;后面7个不握手就好了&#xff0c;没啥讲的&#xff0c;(然后第二个题填空好难&#xff0c;嘻嘻不会&#xff09; 第三题.好数​编辑 第四题0R格式 宝石组合 数字接龙 最后一题:拔河 第…

java实现将图片插入word文档

插入图片所用依赖 private static void insertImage(XWPFDocument document, String path) {List<XWPFParagraph> paragraphs document.getParagraphs();for (XWPFParagraph paragraph : paragraphs) {CTP ctp paragraph.getCTP();for (int dwI 0; dwI < ctp.sizeO…