2. 基于随机出发时间的交通流生成
任务说明
本案例在单交叉口路网上,使用SUMO自带的randomTrips.py脚本生成随机车辆出行需求,运行SUMO仿真,并通过SUMO-GUI观察车辆在路网中的行驶轨迹。完成本案例后,你将掌握从路网文件到随机交通流仿真输出的完整操作链路。
路网准备
首先创建单交叉口路网文件cross.net.xml。该路网包含四条道路(东、南、西、北),每条道路为单向双车道,交汇于中心交叉口。使用SUMO自带的netedit或直接编写XML均可,此处提供可直接使用的路网定义:
<?xml version="1.0" encoding="UTF-8"?>
<!-- cross.net.xml -->
<net version="1.6" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/net_file.xsd">
<location netOffset="0.00,0.00" convBoundary="0.00,0.00,400.00,400.00" origBoundary="10000000000.00,10000000000.00,10000000000.00,10000000000.00" projParameter="!"/>
<edge id="E0" from="0" to="1" priority="1" numLanes="2" speed="13.89">
<lane id="E0_0" index="0" allow="all" speed="13.89" length="200.00" shape="0.00,-3.20 200.00,-3.20"/>
<lane id="E0_1" index="1" allow="all" speed="13.89" length="200.00" shape="0.00,3.20 200.00,3.20"/>
</edge>
<edge id="E1" from="1" to="2" priority="1" numLanes="2" speed="13.89">
<lane id="E1_0" index="0" allow="all" speed="13.89" length="200.00" shape="200.00,-3.20 400.00,-3.20"/>
<lane id="E1_1" index="1" allow="all" speed="13.89" length="200.00" shape="200.00,3.20 400.00,3.20"/>
</edge>
<edge id="E2" from="2" to="3" priority="1" numLanes="2" speed="13.89">
<lane id="E2_0" index="0" allow="all" speed="13.89" length="200.00" shape="200.00,196.80 200.00,396.80"/>
<lane id="E2_1" index="1" allow="all" speed="13.89" length="200.00" shape="200.00,203.20 200.00,403.20"/>
</edge>
<edge id="E3" from="3" to="0" priority="1" numLanes="2" speed="13.89">
<lane id="E3_0" index="0" allow="all" speed="13.89" length="200.00" shape="200.00,-203.20 200.00,-3.20"/>
<lane id="E3_1" index="1" allow="all" speed="13.89" length="200.00" shape="200.00,-196.80 200.00,3.20"/>
</edge>
<junction id="0" x="0.00" y="0.00" type="dead_end"/>
<junction id="1" x="200.00" y="0.00" type="traffic_light">
<request index="0" response="0000" foes="0000" cont="0"/>
<request index="1" response="0000" foes="0000" cont="0"/>
<request index="2" response="0000" foes="0000" cont="0"/>
<request index="3" response="0000" foes="0000" cont="0"/>
</junction>
<junction id="2" x="400.00" y="0.00" type="dead_end"/>
<junction id="3" x="200.00" y="200.00" type="dead_end"/>
</net>
路网拓扑为:节点0(西端)→ 节点1(交叉口)→ 节点2(东端),节点3(北端)→ 节点1,节点1 → 节点0(南向出口)。实际形成西→东、北→南两条通行方向,交叉口由信号灯控制。
生成随机交通流
使用SUMO工具集中的randomTrips.py生成随机出行需求。该脚本位于SUMO安装目录的tools/文件夹下。执行以下命令生成100辆车的随机行程:
python "C:\Program Files (x86)\Eclipse\Sumo\tools\randomTrips.py" -n cross.net.xml -r flows.rou.xml -e 100 -p 1 --random
参数说明:-n指定路网文件;-r指定输出路径文件;-e 100表示仿真结束时间为100秒;-p 1表示每1秒生成一辆车;--random启用随机种子。执行后生成flows.rou.xml,内容包含车辆的出发时间、起点和终点边。
SUMO仿真配置
创建SUMO仿真配置文件cross.sumocfg,将路网与车辆路径关联:
<?xml version="1.0" encoding="UTF-8"?>
<!-- cross.sumocfg -->
<configuration>
<input>
<net-file value="cross.net.xml"/>
<route-files value="flows.rou.xml"/>
</input>
<time>
<begin value="0"/>
<end value="120"/>
</time>
<processing>
<ignore-route-errors value="false"/>
</processing>
</configuration>
运行仿真并观察轨迹
在命令行启动SUMO-GUI进行可视化仿真:
sumo-gui cross.sumocfg
仿真启动后,点击运行按钮(或按Ctrl+D)开始仿真。在GUI窗口中,你可以看到车辆从路网边界进入,沿道路行驶至交叉口,根据信号灯状态决定是否通行,最终驶出路网。通过以下方式观察轨迹:
- 在GUI中点击车辆,可高亮显示该车的行驶路径;
- 开启“车辆轨迹”图层(菜单栏
Settings → Edit Visualisation → Vehicles),可显示车辆历史轨迹线; - 仿真结束后,可在输出文件中查看每辆车的完整轨迹记录。
验证方法
完成上述步骤后,按以下标准验证案例是否成功:
- 文件生成验证:确认
flows.rou.xml已生成,且文件内包含约100个<trip>元素,每个元素具有depart属性和from/to边属性; - 仿真运行验证:SUMO-GUI启动后无报错,仿真能持续运行至120秒结束;
- 轨迹观察验证:在GUI中能看到车辆从起点边出发,经过交叉口节点1,最终到达终点边并消失;车辆在交叉口处会根据信号灯相位停车或通行;
- 无路线错误:仿真过程中控制台不输出
Error: Vehicle ... has no route类错误信息。
若以上条件均满足,则说明随机交通流生成与仿真观察任务完成。若出现路线错误,请检查路网文件中边的连接关系是否与flows.rou.xml中生成的起终点边一致。