Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
57b70c5
修改部分代码,并成功运行
joe-justin369 Oct 21, 2025
a478d24
Move AD_DQN source files from src/ subdirectory to root
joe-justin369 Oct 21, 2025
ab55863
重命名Main.py为main.py并更新README
joe-justin369 Oct 27, 2025
15faa98
提交代码所需依赖库
joe-justin369 Oct 27, 2025
667379f
Merge branch 'OpenHUTB:main' into main
joe-justin369 Oct 27, 2025
ab12bb6
Merge branch 'OpenHUTB:main' into main
joe-justin369 Nov 17, 2025
c8a4b97
更新核心DQN算法文件:环境配置、超参数、模型、测试和主程序
joe-justin369 Nov 17, 2025
710db38
更新核心DQN算法文件:环境配置、超参数、模型、测试和主程序
joe-justin369 Nov 17, 2025
b0d0ae4
Merge branch 'OpenHUTB:main' into main
joe-justin369 Nov 17, 2025
198eb2d
修改注释,并全部替换为中文注释
joe-justin369 Nov 17, 2025
a85483b
Merge branch 'OpenHUTB:main' into main
joe-justin369 Dec 1, 2025
c048059
Signed-off-by: joe-justin369 <Buxiao1999@gmail.com>
joe-justin369 Dec 1, 2025
4e9bfa5
为模型增加转向功能,用于躲避障碍或行人,并优化模拟环境中的人员配置情况
joe-justin369 Dec 1, 2025
68b0ee2
Merge branch 'main' into main
joe-justin369 Dec 1, 2025
1d3e43a
Merge branch 'OpenHUTB:main' into main
joe-justin369 Dec 1, 2025
fcb6f3b
Merge branch 'OpenHUTB:main' into main
joe-justin369 Dec 15, 2025
fb36a8d
解决行人避障问题,同时保持车辆沿道路行驶
joe-justin369 Dec 15, 2025
661dd1f
Merge branch 'OpenHUTB:main' into main
joe-justin369 Dec 15, 2025
ac4480a
算法优化:新增Dueling DQN架构和优先经验回放
joe-justin369 Dec 15, 2025
c136006
增加自动查找路径模型功能
joe-justin369 Dec 16, 2025
e4221ba
Merge branch 'OpenHUTB:main' into main
joe-justin369 Dec 16, 2025
77de327
课程学习增强、模仿学习预训练和多目标优化
joe-justin369 Dec 16, 2025
9a3a122
Merge branch 'OpenHUTB:main' into main
joe-justin369 Dec 16, 2025
03d529a
将模型调用路径,全更改为相对路径
joe-justin369 Dec 16, 2025
3518c7d
Merge branch 'OpenHUTB:main' into main
joe-justin369 Dec 16, 2025
3c130a4
代码结构调整
joe-justin369 Dec 16, 2025
6555093
Merge branch 'OpenHUTB:main' into main
joe-justin369 Dec 16, 2025
ba25df3
Merge branch 'OpenHUTB:main' into main
joe-justin369 Dec 16, 2025
2887a46
代码优化改进
joe-justin369 Dec 16, 2025
e39a431
Merge branch 'OpenHUTB:main' into main
joe-justin369 Dec 17, 2025
f61d2bb
Merge branch 'OpenHUTB:main' into main
joe-justin369 Dec 17, 2025
9b53a4b
提高小车对障碍物和行人的反应速度
joe-justin369 Dec 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 150 additions & 53 deletions src/Unmanned_vehicle_AD_DQN/Environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy as np
import cv2
import math
from collections import deque

import carla
from carla import ColorConverter
Expand All @@ -17,8 +18,8 @@
except ImportError:
# 如果在导入Hyperparameters时出错,使用默认值
SHOW_PREVIEW = False
IM_WIDTH = 640
IM_HEIGHT = 480
IM_WIDTH = 160 # 修改默认值
IM_HEIGHT = 120 # 修改默认值


class CarEnv:
Expand All @@ -40,6 +41,13 @@ def __init__(self):
self.episode_start_time = None # 每轮开始时间
self.last_ped_distance = float('inf') # 上次最近行人距离
self.current_episode = 1 # 当前episode编号
self.obstacle_detected_time = None # 障碍物检测到的时间
self.reaction_start_time = None # 反应开始时间
self.proactive_action_count = 0 # 主动避障动作计数

# 新增:多帧缓存用于运动检测
self.frame_buffer = deque(maxlen=3)
self.motion_detected = False

# 加载世界和蓝图
try:
Expand Down Expand Up @@ -255,6 +263,11 @@ def reset(self, episode=1):
self.last_action = 1
self.episode_start_time = time.time()
self.last_ped_distance = float('inf')
self.obstacle_detected_time = None
self.reaction_start_time = None
self.proactive_action_count = 0
self.frame_buffer.clear()
self.motion_detected = False

# 设置车辆生成点
spawn_point = carla.Transform()
Expand Down Expand Up @@ -366,6 +379,31 @@ def process_img(self, image):
processed_image = np.array(image.raw_data)
processed_image = processed_image.reshape((self.im_height, self.im_width, 4))
processed_image = processed_image[:, :, :3] # 移除alpha通道

# 添加简单的图像增强
# 增加对比度以突出障碍物
processed_image = cv2.convertScaleAbs(processed_image, alpha=1.2, beta=10)

# 存储到帧缓冲区用于运动检测
if len(self.frame_buffer) == 0:
self.frame_buffer.append(processed_image.copy())
else:
# 简单的运动检测
if len(self.frame_buffer) >= 2:
prev_frame = self.frame_buffer[-1]
gray_prev = cv2.cvtColor(prev_frame, cv2.COLOR_RGB2GRAY)
gray_current = cv2.cvtColor(processed_image, cv2.COLOR_RGB2GRAY)

# 计算帧间差异
diff = cv2.absdiff(gray_prev, gray_current)
_, diff_thresh = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)

# 如果差异足够大,检测到运动
motion_pixels = np.sum(diff_thresh > 0)
motion_ratio = motion_pixels / (self.im_width * self.im_height)
self.motion_detected = motion_ratio > 0.01 # 1%的像素变化

self.frame_buffer.append(processed_image.copy())

# 显示预览(如果启用)
if self.SHOW_CAM:
Expand All @@ -377,27 +415,27 @@ def process_img(self, image):
print(f"处理图像时出错: {e}")

def reward(self, speed_kmh, current_steer):
"""增强的奖励函数 - 特别强调行人避障"""
"""增强的奖励函数 - 特别强调行人避障和快速反应"""
reward = 0
done = False

# 获取车辆状态
vehicle_location = self.vehicle.get_location()
vehicle_rotation = self.vehicle.get_transform().rotation.yaw

# 1. 道路保持奖励(重要的基础)
# 1. 道路保持奖励
heading_error = abs(vehicle_rotation)

if heading_error < 5: # 完美保持方向
reward += 0.8 # 略微降低权重,给行人避障更多空间
reward += 0.5
elif heading_error < 15: # 良好保持
reward += 0.4
reward += 0.2
elif heading_error < 30: # 可接受
reward += 0.1
reward += 0.05
else: # 方向偏差过大
reward -= 0.3 * (heading_error / 30.0) # 降低惩罚强度
reward -= 0.2 * (heading_error / 30.0)

# 2. 行人避障(最高优先级) - 增强奖励机制
# 2. 行人避障(最高优先级) - 增强反应机制
min_ped_distance = float('inf')
closest_pedestrian = None

Expand All @@ -422,14 +460,24 @@ def reward(self, speed_kmh, current_steer):
if active_pedestrians == 0:
min_ped_distance = float('inf')

# 行人距离分级奖励 - 增强避障激励
# 行人检测和反应时间记录
current_time = time.time()

# 检测到行人进入危险区域
if min_ped_distance < 100: # 只考虑100米内的行人
if self.obstacle_detected_time is None:
self.obstacle_detected_time = current_time
self.reaction_start_time = current_time

# 行人距离分级奖励/惩罚 - 强调提前反应
if min_ped_distance < 3.0: # 紧急避让距离
reward -= 8.0 # 增加惩罚
reward -= 15.0 # 大幅增加惩罚
done = True
print(f"Episode {self.current_episode}: 与行人距离过近 ({min_ped_distance:.1f}m)!")
print(f"Episode {self.current_episode}: 与行人碰撞! 距离: {min_ped_distance:.1f}m")

elif min_ped_distance < 5.0: # 危险距离
reward -= 3.0 # 增加惩罚
reward -= 5.0 # 增加惩罚

# 计算避让方向
if closest_pedestrian:
ped_y = closest_pedestrian.get_location().y
Expand All @@ -439,58 +487,93 @@ def reward(self, speed_kmh, current_steer):
self.suggested_action = 4 # 右转
else: # 行人在右侧
self.suggested_action = 3 # 左转

# 反应时间惩罚
if self.reaction_start_time:
reaction_time = current_time - self.reaction_start_time
if reaction_time > 0.5: # 反应超过0.5秒
reward -= 2.0 * (reaction_time - 0.5)

elif min_ped_distance < 8.0: # 预警距离
reward -= 0.8
reward -= 1.5

# 计算避让方向(提前建议)
if closest_pedestrian:
ped_y = closest_pedestrian.get_location().y
veh_y = vehicle_location.y
if ped_y < veh_y: # 行人在左侧
self.suggested_action = 4 # 右转
else: # 行人在右侧
self.suggested_action = 3 # 左转

# 提前反应的奖励
if self.last_ped_distance < 12.0 and min_ped_distance > self.last_ped_distance:
# 如果上次距离更近,这次距离更远了,说明正在成功避让
reward += 1.0
self.proactive_action_count += 1

elif min_ped_distance < 12.0: # 安全距离
reward += 0.5 # 增加安全距离奖励
else: # 非常安全
reward += 0.2

# 3. 成功避障奖励 - 当成功避开行人后给予额外奖励
if self.last_ped_distance < 8.0 and min_ped_distance > self.last_ped_distance:
# 如果上次距离危险,这次距离更远了,说明成功避让
reward += 0.3
# 检测到行人但距离安全,给予轻微奖励
reward += 0.3

# 如果检测到运动,给予额外奖励
if self.motion_detected:
reward += 0.2

# 提前规划奖励
if self.last_ped_distance < 8.0:
# 从危险距离进入安全距离,成功避障
reward += 2.0

else:
# 没有检测到行人,重置反应计时
self.obstacle_detected_time = None
self.reaction_start_time = None

self.last_ped_distance = min_ped_distance # 保存当前距离

# 4. 速度奖励(平衡避障和前进)
# 3. 速度奖励(平衡避障和前进)
if 20 <= speed_kmh <= 35: # 理想速度区间
reward += 0.4
elif 10 <= speed_kmh < 20: # 较慢但安全(避障时可能需要减速)
reward += 0.2
reward += 0.5
elif 10 <= speed_kmh < 20: # 较慢但安全
reward += 0.3
elif 35 < speed_kmh <= 40: # 稍快
reward += 0.1
elif speed_kmh > 40: # 过快,在行人环境中危险
reward -= 0.5 # 增加惩罚
reward -= 1.0 # 增加惩罚
else: # 停车或极慢
reward -= 0.05 # 轻微惩罚
reward -= 0.1 # 轻微惩罚

# 5. 转向平滑性奖励(避障时可能需要适当转向)
steer_penalty = abs(current_steer) * 0.3 # 降低惩罚,给避障转向更多空间
# 4. 转向平滑性奖励
steer_penalty = abs(current_steer) * 0.2 # 降低惩罚,给避障转向更多空间
reward -= steer_penalty

# 6. 碰撞检测
# 5. 碰撞检测
if len(self.collision_history) != 0:
reward = -10 # 减少碰撞惩罚
reward = -20 # 增加碰撞惩罚
done = True
print(f"Episode {self.current_episode}: 发生碰撞!")

# 7. 进度奖励(次要于避障)
# 6. 进度奖励
progress = (vehicle_location.x + 81) / 236.0 # 从-81到155
reward += progress * 0.2 # 降低进度权重
reward += progress * 0.3

# 8. 边界检查
# 7. 边界检查
if vehicle_location.x > 155: # 成功到达终点
reward += 15 # 减少到达终点的奖励
reward += 20 # 增加到达终点的奖励
done = True
print(f"Episode {self.current_episode}: 成功到达终点!")
elif vehicle_location.x < -90 or abs(vehicle_location.y + 195) > 30: # 偏离道路
reward -= 3 # 降低偏离惩罚
reward -= 5 # 增加偏离惩罚
done = True
print(f"Episode {self.current_episode}: 偏离道路!")

# 8. 主动避障奖励
if self.proactive_action_count > 0:
reward += self.proactive_action_count * 0.5

# 限制奖励范围
reward = max(min(reward, 20), -15)
reward = max(min(reward, 30), -25)

return reward, done

Expand All @@ -510,32 +593,46 @@ def step(self, action):
brake = 0.0
steer = 0.0

# 速度控制
# 速度控制 - 优化动作响应
if action == 0: # 减速
throttle = 0.0
brake = 0.5
brake = 0.7 # 增加制动强度
elif action == 1: # 保持/轻微加速
throttle = 0.3
throttle = 0.4 # 增加油门
brake = 0.0
elif action == 2: # 加速
throttle = 0.7
throttle = 0.8
brake = 0.0
elif action == 3: # 左转
throttle = 0.4
throttle = 0.5 # 增加转向时的油门
brake = 0.0
steer = -0.2 * speed_factor # 速度相关的转向幅度
steer = -0.25 * speed_factor # 增加转向幅度
elif action == 4: # 右转
throttle = 0.4
throttle = 0.5
brake = 0.0
steer = 0.2 * speed_factor # 速度相关的转向幅度
steer = 0.25 * speed_factor # 增加转向幅度

# 如果有建议的避让动作,调整当前动作
if self.suggested_action is not None:
# 如果当前动作与建议动作不同,调整转向幅度
if self.suggested_action in [3, 4] and action in [3, 4]:
# 如果都是转向动作,但方向不同,增加建议方向的转向
if self.suggested_action != action:
if self.suggested_action == 3: # 建议左转
steer = -0.3 * speed_factor
else: # 建议右转
steer = 0.3 * speed_factor
elif self.suggested_action == 0: # 建议减速
brake = max(brake, 0.8) # 强制制动
throttle = 0.0

# 限制连续同向转向 - 防止过度转向
if (action == 3 and self.last_action == 3) or (action == 4 and self.last_action == 4):
self.same_steer_counter += 1
if self.same_steer_counter > 3: # 连续3次同向转向后强制回正
steer *= 0.5 # 减小转向幅度
throttle *= 0.8 # 减速
brake = 0.1 # 轻微制动
if self.same_steer_counter > 2: # 连续2次同向转向后强制回正
steer *= 0.3 # 大幅减小转向幅度
throttle *= 0.6 # 减速
brake = 0.2 # 轻微制动
else:
self.same_steer_counter = 0

Expand All @@ -549,13 +646,13 @@ def step(self, action):
steer=steer
))

# 等待物理更新
time.sleep(0.05)
# 减少等待时间,加快反应
time.sleep(0.03) # 从0.05减少到0.03

# 计算奖励和完成状态
reward, done = self.reward(speed_kmh, steer)

# 限制极端奖励值
reward = np.clip(reward, -15, 20)
reward = np.clip(reward, -25, 30)

return self.front_camera, reward, done, None
Loading
Loading