原文内容见链接: SUMO FAQ
TraCI 通过套接字进行通信,这种通信速度很慢。 您通常可以通过以下策略减少 TraCI 命令的数量。
- 存储不会改变的结果(即车辆长度)而不是重复检索它们 - 使用
subscriptions
或者context subscriptions
来减少每一步中你需要的东西的“获取”命令的数量。
经过测试,确实用subscribeContext
效率高了很多,以下是订阅一个交叉口车辆信息的示例代码。
sumo_cmd = ["sumo-gui", "-c", "../path/sim.sumocfg"]
traci.start(sumo_cmd, port=9009)
# pick an arbitrary junction
junctionID = traci.junction.getIDList()[0]
# subscribe around that junction with a sufficiently large
# radius to retrieve the speeds of all vehicles in every step
traci.junction.subscribeContext(
junctionID, tc.CMD_GET_VEHICLE_VARIABLE, 1000000,
[tc.VAR_SPEED, tc.VAR_ANGLE, tc.VAR_POSITION,
tc.VAR_STOPSTATE, tc.VAR_POSITION, tc.VAR_TYPE]
)
while traci.simulation.getMinExpectedNumber() > 0 and step_counts < 6000:
traci.simulationStep()
scResults = traci.junction.getContextSubscriptionResults(junctionID)
# scResults是一个key为车辆id,值为订阅数据的dict。对scResults进行遍历即可获取订阅数据
if scResults:
step_counts += 1
- 切换到
libsumo
可以获得更大的收益。 这可以用一行代码完成,并完全消除慢速套接字通信。