Skip to content

Latest commit

ย 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿšฆ TrafficFlowRL

Research-grade multi-agent reinforcement learning for smart city traffic optimisation.

TrafficFlowRL simulates a city grid of intersections where independent RL agents control traffic lights to minimise congestion, reduce vehicle wait times, and prioritise emergency vehicles โ€” all trained end-to-end with Proximal Policy Optimization (PPO).


๐Ÿ“‹ Problem Description

Urban traffic congestion costs billions of hours and dollars annually. Traditional fixed-cycle traffic lights ignore real-time demand. TrafficFlowRL demonstrates how decentralised RL agents โ€” one per intersection โ€” can learn cooperative signal policies that:

  • Minimise city-wide average wait time
  • Maximise vehicle throughput
  • Prioritise emergency vehicles (ambulances, fire trucks)
  • Adapt to rush-hour patterns and weather conditions

๐Ÿ—๏ธ Architecture

trafficflowrl/
โ”œโ”€โ”€ env/                          # Gymnasium environment
โ”‚   โ”œโ”€โ”€ city_env.py               # Main CityTrafficEnv (Gymnasium API)
โ”‚   โ”œโ”€โ”€ intersection.py           # Traffic light phases & queues
โ”‚   โ”œโ”€โ”€ vehicle.py                # Vehicle entity with pathfinding
โ”‚   โ”œโ”€โ”€ road_network.py           # Grid graph (NetworkX)
โ”‚   โ”œโ”€โ”€ traffic_generator.py      # Vehicle spawning & rush hour
โ”‚   โ”œโ”€โ”€ reward_system.py          # Multi-component reward function
โ”‚   โ””โ”€โ”€ state_encoder.py          # Observation normalisation
โ”œโ”€โ”€ agents/
โ”‚   โ”œโ”€โ”€ ppo_agent.py              # Custom PPO with GAE
โ”‚   โ””โ”€โ”€ policy_network.py         # Actor-Critic neural network
โ”œโ”€โ”€ training/
โ”‚   โ”œโ”€โ”€ train.py                  # Training loop with checkpointing
โ”‚   โ”œโ”€โ”€ evaluate.py               # Evaluation & statistics
โ”‚   โ””โ”€โ”€ config.py                 # Centralised hyper-parameters
โ”œโ”€โ”€ visualization/
โ”‚   โ”œโ”€โ”€ city_visualizer.py        # Real-time Pygame renderer
โ”‚   โ””โ”€โ”€ training_dashboard.py     # Matplotlib metrics dashboard
โ”œโ”€โ”€ utils/
โ”‚   โ”œโ”€โ”€ logger.py                 # CSV + JSON logging
โ”‚   โ””โ”€โ”€ seed.py                   # Reproducible seeding
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ README.md

๐ŸŒ Environment Design

City Grid

The environment simulates a configurable Nร—N grid of intersections (default 3ร—3 = 9 intersections) connected by bidirectional roads. Vehicles spawn at city edges and follow the shortest path (BFS on the grid graph) to a random destination edge.

Observation Space

Each intersection produces a 7-dimensional observation vector:

Index Feature Range Description
0 North Queue [0, 1] Normalised queue length
1 South Queue [0, 1] Normalised queue length
2 East Queue [0, 1] Normalised queue length
3 West Queue [0, 1] Normalised queue length
4 Light Phase {0, 1} 0 = NS green, 1 = EW green
5 Phase Timer [0, 1] Normalised time since last switch
6 Emergency Flag {0, 1} 1 if emergency vehicle in queue

Global observation = concatenation of all intersection vectors โ†’ shape (N*N*7,) = (63,) for a 3ร—3 grid.

Action Space

MultiDiscrete โ€” one action per intersection:

Action Meaning
0 Set North-South green
1 Set East-West green
2 Keep current phase

Actions have a minimum phase time constraint (default 3 steps) to prevent unrealistic rapid switching.


๐Ÿ† Reward Function

The reward is a weighted sum of five components, averaged across all intersections:

Component Value Condition
Throughput +1.0 Per vehicle passing through intersection
Waiting penalty โˆ’0.5 Per vehicle still waiting in queue
Emergency bonus +5.0 Emergency vehicle cleared quickly (โ‰ค5 steps)
Emergency penalty โˆ’5.0 Emergency vehicle waited too long
Switch penalty โˆ’0.2 Phase change occurred this step

๐Ÿง  RL Algorithm โ€” PPO

Policy Network (Actor-Critic)

Input (7) โ†’ Dense(256, ReLU) โ†’ Dense(256, ReLU) โ†’ Action Logits (3)
Input (7) โ†’ Dense(256, ReLU) โ†’ Dense(256, ReLU) โ†’ Value (1)

A shared architecture is used for all intersections โ€” the same network weights process every intersection's local observation, enabling parameter-efficient scaling to larger grids.

Training Hyper-parameters

Parameter Value
Episodes 1,000
Max steps/episode 300
Discount (ฮณ) 0.99
GAE ฮป 0.95
Learning rate 3ร—10โปโด
Batch size 64
Clip range (ฮต) 0.2
Entropy coeff 0.01
Value coeff 0.5
PPO epochs/update 4

๐ŸŽฎ Bonus Features

  • ๐Ÿ• Rush Hour Patterns โ€” sinusoidal traffic demand increases
  • ๐ŸŒง๏ธ Weather Effects โ€” reduce vehicle speed dynamically
  • ๐Ÿš‘ Multiple Emergency Vehicles โ€” configurable probability
  • ๐Ÿ“ˆ Adaptive Traffic Demand โ€” spawn rate changes over time

๐Ÿš€ Quick Start

1. Install dependencies

pip install -r requirements.txt

2. Train the agent

cd trafficflowrl
python training/train.py

With bonus features:

python training/train.py --episodes 500 --rush-hour --weather --grid 3

3. Evaluate

python training/evaluate.py --checkpoint checkpoints/ppo_traffic_best.pt --episodes 20

4. Visualise (Pygame)

python visualization/city_visualizer.py --checkpoint checkpoints/ppo_traffic_best.pt

Controls: [R] Reset episode ยท [ESC] Quit

5. Training Dashboard (Matplotlib)

python visualization/training_dashboard.py --log-dir logs

Windows Convenience: run.bat

For Windows users a helper batch file is provided to run the common commands without typing long Python calls. From the project root, you can:

run.bat train       # starts training using training/train.py with default args
run.bat evaluate    # runs evaluation using training/evaluate.py
run.bat visualize   # launches the Pygame visualizer with the best checkpoint

Replace the subcommand with train, evaluate, or visualize. You can also pass additional CLI flags which will be forwarded to the underlying Python script (for example run.bat train --episodes 500).


๐Ÿ“Š Example Visualizations

City Visualizer (Pygame)

The real-time visualizer renders:

  • ๐ŸŸข๐Ÿ”ด Traffic lights at each intersection with phase indicators
  • ๐Ÿš— Moving vehicles as coloured dots on the road network
  • ๐Ÿš‘ Emergency vehicles with red pulsing glow effects
  • ๐Ÿ“Š Stats panel showing step count, reward, queue lengths, and per-intersection details

Training Dashboard (Matplotlib)

The dashboard displays five smoothed metric plots:

  • Episode reward curve (with moving average)
  • Average wait time per episode
  • Vehicles cleared per episode
  • Policy loss convergence
  • Value loss convergence
  • Summary statistics panel

โš™๏ธ Configuration

All hyper-parameters are centralised in training/config.py as a TrainingConfig dataclass. Modify defaults or pass CLI arguments:

from training.config import TrainingConfig

cfg = TrainingConfig(
    grid_rows=4,
    grid_cols=4,
    total_episodes=2000,
    enable_rush_hour=True,
    enable_weather=True,
    device="cuda",
)

๐Ÿ“– Technical Details

Vehicle Routing

Vehicles use BFS shortest path on the NetworkX grid graph. Each vehicle stores its planned path and advances one intersection per green-light timestep.

Emergency Priority

Emergency vehicles bypass the traffic light phase โ€” they are always released from queues regardless of the current signal phase, simulating real-world signal pre-emption.

Multi-Agent Coordination

While agents are decentralised (each intersection acts independently), coordination emerges through:

  1. Shared parameters โ€” the same neural network processes all intersections
  2. Global reward signal โ€” averaged across intersections, encouraging cooperative behaviour
  3. Information flow โ€” queue build-ups from neighbouring intersections implicitly encode coordination signals

๐Ÿ“œ License

MIT License โ€” free for academic and commercial use.


Built for AI hackathon demonstrations and RL research. Contributions welcome!

About

Research-grade multi-agent reinforcement learning for smart city traffic optimisation.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages