License: CC BY 4.0
arXiv:2309.17114v2 [eess.SY] 21 Dec 2023

UXsim: An open source macroscopic and mesoscopic traffic simulator in Python—a technical overview

Toru Seo111Tokyo Institute of Technology; [email protected]; The corresponding author
(December 21, 2023)
Abstract

This note describes a technical overview of UXsim, an open source macro/mesoscopic traffic simulator in pure Python programming language. UXsim is based on Kinematic Wave model (more specifically, mesoscopic version of Newell’s simplified car-following model) and dynamic user optimum-like route choice principle, which are well established methodology in the transportation research field. It can compute dynamical network traffic flow and have basic visualization and analysis capability. Furthermore, users can implement their own models and control methods into the simulator by using Python, thanks to the flexibility of the language. The simulator and its codes are freely available at https://github.com/toruseo/UXsim under the MIT license.

1 Introduction

Vehicular traffic flow plays essential roles in today’s civilization. However, it faces several critical issues such as congestion, accidents, and environmental burden. Macroscopic traffic simulation is important to understand and manage dynamic urban-scale vehicular traffic flow. Mesoscopic traffic simulation is a type of macroscopic traffic simulation, in which some microscopic nature of traffic is incorporated in order to enhance simulation capability while keeping computational efficiency high.

UXsim is a new open source macroscopic and mesoscopic traffic simulator developed by the author. It is written in pure Python and its common libraries. Thus, it can be used and modified flexibly by users. Although Python’s computational efficiency is not so high, UXsim can compute large-scale traffic phenomena fairly effectively thanks to the use of the macro/mesoscopic model.

This note describes a technical overview of UXsim, namely, its simulation logic with simple examples. The details are throughly explained in Seo (2023), a Japanese book on general macroscopic traffic flow theory and simulation. The main functions of UXsim are as follows:

  • Dynamic network traffic simulation with a given network and time-dependent OD demand.

  • Implementation of traffic management schemes (e.g., traffic signals, inflow control, route guidance, congestion pricing).

  • Basic analysis of simulation results (e.g., trip completion rate, total travel time, delay), and their export to pandas.DataFrame and CSV files.

  • Visualization of simulation results (e.g., time-space diagram, MFD, network traffic animation).

Just for information, the origin of the name “UXsim” is as follows. “U” stands for uroboros, a mythical snake that embodies some essence of network traffic congestion. “X” means the position coordinates, which is the most important state variable in the employed mesoscopic traffic flow model. “sim” signifies, of course, simulation. UXsim and its codes are freely available at https://github.com/toruseo/UXsim under the MIT license.

2 Simulation logic

2.1 Models

2.1.1 Overview

The simulator is based on Kinematic Wave (KW) model (Lighthill and Whitham, 1955; Richards, 1956) and dynamic user optimum-like route choice principle, which are well established and common methodology in the transportation research field. More specifically, it is constructed by combining the following models.

Link/Vehicle model determines traffic dynamics on links. The mesoscopic version of Newell’s simplified car-following model (Newell, 2002), also known as X-model (Laval and Leclercq, 2013) is employed for this purpose. This is equivalent to KW model with a triangular fundamental diagram. In this model, vehicles travels as fast as possible, while maintaining safe spacing and headway which are speed-dependent. Thus, it can reproduce traffic congestion and queuing phenomena fairly accurately.

Node model determines inter-link transfer of traffic. The mesoscopic version of the incremental node model (Flötteröd and Rohde, 2011; Flötteröd, 2016) is employed for this purpose. This model can reproduce merging and diverging traffic in a manner consistent with KW model.

Route choice model determines network-level decision of travelers. Dynamic user optimum (Kuwahara and Akamatsu, 2001), also known as the reactive assignment, with stochasticity and delay is employed for this purpose. In this model, travelers tend to choose the shortest path to the destination that minimizes instantaneous travel time. However, instantaneous travel time is highly volatile, so a kind of inertia is added to traveler’s decision making process by incorporating stochasticity and delay.

2.1.2 Details

For reference, important model formula are presented in this section. Users do not necessarily need to understand these contents to use UXsim, but understanding on them would be useful for advanced usage and customization of UXsim. For further details, please refer to Seo (2023) or the original articles mentioned in Section 2.1.1.

Regarding the Link/Vehicle model, the driving behavior of a platoon consists of ΔnΔ𝑛\Delta nroman_Δ italic_n vehicles in a link is expressed as

X(t+Δt,n)=min{X(t,n)+uΔt,X(t+ΔtτΔn,nΔn)δΔn},𝑋𝑡Δ𝑡𝑛𝑋𝑡𝑛𝑢Δ𝑡𝑋𝑡Δ𝑡𝜏Δ𝑛𝑛Δ𝑛𝛿Δ𝑛\displaystyle X(t+\Delta t,n)=\min\left\{\begin{array}[]{l}X(t,n)+u\Delta t,\\ X(t+\Delta t-\tau\Delta n,n-\Delta n)-\delta\Delta n\end{array}\right\},italic_X ( italic_t + roman_Δ italic_t , italic_n ) = roman_min { start_ARRAY start_ROW start_CELL italic_X ( italic_t , italic_n ) + italic_u roman_Δ italic_t , end_CELL end_ROW start_ROW start_CELL italic_X ( italic_t + roman_Δ italic_t - italic_τ roman_Δ italic_n , italic_n - roman_Δ italic_n ) - italic_δ roman_Δ italic_n end_CELL end_ROW end_ARRAY } , (3)

where X(t,n)𝑋𝑡𝑛X(t,n)italic_X ( italic_t , italic_n ) denotes position of platoon n𝑛nitalic_n on time t𝑡titalic_t, ΔtΔ𝑡\Delta troman_Δ italic_t denotes simulation time step width, u𝑢uitalic_u denotes free-flow speed of the link, δ𝛿\deltaitalic_δ denotes jam spacing of the link.

The Node model is computed by the following algorithm:

  1. 1.

    Let i𝑖iitalic_i be time step number

  2. 2.

    Select incoming link l𝑙litalic_l with probability αl/lαlsubscript𝛼𝑙subscript𝑙subscript𝛼𝑙\alpha_{l}/\sum_{l}\alpha_{l}italic_α start_POSTSUBSCRIPT italic_l end_POSTSUBSCRIPT / ∑ start_POSTSUBSCRIPT italic_l end_POSTSUBSCRIPT italic_α start_POSTSUBSCRIPT italic_l end_POSTSUBSCRIPT, where αlsubscript𝛼𝑙\alpha_{l}italic_α start_POSTSUBSCRIPT italic_l end_POSTSUBSCRIPT is the merging priority parameter of link l𝑙litalic_l. If all incoming link have been selected, go to step 6.

  3. 3.

    Select the vehicle that exists at the end of link l𝑙litalic_l. Hereafter, the vehicle is denoted as n𝑛nitalic_n. If there is no such vehicle, go back to step 2.

  4. 4.

    Let onisuperscriptsubscript𝑜𝑛𝑖o_{n}^{i}italic_o start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_i end_POSTSUPERSCRIPT be the outgoing link that vehicle n𝑛nitalic_n want to go. Check whether link onisuperscriptsubscript𝑜𝑛𝑖o_{n}^{i}italic_o start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_i end_POSTSUPERSCRIPT has a sufficient (larger than δΔn𝛿Δ𝑛\delta\Delta nitalic_δ roman_Δ italic_n) vacant space at its starting position, and do the following:

    • If yes, transfer vehicle n𝑛nitalic_n from link l𝑙litalic_l to link onisuperscriptsubscript𝑜𝑛𝑖o_{n}^{i}italic_o start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_i end_POSTSUPERSCRIPT.

    • If no, vehicle n𝑛nitalic_n cannot move, so do nothing.

  5. 5.

    Go back to step 2.

  6. 6.

    Increment time step (i:=i+1assign𝑖𝑖1i:=i+1italic_i := italic_i + 1) and go back to step 2.

The Route choice model is computed by the following steps. Let boz,isuperscriptsubscript𝑏𝑜𝑧𝑖b_{o}^{z,i}italic_b start_POSTSUBSCRIPT italic_o end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_z , italic_i end_POSTSUPERSCRIPT be a dummy variable that is 1 if link o𝑜oitalic_o is a part of the shortest path from every nodes to destination z𝑧zitalic_z on time step i𝑖iitalic_i, and 0 otherwise. Shortest path search is performed every ΔiBΔsubscript𝑖𝐵\Delta i_{B}roman_Δ italic_i start_POSTSUBSCRIPT italic_B end_POSTSUBSCRIPT time steps. When shortest path search is performed, we update Boz,isuperscriptsubscript𝐵𝑜𝑧𝑖B_{o}^{z,i}italic_B start_POSTSUBSCRIPT italic_o end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_z , italic_i end_POSTSUPERSCRIPT, an attractiveness of link o𝑜oitalic_o for vehicles with destination z𝑧zitalic_z on time step i𝑖iitalic_i, as

Boz,i=(1λ)Boz,iΔiB+λboz,i,superscriptsubscript𝐵𝑜𝑧𝑖1𝜆superscriptsubscript𝐵𝑜𝑧𝑖Δsubscript𝑖𝐵𝜆superscriptsubscript𝑏𝑜𝑧𝑖\displaystyle B_{o}^{z,i}=(1-\lambda)B_{o}^{z,i-\Delta i_{B}}+\lambda b_{o}^{z% ,i},italic_B start_POSTSUBSCRIPT italic_o end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_z , italic_i end_POSTSUPERSCRIPT = ( 1 - italic_λ ) italic_B start_POSTSUBSCRIPT italic_o end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_z , italic_i - roman_Δ italic_i start_POSTSUBSCRIPT italic_B end_POSTSUBSCRIPT end_POSTSUPERSCRIPT + italic_λ italic_b start_POSTSUBSCRIPT italic_o end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_z , italic_i end_POSTSUPERSCRIPT , (4)

where λ𝜆\lambdaitalic_λ is a given weight. We assume that the initial Boz,0superscriptsubscript𝐵𝑜𝑧0B_{o}^{z,0}italic_B start_POSTSUBSCRIPT italic_o end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_z , 0 end_POSTSUPERSCRIPT is equal to boz,0superscriptsubscript𝑏𝑜𝑧0b_{o}^{z,0}italic_b start_POSTSUBSCRIPT italic_o end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_z , 0 end_POSTSUPERSCRIPT. Finally, outgoing link onisubscriptsuperscript𝑜𝑖𝑛o^{i}_{n}italic_o start_POSTSUPERSCRIPT italic_i end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT of vehicle n𝑛nitalic_n is determined as o𝑜oitalic_o with probability Boz,i/oBoz,isuperscriptsubscript𝐵𝑜𝑧𝑖subscript𝑜superscriptsubscript𝐵𝑜𝑧𝑖B_{o}^{z,i}/\sum_{o}B_{o}^{z,i}italic_B start_POSTSUBSCRIPT italic_o end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_z , italic_i end_POSTSUPERSCRIPT / ∑ start_POSTSUBSCRIPT italic_o end_POSTSUBSCRIPT italic_B start_POSTSUBSCRIPT italic_o end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_z , italic_i end_POSTSUPERSCRIPT, where z𝑧zitalic_z is the destination of n𝑛nitalic_n.

2.2 Implementation

The implementation of the models are summarized in the following diagrams. Fig. 1 shows a static structure of UXsim as a class diagram (based roughly on Unified Modeling Language). Each three-row rectangle represents a class. The top row denotes the class name, the second row denotes key variables, and the third row denotes key functions.

Similarly, Fig. 2a shows a dynamic computational flow of entire simulation of UXsim as an activity diagram. Fig. 2b shows that of an instance of Vehicle class in UXsim. For the details, please see the codes.

Refer to caption
Figure 1: Class diagram of UXsim (translated and modified from Seo (2023))
Refer to caption
(a) Entire simulation
Refer to caption
(b) Specific vehicle
Figure 2: Activity diagrams of UXsim (translated and modified from Seo (2023))

2.3 Key inputs

Some of the key inputs for simulation are as follows:

  • Reaction time of vehicles τ𝜏\tauitalic_τ. This value determines the simulation time step width. This is a global parameter.

  • Platoon size for mesoscopic simulation ΔnΔ𝑛\Delta nroman_Δ italic_n. This value determines the simulation time step width. This is a global parameter.

  • Route choice model parameters: shortest path update interval ΔiBΔsubscript𝑖𝐵\Delta i_{B}roman_Δ italic_i start_POSTSUBSCRIPT italic_B end_POSTSUBSCRIPT and weight value λ𝜆\lambdaitalic_λ.

  • Lists of nodes and links. They define the network structure.

  • Parameters of each link. For example, length, free flow speed u𝑢uitalic_u, jam density κ𝜅\kappaitalic_κ, and merging priority parameter α𝛼\alphaitalic_α.

  • Parameters of each node. For example, position and traffic signal setting.

  • Demand. For example, origin, destination, and departure time of each vehicle are specified.

Roughly estimating, the computational cost of the simulation is proportional to the total number of vehicles, and inverse proportional to τ𝜏\tauitalic_τ and Δn2Δsuperscript𝑛2\Delta n^{2}roman_Δ italic_n start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT.

2.4 Analysis

UXsim has several built-in analysis and visualization functions. Examples are as follows:

  • Overall traffic analysis (e.g., total travel time)

  • OD-level traffic analysis (e.g., travel time, delay)

  • Link-level traffic analysis (e.g., traffic volume, delay, dynamic traffic states)

  • Output simulation and analysis results to file or pandas dataframe

  • Time-space diagram of trajectories of each link

  • Time-space diagram of traffic states (i.e., flow, density, and speed) of each link

  • Cumulative counts of each link

  • Macroscopic fundamental diagram (MFD) (Geroliminis and Daganzo, 2007)

  • Animation of network traffic state dynamics

  • Animation of vehicle flow in network

3 Examples

3.1 Gridlock and its prevention

Simulation of a simple gridlock congestion is shown as an example. The scenario setting is illustrated in Fig. 3. There is one circular road, and the demands are arranged in such a way that they interfere with each other. In this way, the beginning of some traffic jams and the end of others may engage, resulting in a gridlock condition.

Refer to caption
Figure 3: Gridlock scenario (translated and modified from Seo (2023))

Such simulation can be executed by Code LABEL:gridlock_code. The contents of each CSV file loaded at lines 16 and 17 are shown in Fig. 4. These code and CSV files are included in the repository as demos_and_examples/example_05_gridlock_and_prevention.py.

Code 1: Gridlock
1from uxsim import *
2import pandas as pd
3
4if __name__ == "__main__":
5    # Define simulation
6    W = World(
7        name="",
8        deltan=5,
9        print_mode=1, save_mode=1, show_mode=0,
10        random_seed=0
11    )
12
13    # Define scenario
14    # import CSV files
15    W.load_scenario_from_csv("dat/uroboros_nodes.csv",
16        "dat/uroboros_links.csv", "dat/uroboros_demand.csv")
17
18    W.finzalize_scenario()
19
20    # Execute simulation
21    W.exec_simulation()
22
23    # Result analysis and save
24    W.analyzer.print_simple_stats()
25    W.analyzer.output_data()
26    W.analyzer.time_space_diagram_traj_links([["WN", "NE", "ES", "SW"]])
27    W.analyzer.macroscopic_fundamental_diagram()
28    W.analyzer.network_anim(detailed=1, network_font_size=0, figsize=(12,12))
29    W.analyzer.network_fancy(animation_speed_inverse=15, sample_ratio=0.3, interval=5, trace_length=5)
30
31    df_vehicles = W.analyzer.log_vehicles_to_pandas()
Refer to caption
(a) uroboros_nodes.csv
Refer to caption
(b) uroboros_links.csv
Refer to caption
(c) uroboros_demand.csv
Figure 4: Contents of CSV files defining scenario

Some of the outputs of this simulation are shown in Fig. 5. According to vehicle trajectories shown in Fig. 5a, we can see that traffic was free-flowing until 1200 s when only one direction traffic demand was traveling. However, after the second demand started traveling, traffic congestion immediately occurred at the merging nodes W and E. Each queue generated by traffic congestion extended quickly and reaches the heads of the other queue. Then, gridlock happened. The MFD in Fig. 5b clearly shows a typical gridlock phenomenon. Other outputs such as gif animations are also useful to confirm the dynamics of gridlock phenomena.

Refer to caption
(a) Vehicle trajectories in circular road
Refer to caption
(b) MFD
Figure 5: Gridlock simulation

Now let us consider how to prevent a gridlock by implementing proper traffic management. For example, because the merging nodes are the sources of the queues that triggered the gridlock, we can prevent the gridlock by increasing the merging priority of links in the circular road. This can be done by inserting Code LABEL:gridlock_code2 to line 17 of Code LABEL:gridlock_code: here, the merging priority parameters of links NE and SW are increased from the default value 0.5 to 2 In the real world, this kind of management can be executed by doing ramp metering, signal control, and perimeter control.

Code 2: Gridlock prevention management
1    W.get_link("NE").merge_priority = 2
2    W.get_link("SW").merge_priority = 2

The results are shown in Fig. 6. It is obvious that gridlock is perfectly prevented. The circular road was almost always free-flowing, and the MFD’s state was also in free-flowing or critical regime. Although some congestion occurred at the entry links (this can confirmed by analyzing other outputs), such congestion quickly diminishes one traffic in the circular road finishes traveling. Thus, this kind of traffic management is very effective to prevent traffic congestion and gridlock with little or no loss to anyone.

Refer to caption
(a) Vehicle trajectories in circular road
Refer to caption
(b) MFD
Figure 6: Simulation that prevented gridlock

3.2 Sioux Falls network

Sioux Falls network is a popular network for testing and benchmarking in the transportation research field (Transportation Networks for Research Core Team, 2021). Fig. 7 shows simulation results by UXsim. A qualitatively reasonable traffic pattern on the network was obtained by considering traffic dynamics and route choice. The specification of the simulation scenario is as follows:

  • Simulation duration: 7200 s

  • Total number of vehicles: 34690 veh

  • Number of links: 76

  • Total road length: 314000 m

  • Time step width ΔtΔ𝑡\Delta troman_Δ italic_t: 5 s

  • Platoon size ΔnΔ𝑛\Delta nroman_Δ italic_n: 5 veh

  • Route choice update interval ΔiBΔtΔsubscript𝑖𝐵Δ𝑡\Delta i_{B}\Delta troman_Δ italic_i start_POSTSUBSCRIPT italic_B end_POSTSUBSCRIPT roman_Δ italic_t: 600 s

The computation time was about 16 s using Windows 10 computer with 3.79 GHz CPU and 32 GB RAM.

Refer to caption
(a) Network traffic state. The width of each link denotes the number of vehicles on the link, and the color of each link denotes its average speed (the darker the slower).
Refer to caption
(b) Vehicle trajectories in a corridor at the center of network, northbound direction.
Figure 7: Simulation results in Sioux Falls network

The repository https://github.com/toruseo/UXsim also contains detailed other examples such as highway bottleneck, traffic signal control, scenario generation by script, along with detailed documents.

References

  • Flötteröd (2016) Flötteröd, G., 2016. Queueing representation of kinematic waves. In: The Multi-Agent Transport Simulation MATSim. pp. 347–351.
  • Flötteröd and Rohde (2011) Flötteröd, G., Rohde, J., 2011. Operational macroscopic modeling of complex urban road intersections. Transportation Research Part B: Methodological 45 (6), 903–922.
  • Geroliminis and Daganzo (2007) Geroliminis, N., Daganzo, C. F., 2007. Macroscopic modeling of traffic in cities. In: Transportation Research Board 86th Annual Meeting.
  • Kuwahara and Akamatsu (2001) Kuwahara, M., Akamatsu, T., 2001. Dynamic user optimal assignment with physical queues for a many-to-many OD pattern. Transportation Research Part B: Methodological 35 (5), 461–479.
  • Laval and Leclercq (2013) Laval, J. A., Leclercq, L., 2013. The Hamilton–Jacobi partial differential equation and the three representations of traffic flow. Transportation Research Part B: Methodological 52, 17–30.
  • Lighthill and Whitham (1955) Lighthill, M. J., Whitham, G. B., 1955. On kinematic waves. II. a theory of traffic flow on long crowded roads. Proceedings of the Royal Society of London. Series A. Mathematical and Physical Sciences 229 (1178), 317–345.
  • Newell (2002) Newell, G. F., 2002. A simplified car-following theory: a lower order model. Transportation Research Part B: Methodological 36 (3), 195–205.
  • Richards (1956) Richards, P. I., 1956. Shock waves on the highway. Operations Research 4 (1), 42–51.
  • Seo (2023) Seo, T., 2023. Macroscopic Traffic Flow Simulation: Fundamental Mathematical Theory and Python Implementation. Corona Publishing Co., Ltd., (in Japanese).
  • Transportation Networks for Research Core Team (2021) Transportation Networks for Research Core Team, 2021. Transportation networks for research, https://github.com/bstabler/TransportationNetworks, Accessed 2021-10-01.