当前位置:   article > 正文

要点初见:安装教程与二度拓展——基于SUMO的Flow Project(附代码链接)_仿真flow安装教程

仿真flow安装教程

前文链接:要点初见:用Python进行微观交通模型仿真——基于SUMO的伯克利开源项目Flow Project初探与拓展

 

前文链接中是博主先前写TRB论文时根据个人研究所写的Flow Project介绍、代码解释以及初步的功能拓展方法介绍,简单说就是一个能用python设计运行微观交通流仿真、方便在微观交通流中进行强化学习(Reinforcement Learning)的好东西(官方链接:https://github.com/flow-project/flow)。最近写毕业论文又捡起了Flow Project用来做微观交通流仿真,安装运行了Flow官方Github上的master分支代码(最新flow版本:flow0.5.0)后发现这新版本仍然没有实现众多仿真时必要的功能,譬如每个车辆的长宽等参数定制、每条道路的行驶速度等参数定制等等。其实这些功能都是SUMO中已有的,只需要一点衔接就能在Flow中实现这些功能,Flow作为SUMO的二次开发框架可能更集中于强化学习部分的功能开发,故在基础功能开发方面未有深入。

本文不会具体介绍这些功能衔接的具体实现(想了解可以将highway_newfunc分支的代码覆盖至master分支,并用VSCode之类的IDE查看Source Control-CHANGES),本文主要是给出链接、介绍优化后的Flow超简单安装方法、介绍此次拓展出的功能,并分析示例代码(test0205.py)。本文的运行环境是 Ubuntu16.04, anaconda 4.6.11

**************************************************************

注意:修改flow/flow中的源码后需要重新运行python setup.py develop对项目进行rebuild,Flow核心中部分的代码需要rebuild才会生效,例如flow/flow/core/kernel/simulation/、flow/flow/core/experiment.py,部分核心代码不需要rebuild也能生效,例如flow/flow/core/kernel/network/,但每次修改源码后运行python setup.py develop能避免改动未生效。

**************************************************************

不多废话,此处是在Flow(2021.2.8 master分支)上实现了这些功能衔接的代码链接(注意是highway_newfunc分支而非master分支):

Github 链接:https://github.com/BingLiHanShuang/flow/tree/highway_newfunc

百度网盘链接:https://pan.baidu.com/s/1GD3gb5sHthAzTBpvM0JTSw  提取码:59pe

1  下载并切换分支

此处展示通过git下载并切换至本分支所需的命令:

1、git clone https://github.com/BingLiHanShuang/flow.git

2、cd flow

3、git checkout highway_newfunc

2  安装Flow

切换至highway_newfunc分支后,Flow安装起来相比master分支更简单!具体方法如下(此时在flow目录下,需提前安装好anaconda)(参考Flow官方文档:https://flow.readthedocs.io/en/latest/flow_setup.html):

1、pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple(更适合中国宝宝体质)

2、conda env create -f environment.yml

3、pip install -r requirements.txt

4、安装sumo:根据自己的系统运行如下对应的脚本

Ubuntu 14.04:

运行flow/scripts/setup_sumo_ubuntu1404.sh

Ubuntu 16.04:

运行flow/scripts/setup_sumo_ubuntu1604.sh

Ubuntu 18.04:

运行flow/scripts/setup_sumo_ubuntu1804.sh

Mac:

运行flow/scripts/setup_sumo_osx.sh

5、运行如下命令测试sumo是否安装成功:

which sumo

sumo --version

sumo-gui

6、conda activate flow_new2

7、python setup.py develop(此步骤若运行timeout超时可多次运行)

3  Flow拓展的原理以及修改的文件

Flow的本质实际上是通过代码自动化撰写SUMO相关的xml等文件、设定Flow参数,并调用SUMO中的Python接口TraCI从而实现SUMO仿真:前者主要由example/exp_configs里的python脚本实现,后者估摸着是由simulate.py实现。先前在stackoverflow上博主曾询问了Flow是否能实现定制每条道路单独速度的问题(https://stackoverflow.com/questions/56901221/can-we-choose-max-and-min-speeds-for-each-traffic-lane-in-flow-project),得到的回答说是可以直接单独调用TraCI实现该功能,实际上博主尝试后发现暂时还无法介入Flow调用TraCI的层面进行功能实现,但可以从撰写xml文件(即前文所述的衔接)的角度入手进行源码修改从而将SUMO中已有的功能移植到flow中。

本highway_newfunc分支修改的文件如下:

1、flow/environment.yml

2、flow/README.md

3、flow/requirements.txt

4、flow/examples/exp_configs/non_rl/test0205.py

5、flow/core/params.py

6、flow/core/kernel/network/traci.py

7、flow/networks/highway.py

4  highway_newfunc分支的拓展功能

本highway_newfunc分支主要实现的功能如下:

1、改进了Flow安装方法,解决了一个依赖库的版本bug:在conda env create时pip应先被安装再安装其他库。而redis需>=3.3.2才能符合environment.yml中某个库的依赖。(具体哪个忘了……)

2、为VehicleParams类增加了length、width、height和vClass参数,即可在Flow代码中添加车辆时设定该种车辆的长、宽、高、车辆类型,这些设定能在SUMO仿真中直观生效。车辆类型默认为passenger,具体内容选择可在该链接中查询: https://sumo.dlr.de/docs/Definition_of_Vehicles,_Vehicle_Types,_and_Routes.html#abstract_vehicle_class。未来还能为VehicleParams类添加的参数可以参考这个链接:https://sumo.dlr.de/docs/Definition_of_Vehicles,_Vehicle_Types,_and_Routes.html#available_vtype_attributes

3、在highway路网中增加了lane_list参数(其他路网中未添加,若想添加请参考flow/flow/networks/highway.py),即可为每个edge上的每条lane单独设定专门的行驶速度上限、禁行的车辆种类名单。SUMO中的道路(lane)是建立在edge(出发边缘)上的。禁行的车辆种类名单的书写格式为“A”或“A B C 等等”,A的内容需从上文车辆类型的具体内容链接中选取,不同种类由空格隔开。未来还能为每条道路添加的参数可以参考这个链接:https://sumo.dlr.de/docs/Networks/PlainXML.html#lane-specific_definitions

4、为TraCIKernelNetwork类增加了width参数,即可设定整个仿真中的道路宽度,同样能在SUMO仿真中直观生效。未来如果想定制每个edge中每条lane的宽度,方法可以参考3中lane_list的修改方法对源码进行修改。

5、增加了对如上4项功能的示例代码flow/examples/exp_configs/non_rl/test0205.py,也是博主毕业论文的仿真基础……目前未写测试,待问问Flow官方看不看的上这个拓展版本。

6、以上所有拓展内容都是可选的,仿真不设置上述内容一样可跑。

5  运行示例代码

示例代码flow/examples/exp_configs/non_rl/test0205.py的运行方法:

1、新开一个Terminal窗口,切换到flow项目目录下

2、conda deactivate

3、source activate flow_new2

4、export PYTHONPATH="/home/hongyong/.conda/envs/flow_new2/lib/python3.7/site-packages:$PYTHONPATH"

5、python examples/simulate.py test0205 --gen_emission

输出结果:

  1. /home/hongyong/.conda/envs/flow_new2/lib/python3.7/site-packages/numpy/core/fromnumeric.py:3335: RuntimeWarning: Mean of empty slice.
  2. out=out, **kwargs)
  3. /home/hongyong/.conda/envs/flow_new2/lib/python3.7/site-packages/numpy/core/_methods.py:161: RuntimeWarning: invalid value encountered in double_scalars
  4. ret = ret.dtype.type(ret / rcount)
  5. Round 0, return: -0.1
  6. ./data/test0205_20210208-1735491612776949.4998093-0_emission.csv ./data
  7. Average, std returns: -0.1, 0.0
  8. Average, std velocities: nan, nan
  9. Average, std outflows: 2030.4, 0.0
  10. Total time: 220.84119844436646
  11. steps/second: 48.04350433419724

运行生成的文件:

flow/data/test0205.json

flow/data/test0205_20210208-1735491612776949.4998093-0_emission.csv (237.8 MB, 研究Flow仿真的核心所在,但太大了所以git pull前和json文件一起删了)

6  示例代码flow/examples/exp_configs/non_rl/test0205.py分析

先给出全貌:

  1. """Example of an open multi-lane network with human-driven vehicles."""
  2. import traci
  3. from flow.core.kernel.vehicle import KernelVehicle
  4. from flow.core.kernel.vehicle import TraCIVehicle
  5. from flow.core.kernel import Kernel
  6. from flow.core.params import SimParams
  7. from flow.controllers import IDMController, SimLaneChangeController, RLController
  8. from flow.core.params import SumoParams, EnvParams, NetParams, InitialConfig, SumoLaneChangeParams, SumoCarFollowingParams
  9. from flow.core.params import VehicleParams, InFlows
  10. from flow.envs.ring.lane_change_accel import ADDITIONAL_ENV_PARAMS
  11. from flow.networks.highway import HighwayNetwork, ADDITIONAL_NET_PARAMS
  12. from flow.envs import LaneChangeAccelEnv
  13. vehicles = VehicleParams()
  14. vehicles.add(
  15. veh_id="rlcar",# Lincoln MKC 4552*1864*1654 THIS IS TYPE NAME
  16. length = 4.552,
  17. width = 1.864,
  18. height = 1.654,
  19. vClass = "passenger",
  20. #color = "1,0,0",
  21. acceleration_controller=(RLController, {}),
  22. car_following_params=SumoCarFollowingParams(
  23. speed_mode="obey_safe_speed",
  24. max_speed=33.333,
  25. accel=2.6, #Wait changed
  26. decel=4.5,
  27. sigma=0.5,
  28. tau=1.0,
  29. min_gap=2.5,
  30. speed_factor=1.0,
  31. speed_dev=0.1,
  32. impatience=0.5,
  33. car_follow_model="IDM"
  34. ),
  35. lane_change_params=SumoLaneChangeParams(
  36. lane_change_mode="only_speed_gain_safe",# no_lc_safe, Disable all SUMO lane changing but still handle safety checks (collision avoidance and safety-gap enforcement) in the simulation.
  37. model="SL2015",
  38. lc_sublane=2.0,
  39. ),
  40. num_vehicles=0)
  41. vehicles.add(
  42. veh_id="humancar",# Volkswagen LAVIDA 4670*1806*1474 max:120km/h
  43. length = 4.67,
  44. width = 1.806,
  45. height = 1.474,
  46. vClass = "passenger",
  47. #v0 : desirable velocity, in m/s (default: 30) in flow/flow/controllers/car_following_models.py 352
  48. acceleration_controller=(IDMController,{'v0':32}),# desirable velocity 115km/h
  49. car_following_params=SumoCarFollowingParams(
  50. speed_mode="obey_safe_speed", # default
  51. max_speed=33.333,
  52. accel=2.6, #Wait changed
  53. decel=4.5,
  54. sigma=0.5,
  55. tau=1.0,
  56. min_gap=2.5,
  57. speed_factor=1.0,
  58. speed_dev=0.1,
  59. impatience=0.5,
  60. car_follow_model="IDM"
  61. ),
  62. lane_change_params=SumoLaneChangeParams(
  63. lane_change_mode="only_speed_gain_safe",# sumo_default, only_speed_gain_safe, only_strategic_safe, only_cooperative_safe
  64. model="SL2015", # Lane-changing model for sublane-simulation [https://sumo.dlr.de/docs/Definition_of_Vehicles,_Vehicle_Types,_and_Routes.html] [https://sumo.dlr.de/docs/Simulation/SublaneModel.html]
  65. lc_sublane=2.0, # The eagerness for using the configured lateral alignment within the lane. Higher values result in increased willingness to sacrifice speed for alignment. default: 1.0, range [0-inf]
  66. ),
  67. num_vehicles=0)
  68. vehicles.add(
  69. veh_id="humanbus",# YUTONG ZK6826BEV 8245*2500*3240 max:100km/h
  70. length = 8.245,
  71. width = 2.500,
  72. height = 3.240,
  73. vClass = "bus",
  74. color = "1,1,0",
  75. acceleration_controller=(IDMController, {'v0':26.4}),# 95km/h
  76. car_following_params=SumoCarFollowingParams(
  77. speed_mode="obey_safe_speed", # default
  78. max_speed=27.778,
  79. accel=2.6, #Wait changed
  80. decel=4.5,
  81. sigma=0.5,
  82. tau=1.0,
  83. min_gap=2.5,
  84. speed_factor=1.0,
  85. speed_dev=0.1,
  86. impatience=0.5,
  87. car_follow_model="IDM"
  88. ),
  89. lane_change_params=SumoLaneChangeParams(
  90. lane_change_mode="only_speed_gain_safe",
  91. model="SL2015",
  92. lc_sublane=2.0,
  93. ),
  94. num_vehicles=0)
  95. vehicles.add(
  96. veh_id="humantruck",# FOTON BJ5319XXY-AB 12000*2550*3950 max:100km/h
  97. length = 12,
  98. width = 2.550,
  99. height = 3.950,
  100. vClass = "truck",
  101. color = "0,1,0",
  102. acceleration_controller=(IDMController, {'v0':25}),# 90km/h
  103. car_following_params=SumoCarFollowingParams(
  104. speed_mode="obey_safe_speed", # default
  105. max_speed=27.778,
  106. accel=2.6, #Wait changed
  107. decel=4.5,
  108. sigma=0.5,
  109. tau=1.0,
  110. min_gap=2.5,
  111. speed_factor=1.0,
  112. speed_dev=0.1,
  113. impatience=0.5,
  114. car_follow_model="IDM"
  115. ),
  116. lane_change_params=SumoLaneChangeParams(
  117. lane_change_mode="only_speed_gain_safe",
  118. model="SL2015",
  119. lc_sublane=2.0,
  120. ),
  121. num_vehicles=0)
  122. env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)
  123. inflow = InFlows()
  124. inflow.add(
  125. veh_type="rlcar",
  126. edge="highway_0",
  127. #probability=0.025,# 0.25 probability for emitting a vehicle each second (not together with vehsPerHour or period)
  128. vehs_per_hour=120,#250
  129. depart_lane=3,# the index of the lane, starting with rightmost=0
  130. depart_speed=30)
  131. inflow.add(
  132. veh_type="humancar",
  133. edge="highway_0",
  134. #probability=0.85,# 0.25 probability for emitting a vehicle each second (not together with vehsPerHour or period)
  135. vehs_per_hour=2500,#15000
  136. depart_lane="random",#free random allowed best first
  137. depart_speed=30)
  138. inflow.add(
  139. veh_type="humanbus",
  140. edge="highway_0",
  141. #probability=0.1,
  142. vehs_per_hour=486,#486
  143. depart_lane="random",
  144. depart_speed=26.4)
  145. inflow.add(
  146. veh_type="humantruck",
  147. edge="highway_0",
  148. #probability=0.05,
  149. vehs_per_hour=486,#486
  150. depart_lane="random",
  151. depart_speed=25)
  152. flow_params = dict(
  153. # name of the experiment
  154. exp_tag='test0205',
  155. # name of the flow environment the experiment is running on
  156. env_name=LaneChangeAccelEnv,
  157. # name of the network class the experiment is running on
  158. network=HighwayNetwork,
  159. # simulator that is used by the experiment
  160. simulator='traci',
  161. # sumo-related parameters (see flow.core.params.SumoParams)
  162. #sim=SumoParams(
  163. #render=True,
  164. #lateral_resolution=1.0,
  165. #),
  166. sim=SumoParams(
  167. restart_instance=True,
  168. sim_step=0.1, # seconds per simulation step, default
  169. emission_path="./data/",
  170. render=True, # delegate rendering to sumo-gui for back-compatibility(Color)
  171. lateral_resolution=3.75,
  172. sight_radius=120, # sets the radius of observation for RL vehicles (meter)
  173. pxpm=3, # specifies rendering resolution (pixel / meter)
  174. show_radius=True # specifies whether to render the radius of RL observation
  175. #save_render=True # specifies whether to save rendering data to disk
  176. ),
  177. # environment related parameters (see flow.core.params.EnvParams)
  178. env=EnvParams(
  179. horizon=5000, # number of steps per rollouts
  180. additional_params=ADDITIONAL_ENV_PARAMS.copy(),
  181. ),
  182. # network-related parameters (see flow.core.params.NetParams and the
  183. # network's documentation or ADDITIONAL_NET_PARAMS component)
  184. net=NetParams(
  185. inflows=inflow,
  186. #additional_params=ADDITIONAL_NET_PARAMS.copy(),
  187. additional_params={
  188. 'length': 6000,
  189. 'width': 3.75,
  190. 'lanes': 4,# highway_0_0(right) highway_0_3(left)
  191. 'speed_limit': 33.333,
  192. 'num_edges': 1,
  193. # 'lane_list': {}, # must available
  194. 'lane_list': {'0': # edge index
  195. [
  196. {
  197. 'index': '0', # 0(right) n-1(left)
  198. 'speed': '27.778'
  199. },
  200. {
  201. 'index': '1',
  202. 'speed': '27.778'
  203. },
  204. {
  205. 'index': '3',
  206. 'speed': '33.333',
  207. 'disallow': "bus truck"
  208. }
  209. ]
  210. }, #In the order of edges index
  211. "use_ghost_edge": False,
  212. "ghost_speed_limit": 25,
  213. "boundary_cell_length": 500
  214. },
  215. ),
  216. # vehicles to be placed in the network at the start of a rollout (see
  217. # flow.core.params.VehicleParams)
  218. veh=vehicles,
  219. # parameters specifying the positioning of vehicles upon initialization/
  220. # reset (see flow.core.params.InitialConfig)
  221. initial=InitialConfig(
  222. spacing="uniform",
  223. shuffle=True,
  224. ),
  225. )

单步分解可以参考文首的前文链接,里面有非常详细的介绍啦。此处主要介绍与本文拓展有关的部分:

a. 通过VehicleParams类定制每辆车

  1. vehicles.add(
  2. veh_id="humanbus",# YUTONG ZK6826BEV 8245*2500*3240 max:100km/h
  3. length = 8.245,
  4. width = 2.500,
  5. height = 3.240,
  6. vClass = "bus",
  7. color = "1,1,0",
  8. acceleration_controller=(IDMController, {'v0':26.4}),# 95km/h
  9. car_following_params=SumoCarFollowingParams(
  10. speed_mode="obey_safe_speed", # default
  11. max_speed=27.778,
  12. accel=2.6, #Wait changed
  13. decel=4.5,
  14. sigma=0.5,
  15. tau=1.0,
  16. min_gap=2.5,
  17. speed_factor=1.0,
  18. speed_dev=0.1,
  19. impatience=0.5,
  20. car_follow_model="IDM"
  21. ),
  22. lane_change_params=SumoLaneChangeParams(
  23. lane_change_mode="only_speed_gain_safe",
  24. model="SL2015",
  25. lc_sublane=2.0,
  26. ),
  27. num_vehicles=0)

以大客车的添加为例,你可以自由设定它的长、宽、高、车辆种类啦。在SUMO仿真中的效果图如下(即图中的黄色车辆,右侧是其SUMO参数界面):

b.为highway路网增加增加道路宽度参数,并定制每条道路的最高限速、禁行车辆种类名单

  1. net=NetParams(
  2. inflows=inflow,
  3. #additional_params=ADDITIONAL_NET_PARAMS.copy(),
  4. additional_params={
  5. 'length': 6000,
  6. 'width': 3.75,
  7. 'lanes': 4,# highway_0_0(right) highway_0_3(left)
  8. 'speed_limit': 33.333,
  9. 'num_edges': 1,
  10. # 'lane_list': {}, # must available
  11. 'lane_list': {'0': # edge index
  12. [
  13. {
  14. 'index': '0', # 0(right) n-1(left)
  15. 'speed': '27.778'
  16. },
  17. {
  18. 'index': '1',
  19. 'speed': '27.778'
  20. },
  21. {
  22. 'index': '3',
  23. 'speed': '33.333',
  24. 'disallow': "bus truck"
  25. }
  26. ]
  27. }, #In the order of edges index
  28. "use_ghost_edge": False,
  29. "ghost_speed_limit": 25,
  30. "boundary_cell_length": 500
  31. },
  32. ),

可以看到,上述代码中将道路宽度设定为3.75m,通过lane_list为index为0(edge的index可能需要参考源码中读取edge的顺序)的edge上的0、1、3号道路(Flow中0号道路是edge最右侧那条)分别定制了最高限速,并为3号道路定制了禁行bus和truck的规则。在SUMO仿真中的0到3号道路的参数图如下:

建议大家在该示例代码的基础上进行修改开发,如果需要在其他路网(例如下图Flow官方示例中魔幻的minicity)中实现这些功能,如文首所说可以将highway_newfunc分支的代码覆盖至master分支,并用VSCode之类的IDE查看Source Control-CHANGES,从而对其他路网进行类似的修改(主要还是修改flow/flow/networks中的文件)。

欢迎交流讨论!对Flow Project有更深入的疑惑可以在stackoverflow的专题链接中提问(带上flow-project的标签,回的挺快):https://stackoverflow.com/questions/tagged/flow-project

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号