【路径规划】二维Dijkstra启发式改进算法

我们使用了A*算法的启发式(曼哈顿距离)来改进Dijkstra算法的性能。当我们将邻居节点添加到优先队列时,我们使用了distance + heuristic作为优先级,这样我们可以更快地找到通往目标节点的路径。

import heapq
import numpy as np


def heuristic(a, b):
    (x1, y1) = a
    (x2, y2) = b
    return abs(x1 - x2) + abs(y1 - y2)  # 使用曼哈顿距离作为启发式


def dijkstra_with_a_star_heuristic(graph, start, end):
    # 初始化距离字典,将所有节点设置为无穷大,除了起点
    distances = {position: float('infinity') for position in np.ndindex(graph.shape)}
    distances[start] = 0

    # 优先队列,存储待检查的节点和它们的距离
    pq = [(0, start)]

    while pq:
        # 弹出当前最小距离的节点
        current_distance, current_position = heapq.heappop(pq)

        # 如果已经找到更短的路径到当前节点,则跳过
        if current_distance > distances[current_position]:
            continue

        # 如果到达目标节点,返回路径(这里未实现路径重构)
        if current_position == end:
            return distances[current_position]  # 这里只返回距离,路径重构需要额外工作

        # 遍历当前节点的邻居
        for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
            x2, y2 = current_position[0] + dx, current_position[1] + dy

            # 检查邻居是否在网格内且不是障碍物
            if 0 <= x2 < graph.shape[0] and 0 <= y2 < graph.shape[1] and graph[x2, y2] != 1:
                # 计算到达邻居节点的距离
                distance = current_distance + 1

                # 使用启发式来改进Dijkstra的选择
                priority = distance + heuristic((x2, y2), end)

                # 如果找到更短的路径到邻居节点,则更新距离并添加到优先队列中
                if priority < distances[(x2, y2)]:
                    distances[(x2, y2)] = priority
                    heapq.heappush(pq, (priority, (x2, y2)))

    # 如果没有找到路径到目标节点
    return None
if __name__ == '__main__':


    # 示例用法
    graph = np.array([
        [0, 0, 0, 1, 0],
        [1, 1, 0, 1, 0],
        [0, 0, 0, 0, 0],
        [0, 1, 1, 1, 1],
        [0, 0, 0, 0, 0]
    ])
    start = (0, 0)
    end = (4, 4)

    distance = dijkstra_with_a_star_heuristic(graph, start, end)
    print(f"The shortest distance from {start} to {end} is: {distance}")

相关推荐

  1. 路径规划Dijkstra启发改进算法

    2024-06-07 13:14:03       32 阅读
  2. 路径规划):Dijkstra算法

    2024-06-07 13:14:03       61 阅读
  3. 算法 & 动态规划 &路径问题】dp问题

    2024-06-07 13:14:03       39 阅读
  4. 路径规划深度矩阵寻路算法

    2024-06-07 13:14:03       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 13:14:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 13:14:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 13:14:03       87 阅读
  4. Python语言-面向对象

    2024-06-07 13:14:03       96 阅读

热门阅读

  1. LeetCode|938. Range Sum of BST

    2024-06-07 13:14:03       29 阅读
  2. 什么情况下需要进行网络安全等级保护?

    2024-06-07 13:14:03       32 阅读
  3. 如何将 Vue 应用程序部署到 Cloudflare Pages

    2024-06-07 13:14:03       27 阅读
  4. MySQL8.0默认TCP端口介绍

    2024-06-07 13:14:03       29 阅读
  5. Inner-IoU

    Inner-IoU

    2024-06-07 13:14:03      66 阅读
  6. PLM 解决方案如何提高您的业务效率?

    2024-06-07 13:14:03       30 阅读
  7. ffmplay 源码解读

    2024-06-07 13:14:03       20 阅读
  8. MySQL清空所有表的数据的方法

    2024-06-07 13:14:03       26 阅读
  9. Python里cv2是什么包?怎么安装使用?

    2024-06-07 13:14:03       26 阅读
  10. VBA实战(Excel)(4):实用功能整理

    2024-06-07 13:14:03       28 阅读