-1

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.

Nicolas
  • 699
  • 1
  • 7
  • 9
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [On topic](http://stackoverflow.com/help/on-topic), [how to ask](http://stackoverflow.com/help/how-to-ask), and [... the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. StackOverflow is not a design, coding, research, or tutorial resource. – Prune Nov 01 '18 at 19:29
  • In particular, you seem to ask us to make up for the lack of sumo/traci documentation and examples. This seems *very* broad and quite off-topic for Stack Overflow. If you have a particular example that needs help, please post a [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve), and someone will probably try to help. – Prune Nov 01 '18 at 19:31
  • I would prefer to have a more general answer, but if you want to give me an answer to only one coding bug, there is this one that I mention in my question: 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() – Nicolas Nov 01 '18 at 20:33
  • 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' – Nicolas Nov 01 '18 at 20:35
  • Please edit such things into you question. As you can see, code and output do *not* format well in comments. Make sure that your code example is complete. – Prune Nov 01 '18 at 20:36

2 Answers2

2

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.

Michael
  • 3,510
  • 1
  • 11
  • 23
0

You could also use the following way to get vehicle ID list

vehicle_id_list = traci.vehicle.getIDList()

And then IDs obtained can be used to get various parameters like

  1. waiting time: traci.vehicle.getAccumulatedWaitingTime(vehicle_id)
  2. road id: traci.vehicle.getRoadID(vehicle_id) and so on.