人工智能D* Lite 算法-动态障碍物处理、多步预测和启发式函数优化

ops/2025/2/21 4:21:17/

在智能驾驶领域,D* Lite 算法是一种高效的动态路径规划算法,适用于处理环境变化时的路径重规划问题。以下将为你展示 D* Lite 算法的高级用法,包含动态障碍物处理、多步预测和启发式函数优化等方面的代码实现。

代码实现

import heapq
import math# 地图类,用于管理地图信息和更新
class Map:def __init__(self, grid):self.grid = gridself.rows = len(grid)self.cols = len(grid[0])def get_neighbors(self, node):x, y = nodeneighbors = []for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]:new_x, new_y = x + dx, y + dyif 0 <= new_x < self.rows and 0 <= new_y < self.cols and self.grid[new_x][new_y] == 0:neighbors.append((new_x, new_y))return neighborsdef update_cell(self, x, y, new_value):self.grid[x][y] = new_valuedef is_obstacle(self, node):x, y = nodereturn self.grid[x][y] == 1# 节点类,用于存储节点信息
class Node:def __init__(self, x, y):self.x = xself.y = yself.g = float('inf')self.rhs = float('inf')self.key = [float('inf'), float('inf')]def __lt__(self, other):return self.key < other.key# 优化的启发式函数:考虑对角线移动的欧几里得距离
def heuristic(a, b):dx = abs(a[0] - b[0])dy = abs(a[1] - b[1])return math.sqrt(dx**2 + dy**2)# 计算节点的键值
def calculate_key(node, s_start, k_m):node.key = [min(node.g, node.rhs) + heuristic((node.x, node.y), s_start) + k_m,min(node.g, node.rhs)]return node# 初始化 D* Lite 算法
def initialize(s_start, s_goal):U = []nodes = {}for i in range(map_obj.rows):for j in range(map_obj.cols):node = Node(i, j)nodes[(i, j)] = nodes_goal_node = nodes[s_goal]s_goal_node.rhs = 0s_goal_node = calculate_key(s_goal_node, s_start, 0)heapq.heappush(U, s_goal_node)return U, nodes# 更新节点的 rhs 值
def update_vertex(U, node, s_start, k_m):if node.g != node.rhs:node = calculate_key(node, s_start, k_m)for i, n in enumerate(U):if n.x == node.x and n.y == node.y:U[i] = nodeheapq.heapify(U)breakelse:heapq.heappush(U, node)else:for i, n in enumerate(U):if n.x == node.x and n.y == node.y:U.pop(i)heapq.heapify(U)break# 计算最短路径
def compute_shortest_path(U, s_start, nodes, k_m):while U and (U[0].key < calculate_key(nodes[s_start], s_start, k_m) ornodes[s_start].rhs > nodes[s_start].g):u = heapq.heappop(U)if u.g > u.rhs:u.g = u.rhselse:u.g = float('inf')update_vertex(U, u, s_start, k_m)for neighbor in map_obj.get_neighbors((u.x, u.y)):neighbor_node = nodes[neighbor]if neighbor != s_start:cost = 1if abs(u.x - neighbor[0]) + abs(u.y - neighbor[1]) == 2:cost = math.sqrt(2)  # 对角线移动代价neighbor_node.rhs = min(neighbor_node.rhs,u.g + cost)update_vertex(U, neighbor_node, s_start, k_m)# 动态障碍物处理和多步预测
def handle_dynamic_obstacles(U, nodes, s_start, s_goal, k_m, dynamic_obstacles):for obstacle in dynamic_obstacles:obstacle_node = nodes[obstacle]map_obj.update_cell(obstacle[0], obstacle[1], 1)for neighbor in map_obj.get_neighbors(obstacle):neighbor_node = nodes[neighbor]neighbor_node.rhs = float('inf')update_vertex(U, neighbor_node, s_start, k_m)compute_shortest_path(U, s_start, nodes, k_m)# 路径规划函数
def d_star_lite(s_start, s_goal, dynamic_obstacles=[]):U, nodes = initialize(s_start, s_goal)k_m = 0compute_shortest_path(U, s_start, nodes, k_m)path = []current = s_startwhile current != s_goal:path.append(current)neighbors = map_obj.get_neighbors(current)min_rhs = float('inf')next_node = Nonefor neighbor in neighbors:neighbor_node = nodes[neighbor]if neighbor_node.rhs < min_rhs:min_rhs = neighbor_node.rhsnext_node = neighborif next_node is None:print("未找到可行路径!")return []current = next_nodepath.append(s_goal)# 处理动态障碍物if dynamic_obstacles:handle_dynamic_obstacles(U, nodes, s_start, s_goal, k_m, dynamic_obstacles)path = []current = s_startwhile current != s_goal:path.append(current)neighbors = map_obj.get_neighbors(current)min_rhs = float('inf')next_node = Nonefor neighbor in neighbors:neighbor_node = nodes[neighbor]if neighbor_node.rhs < min_rhs:min_rhs = neighbor_node.rhsnext_node = neighborif next_node is None:print("未找到可行路径!")return []current = next_nodepath.append(s_goal)return path# 示例地图
map_grid = [[0, 0, 0, 0, 0],[0, 1, 1, 0, 0],[0, 0, 0, 0, 0],[0, 0, 1, 1, 0],[0, 0, 0, 0, 0]
]
map_obj = Map(map_grid)# 起点和终点
s_start = (0, 0)
s_goal = (4, 4)# 初始路径规划
path = d_star_lite(s_start, s_goal)
if path:print("初始规划的路径:", path)# 模拟动态障碍物出现
dynamic_obstacles = [(2, 2)]
path = d_star_lite(s_start, s_goal, dynamic_obstacles)
if path:print("出现动态障碍物后重新规划的路径:", path)

代码解释

1. 地图类(Map
  • get_neighbors:不仅考虑上下左右移动,还考虑了对角线移动,扩大了节点的搜索范围。
  • is_obstacle:判断节点是否为障碍物。
2. 节点类(Node
  • 存储节点的坐标、g 值(从起点到该节点的实际代价)、rhs 值(到该节点的最短路径的估计代价)和键值 key
3. 启发式函数(heuristic
  • 采用考虑对角线移动的欧几里得距离作为启发式函数,更准确地估计节点到目标节点的代价。
4. D* Lite 算法核心函数
  • initialize:初始化算法,创建节点字典和优先队列 U,将目标节点加入队列。
  • calculate_key:计算节点的键值,用于优先队列的排序。
  • update_vertex:更新节点的 rhs 值,并根据情况更新优先队列。
  • compute_shortest_path:计算最短路径,不断更新节点的 grhs 值,直到找到最短路径或队列为空。
5. 动态障碍物处理和多步预测
  • handle_dynamic_obstacles:处理动态障碍物的出现,更新受影响节点的 rhs 值,并重新计算最短路径。
6. 路径规划函数(d_star_lite
  • 主路径规划函数,先进行初始路径规划,若存在动态障碍物,则调用 handle_dynamic_obstacles 重新规划路径。

注意事项

  • 代码中的动态障碍物处理是简单模拟,实际应用中需要结合传感器数据实时更新障碍物信息。
  • 启发式函数和移动代价的计算可以根据具体场景进行调整,以提高路径规划的效率和准确性。
  • 代码中未考虑车辆的运动学约束,实际智能驾驶中需要进一步考虑车辆的转弯半径、速度限制等因素。

http://www.ppmy.cn/ops/157021.html

相关文章

深度学习-102-RAG技术之基于langchain模块构建的检索增强生成RAG系统

文章目录 1 RAG1.1 RAG的工作流程1.2 RAG的组成2 langchain中的模块2.1 Load文档加载器2.2 Transform文档转换器2.2.1 CharacterTextSplitter2.2.2 RecursiveCharacterTextSplitter2.3 Embed文本嵌入模型2.3.1 自定义SemanticEmbedding2.3.2 OllamaEmbeddings2.4 Store向量存储…

开源流程引擎对比:compileflow、Turbo、Warm-Flow、 flowable、activiti

文章目录 开源流程引擎对比I 工作流引擎阿里的Compileflowflowableactivitiwarm-flow(国产)Turbo (didiopensource)II 知识扩展开发流程开源流程引擎对比 ActivitiCamundaCompileflowturbo核心表量282205特性 中断可重入√√√支持回滚√√运行模式独立运行和内嵌独立运行和…

快速在wsl上部署学习使用c++轻量化服务器-学习笔记

知乎上推荐的Tinywebserver这个服务器&#xff0c;快速部署搭建&#xff0c;学习c服务器开发 仓库地址 githubhttps://link.zhihu.com/?targethttps%3A//github.com/qinguoyi/TinyWebServerhttps://link.zhihu.com/?targethttps%3A//github.com/qinguoyi/TinyWebServer 在…

macOs安装docker且在docker上部署nginx+php

一 环境 系统&#xff1a;macOS Sonoma 14.6芯片&#xff1a;Apple M3docker 版本&#xff1a;27.2.0 二 软件安装 2.1 docker下载&#xff1a; Get Started | Docker进入官网&#xff0c;如图位置&#xff0c;点击mac版本的docker下载. 根据你电脑芯片类型来选择下载的版本…

CI/CD相关概念

目录 CI/CD 蓝绿部署 回滚机制 金丝雀发布 CI/CD CI/CD&#xff08;持续集成和持续交付/部署&#xff09;是现代软件开发和运维中的重要实践&#xff0c;旨在通过自动化构建、测试、部署等流程&#xff0c;提升软件交付的速度、质量和一致性。下面详细介绍 CI/CD 的概念、…

sqli-labs靶场实录(二): Advanced Injections

sqli-labs靶场实录: Advanced Injections Less21Less22Less23探测注入点 Less24Less25联合注入使用符号替代 Less25aLess26逻辑符号绕过and/or过滤双写and/or绕过 Less26aLess27Less27aLess28Less28aLess29Less30Less31Less32&#xff08;宽字节注入&#xff09;Less33Less34Le…

使用TensorFlow和Keras构建卷积神经网络:图像分类实战指南

使用TensorFlow和Keras构建卷积神经网络&#xff1a;图像分类实战指南 一、前言&#xff1a;为什么选择CNN进行图像分类&#xff1f; 在人工智能领域&#xff0c;图像分类是计算机视觉的基础任务。传统的机器学习方法需要人工设计特征提取器&#xff0c;而深度学习通过卷积神经…

基于SeaTunnel同步数据

SeaTunnel&#xff08;原名Waterdrop&#xff09;是一个高性能、分布式、易扩展的数据集成平台&#xff0c;旨在简化大规模数据的抽取、转换和加载&#xff08;ETL&#xff09;过程。它支持从多种数据源&#xff08;如数据库、消息队列、文件系统等&#xff09;中提取数据&…