您的位置:首页 > 教育 > 锐评 > 程序员一个月能挣多少钱_四川建设人力资源网官网_接外包项目的网站_谷歌收录提交入口

程序员一个月能挣多少钱_四川建设人力资源网官网_接外包项目的网站_谷歌收录提交入口

2025/9/11 11:15:52 来源:https://blog.csdn.net/testManger/article/details/145522738  浏览:    关键词:程序员一个月能挣多少钱_四川建设人力资源网官网_接外包项目的网站_谷歌收录提交入口
程序员一个月能挣多少钱_四川建设人力资源网官网_接外包项目的网站_谷歌收录提交入口

在自动驾驶的路径规划里,为了让规划出的路径在距离、时间和能耗之间达到最优平衡,我们可以在 A* 算法的代价函数中加入时间因素和能耗因素。以下是具体的代码实现:

思路分析

  1. 地图表示:使用二维数组表示地图,不同的值代表不同的路况,路况会影响行驶的速度和能耗。
  2. 代价函数:在计算节点的代价时,综合考虑距离、时间和能耗。距离通过曼哈顿距离等计算,时间根据路况对应的速度计算,能耗根据路况对应的能耗系数计算。
  3. A 算法*:在标准 A* 算法的基础上,使用新的代价函数进行节点扩展和路径搜索。

代码实现

import heapq
import numpy as np# 地图表示,不同值代表不同路况
# 0: 普通道路,速度快,能耗低
# 1: 拥堵道路,速度慢,能耗高
# 2: 爬坡路段,速度慢,能耗高
map_grid = np.array([[0, 0, 0, 0, 0],[0, 1, 1, 0, 0],[0, 0, 0, 2, 0],[0, 0, 1, 1, 0],[0, 0, 0, 0, 0]
])# 不同路况的速度和能耗系数
speed_factors = {0: 2,  # 普通道路速度1: 0.5,  # 拥堵道路速度2: 0.3  # 爬坡路段速度
}energy_factors = {0: 1,  # 普通道路能耗系数1: 3,  # 拥堵道路能耗系数2: 4  # 爬坡路段能耗系数
}# 节点类
class Node:def __init__(self, x, y, g=float('inf'), h=float('inf'), parent=None):self.x = xself.y = yself.g = gself.h = hself.f = g + hself.parent = parentdef __lt__(self, other):return self.f < other.f# 启发式函数(曼哈顿距离)
def heuristic(current, goal):return abs(current[0] - goal[0]) + abs(current[1] - goal[1])# 计算移动代价,考虑距离、时间和能耗
def get_move_cost(current, next_node):x1, y1 = currentx2, y2 = next_node# 距离代价distance_cost = 1# 获取路况terrain = map_grid[x2][y2]speed = speed_factors[terrain]energy = energy_factors[terrain]# 时间代价time_cost = 1 / speed# 能耗代价energy_cost = energy# 综合代价,这里简单加权求和,可根据实际情况调整权重total_cost = distance_cost + time_cost + energy_costreturn total_cost# A* 算法
def astar(grid, start, goal):rows, cols = grid.shapeopen_list = []closed_set = set()start_node = Node(start[0], start[1], g=0, h=heuristic(start, goal))heapq.heappush(open_list, start_node)while open_list:current_node = heapq.heappop(open_list)if (current_node.x, current_node.y) == goal:path = []while current_node:path.append((current_node.x, current_node.y))current_node = current_node.parentreturn path[::-1]closed_set.add((current_node.x, current_node.y))neighbors = [(0, 1), (0, -1), (1, 0), (-1, 0)]for dx, dy in neighbors:new_x, new_y = current_node.x + dx, current_node.y + dyif 0 <= new_x < rows and 0 <= new_y < cols and (new_x, new_y) not in closed_set:new_g = current_node.g + get_move_cost((current_node.x, current_node.y), (new_x, new_y))new_h = heuristic((new_x, new_y), goal)new_node = Node(new_x, new_y, g=new_g, h=new_h, parent=current_node)found = Falsefor i, node in enumerate(open_list):if node.x == new_x and node.y == new_y:if new_g < node.g:open_list[i] = new_nodeheapq.heapify(open_list)found = Truebreakif not found:heapq.heappush(open_list, new_node)return None# 主程序
start = (0, 0)
goal = (4, 4)
path = astar(map_grid, start, goal)
if path:print("规划的路径:", path)
else:print("未找到可行路径!")

代码解释

  1. 地图和路况map_grid 表示地图,不同的值代表不同的路况。speed_factorsenergy_factors 分别存储了不同路况下的速度和能耗系数。
  2. 节点类Node 类用于存储节点的信息,包括坐标、g 值(从起点到该节点的实际代价)、h 值(从该节点到目标节点的估计代价)、f 值(总代价)和父节点。
  3. 启发式函数heuristic 函数使用曼哈顿距离作为启发式函数,估计从当前节点到目标节点的距离。
  4. 移动代价计算get_move_cost 函数计算从一个节点移动到另一个节点的代价,综合考虑了距离、时间和能耗。时间代价根据路况对应的速度计算,能耗代价根据路况对应的能耗系数计算。
  5. A 算法*:astar 函数实现了 A* 算法的核心逻辑,使用新的代价函数进行节点扩展和路径搜索。

注意事项

  • 代码中的权重是简单的加权求和,实际应用中可以根据具体需求调整距离、时间和能耗的权重,以达到更好的平衡。
  • 速度和能耗系数是预先设定的,实际场景中可以根据实时数据进行动态调整。
  • 代码中只考虑了上下左右四个方向的移动,可根据需要扩展到斜向移动。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com