A Python traffic intersection simulation built with SimPy. It models vehicles arriving from the north, south, east, and west, queues them at an intersection, and schedules traffic lights using round-robin signaling with priority handling.
The project also includes an interactive Pygame visualization of the intersection, traffic lights, and vehicle queues.
- Discrete-event traffic simulation powered by SimPy
- Four-direction intersection model:
N,S,E, andW - Round-robin traffic-light scheduling
- Five-time-unit green-light quantum
- Priority-aware preemption for priority vehicles
- Vehicle arrival, crossing, completion, and waiting-time tracking
- Optional Pygame visualization with normal and priority vehicle sprites
- Pause and resume support in the visual simulation
- Python 3.9 or newer
simpypygame(required only for the visual simulation)
Clone the repository and create a virtual environment:
git clone https://github.com/AgniprabhaGanguly/Traffic-Light-Simulation.git
cd Traffic-Light-Simulation
python -m venv .venv
.\.venv\Scripts\Activate.ps1Install the dependencies:
python -m pip install simpy pygamepython main.pyThis runs a 20-time-unit simulation using six predefined vehicles and prints traffic-light changes, vehicle activity, and final simulation time.
python visual_main.pyThe visualizer displays the intersection and the current vehicle queues. Press Space to pause or resume the simulation, and close the window to exit.
Run the commands from the repository root so the visualizer can locate the images in assets/.
Each vehicle has:
- An arrival time
- A source and destination direction
- A priority value (
0for normal vehicles,1for priority vehicles) - A one-time-unit crossing duration
Vehicles are placed in the queue corresponding to their source direction. The intersection scheduler:
- Checks the current direction for waiting vehicles.
- Gives that direction a green light.
- Allows vehicles to cross for up to five time units.
- Switches direction when the quantum expires, the queue is empty, or a higher-priority queue is detected.
- Continues in round-robin order.
.
├── main.py # Terminal simulation entry point
├── visual_main.py # Pygame simulation entry point
├── Models/
│ ├── intersection.py # Traffic lights, queues, and scheduler
│ └── vehicles.py # Vehicle behavior and timing statistics
├── visualization/
│ ├── visual_components.py # Pygame rendering
│ └── visual_simulation.py # Simulation-to-visualization adapter
└── assets/
├── car_normal.png # Normal vehicle sprite
└── car_priority.png # Priority vehicle sprite
The predefined vehicles and simulation duration can be changed in main.py or visual_main.py. Continuous random vehicle generation is also available through setup_vehicle_generator in visual_main.py.
No license has been specified for this project.