diff --git a/data/undergraduate/.gitignore b/data/undergraduate/.gitignore deleted file mode 100644 index c89dc294..00000000 --- a/data/undergraduate/.gitignore +++ /dev/null @@ -1,246 +0,0 @@ -## Core latex/pdflatex auxiliary files: -*.aux -*.lof -*.log -*.lot -*.fls -*.out -*.toc -*.fmt -*.fot -*.cb -*.cb2 -.*.lb -clean.bat -elegant*-cn.pdf -elegant*-en.pdf -*.dates - -## Intermediate documents: -*.dvi -*.xdv -*-converted-to.* -# these rules might exclude image files for figures etc. -# *.ps -# *.eps -# *.pdf - -## Generated if empty string is given at "Please type another file name for output:" -.pdf - -## Bibliography auxiliary files (bibtex/biblatex/biber): -*.bbl -*.bcf -*.blg -*-blx.aux -*-blx.bib -*.run.xml - -## Build tool auxiliary files: -*.fdb_latexmk -*.synctex -*.synctex(busy) -*.synctex.gz -*.synctex.gz(busy) -*.pdfsync - -## Auxiliary and intermediate files from other packages: -# algorithms -*.alg -*.loa - -# achemso -acs-*.bib - -# amsthm -*.thm - -# beamer -*.nav -*.pre -*.snm -*.vrb - -# changes -*.soc - -# cprotect -*.cpt - -# elsarticle (documentclass of Elsevier journals) -*.spl - -# endnotes -*.ent - -# fixme -*.lox - -# feynmf/feynmp -*.mf -*.mp -*.t[1-9] -*.t[1-9][0-9] -*.tfm - -#(r)(e)ledmac/(r)(e)ledpar -*.end -*.?end -*.[1-9] -*.[1-9][0-9] -*.[1-9][0-9][0-9] -*.[1-9]R -*.[1-9][0-9]R -*.[1-9][0-9][0-9]R -*.eledsec[1-9] -*.eledsec[1-9]R -*.eledsec[1-9][0-9] -*.eledsec[1-9][0-9]R -*.eledsec[1-9][0-9][0-9] -*.eledsec[1-9][0-9][0-9]R - -# glossaries -*.acn -*.acr -*.glg -*.glo -*.gls -*.glsdefs - -# gnuplottex -*-gnuplottex-* - -# gregoriotex -*.gaux -*.gtex - -# htlatex -*.4ct -*.4tc -*.idv -*.lg -*.trc -*.xref - -# hyperref -*.brf - -# knitr -*-concordance.tex -# TODO Comment the next line if you want to keep your tikz graphics files -*.tikz -*-tikzDictionary - -# listings -*.lol - -# makeidx -*.idx -*.ilg -*.ind -*.ist - -# minitoc -*.maf -*.mlf -*.mlt -*.mtc[0-9]* -*.slf[0-9]* -*.slt[0-9]* -*.stc[0-9]* - -# minted -_minted* -*.pyg - -# morewrites -*.mw - -# nomencl -*.nlg -*.nlo -*.nls - -# pax -*.pax - -# pdfpcnotes -*.pdfpc - -# sagetex -*.sagetex.sage -*.sagetex.py -*.sagetex.scmd - -# scrwfile -*.wrt - -# sympy -*.sout -*.sympy -sympy-plots-for-*.tex/ - -# pdfcomment -*.upa -*.upb - -# pythontex -*.pytxcode -pythontex-files-*/ - -# thmtools -*.loe - -# TikZ & PGF -*.dpth -*.md5 -*.auxlock - -# todonotes -*.tdo - -# easy-todo -*.lod - -# xmpincl -*.xmpi - -# xindy -*.xdy - -# xypic precompiled matrices -*.xyc - -# endfloat -*.ttt -*.fff - -# Latexian -TSWLatexianTemp* - -## Editors: -# WinEdt -*.bak -*.sav - -# Texpad -.texpadtmp - -# Kile -*.backup - -# KBibTeX -*~[0-9]* - -# auto folder when using emacs and auctex -./auto/* -*.el - -# expex forward references with \gathertags -*-tags.tex - -# standalone packages -*.sta - -# generated if using elsarticle.cls -*.spl diff --git a/data/undergraduate/README.md b/data/undergraduate/README.md deleted file mode 100644 index 74029dba..00000000 --- a/data/undergraduate/README.md +++ /dev/null @@ -1,38 +0,0 @@ -# 本科毕业设计模板 - -湖南工商大学本科毕业设计模板。 - -# 使用 -推荐使用 [texstudio](https://pan.baidu.com/s/1Is2-VR1z-tMYvmdinsVY_g?pwd=hutb) 或 [overleaf](https://cn.overleaf.com/) 进行论文撰写。 - -注:在 latex 2023 中编译成功,latex 2016 编译失败(需要更高的版本,其他版本的 latex 没试过)。 - -# 贡献 -有任何对模板格式进行调整的可以提交 [Issues](https://github.com/OpenHUTB/undergraduate/issues) 或 [Pull Request](https://github.com/OpenHUTB/undergraduate/pulls) 。 - -# 选题 -[选题列表](https://github.com/OpenHUTB/undergraduate/wiki/%E6%AF%95%E8%AE%BE%E9%80%89%E9%A2%98) - - -## 要求 -1. 格式化、注释、文档翻译 -2. 运行 -3. 测试(例子通过) - - -## 初始化 - -1. 新建github工程 - -2. 运行`init_proj('PROJECT_NAME');` - -3. 分配开发者 - - -## 使用 -克隆仓库 -```shell -git clone --recursive https://github.com/OpenHUTB/undergraduate.git -``` - -# 贡献者 diff --git a/data/undergraduate/cmdel.bat b/data/undergraduate/cmdel.bat deleted file mode 100644 index c9642e0a..00000000 --- a/data/undergraduate/cmdel.bat +++ /dev/null @@ -1,2 +0,0 @@ -del /a /f *.xml *.bcf *.out *.log *.sav *.gz *.aux *.bbl *.bak *.dvi *.blg - diff --git a/data/undergraduate/content/abstracten.tex b/data/undergraduate/content/abstracten.tex deleted file mode 100644 index 37502ce6..00000000 --- a/data/undergraduate/content/abstracten.tex +++ /dev/null @@ -1,9 +0,0 @@ -%!TEX root = ../csuthesis_main.tex -\keywordsen{Intelligent Driving \ \ Simulation Scenarios \ \ Dangerous Scenario Generation \ \ Automation Technology} -\begin{abstracten} - -In the development of intelligent driving systems, the generation and optimization of simulation scenarios are crucial for ensuring their safety and reliability. This project reviews the technologies for generating and optimizing dangerous simulation scenarios in intelligent driving. Firstly, based on natural driving data, representative dangerous driving scenarios are identified and extracted, providing a data foundation for the construction of simulation scenarios. Secondly, through multi-dimensional scenario automatic extraction and fusion methods, typical driving scenarios such as line-following, following, and lane-changing are identified and integrated with dynamic driving scenarios to generate more complex and realistic test scenarios. Additionally, to address the issue of insufficient dangerous scenarios in existing test scenarios, a test case generation and enhancement method based on cluster analysis and importance sampling is proposed, which effectively improves the test coverage and efficiency of dangerous scenarios. Finally, an automated simulation testing platform has been developed, enabling the rapid construction of test scenarios, joint invocation of simulation software, and fully automated execution of result analysis and report generation. These methods can significantly improve the safety testing efficiency of intelligent driving systems in simulation environments, providing strong support for the further development of intelligent driving technology. - - - -\end{abstracten} \ No newline at end of file diff --git a/data/undergraduate/content/abstractzh.tex b/data/undergraduate/content/abstractzh.tex deleted file mode 100644 index 8c0560fe..00000000 --- a/data/undergraduate/content/abstractzh.tex +++ /dev/null @@ -1,11 +0,0 @@ -%!TEX root = ../csuthesis_main.tex -% 设置中文摘要 -\keywordscn{智能驾驶\quad 仿真场景\quad 危险场景生成\quad 自动化技术} -%\categorycn{TP391} -\begin{abstractzh} - - -在智能驾驶系统的发展过程中,仿真场景的生成与优化是确保其安全性和可靠性的重要手段。本项目综述了智能驾驶危险仿真场景的生成和优化技术。首先,基于自然驾驶数据,识别并提取出具有代表性的危险驾驶场景,为仿真场景的构建提供了数据基础。其次,通过多维场景自动提取和融合方法,识别出典型的行车场景,如巡线、跟车、邻车切入等,并将其与动态驾驶场景进行融合,以生成更为复杂和真实的测试场景。此外,针对现有测试场景中危险场景数量不足的问题,提出了一种基于聚类分析和重要性采样的测试用例生成和增强方法,有效提高了危险场景的测试覆盖率和测试效率。最后,开发了一种自动化仿真测试平台,实现了测试场景的快速构建、仿真软件的联合调用以及结果分析与报告生成的全自动化执行。通过这些方法,能够显著提高智能驾驶系统在仿真环境中的安全测试效率,为智能驾驶技术的进一步发展提供了有力支持。 - - -\end{abstractzh} \ No newline at end of file diff --git a/data/undergraduate/content/acknowledgements.tex b/data/undergraduate/content/acknowledgements.tex deleted file mode 100644 index 5b7a3d87..00000000 --- a/data/undergraduate/content/acknowledgements.tex +++ /dev/null @@ -1,18 +0,0 @@ -%!TEX root = ../csuthesis_main.tex -\begin{acknowledgements} - -感谢制作出中南大学本科学位论文 LaTeX 模板的edwardzcn。 - -感谢制作出中南大学博士学位论文 LaTeX 模板的郭大侠@CSGrandeur。 - -感谢添加本科学位论文样式支持的@BlurryLight。 - -感谢帮助重构项目并进行测试的@burst-bao以及为独立使用LaTeX进行毕业论文写作提供宝贵经验的16级的姜析阅学长。 - -感谢 CTeX-kit 提供了 LaTeX 的中文支持。 - -感谢上海交通大学学位论文 LaTeX 模板的维护者们 @sjtug 和清华大学学位论文 LaTeX 模板的维护者们 @tuna 给予的宝贵设计经验。 - -感谢所有为模板贡献过代码的同学们! - -\end{acknowledgements} diff --git a/data/undergraduate/content/appendix.tex b/data/undergraduate/content/appendix.tex deleted file mode 100644 index d952b542..00000000 --- a/data/undergraduate/content/appendix.tex +++ /dev/null @@ -1,109 +0,0 @@ -%!TEX root = ../csuthesis_main.tex -% \begin{appendixs} % 无章节编号 -\chapter{附录代码} - -附录部分用于存放这里用来存放不适合放置在正文的大篇幅内容、典型如代码、图纸、完整数学证明过程等内容。 - -\section{堆溢出检测算法} - -\begin{algorithm}[h] - \caption{堆溢出检测算法}\label{alg:ovf} - \begin{algorithmic}[1] - \IF {$\beta \in \mathbb{N^{*}} \land \Delta_\beta = \Delta_{\beta - 1} \land \beta < S$} - \STATE 正常写入 - \ELSIF {$\beta \in \mathbb{N^{*}} \land \Delta_\beta \neq \Delta_{\beta - 1} \land \beta \geq S$} - \STATE 发生堆溢出 - \ENDIF - \end{algorithmic} -\end{algorithm} - -\section{KMP算法C++描述} - -% \begin{minted}[linenos]{c} -\begin{lstlisting} - const int maxn=2e5+5; - int nt[maxn]; - int aa[maxn],bb[maxn]; - int a[maxn],b[maxn]; - int n; - //参数为模板串和next数组 - //字符串均从下标0开始 - void kmpGetNext(int *s,int *Next) - { - Next[0]=0; - // int len=strlen(s); - for(int i=1,j=0;i>n) - { - memset(a,0,sizeof(a)); - memset(b,0,sizeof(b)); - rep(i,0,n) cin>>aa[i]; - rep(i,0,n) cin>>bb[i]; - sort(aa,aa+n); - sort(bb,bb+n); - rep(i,0,n-1){ - a[i]=aa[i+1]-aa[i]; - b[i]=bb[i+1]-bb[i]; - } - a[n-1]=360000+aa[0]-aa[n-1]; - // rep(i,0,n) cout< + + + + + + + + + + + + + + + + + + + + + + + +]> + + + latex + + hutbthesis_main.bcf + + + hutbthesis_main.bbl + + + blx-dm.def + blx-unicode.def + blx-compat.def + biblatex.def + standard.bbx + numeric.bbx + numeric-comp.bbx + gb7714-2015.bbx + numeric-comp.cbx + gb7714-2015.cbx + biblatex.cfg + english.lbx + + + + biber + + biber + hutbthesis_main + + + hutbthesis_main.bcf + + + hutbthesis_main.bbl + + + hutbthesis_main.bbl + + + hutbthesis_main.bcf + + + content/reference.bib + + + diff --git a/hutbthesis_main.synctex.gz b/hutbthesis_main.synctex.gz new file mode 100644 index 00000000..f3d79f76 Binary files /dev/null and b/hutbthesis_main.synctex.gz differ diff --git a/hutbthesis_main.toc b/hutbthesis_main.toc new file mode 100644 index 00000000..df7b335b --- /dev/null +++ b/hutbthesis_main.toc @@ -0,0 +1,30 @@ +\contentsline {chapter}{\numberline {第1章\hspace {.3em}}绪论}{1}{chapter.1}% +\contentsline {section}{\numberline {1.1}研究背景}{1}{section.1.1}% +\contentsline {section}{\numberline {1.2}研究意义}{1}{section.1.2}% +\contentsline {subsection}{\numberline {1.2.1}理论意义}{1}{subsection.1.2.1}% +\contentsline {subsection}{\numberline {1.2.2}现实意义}{1}{subsection.1.2.2}% +\contentsline {section}{\numberline {1.3}国内外研究现状}{2}{section.1.3}% +\contentsline {subsection}{\numberline {1.3.1}目标检测研究现状}{2}{subsection.1.3.1}% +\contentsline {subsection}{\numberline {1.3.2}目标跟踪研究现状}{3}{subsection.1.3.2}% +\contentsline {subsection}{\numberline {1.3.3}意图分析研究现状}{5}{subsection.1.3.3}% +\contentsline {subsection}{\numberline {1.3.4}本文结构框架}{6}{subsection.1.3.4}% +\contentsline {chapter}{\numberline {第2章\hspace {.3em}}图表示例}{8}{chapter.2}% +\contentsline {section}{\numberline {2.1}图片与布局}{8}{section.2.1}% +\contentsline {subsection}{\numberline {2.1.1}插图}{8}{subsection.2.1.1}% +\contentsline {subsection}{\numberline {2.1.2}横向布局}{8}{subsection.2.1.2}% +\contentsline {section}{\numberline {2.2}纵向布局}{9}{section.2.2}% +\contentsline {section}{\numberline {2.3}竖排多图横排布局}{9}{section.2.3}% +\contentsline {section}{\numberline {2.4}横排多图竖排布局}{9}{section.2.4}% +\contentsline {chapter}{\numberline {第3章\hspace {.3em}}表格插入示例}{11}{chapter.3}% +\contentsline {chapter}{\numberline {第4章\hspace {.3em}}公式插入示例}{12}{chapter.4}% +\contentsline {chapter}{\numberline {第5章\hspace {.3em}}引用文献标注}{13}{chapter.5}% +\contentsline {section}{\numberline {5.1}顺序编码}{13}{section.5.1}% +\contentsline {section}{\numberline {5.2}获取BibTeX格式索引}{13}{section.5.2}% +\contentsline {section}{\numberline {5.3}参考文献插入示例}{13}{section.5.3}% +\contentsline {chapter}{致谢}{14}{section.5.3}% +\contentsline {chapter}{\numberline {附录 A\hspace {.3em}}附录代码}{15}{appendix.A}% +\contentsline {section}{\numberline {A.1}堆溢出检测算法}{15}{section.A.1}% +\contentsline {section}{\numberline {A.2}KMP算法C++描述}{15}{section.A.2}% +\contentsline {chapter}{\numberline {附录 B\hspace {.3em}}康托尔辩辞录:数学的自由与制约}{18}{appendix.B}% +\contentsline {chapter}{参考文献}{15}{chapter*.16}% +\contentsfinish diff --git "a/sim/\344\273\243\347\240\201" "b/sim/\344\273\243\347\240\201" new file mode 100644 index 00000000..c62ae137 --- /dev/null +++ "b/sim/\344\273\243\347\240\201" @@ -0,0 +1,457 @@ +import carla +import numpy as np +import cv2 +import random +from collections import deque +import tensorflow as tf +from tensorflow.keras.models import Sequential +from tensorflow.keras.layers import Dense, Conv2D, Flatten +from tensorflow.keras.optimizers import Adam +import os +import threading +from queue import Queue +class CarlaSyncMode(object): + def __init__(self, world, *sensors, **kwargs): + self.world = world + self.sensors = sensors + self.frame = None + self._queues = [] + self._settings = None + + def __enter__(self): + self._settings = self.world.get_settings() + self.frame = self.world.apply_settings(carla.WorldSettings( + no_rendering_mode=False, + synchronous_mode=True, + fixed_delta_seconds=0.05)) + def make_queue(register_event): + q = deque() + register_event(q.append) + self._queues.append(q) + make_queue(self.world.on_tick) + for sensor in self.sensors: + make_queue(sensor.listen) + return self + + def tick(self, timeout): + self.frame = self.world.wait_for_tick(self.frame + 1, timeout) + + def __exit__(self, *args, **kwargs): + self.world.apply_settings(self._settings) + for q in self._queues: + q.clear() +# 初始化CARLA客户端 +def init_carla_client(): + client = carla.Client('localhost', 2000) + client.set_timeout(10.0) + return client +def main(): + client = init_carla_client() + world = setup_world(client) + bp_lib = get_blueprint_library(world) + vehicle = spawn_vehicle(world, bp_lib) + camera = spawn_rgb_camera(world, bp_lib, vehicle) + semantic_camera = spawn_semantic_camera(world, bp_lib, vehicle) + lidar = spawn_lidar(world, bp_lib, vehicle) + create_output_dir() + state_size, action_size, batch_size, episodes = setup_experiment_parameters() + agent = initialize_training(state_size, action_size) + actions = get_action_space() + try: + with CarlaSyncMode(world, camera, semantic_camera, lidar) as sync_mode: + spawn_points = world.get_map().get_spawn_points() + for e in range(episodes): + spawn_point = random.choice(spawn_points) + vehicle.set_transform(spawn_point) + sync_mode.tick(3.0) + image = sync_mode._queues[1][-1] + state = np.array(image.raw_data).reshape(200, 200, 3) + state = np.expand_dims(state, axis=0) + total_reward = 0 + done = False + steps = 0 + while not done and steps < 1000: + action_idx = agent.act(state) + action = actions[action_idx] + vehicle.apply_control(carla.VehicleControl( + steer=action[0], + throttle=action[1], + brake=action[2] + )) + sync_mode.tick(3.0) + next_image = sync_mode._queues[1][-1] + next_state = np.array(next_image.raw_data).reshape(200, 200, 3) + next_state = np.expand_dims(next_state, axis=0) + reward = calculate_reward(vehicle, action) + total_reward += reward + steps += 1 + done = steps >= 1000 + agent.remember(state, action_idx, reward, next_state, done) + state = next_state + if len(agent.memory) > batch_size: + agent.replay(batch_size) + print(f"Episode: {e}, Total Reward: {total_reward}, Steps: {steps}") + finally: + save_model(agent) + cleanup(vehicle, camera, semantic_camera, lidar) +# 加载地图并设置天气 +def setup_world(client, map_name='Town01'): + world = client.load_world(map_name) + weather = carla.WeatherParameters.ClearNoon + world.set_weather(weather) + return world + +# 获取蓝图库 +def get_blueprint_library(world): + return world.get_blueprint_library() + +# 添加车辆 +def spawn_vehicle(world, bp_lib, vehicle_name='vehicle.tesla.model3'): + vehicle_bp = bp_lib.find(vehicle_name) + spawn_points = world.get_map().get_spawn_points() + spawn_point = random.choice(spawn_points) + vehicle = world.spawn_actor(vehicle_bp, spawn_point) + vehicle.set_autopilot(False) + return vehicle + +# 添加RGB相机 +def spawn_rgb_camera(world, bp_lib, vehicle): + camera_bp = bp_lib.find('sensor.camera.rgb') + camera_bp.set_attribute('image_size_x', '200') + camera_bp.set_attribute('image_size_y', '200') + camera_bp.set_attribute('fov', '110') + camera_location = carla.Location(2, 0, 1) + camera_rotation = carla.Rotation(0, 0, 0) + camera_transform = carla.Transform(camera_location, camera_rotation) + camera = world.spawn_actor(camera_bp, camera_transform, attach_to=vehicle) + return camera + +# 添加语义分割相机 +def spawn_semantic_camera(world, bp_lib, vehicle): + semantic_camera_bp = bp_lib.find('sensor.camera.semantic_segmentation') + semantic_camera_bp.set_attribute('image_size_x', '200') + semantic_camera_bp.set_attribute('image_size_y', '200') + semantic_camera_bp.set_attribute('fov', '110') + semantic_camera_location = carla.Location(2, 0, 1) + semantic_camera_rotation = carla.Rotation(0, 0, 0) + semantic_camera_transform = carla.Transform(semantic_camera_location, semantic_camera_rotation) + semantic_camera = world.spawn_actor(semantic_camera_bp, semantic_camera_transform, attach_to=vehicle) + return semantic_camera + +# 添加激光雷达 +def spawn_lidar(world, bp_lib, vehicle): + lidar_bp = bp_lib.find('sensor.lidar.ray_cast') + lidar_bp.set_attribute('channels', '32') + lidar_bp.set_attribute('range', '5000') + lidar_location = carla.Location(0, 0, 2.5) + lidar_rotation = carla.Rotation(0, 0, 0) + lidar_transform = carla.Transform(lidar_location, lidar_rotation) + lidar = world.spawn_actor(lidar_bp, lidar_transform, attach_to=vehicle) + return lidar + +# 经验回放缓冲区 +class ReplayBuffer: + def __init__(self, capacity): + self.buffer = deque(maxlen=capacity) + + def add(self, experience): + self.buffer.append(experience) + + def sample(self, batch_size): + return random.sample(self.buffer, batch_size) + + def __len__(self): + return len(self.buffer) + +# DQN模型构建 +def build_model(input_shape, action_size): + model = Sequential() + model.add(Conv2D(32, (3, 3), activation='relu', input_shape=input_shape)) + model.add(Flatten()) + model.add(Dense(64, activation='relu')) + model.add(Dense(action_size, activation='linear')) + model.compile(loss='mse', optimizer=Adam(learning_rate=0.001)) + return model + +# PERDQN代理类 +class PERDQNAgent: + def __init__(self, state_size, action_size): + self.state_size = state_size + self.action_size = action_size + self.memory = ReplayBuffer(2000) + self.gamma = 0.95 + self.epsilon = 1.0 + self.epsilon_min = 0.01 + self.epsilon_decay = 0.995 + self.model = build_model(state_size, action_size) + + def act(self, state): + if np.random.rand() <= self.epsilon: + return random.randrange(self.action_size) + act_values = self.model.predict(state) + return np.argmax(act_values[0]) + + def remember(self, state, action, reward, next_state, done): + self.memory.add((state, action, reward, next_state, done)) + + def replay(self, batch_size): + minibatch = self.memory.sample(batch_size) + for state, action, reward, next_state, done in minibatch: + target = reward + if not done: + target = reward + self.gamma * np.amax(self.model.predict(next_state)[0]) + target_f = self.model.predict(state) + target_f[0][action] = target + self.model.fit(state, target_f, epochs=1, verbose=0) + if self.epsilon > self.epsilon_min: + self.epsilon *= self.epsilon_decay + +# 创建输出目录 +def create_output_dir(): + if not os.path.exists('output'): + os.makedirs('output') + +# 定义奖励函数 +def calculate_reward(vehicle, action): + velocity = vehicle.get_velocity() + speed = np.linalg.norm([velocity.x, velocity.y, velocity.z]) + transform = vehicle.get_transform() + lane_offset = transform.location.y + reward = 0.0 + if abs(lane_offset) > 2.0: + reward -= 10.0 + else: + reward += 5.0 + if speed < 5 or speed > 15: + reward -= 2.0 + else: + reward += 1.0 + if transform.location.x > 100.0: + reward += 10.0 + if abs(transform.rotation.yaw) > 15.0: + reward -= 1.0 + return reward + +# 设置实验参数 +def setup_experiment_parameters(): + state_size = (200, 200, 3) + action_size = 5 + batch_size = 32 + episodes = 300 + return state_size, action_size, batch_size, episodes + +# 初始化训练过程 +def initialize_training(state_size, action_size): + agent = PERDQNAgent(state_size, action_size) + return agent + +# 定义动作空间 +def get_action_space(): + steer_space = [-0.5, 0.0, 0.5] + throttle_space = [0.3, 0.6] + brake_space = [0.0, 0.3] + actions = [] + action_idx = agent.act(state) + action = actions[action_idx] + for steer in steer_space: + for throttle in throttle_space: + for brake in brake_space: + actions.append((steer, throttle, brake)) + return actions +def spawn_collision_sensor(world, bp_lib, vehicle): + collision_bp = bp_lib.find('sensor.other.collision') + collision_location = carla.Location(0, 0, 0) + collision_rotation = carla.Rotation(0, 0, 0) + collision_transform = carla.Transform(collision_location, collision_rotation) + collision_sensor = world.spawn_actor(collision_bp, collision_transform, attach_to=vehicle) + return collision_sensor + +def main(): + client = init_carla_client() + world = setup_world(client) + bp_lib = get_blueprint_library(world) + vehicle = spawn_vehicle(world, bp_lib) + camera = spawn_rgb_camera(world, bp_lib, vehicle) + semantic_camera = spawn_semantic_camera(world, bp_lib, vehicle) + lidar = spawn_lidar(world, bp_lib, vehicle) + collision_sensor = spawn_collision_sensor(world, bp_lib, vehicle) + create_output_dir() + state_size, action_size, batch_size, episodes = setup_experiment_parameters() + agent = initialize_training(state_size, action_size) + actions = get_action_space() + try: + with CarlaSyncMode(world, camera, semantic_camera, lidar, collision_sensor) as sync_mode: + spawn_points = world.get_map().get_spawn_points() + for e in range(episodes): + spawn_point = random.choice(spawn_points) + vehicle.set_transform(spawn_point) + sync_mode.tick(3.0) + image = sync_mode._queues[1][-1] + state = np.array(image.raw_data).reshape(200, 200, 3) + state = np.expand_dims(state, axis=0) + total_reward = 0 + done = False + steps = 0 + while not done and steps < 1000: + action_idx = agent.act(state) + action = actions[action_idx] + vehicle.apply_control(carla.VehicleControl( + steer=action[0], + throttle=action[1], + brake=action[2] + )) + sync_mode.tick(3.0) + next_image = sync_mode._queues[1][-1] + next_state = np.array(next_image.raw_data).reshape(200, 200, 3) + next_state = np.expand_dims(next_state, axis=0) + reward = calculate_reward(vehicle, action) + total_reward += reward + steps += 1 + # 检查碰撞 + if len(sync_mode._queues[4]) > 0: + done = True + agent.remember(state, action_idx, reward, next_state, done) + state = next_state + if len(agent.memory) > batch_size: + agent.replay(batch_size) + print(f"Episode: {e}, Total Reward: {total_reward}, Steps: {steps}") + finally: + save_model(agent) + cleanup(vehicle, camera, semantic_camera, lidar, collision_sensor) +# 训练循环 +def training_loop(world, vehicle, camera, semantic_camera, lidar, agent, actions, batch_size, episodes): + spawn_points = world.get_map().get_spawn_points() + max_steps_per_episode = 1000 + for e in range(episodes): + spawn_point = random.choice(spawn_points) + vehicle.set_transform(spawn_point) + image = camera.get() + image = sync_mode._queues[1][-1] # 假设第二个传感器是RGB相机 + state = np.array(image.raw_data).reshape(200, 200, 3) + state = np.expand_dims(state, axis=0) + total_reward = 0 + done = False + steps = 0 + while not done and steps < max_steps_per_episode: + action_idx = agent.act(state) + action = actions[action_idx] + vehicle.apply_control(carla.VehicleControl( + steer=action[0], + throttle=action[1], + brake=action[2] + )) + next_image = camera.get() + next_state = np.array(next_image.raw_data).reshape(200, 200, 3) + next_state = np.expand_dims(next_state, axis=0) + reward = calculate_reward(vehicle, action) + total_reward += reward + steps += 1 + if steps >= max_steps_per_episode: + done = True + agent.remember(state, action_idx, reward, next_state, done) + state = next_state + if len(agent.memory) > batch_size: + agent.replay(batch_size) + print(f"Episode: {e}, Total Reward: {total_reward}, Steps: {steps}") + +# 保存模型 +def save_model(agent, model_path='perdqn_model.h5'): + agent.model.save(model_path) + +# 清理环境 +def cleanup(vehicle, camera, semantic_camera, lidar, collision_sensor): + vehicle.destroy() + camera.destroy() + semantic_camera.destroy() + lidar.destroy() + collision_sensor.destroy() + +# 同步模式辅助类 +class CarlaSyncMode(object): + def __init__(self, world, *sensors, **kwargs): + self.world = world + self.sensors = sensors + self.frame = None + self._queues = [] + self._settings = None + + def __enter__(self): + self._settings = self.world.get_settings() + self.frame = self.world.apply_settings(carla.WorldSettings( + no_rendering_mode=False, + synchronous_mode=True, + fixed_delta_seconds=0.05)) + def make_queue(register_event): + q = deque() + register_event(q.append) + self._queues.append(q) + make_queue(self.world.on_tick) + for sensor in self.sensors: + make_queue(sensor.listen) + return self + + def tick(self, timeout): + self.frame = self.world.wait_for_tick(self.frame + 1, timeout) + + def __exit__(self, *args, **kwargs): + self.world.apply_settings(self._settings) + for q in self._queues: + q.clear() + +# 主函数 +def main(): + client = init_carla_client() + world = setup_world(client) + bp_lib = get_blueprint_library(world) + vehicle = spawn_vehicle(world, bp_lib) + camera = spawn_rgb_camera(world, bp_lib, vehicle) + semantic_camera = spawn_semantic_camera(world, bp_lib, vehicle) + lidar = spawn_lidar(world, bp_lib, vehicle) + create_output_dir() + state_size, action_size, batch_size, episodes = setup_experiment_parameters() + agent = initialize_training(state_size, action_size) + actions = get_action_space() + try: + with CarlaSyncMode(world, camera, semantic_camera, lidar) as sync_mode: + spawn_points = world.get_map().get_spawn_points() + for e in range(episodes): + spawn_point = random.choice(spawn_points) + vehicle.set_transform(spawn_point) + sync_mode.tick(3.0) + image = sync_mode._queues[1][-1] + state = np.array(image.raw_data).reshape(200, 200, 3) + state = np.expand_dims(state, axis=0) + total_reward = 0 + done = False + steps = 0 + while not done and steps < 1000: + action_idx = agent.act(state) + action = actions[action_idx] + vehicle.apply_control(carla.VehicleControl( + steer=action[0], + throttle=action[1], + brake=action[2] + )) + sync_mode.tick(3.0) + next_image = sync_mode._queues[1][-1] + next_state = np.array(next_image.raw_data).reshape(200, 200, 3) + next_state = np.expand_dims(next_state, axis=0) + reward = calculate_reward(vehicle, action) + total_reward += reward + steps += 1 + done = steps >= 1000 + agent.remember(state, action_idx, reward, next_state, done) + state = next_state + if len(agent.memory) > batch_size: + agent.replay(batch_size) + print(f"Episode: {e}, Total Reward: {total_reward}, Steps: {steps}") + finally: + save_model(agent) + cleanup(vehicle, camera, semantic_camera, lidar) + +if __name__ == '__main__': + main() +# 检查碰撞 +if len(sync_mode._queues[4]) > 0: # 假设第五个传感器是碰撞传感器 + done = True + \ No newline at end of file