A Programmer's Note On TraCI - TLS, TraCI, and SUMO
A Programmer's Note On TraCI - TLS, TraCI, and SUMO
Introduction
TraCI_tls is a simple SUMO model developed as part of SUMO’s tutorial on TraCI. It is a perfect starting
point for anyone who is interested in using SUMO and the TraCI tool. After going through the tutorial
material given here, we find that the tutorial may be improved in terms of helping the user to understand
the model from a software’s point of view. The official tutorial also does not provide a complete list of
wiki pages that are essential to help the user to understand the model. Therefore, we provide this study
note to supplement the TraCI_tls tutorial. Source codes of this model is hosted here.
First let’s take a look at the file structure of TraCI_tls:
+-- data
| +-- cross.nod.xml
| +-- cross.edg.xml
| +-- [cross.typ.xml]
| +-- cross.con.xml
| +-- cross.netccfg
| +-- cross.net.xml
| +-- cross.det.xml
| +-- cross.rou.xml
| +-- cross.sumocfg
| +-- cross.out
+-- embedded
| +--
+-- plain
| +--
+-- embedded.py
+-- tripinfo.xml
We will begin by going through the files contained in the data folder.
we illustrate below:
Indeed, from and to are the most essential attributes of a connection. Other attributes are also available for
defining a connection:
connection attributes
According to these attributes, there are a lot you can do with a connection, but let’s concentrate on
TraCI_tls for now.
Further reading on connections is available here .
.+-- data
| +-- cross.netccfg
| +-- cross.net.xml
| +-- cross.rou.xml
| +-- cross.sumocfg
| +-- cross.det.xml
| +-- cross.out
With the 4 checked files, we are now ready to put the nodes, edges, and connections together to construct
a complete “network”. In order to do this, first we need to write a network configuration file
named cross.netccfg:
cross.netccfg
Tip: Note that both cross.netccfg and cross.netc.cfg are acceptable suffixes to SUMO’s GUI. If you are not
using the GUI then this file can have any name as long as you give it correctly to netconvert.
cross.netccfg is very literal with input and output files specified in the first two sections. The verbose option
in the report section controls the verbose output behaviour of netconvert.
On a command line, execute the following command to generate a cross.net.xml file.
netconvert -c cross.netccfg
netconvert is a SUMO binary just like sumo and sumo-gui. If you compiled SUMO from source then this
together with all other SUMO binaries should be in your bin folder, for example in my case this is
the ~/sumo-0.29.0/bin/ folder. If you make install-ed, then these binaries will be located somewhere
like /usr/local/bin/. Issue a which netconvert command to find out where it is.
Tip: The purpose of the cross.netccfg file is to make calling netconvert earlier for the user.
Without cross.netccfg, an equivalent result can be achieved by executing
on command line.
Note: Although it is generated by netconvert automatically, it is worth taking a look at cross.net.xml as it
gives detail on the default behaviours of the netconvert tool. We will see more on this file later on.
Further reading on netconvert is available here.
Network Summary
Let’s summarise the process of generating a network with the following illustration:
+-- data
| +-- cross.rou.xml
| +-- cross.sumocfg
| +-- cross.out
Note that cross.det.xml is not part of the network for a SUMO model. We will see it in action later on.
Defining Traffic
Having constructed the road network in the previous section, the stage is now ready for our actors.
trafficlights.setPhase(string,int) -> None Set the given traffic light to the given phase.
Read more after this table.
Let’s focus on the trafficlights.setPhase method. For a start, what does traci.trafficlights.getPhase("0") == 2 do?
We know that it sets the traffic light at node 0 to phase 2, but what exactly is phase 2? And how is it giving
green to EW traffic? Going through the model files we went through for this model, our only definition
relevant to this traffic light is <node id="0" x="0.0" y="0.0" type="traffic_light"/> in cross.nod.xml. Therefore
the phase data of this junction must have a default behaviour, but what is the default behaviour of this
type of function? The answer to this question lies in the cross.net.xml file which includes the following
definition (tlLogic) for this traffic light:
</tlLogic>
This teels us that phase 2 is <phase duration="31" state="rGrG"/>. From this, we understand that each
character in the state attribute corresponds to a positive number of connections (as put in the official
documentation: “a one-to-n dependency between signals and links is implemented, this means each
signal may control more than a single link - though networks generated by NETCONVERT or
NETGENERATE usually use one signal per link”, with “link” meaning “connection”). We know that
we have 4 connections defined in cross.con.xml and they corresponds to the 4 characters in
the state attribute of each phase.
The next question is which character corresponds to which connection? Again we need to
consult cross.net.xml:
<connection from="1i" to="2o" fromLane="0" toLane="0" via=":0_3_0" tl="0" linkIndex="3" dir="s" state="o"/>
<connection from="2i" to="1o" fromLane="0" toLane="0" via=":0_1_0" tl="0" linkIndex="1" dir="s" state="o"/>
...<connection from="3i" to="4o" fromLane="0" toLane="0" via=":0_2_0" tl="0" linkIndex="2" dir="s" state="o"/>
...<connection from="4i" to="3o" fromLane="0" toLane="0" via=":0_0_0" tl="0" linkIndex="0" dir="s" state="o"/>
The linkIndex attributes of these 4 connections give us their order in the state string, meaning for
example phase 0 is:
1. the connection from “4i” to “3o” is “r”
2. the connection from “3i” to “4o” is “G”
3. the connection from “2i” to “1o” is “r”
4. the connection from “1i” to “2o” is “G”
We illustrate the use of linkIndex as below:
getLastStepVehicleNumber(self, loopID)
Boilerplates of runner.py
Thirdly, let’s look at the header section of runner.py:
header of runner.py
The try block tries to locate and include the TraCI python module traci and sumolib from
the $SUMO_HOME/tools directory.
Finally, once import has been completed, the script triggers the sumo model and then execute
the run() function to establish the connection with the following code section:
entry point of runner.py
Here, we that after the binary has been located, routes and vehicles are generated by generate_routefile(),
then a sumo server is started and a connection is established between the model and TraCI, before finally
the controls are executed with the run() function. We also see the use of cross.sumocfg when the sumo
binary is called.
cross.sumocfg
cross.sumocfg is similar to cross.netccfg in functionalities. It provides the parameters for the sumo/sumo-
gui binary rather than to the netconvert tool which is aided by cross.netccfg.