机器人路径规划:基于迪杰斯特拉算法(Dijkstra)的机器人路径规划(提供Python代码)

迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学家狄克斯特拉于1959年提出的,因此又叫狄克斯特拉算法。是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最短路径问题。迪杰斯特拉算法主要特点是从起始点开始,采用贪心算法策略,每次遍历到始点距离最近且未访问过的顶点的邻接节点,直到扩展到终点为止。它可以找到从一个起始节点到其他所有节点的最短路径。

一、算法介绍

Dijkstra算法采用贪心策略,通过逐步扩展已知最短路径集合来逐步确定最短路径。它使用一个距离数组来记录从起始节点到其他节点的当前最短距离,并通过不断更新距离数组来逐步确定最短路径。

二、算法描述

  1. 创建一个距离数组dist[],用于记录起始节点到其他节点的当前最短距离。初始化dist[],将起始节点的距离设为0,其他节点的距离设为无穷大。

  2. 创建一个集合visited[],用于记录已经确定最短路径的节点。

  3. 重复以下步骤,直到visited[]包含所有节点:

a. 从未访问的节点中选择一个距离最小的节点u,将其加入visited[]。

b. 对于节点u的所有邻居节点v,更新其距离数组dist[]:

  • 如果通过节点u可以获得更短的路径,则更新dist[v]为新的最短距离。
  1. 最终,dist[]数组中记录了起始节点到其他所有节点的最短距离。

三、算法流程

  1. 初始化dist[]数组和visited[]集合。

  2. 将起始节点的距离设为0。

  3. 重复以下步骤,直到visited[]包含所有节点:

a. 从未访问的节点中选择一个距离最小的节点u。

b. 将节点u加入visited[]。

c. 对于节点u的所有邻居节点v,更新其距离数组dist[]:

  • 如果通过节点u可以获得更短的路径,则更新dist[v]为新的最短距离。
  1. 返回dist[]数组作为最短路径结果。

四、部分代码

复制代码
import matplotlib.pyplot as plt
import math

show_animation = True


class Dijkstra:

    def __init__(self, ox, oy, resolution, robot_radius):
        """
        Initialize map for a star planning

        ox: x position list of Obstacles [m]
        oy: y position list of Obstacles [m]
        resolution: grid resolution [m]
        rr: robot radius[m]
        """

        self.min_x = None
        self.min_y = None
        self.max_x = None
        self.max_y = None
        self.x_width = None
        self.y_width = None
        self.obstacle_map = None

        self.resolution = resolution
        self.robot_radius = robot_radius
        self.calc_obstacle_map(ox, oy)
        self.motion = self.get_motion_model()

    class Node:
        def __init__(self, x, y, cost, parent_index):
            self.x = x  # index of grid
            self.y = y  # index of grid
            self.cost = cost
            self.parent_index = parent_index  # index of previous Node

        def __str__(self):
            return str(self.x) + "," + str(self.y) + "," + str(
                self.cost) + "," + str(self.parent_index)

五、部分结果

六、完整Python代码

见下方联系方式

相关推荐
如竟没有火炬2 分钟前
LRU缓存——双向链表+哈希表
数据结构·python·算法·leetcode·链表·缓存
Greedy Alg4 分钟前
LeetCode 236. 二叉树的最近公共祖先
算法
Serverless 社区8 分钟前
阿里云函数计算 AgentRun 全新发布,构筑智能体时代的基础设施
人工智能·阿里云·云原生·serverless·云计算
IT_陈寒21 分钟前
Python开发者必看!10个高效数据处理技巧让你的Pandas代码提速300%
前端·人工智能·后端
爱吃生蚝的于勒22 分钟前
【Linux】零基础学会Linux之权限
linux·运维·服务器·数据结构·git·算法·github
咖啡续命又一天28 分钟前
python 自动化采集 ChromeDriver 安装
开发语言·python·自动化
新智元1 小时前
全球 AI 视频大战升级!「中国版 Sora」Vidu Q2 参考生月底发布,能力对标 Sora 2
人工智能·openai
新智元1 小时前
刚刚,Figure 03 惊天登场!四年狂造 10 万台,人类保姆集体失业
人工智能·openai
兮山与1 小时前
算法3.0
算法
万猫学社1 小时前
我们为什么需要Agent?
人工智能