UXsim is a free, open-source macroscopic and mesoscopic network traffic flow simulator written in Python. It simulates the movements of car travelers and traffic congestion in road networks. It is suitable for simulating large-scale (e.g., city-scale) traffic phenomena. UXsim is especially useful for scientific and educational purposes because of its simple, lightweight, and customizable features, but users are free to use UXsim for any purpose.
If you are interested, please see:
pandas.DataFrame
and CSV filesMatplotlib
; interactive GUI is also availablePyTorch
for deep reinforcement learning traffic controluxsim.py
is only about 1800 lines of code. Users may easily understand and customize itBelow are simulation results where approximately 60000 vehicles pass through a 10km x 10km grid network in 2 hours. The computation time was about 30 seconds on a standard desktop PC.
Visualization of link traffic states (thicker lines mean more vehicles, darker colors mean slower speeds) and some vehicle trajectories:
Vehicle trajectory diagram on a corridor of the above network:
A traffic signal controller is trained by deep reinforcement learning (DRL) using PyTorch. The left (or upper) scenario shows no control with fixed signal timing; the traffic demand exceeds the network capacity with the naive signal setting, and a gridlock occurs. The right (or bottom) scenario shows DRL control, where the traffic signal can be changed by observing queue length; although the demand level is the same, traffic flows smoothly. A Jupyter Notebook of this example is available.
https://github.com/toruseo/UXsim/assets/34780089/ec780a33-d9ba-4068-a005-0b06127196d9
The simplest way is to use pip
to install from PyPI:
pip install uxsim
You can also install with conda
from conda-forge
channel:
conda install uxsim
For the details, please see here.
As a simple example, the following code will simulate traffic flow in a Y-shaped network.
from uxsim import *
# Define the main simulation
# Units are standardized to seconds (s) and meters (m)
W = World(
name="", # Scenario name
deltan=5, # Simulation aggregation unit delta n
tmax=1200, # Total simulation time (s)
print_mode=1, save_mode=1, show_mode=0, # Various options
random_seed=0 # Set the random seed
)
# Define the scenario
## Create nodes
W.addNode(name="orig1", x=0, y=0)
W.addNode("orig2", 0, 2)
W.addNode("merge", 1, 1)
W.addNode("dest", 2, 1)
## Create links between nodes
W.addLink(name="link1", start_node="orig1", end_node="merge",
length=1000, free_flow_speed=20, number_of_lanes=1)
W.addLink("link2", "orig2", "merge", length=1000, free_flow_speed=20, number_of_lanes=1)
W.addLink("link3", "merge", "dest", length=1000, free_flow_speed=20, number_of_lanes=1)
## Create OD traffic demand between nodes
W.adddemand(orig="orig1", dest="dest", t_start=0, t_end=1000, flow=0.45)
W.adddemand("orig2", "dest", 400, 1000, 0.6)
# Run the simulation to the end
W.exec_simulation()
# Print summary of simulation result
W.analyzer.print_simple_stats()
# Visualize snapshots of network traffic state for several timesteps
W.analyzer.network(100, detailed=1, network_font_size=12)
W.analyzer.network(600, detailed=1, network_font_size=12)
W.analyzer.network(800, detailed=1, network_font_size=12)
It will output text to the terminal and images to the out
directory like below:
simulation setting:
scenario name:
simulation duration: 1200 s
number of vehicles: 810 veh
total road length: 3000 m
time discret. width: 5 s
platoon size: 5 veh
number of timesteps: 240
number of platoons: 162
number of links: 3
number of nodes: 4
setup time: 0.00 s
simulating...
time| # of vehicles| ave speed| computation time
0 s| 0 vehs| 0.0 m/s| 0.00 s
600 s| 130 vehs| 13.7 m/s| 0.03 s
1195 s| 75 vehs| 12.3 m/s| 0.06 s
simulation finished
results:
average speed: 11.6 m/s
number of completed trips: 735 / 810
average travel time of trips: 162.6 s
average delay of trips: 62.6 s
delay ratio: 0.385
To learn more about UXsim, please see:
uxsim
directory: UXsim main package
uxsim/uxsim.py
: UXsim main codedemos_and_examples
directory: Tutorials and examples of UXsimdat
directory: Sample scenario filesUXsim is released under the MIT License. You are free to use it as long as the source is acknowledged.
When publishing works based on UXsim, please cite:
Works using UXsim is summarized on the Github Wiki page. Please feel free to edit.
Contributions are welcome! Please see the Contributing Guideline.
If you have any questions or suggestions, please post them to the Issues or Discussions (in English or Japanese).
I (Toru Seo) work on this project in my spare time. Please understand that my response may be delayed.
UXsim is based on various works in traffic flow theory and related fields. We acknowledge the contributions of the research community in advancing this field. Specifically, UXsim directly uses the following works: