TraCI/Interfacing TraCI from Python

The traci commands are split into the 13 domains gui, lane, poi, simulation, trafficlights, vehicletype, edge, inductionloop, junction, multientryexit, polygon, route, and vehicle, which correspond to individual modules. For a detailed list of available functions see the pydoc generated documentation.

importing traci in a script
To use the library, the /tools directory must be on the python load path. This is typically done with a stanza like this:

import os, sys 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'")

First Steps
In general it is very easy to interface with SUMO from Python (the following example is a modification of tutorial/traci_tls):

import traci PORT = 8813 traci.init(PORT) step = 0 while step < 1000: traci.simulationStep no = traci.inductionloop.getLastStepVehicleNumber("0") traci.trafficlights.setRedYellowGreenState("0", "GrGr") step += 1 traci.close

After opening the connection to the port which was given to sumo as the --remote-port Option, you can emit various commands and execute simulation steps until you want to finish by closing the connection.

Subscriptions
Subscriptions can be thought of as a batch mode for retrieving variables. Instead of asking for the same variables over and over again, you can retrieve the values of interest automatically after each time step. TraCI subscriptions are handled on a per module basis. That is you can ask for the module for the result of all current subscriptions after each time step. In order to subscribe for variables you need to know their variable ids which can be looked up in the traci/constants.py file.

import traci import traci.constants as tc PORT = 8813 traci.init(PORT) traci.vehicle.subscribe(vehID, (tc.VAR_ROAD_ID, tc.VAR_LANEPOSITION)) print traci.vehicle.getSubscriptionResults(vehID) for step in range(3): print "step", step traci.simulationStep print traci.vehicle.getSubscriptionResults(vehID) traci.close

The values retrieved are always the ones from the last time step, it is not possible to retrieve older values.

Embedded Python
As an experimental feature, it is also possible to link SUMO with python directly and have the scripts executed in SUMO. The syntax is completely the same, except that you leave out the calls to init and close and you need to start sumo with the option --python-script. This feature does currently not work with the GUI version of sumo.

Since the feature is not well tested yet, you need to enable embedded python explicitly when building SUMO (it is not enabled in the release versions and the nightly build). In order to do so, follow the instructions below

Linux

 * install the python devel package files
 * call configure using the --with-python option
 * make && make install as usual

Windows

 * make sure python is installed and is in your PATH
 * call tools/build/pythonPropsMSVC.py to generate a python.props file
 * enable the inclusion of python.props by uncommenting the relevant line in build/msvc10/Win32.props
 * build the Win32 Release version as usual
 * the debug build is somewhat more involved (taken from here)
 * download the python source package fitting your python version
 * open the pcbuild.sln in the PCbuild subdir with Visual Studio
 * do the Win32 Debug build for python, it will have lots of errors but the main parts (hopefully) succeed
 * from the PCbuild dir copy
 * python27_d.dll to the Python dir (something like C:\Python27)
 * python27_d.lib, python27_d.pdb, python27_d.exp to the libs dir (C:\Python27\libs)
 * every *_d.pyd to the DLLs dir (C:\Python27\DLLs)
 * now you can do the Win32 Debug build for SUMO

Earlier versions of Visual Studio and 64bit build are currently not directly supported (but the interested programmer should be able to modify the files accordingly).

Pitfalls and Solutions

 * Note that strings, if exchanged, have to be ASCII-encoded.
 * If you start sumo from within your python script using subprocess.Popen, be sure to call wait on the resulting process object before quitting your script. You might loose output otherwise.