sumo中设置多车辆类型、车型分配比例、跟车换道模型(rou.xml、python)
1.rou文件修改,记事本打开修改,参数解释在第二部分:
<routes>
<vType id="CAV" accel="2" decel="2" sigma="1" length="5" mingap="2" maxSpeed="70" lcCooperative="0" tau="0.9" carFollowModel="CACC" laneChangeModel="LC2013" color="0,255,0" probability="0.1"/>
<vType id="HV" accel="1.5" decel="1.5" sigma="0.5" length="5" maxSpeed="50" lcCooperative="0.5" tau="1.8" carFollowModel="IDM" laneChangeModel="LC2013" color="250,0,250" probability="0.9"/>
<vTypeDistribution id="typedist1" vTypes="CAV HV"/>
<flow id="100" begin="0" end= "150000" vehsPerHour='1' type="typedist1" departLane="random" departSpeed="random">
<route edges="E0 -E8"/>
</flow>
</routes>
2.python写入rou文件、python设置检测器输出文件格式
import os
import sys
import optparse
import random
import pandas as pd
import json
from collections import OrderedDict
from sumolib import checkBinary # noqa
import traci # noqa
import math
import xml.dom.minidom as minidom
import sumolib
import subprocess
import xml.etree.ElementTree as xee
import matplotlib.pyplot as plt
import time
from scipy.optimize import minimize
import scipy
import numpy as np
import xml.etree.ElementTree as ET
pd.set_option('expand_frame_repr', False) # 禁止换行
# pd.set_option('display.max_columns', 20) # 显示所有行
pd.set_option('display.max_rows', 10) # 设置Dataframe数据的显示长度,默认为50
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
else:
sys.exit("please declare environment variable 'SUMO_HOME'")
def get_options():
optParser = optparse.OptionParser()
optParser.add_option("--nogui", action="store_true",
default=False, help="run the commandline version of sumo")
options, args = optParser.parse_args()
return options
if__show__gui = True
if not if__show__gui:
sumoBinary = checkBinary('sumo')
else:
sumoBinary = checkBinary('sumo-gui')
def generate_routefile():
random.seed(42) # make tests reproducible
# 司机完美驾驶范围(0,1)sigma="0.5",最小跟车车头时距、:tau 换道合作系数:lcCooperative
# 车型占比 probability
with open("p/100/ca00/wanquan.rou.xml", "w") as routes:
print("""<routes>
<vType id="CAV_cacc" accel="2" decel="2" sigma="1" length="5" mingap="2" maxSpeed="70" lcCooperative="0" tau="0.8" carFollowModel="CACC" laneChangeModel="LC2013" color="0,255,0" probability="0.0"/>
<vType id="CAV_acc" accel="2" decel="2" sigma="1" length="5" mingap="2" maxSpeed="70" lcCooperative="0" tau="0.9" carFollowModel="ACC" laneChangeModel="LC2013" color="250,255,0" probability="1"/>
<vType id="HV_car" accel="2" decel="2" sigma="0.5" length="5" maxSpeed="50" lcCooperative="0.5" tau="1.8" carFollowModel="IDM" laneChangeModel="LC2013" color="250,0,250" probability="0.0"/>
<vTypeDistribution id="typedist1" vTypes="CAV_cacc CAV_acc HV_car"/>
<flow id="100" begin="0" end= "150000" vehsPerHour='1' type="typedist1" departLane="random" departSpeed="random">
<route edges="E0 -E8"/>
</flow>
</routes>""", file=routes)
def run(): # 所需要进行的操作就放在这里面
simulationSteps = 0
while simulationSteps < 6000: # 仿真
if simulationSteps > 1000:
traci.simulationStep() # 延时
flow1 = traci.inductionloop.getLastStepVehicleNumber('E1_1')
flow2 = traci.inductionloop.getLastStepVehicleNumber('E1_2')
# mean_jl23 = traci.lanearea.getJamLengthVehicle('E2_3') # 排队长度
# mean_occ23 = traci.lanearea.getLastStepOccupancy('E2_3') # 占有率|密度
# mean_spend23 = traci.lanearea.getLastStepMeanSpeed('E2_3') # 平均车速
# v_number23 = traci.lanearea.getLastStepVehicleNumber('E2_1') # 车辆数
# meanHaltingDuration23 = traci.lanearea.getLastStepHaltingNumber('E2_3')# 平均停车次数
# list_E2 = {'仿真时间': simulationSteps, '车道1流量': flow1, '车道2流量': flow2,'车道3流量': flow3,'车道1平均排队车辆数': mean_jl, '平均占有率': mean_occ,
# '平均车速': mean_spend, '车辆数': v_number, '平均停车次数': meanHaltingDuration}
# print(1)
#
# dflist.append(list_E2)
# pd.DataFrame(dflist).to_csv("仿真输出.csv", encoding="utf-8-sig")
simulationSteps += 1
traci.close() # 仿真结束 关闭TraCI
if __name__ == "__main__":
generate_routefile()
print("simulation start:")
options = get_options()
sumocfg_file = "wanquan.sumocfg" # 这里输入仿真的cfg文件
traci.start([sumoBinary, "-c", sumocfg_file]) # 这里通过traci接口启动仿真程序
# # logic = traci.trafficlight.getAllProgramLogics("J12") # 获取信号灯J12控制方案
# print(logic)
run()
更多sumo学习可参考gitee:1.sumo/SUMO中的车辆类型跟驰换道模型设置.md · itsncut/交通仿真技术 – 码云 – 开源中国 (gitee.com)2.sumo · itsncut/交通仿真技术 – 码云 – 开源中国 (gitee.com)
作者:真心白菜