How To Simulate Traffic On Urban Networks Using SUMO | by Skanda Vivek | Towards Data Science

Understanding, predicting, and ultimately — reducing traffic congestion in urban networks is a complex problem. Even understanding the emergence of traffic congestion in the most simple case — a single lane road, is challenging. The Simulation of Urban Mobility (SUMO) platform is an open source platform that enables simulation of traffic flows in complex environments. But apart from the SUMO documentation, a few Stack Overflow posts, and some YouTube videos, there aren’t many tutorials I’ve come across that teach you how to create a complex traffic simulation from start to finish. This article is going to do this exact thing, using a case-study of traffic on grid networks. The article is structured as follows:
Why study traffic in urban networks?Getting Started with SUMOSimulating traffic in grid networksAnalysis of key traffic performance metricsFuture directions in simulating realistic traffic
Why study traffic in urban networks?
Getting Started with SUMO
Simulating traffic in grid networks
Analysis of key traffic performance metrics
Future directions in simulating realistic traffic
In a previous article, I discuss the first paper that conclusively showed how traffic “phantom” shock-waves emerge from nothing, except driver interactions.
Recent research suggests that streamlined interactions between autonomous vehicles could potentially reduce traffic jams for artificial scenarios like vehicles driving in a circle. But what happens when you have multiple roads, as in typical urban road networks? Interestingly, just increasing capacity through more lanes or longer roads might not work as well as you thing, in road networks. In another article I show how “Braess’s Paradox” leads to the unusual result, that increasing the number of roads in an urban network, could make traffic worse!
Before engaging in intensive infrastructure projects such as building new roads, adding lanes, traffic lights, etc. — it is important to have realistic simulations of traffic flows so that the proposed projects have a best chance of succeeding in alleviating traffic. The situation gets more complicated when integrating major events such as concerts, sporting events, etc. or public buildings like airports and hospitals into the picture. In the near future, it’s important to simulate the effects of proposed connected vehicle and intelligent transportation technological innovations, to best realize their potential in streamlining traffic flows.
Traffic simulations seem to belong to a niche community of traffic flow researchers, or engineering contracting companies. For example, Anylogic, VISSIM, and Aimsun are companies that offer traffic and mobility modeling solutions. However, SUMO is open access and is fairly easy to get started in.
There are multiple ways to install SUMO, but I prefer the pip install way, that installs SUMO as well as the python libraries to interface with SUMO.
python -m pip install sumo
That’s it! Now let’s get to creating your first simulation of traffic flow on a network!
In urban planning, grid road networks are pretty common. In SUMO, we setup a 5x5 grid with each road of length 200m, and 3 lanes, as below:
netgenerate — grid — grid.number=5 -L=3 — grid.length=200 — output-file=grid.net.xml
Next, we use randomTrips.py located in the tools folder within the SUMO home directory (sumo -> tools), to generate random trips for a certain number of vehicles (200 vehicles in the example below). The begin and end times denote the times during which the vehicles enter the simulation. I’ve chosen 0&1, meaning all vehicles enter the simulation in the first 1 second of the simulation. Period denotes the arrival rate of vehicles.
randomTrips.py -n grid.net.xml -o flows.xml — begin 0 — end 1 — period 1 — flows 200
Next, we generate the routes taken by individual vehicles using SUMO’s jtrrouter, between times 0 to 10000.
jtrrouter — flow-files=flows.xml — net-file=grid.net.xml — output-file=grid.rou.xml — begin 0 — end 10000 — accept-all-destinations
Finally, for simplicity we want to maintain a constant density. The most obvious way to do this is by vehicles driving randomly, and not exiting the simulation. For this, we use the Manhattan traffic model, where vehicles encountering an intersection choose to either go straight, left, or right based on set probabilities. By default in SUMO, vehicles exit the simulation once they reach their destination. However, SUMO has an implementation of the Manhattan model, using a continuous rerouter python script.
generateContinuousRerouters.py -n grid.net.xml — end 10000 -o rerouter.add.xml
Next, we create a sumo config file, in order to run the simulation in SUMO, which is basically a .xml file with certain attributes, containing the names of the network file, route file, and additional rerouting file for vehicles to stay in the simulation until the simulation is completed. We define an output file, to store the detailed vehicle information during the traffic simulation.
<configuration>  <input>    <net-file value=”grid.net.xml”/>    <route-files value=”grid.rou.xml”/>    <additional-files value=”rerouter.add.xml”/>  </input>  <time>    <begin value=”0"/>    <end value=”10000"/>  </time>   <output>     <fcd-output value=”grid.output.xml”/>   </output></configuration>
Finally, we run the simulation in the terminal as below. Period denotes the time interval at which data is saved — 100 denotes saving vehicular information i.e. speed and position, every 100 timesteps.
sumo-gui -c grid.sumocfg — device.fcd.period 100
Running this pops up the SUMO GUI, where you can see the entire simulation!
Vehicle colors indicate their speeds from slowest (red) to fastest (green).
It is quite annoying to manually go and change every parameter and output file by hand. This adds up if you have 100+ runs for exploring how traffic changes with different parameters such as the number of vehicles, and multiple runs for statistical averaging.
For this, I interfaced SUMO on the command prompt with python using the OS module:
The SUMO xml outputs contain information on individual vehicle time, position, and velocity at every time step. I want to analyze how velocity depends on density, or number of vehicles in the simulation — basically obtain velocity at each time step, averaging over all vehicles in the simulation.
Finally, I plot velocity vs density where each simulation run has an individual density.
As you can see, as density increases, velocity decreases. This is because the more the vehicles in the simulation, the more the congestion, resulting in lower travel speeds. This is something all of us have seen on the highway, especially during rush hour.
Another commonly used parameter in traffic simulations is flux. As I discuss in a previous article, flux measures the number of vehicles per time that pass through a given point, and is a measure of vehicle throughput. Flux is given as:
Where the summation is over all vehicle velocities within a length L, and number of lanes=n_l.
At low densities, every vehicle is basically traveling at the speed limit so flux increases linearly with density (red line in the plot below). However, at higher densities, vehicles can’t travel at the speed limit and at a certain point, the effect of a larger number of vehicles is countered by each vehicle traveling at a smaller speed, resulting in a reduced flux. Above a characteristic density (~0.1–0.2 in this simulation), traffic jams emerge and flux decays with density.
I’ve shown how to set up basic traffic simulations and ensemble runs in representative grid networks using SUMO+Python. However, this is by no means a comprehensive simulation on urban networks. In a recent study leveraging OpenStreetMap urban street network data and OSMnx, it was found that some cities feature prominent grid like networks, whereas some other’s don’t. SUMO includes the ability to simulate traffic in urban street networks, by converting OpenStreetMap data into SUMO .net files.
But apart from simulations on realistic city networks, there is also the issue of calibrating these traffic simulations to match daily traffic patterns. To match detailed traffic patterns that are seen in daily life, one needs to account for the ebbs and flows in people entering roads — where they enter, when they enter, and when/where they leave. This becomes an extremely complex problem as it is not possible to know every vehicle trajectory. Many vehicles nowadays transmit GPS data to companies like INRIX and HERE technologies. Companies like Google and Apple make use of cell-phone data for sparse location and speed information. However, this can only provide information on a small sample of the entire population. Traffic flow is a highly non — linear problem; meaning small changes can have extreme consequences.
At the same time, you want your traffic simulation results to be robust to initial conditions. The results should clearly indicate whether or not a proposed project has a significant traffic flow improvement or not — for a broad range of scenarios. It is a challenge to have detailed large-scale traffic simulations, that run in a feasible amount of time, and are realistic.
On the SUMO page, there’s only a handful of such realistic scenarios. The top of the page reads:
Building a scenario is a lot of work. If you have built a SUMO scenario you can share (under a sufficiently open license), please contact us.
Hopefully the democratization of traffic mobility data, increasing availability of computational resources, and open-source traffic modeling platforms will make these large-scale simulations more accessible. Urban traffic is a wonderfully complex game that provides insights into breathing, living cities.
The code for this project is available on GitHub:
github.com
References:
R. E. Stern, et al. “Dissipation of stop-and-go waves via control of autonomous vehicles: Field experiments,” arXiv (2017).G. Boeing, “Urban spatial order: street network orientation, configuration, and entropy,” Applied Network Science (2019).P. L. Alvarez, et al. “Microscopic traffic simulation using SUMO,” International Conference on Intelligent Transportation Systems. IEEE (2018).
R. E. Stern, et al. “Dissipation of stop-and-go waves via control of autonomous vehicles: Field experiments,” arXiv (2017).
G. Boeing, “Urban spatial order: street network orientation, configuration, and entropy,” Applied Network Science (2019).
P. L. Alvarez, et al. “Microscopic traffic simulation using SUMO,” International Conference on Intelligent Transportation Systems. IEEE (2018).
Follow me if you liked this article.
Subscribe to my newsletter if you enjoy holistic perspectives on the interconnections between technology and modern societies.