Search code examples
pythonsumo

How to use traci and sumo in python?


I am trying to simulate an intersection with traffic light and detectors and train a machine learning classifier to use the information from the detectors to set the traffic light phase.

I am able to run the simulation using:

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

However, I do not know how to get information about cars. I have e2 detectors, but I don't know how to use their output. I don't understand traci and sumo documentation.

I tried this code:

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()

but it does not work. I am getting this error

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

Can anyone tell me how to fix this code? Or more generally, if anyone knows it, how to use any function in: http://sumo.dlr.de/wiki/TraCI/Lane_Area_Detector_Value_Retrieval or any other ways to get information about cars.


Solution

  • There is no need to instantiate the lanearea yourself. Just use traci.lanearea.getIDList() and traci.lanearea.getLastStepVehicleNumber(det) so your program should look like:

    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
    

    Also the close is not necessary.