如何在 python 中使用 traci 和 sumo?

How to use traci and sumo in python?

我正在尝试模拟带有交通灯和探测器的十字路口,并训练机器学习分类器使用来自探测器的信息来设置交通灯相位。

我能够运行模拟使用:

import traci
traci.start(sumoCmd) 
step = 0
while step < 1000:
    traci.simulationStep()
    step += 1
traci.close()

但是,我不知道如何获取有关汽车的信息。我有 e2 探测器,但我不知道如何使用它们的输出。我不明白 traci 和 sumo 文档。

我试过这段代码:

import traci
traci.start(sumoCmd) 
step = 0
lanearea = traci._lanearea.LaneAreaDomain()
detlist = lanearea.getIDList()
while step < 1000:
    traci.simulationStep()
    print([lanearea.getLastStepVehicleNumber(det) for det in detlist])
    step += 1
traci.close()

但它不起作用。我收到这个错误

detlist = lanearea.getIDList()
return self._getUniversal(tc.ID_LIST, "")
result = self._connection._sendReadOneStringCmd(self._cmdGetID, varID, objectID)
AttributeError: 'NoneType' object has no attribute '_sendReadOneStringCmd'

谁能告诉我如何修复这段代码?或者更一般地说,如果有人知道如何使用以下任何功能:http://sumo.dlr.de/wiki/TraCI/Lane_Area_Detector_Value_Retrieval 或任何其他获取汽车信息的方法。

不需要自己实例化lanearea。只需使用 traci.lanearea.getIDList()traci.lanearea.getLastStepVehicleNumber(det) 所以您的程序应该如下所示:

import traci
traci.start(sumoCmd) 
step = 0
detlist = traci.lanearea.getIDList()
while step < 1000:
    traci.simulationStep()
    print([traci.lanearea.getLastStepVehicleNumber(det) for det in detlist])
    step += 1

也不需要关闭。

您也可以通过以下方式获取车辆ID列表

vehicle_id_list = traci.vehicle.getIDList()

然后获取的ID可以用来获取各种参数,比如

  1. 等待时间:traci.vehicle.getAccumulatedWaitingTime(vehicle_id)
  2. 道路编号:traci.vehicle.getRoadID(vehicle_id) 等等。