路径规划算法(2) - A*寻路算法 python实现及解析

2023-05-16

代码

#coding=utf-8
import math

# 启发距离, 当前点和目标点的启发距离; -- 就是简单的曼哈顿距离
def heuristic_distace(Neighbour_node,target_node):
    H = abs(Neighbour_node[0] - target_node[0]) + abs(Neighbour_node[1] - target_node[1])
    return H

def go_around(direction):
    box_length = 1
    diagonal_line = box_length * 1
    if (direction==0 or direction==2 or direction==6 or direction==8):
        return diagonal_line
    elif (direction==1 or direction==3 or direction==4 or direction==5 or direction==7):
        return diagonal_line

def find_coordinate(map,symble):
    #store coordinate
    result=[]
    for index1,value1 in enumerate(map):
        if symble in value1:
            row = index1
            for index2, value2 in enumerate(map[index1]):
                if symble==value2:
                   column = index2
                   result.append([row, column])
    return result

def show_map(map):
    for idx in map:
        print idx

map =[[".", ".", ".", "#", ".", "#", ".", ".", ".", "."],
      [".", ".", "#", ".", ".", "#", ".", "#", ".", "#"],
      ["s", ".", "#", ".", "#", ".", "#", ".", ".", "."],
      [".", "#", "#", ".", ".", ".", ".", ".", "#", "."],
      [".", ".", ".", ".", "#", "#", ".", ".", "#", "."],
      [".", "#", ".", ".", ".", ".", "#", ".", ".", "."],
      [".", "#", ".", ".", ".", "#", "#", ".", "#", "."],
      [".", ".", ".", ".", ".", ".", ".", ".", "#", "."],
      [".", "#", "#", ".", ".", ".", "#", ".", ".", "."],
      [".", ".", ".", "#", "#", "#", ".", ".", "#", "f"],
      ["#", "#", ".", ".", "#", "#", "#", ".", "#", "."],
      [".", "#", "#", ".", ".", ".", "#", ".", ".", "."],
      [".", ".", ".", ".", "#", "#", ".", ".", "#", "."]]

#these datas are store in the form of list in a singal list
# 记录所有的障碍物点 坐标
obstacle = find_coordinate(map,"#")
start_node = find_coordinate(map,"s")[0]
target_node = find_coordinate(map,"f")[0]
current_node = start_node

# 设置路径起点为当前节点
path_vertices = [start_node]

#visited_vertices should be stored in the form of a singal list
Neighbour_vertices = []


# 进入查找
while current_node != target_node:

    x_coordinate = current_node[0]
    y_coordinate = current_node[1]
    F = []  # 节点权值 F = g + h
    Neighbour_vertices =   [[x_coordinate - 1, y_coordinate - 1],
                            [x_coordinate - 1, y_coordinate    ],
                            [x_coordinate - 1, y_coordinate + 1],
                            [x_coordinate,     y_coordinate - 1],
                            [x_coordinate    , y_coordinate    ],
                            [x_coordinate,     y_coordinate + 1],
                            [x_coordinate + 1, y_coordinate - 1],
                            [x_coordinate + 1, y_coordinate    ],
                            [x_coordinate + 1, y_coordinate + 1]]
    # 遍历相邻坐标
    for index, value in enumerate(Neighbour_vertices):
        if value[0] in range(len(map)):
            if value[1] in range(len(map)):
               if value not in obstacle+path_vertices:
                    # 如果满足节点 1, 在地图边界内 2, 不在障碍物点和已经经过的点, 计算权重
                    F.append(heuristic_distace(value, target_node) + go_around(index))
                    map[value[0]][value[1]] = str(F[-1])
               else:
                    F.append(10000)
            else:
                    F.append(10000)
        else:
                    F.append(10000)
               #a very large number

    # print(F) # 打印出遍历的 节点的权重
    #将当前点设置为 权重最小的点
    current_node=Neighbour_vertices[F.index(min(total_distance for total_distance in F))]
    map[current_node[0]][current_node[1]] = str(min(F))
    show_map(map)
    print(len(path_vertices))
    path_vertices.append(current_node)
      # if current_node not in visited_vertices:
      #     visited_vertices.append(current_node)
      # else:
      #     print("there is no route between")
      #     break

print(path_vertices)

运行完成

  • 数字表示 A* 算法算出来的启发距离, 代码采用的是简单的曼哈顿距离实现的
    在这里插入图片描述
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

路径规划算法(2) - A*寻路算法 python实现及解析 的相关文章

  • border-image属性分析

    border image是CSS3的一个属性 xff0c 由于比较复杂 xff0c 总是处于一知半解的状态 xff0c 今天下定决心 xff0c 花时间整理了一下 xff0c 供大家共勉和学习 border image的用处 没用borde
  • 纯CSS绘制箭头

    这几天研究别人的网站 xff0c 发现别人的箭头居然是用纯CSS写的 瞬间觉得高大上 细细研究其中的原理 xff0c 发现其实也很简单 CSS绘制三角形 绘制箭头 xff0c 首先应该懂得如何用纯CSS绘制三角形 我们先做一个宽高为0 xf
  • 如何更新npm至最新版本

    去年曾遇到过执行npm某项命令时提示要求更高版本的npm xff0c 当时百度解决了 xff0c 今天又再次遇到该问题 现记下来 xff0c 以供以后参考 命令行运行 xff1a span class hljs built in npm s
  • .*? 和 .*的区别

    在看js代码的时候 xff0c 有时候会遇到 这样的写法 xff0c 那么连续两个限制符是什么意思呢 xff1f 我们先来回顾一下正则表达式的基本知识 基本语法 符号用法 匹配除 r n 之外的任何单个字符 要匹配包括 r n 在内的任何字
  • 微信清除缓存的两种方法

    网址清除 微信浏览器打开网址 http debugx5 qq com xff0c 勾选如下设置 xff1a 点击 清除 按钮即可 代码清除 如果是自己写的网页 xff0c 在调试阶段希望不要缓存 xff0c 可以在html页面的head中加
  • PHP时间戳和日期转换

    时间戳转日期 date date format timestamp format 必需 规定时间戳的格式 timestamp 可选 规定时间戳 默认是当前时间和日期 例如 xff1a date Y m d H i s 具体时间戳 Y xff
  • 微信H5支付:网络环境未能通过安全验证,请稍后再试

    最近在开发微信H5支付 xff0c 并且已应用到某个网站进行支付 但奇怪的是 xff0c 一模一样的支付代码 xff0c 换了一个网站 xff0c 竟不能支付了 出现如图错误提示 xff1a 百度一圈 xff0c 均是说IP传得不对 微信s
  • 如何比较字符串大小

    今天我分享的是如何比较字符串的大小 xff0c 希望大家看完能有深刻的理解 字符比较 xff08 character comparison xff09 是指按照字典次序对单个字符或字符串进行比较大小的操作 xff0c 一般都是以ASCII码
  • node异步用await和不用await的区别

    最近在用node写项目 新版node异步用的是async await这两个关键字 我们都知道 xff0c 一般这两个关键字要成对出现 但是 xff0c 笔者发现 xff0c 如果不需要等待返回值的话 xff0c await可以不加 那么aw
  • NtripShare Cloud差分数据共享云平台

    RTK差分数据共享猫已更新至1 6 2版本 xff0c APP运行十分稳定 高级版本服务器已升级至华为云 xff0c 独享10M带宽 xff0c 进一步降低数据延迟 近期准备升级普通版服务器 xff0c 升级后将停止对1 6 0版本之前的不
  • RTK差分共享猫APP后台系统已开源

    RTK差分共享猫APP后台系统开源 xff0c 开源地址https gitee com forgy NtripShareCatServer 后台系统基于GUNS 6 0 xff08 https gitee com stylefeng gun
  • RTK差分数据网络播发软件

    解决短基线范围内多台设备同时作业 xff0c CORS差分账号资源不足的问题 基于串口或模拟网络RTK请求 xff0c 将实时差分数据进行转发 xff0c 支持Ntrip协议 xff0c 支持市面所有网络RTK系统 个人作品 xff0c 不
  • STM32使用cubemx的uart空闲中断接收不定长度数据

    void USART3 IRQHandler void USER CODE BEGIN USART3 IRQn 0 USER CODE END USART3 IRQn 0 HAL UART IRQHandler amp huart3 USE
  • 基于ROS平台的移动机器人-8-使用Kinect2导航

    基于ROS平台的移动机器人 8 使用Kinect2导航 ready 终于到写最后一篇了 不是经常写博文的老司机果然伤不起 xff01 在这一篇教程就是利用KinectV2来导航啦 go 1 安装一下所需的包 xff08 1 xff09 cd
  • kali linux学习——安装WingIDE(libqt4-webkit软件依靠问题)

    kali linux 中安装wingide xff08 libqt4 webkit软件依靠问题 xff09 走过的坑缺失的libqt4 webkit成功安装WingIDE 走过的坑 在kali linux上利用命令 dpkg i wingi
  • 华为的OD,值得去吗?

    最近有不少小伙伴接到了华为OD的面试邀约 xff0c 但搞不清楚OD到底怎么回事儿 xff0c 要不要去 所以今天来说说华为的OD到底是怎么回事儿 xff0c 怎么判断是否值得去 1 华为的OD是怎么回事儿 OD xff0c 是Outsou
  • 第01课:技术成长的三阶段模型

    引言 作为整个系统课程的第一部分 xff0c 我想先跟大家分享的是如何选择技术方向 xff0c 我将结合技术成长的三阶段模型 xff0c 讨论在入行 构建技能树 技术转型 团队技术方案选型等常见场景中如何选择适合自己的技术 努力只有在方向正
  • 开篇词 | 程序员的成长课

    大家好 xff0c 我是安晓辉 xff0c 做过开发工程师 研发经理 技术总监等岗位 xff0c 现在自由职业 xff0c 专注写作和开发者生涯咨询 出版过 程序员的成长课 Qt Quick 核心编程 你好哇 xff0c 程序员 解忧程序员
  • const与define的区别

    1 define是预编译指令 xff0c const是普通变量的定义 xff0c define定义的宏是在预处理阶段展开的 xff0c 而const定义的只读变量是在编译运行阶段使用的 2 const定义的是变量 xff0c 而define
  • 如何摆脱CRUD等打杂状态,从事更高价值工作

    每个月都会有十来个来询者向我抱怨工作低端 xff0c 程序员说自己每天CRUD xff0c 重复 枯燥 没技术含量 xff0c 销售助理说自己天天搜集客户信息 打印资料 帮老大带饭 xff0c 繁琐 无聊 不重要 xff0c 他们都说自己整

随机推荐

  • Windows下Qt 5.2 for Android开发入门

    Qt 5 2 发布了 xff0c 支持 Android 平台 xff0c 太好了 之前公司项目 xff0c 为了移植一个依赖 Qt 的程序到安卓平台上 xff0c 我自己交叉编译了 Qt Embedded 4 5 2 xff0c 费了老大劲
  • Qt Quick 之 QML 与 C++ 混合编程详解

    Qt Quick 技术的引入 xff0c 使得你能够快速构建 UI xff0c 具有动画 各种绚丽效果的 UI 都不在话下 但它不是万能的 xff0c 也有很多局限性 xff0c 原来 Qt 的一些技术 xff0c 比如低阶的网络编程如 Q
  • 漫谈程序员系列:一张图道尽程序员的出路

    推背图 相传由唐太宗时期的司天监李淳风和袁天罡合著 xff08 此两人其实是超级武学高手 xff0c 参见小椴的 开唐 xff09 xff0c 推算大唐以后中国两千多年的国运盛衰 xff0c 在中国七大预言书中居首 xff0c 是当之无愧的
  • 漫谈程序员系列:咦,你也在混日子啊

    戳你一下 xff0c 疼吗 xff1f 混日子的定义 来自百度百科的定义 xff1a 生活等方面过得不怎么好 xff0c 无目标 xff0c 混混沌沌 混日子 xff1a 即没有理想 xff0c 没有抱负 xff0c 糊里糊涂地生活 也指工
  • QtAndroid详解(1):QAndroidJniObject

    Qt 5 3之后 xff0c 新增了 QtAndroid 名字空间 xff0c 内有下列四个方法 xff1a QAndroidJniObject AndroidActivity int androidSdkVersion void star
  • freeSWITCH安装、配置与局域网测试

    这次来说说 freeSWITCH 的安装和配置 1 安装 freeSWITCH 下载页面 xff1a https freeswitch org confluence display FREESWITCH Installation 我们 Wi
  • 就 3 点,提升工作效率

    要想提高工作效率 xff0c 不论你看什么书 xff0c 看什么文章 xff0c 用什么工具 xff0c 只有下面这三点最重要 xff1a 动力剖析自己 xff0c 找到改善的切入点付诸行动并且坚持 目标驱动 有目标才能高效 我们爬山 xf
  • Python3 下 ROS 的使用 cv_bridge

    Python 3 下 ROSmsg 转 cv2 项目中用到的 Tensorflow2 4 的环境 xff0c 该环境只支持python3 版本 xff0c 项目中遇到不少需要和 ROS 交互的地方 xff0c 所以不断探索 python3
  • 深度图和RGB图对齐

    深度图 canny RGB canny Alignment xff1a code span class token function import span cv2 span class token function import span
  • 认识romfs文件系统

    1 1 什么是romfs romfs 是一个只读文件系统 xff0c 主要用在 mainly for initial RAM disks of installation disks 使用romfs 文件系统可以构造出一个最小的内核 xff0
  • Livox Lidar 特征提取方式总结

    传统的 旋转式雷达 xff0c 激光固定在雷达的旋转部件上 xff0c 依靠转子的转动而获得360的旋转视野 xff0c 由于旋转部件的存在 xff0c 为了获得更加精准的数据 xff0c 需要跟多的人工校准和更复杂的设计 xff0c 也因
  • C++ 菜鸟之路 (四) boost::thread 多线程全解析

    boost thread 的一般用法 boost thread的几个函数 锁 lock 函数多线程函数的限制 官方解释 xff1a http www cplusplus com reference thread thread joinabl
  • ROS 之 advertise 详解

    在学习过程中接触到如下的一段话 span class hljs comment ROS handles span ros span class hljs tag NodeHandle span node tf span class hljs
  • Linux 下 openMP 效率并未提升的解决方案

    OpenMP 正确观察计算时间OpenMP 经验总结 xff08 1 xff09 openmp 线程使用范围 xff08 2 xff09 openmp 多层嵌套的问题 OpenMP 正确观察计算时间 在使用 openmp的过程中 xff0c
  • C++ Yaml文件解析安装及使用 yaml-cpp

    C 43 43 Yaml文件解析安装及使用 安装 yaml cpp克隆官方库编译 yaml cpp 示例代码robot cpprobot yaml编译 robot cpp运行结果 难点分析与总结什么是 a 与 so 文件静态链接库 a 与动
  • 点云数据格式解析 sensor_msgs::PointCloud2

    在使用多线激光的时候需要总是会碰到点云数据 xff0c 这里简单的接受一下点云数据 xff0c 并堆数据结构进行分析 xff0c 方便自己后期对点云特征数据进行处理 文章目录 Rviz中的点云数据点云数据结构分析点云数据 python 解析
  • Arduino 读取GPS 数据发送解析并发布ROS topic(二)

    Arduino 读取GPS 数据发送解析并发布ROS topic 一 https blog csdn net Fourier Legend article details 84107494 概述 本部分将主要讲将串口接受到的数据 xff0c
  • LOAM进行点云地图创建

    3D激光点云数据处理入门 xff08 一 xff09 使用LOAM进行点云地图创建 LOAM 原理简述topic关系算法分析算法伪代码 LOAM 建图实践创建你的 ROS Workspace下载LOAM Package下载数据包运行 LOA
  • ROS - teb_local_planner 参数总结

    参考官方教程 http wiki ros org teb local planner Tutorials 全英文看着有点累 在此总结一下调试的过程和小小的经验 安装 teb local planner sudo apt get instal
  • 路径规划算法(2) - A*寻路算法 python实现及解析

    代码 span class token comment coding 61 utf 8 span span class token keyword import span math span class token comment 启发距离