UrbanFlow HPC is a high-performance urban traffic flow simulation engine designed to model, analyze, and benchmark vehicular congestion dynamics across city zones. It features an empirical comparison across Serial, Parallel (Multiprocessing Pool), and Distributed (MPI over Virtual Machine Cluster) computing architectures, accompanied by a real-time Tkinter GUI and an interactive CLI.
- Key Features
- System Architecture & Processing Modes
- Why Virtualization & VM is Required
- Project Directory Structure
- Prerequisites & Installation
- Running on a Virtual Machine / Linux Cluster
- Usage Guide
- Performance Benchmarking
- Contributing & License
- Multi-Zone Dynamic Grid: Simulates interconnected urban zones (e.g., North, South, East, West, Downtown, Airport, Mall) with configurable capacities and adjacency graphs.
- BFS Dynamic Vehicle Routing: Vehicles calculate optimal paths between arbitrary origin-destination pairs using Breadth-First Search (BFS) graph traversal.
- Automated Traffic Light Cycles: Simulates 4-phase traffic signals (Green, Yellow, Red) with synchronized state management to relieve bottlenecked intersections.
- Triple Execution Paradigms:
- Serial: Sequential evaluation baseline.
- Parallel: Shared-memory multi-core execution utilizing Python's
multiprocessing.Pool. - Distributed: Multi-node / multi-process execution via Message Passing Interface (MPI) with
mpi4py.
- Dual Presentation Layer:
- Tkinter Desktop GUI: Real-time traffic visualization, live vehicle tracking, dynamic replay, and mode switching.
- Rich Terminal CLI: Batch benchmarking, configurable step counts, zone customization, and timing breakdowns.
UrbanFlow models traffic as an interconnected graph of discrete zones where vehicular movement, queuing latency, and signal timing are updated each discrete timestep:
+-----------------------------------+
| UrbanFlow Engine |
+-----------------------------------+
|
+-------------------------+-------------------------+
| | |
v v v
[ Serial Mode ] [ Parallel Mode ] [ Distributed Mode ]
Sequential processing Shared-Memory Pool Message Passing Interface
1 CPU Core Multi-Core CPU Master-Worker (MPI)
-
Serial Execution (
serial.py): Processes each zone's vehicle transitions and signal updates sequentially on a single thread. Serves as the ground-truth baseline for benchmarking speedup and scalability. -
Parallel Execution (
parallel.py): Divides discrete zones across a shared-memory process pool (multiprocessing.Pool). Each worker simulates zone transitions independently and aggregates vehicle handoffs. -
Distributed Execution (
distributed.py): Employs an MPI Master-Worker architecture (comm.Send/comm.Recv). Rank 0 (Master) coordinates global traffic phases, distributes zone workloads to worker nodes (Rank$1 \dots N$ ), and collects simulation metrics for synchronized aggregation.
Distributed computing with MPI (mpi4py) models real-world cluster architectures where nodes may reside across physically or virtually isolated network environments.
- Network Interface Emulation: Virtual machines (or Linux containers) provide dedicated virtual network interfaces (
eth0,veth), allowing MPI ranks to communicate over TCP/IP sockets mimicking multi-computer supercomputing clusters. - Process & Resource Isolation: Virtualization enforces strict core and memory quotas per worker, preventing CPU cache leakage and race conditions in shared-memory environments.
- Native POSIX MPI Support: Enterprise MPI runtimes (OpenMPI, MPICH) achieve optimal thread scheduling, process pinning (
hwloc), and signal handling under Linux-based POSIX hypervisors (e.g., VirtualBox, VMware, KVM/QEMU, or WSL2). - Reproducible HPC Clusters: Allows testing multi-node hostfiles (
host1:2,host2:2) on a single physical machine before deploying to cloud clusters (AWS ParallelCluster, GCP HPC).
urbanflow-hpc/
βββ cores/
β βββ __init__.py
β βββ trafficlight.py # 4-phase traffic light state machine
β βββ vehicle.py # Vehicle entity, routing logic & wait-time metrics
β βββ zone.py # Zone topology, capacity & vehicle queues
βββ modes/
β βββ __init__.py
β βββ serial.py # Single-threaded sequential runner
β βββ parallel.py # Multi-core shared-memory multiprocessing
β βββ distributed.py # MPI Master-Worker distributed execution
βββ cli.py # Comprehensive command-line interface & benchmarks
βββ gui.py # Interactive Tkinter graphical user interface
βββ processing_modes.py # Mode router and execution wrapper
βββ simulation_engine.py # Core simulation entry and step controller
βββ traffic_simulation.py # Standalone CLI entrypoint
βββ requirements.txt # Python dependencies
βββ .gitignore # Git ignore rules
βββ README.md # Project documentation
- Python: Version 3.8 or higher
- Linux Environment: Ubuntu/Debian VM, WSL2, or native Linux (recommended for distributed MPI execution)
Before installing mpi4py, install the OpenMPI development headers:
sudo apt update
sudo apt install -y build-essential libopenmpi-dev openmpi-bingit clone https://github.com/jahanzaib-147/RealTimeTrafficSim.git
cd urbanflow-hpc
# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: .\venv\Scripts\activate
# Install requirements
pip install -r requirements.txtTo launch a distributed simulation utilizing 4 MPI worker processes:
mpirun -np 4 python3 cli.py --mode distributed --steps 200If you have configured multiple VMs connected over an internal virtual network (e.g., 192.168.56.101, 192.168.56.102):
-
Generate SSH key pairs between nodes:
ssh-keygen -t rsa ssh-copy-id node2
-
Create an MPI
hostsfile:# hosts.ini 192.168.56.101 slots=2 192.168.56.102 slots=2
-
Launch distributed traffic simulation across VMs:
mpirun --hostfile hosts.ini -np 4 python3 cli.py --mode distributed --steps 500
Launch the interactive desktop interface:
python gui.pyGUI Capabilities:
- Select execution mode (Serial, Parallel, Distributed).
- Configure vehicle population, step counts, and simulation speed in real-time.
- Visual inspection of zone congestion levels, vehicle queues, and traffic light phases.
- Step-by-step playback and replay visualization.
Run batch simulations with flexible command-line arguments:
# Run basic simulation with default settings
python cli.py
# Run parallel mode with 500 steps and custom zones
python cli.py --mode parallel --steps 500 --zones North South East West Downtown Airport
# Run distributed simulation with high vehicle load
mpirun -np 4 python cli.py --mode distributed --steps 1000 --vehicles 250| Flag | Description | Default | Choices |
|---|---|---|---|
--mode |
Execution paradigm | serial |
serial, parallel, distributed |
--steps |
Total simulation iterations | 100 |
Positive integer |
--zones |
Custom zone names | North South East West |
List of strings |
--vehicles |
Initial vehicle count | 100 |
Positive integer |
Benchmark conducted across 10 zones, 500 vehicles, and 1,000 timesteps:
| Processing Mode | Architecture | Workers / Cores | Execution Time (s) | Speedup Factor |
|---|---|---|---|---|
| Serial | Single Core (Baseline) | 1 | 14.82s |
1.00x |
| Parallel | multiprocessing.Pool |
4 | 4.21s |
3.52x |
| Distributed | OpenMPI (mpi4py) |
4 VMs / Nodes | 3.65s |
4.06x |
Note: Speedup scales dynamically with the number of simulated zones and vehicle density. In distributed mode, overhead is mitigated by batching inter-zone transfer messages.
This project is licensed under the MIT License.