Skip to content

Latest commit

ย 

History

3 Commits

Folders and files

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

Repository files navigation

Multi-Agent Traffic Control Simulation ๐Ÿšฆ๐Ÿš™๐Ÿš‘๐Ÿšถ

Python Version License: MIT Framework: SPADE

A distributed Multi-Agent System (MAS) for autonomous urban traffic control, emergency vehicle preemption, and pedestrian safety management built using SPADE (Smart Python Agent Development Environment) and asynchronous XMPP message passing.

Simulation Demo


๐Ÿ“‹ Table of Contents


๐Ÿ“Œ Overview

Urban traffic management requires real-time coordination among diverse entities with competing priorities. Traditional fixed-timer traffic signals often cause unnecessary delay, fail to adapt to dynamic queue lengths, and struggle to clear paths for emergency responders efficiently.

This project implements a multi-agent traffic simulation where vehicles, emergency responders, pedestrians, traffic lights, and a central coordinator communicate asynchronously. Using SPADE (an agent framework based on the FIPA standard), agents observe local states, negotiate intersection access via XMPP performative messages, and dynamically modify traffic light schedules to optimize throughput and safety.


๐ŸŽฏ Motivation

  1. Emergency Preemption: Emergency vehicles (ambulances) must navigate dense urban networks with zero latency, overriding normal traffic signal cycles to prevent life-threatening delays.
  2. Pedestrian Safety: Pedestrians at zebra crossings require instant, safe passage requests without creating sudden vehicle collisions or gridlock.
  3. Decentralized & Coordinated Control: Evaluating how message passing between specialized agents (CarAgent, AmbulanceAgent, TrafficLightAgent, PersonAgent, and CentralCordinateAgent) resolves complex bottleneck scenarios in real time.

๐Ÿ—บ๏ธ Simulation Environment

The simulation environment is defined in simulation/environment.py. It models a multi-intersection urban grid with signalized roads, unsignalized priority roads, zebra crossings, and spatial occupancy tracking.

Road Network Layout

State Representation

  • Grid / Discrete Spatial Positions: Each road segment consists of discrete position slots (position 0 through 5). Reaching position = 4 triggers an intersection request, while position = 5 marks the intersection boundary.
  • Dynamic Traffic Light Registry: Tracks active light colors (green, yellow, red) across all signalized intersections.
  • Agent Registry: Dict mapping agent JIDs to their instantiated objects (cars, ambulances, people).

Roads & Network Topology

  • Signalized Roads (road_1 to road_4): Equipped with traffic lights (Traffic_Light_1, Traffic_Light_2, Traffic_Light_3) and pedestrian zebra crossings (Zebra_Crossing_1 to Zebra_Crossing_3).
  • Unsignalized Priority Road (road_9): Governed by priority yielding rules ("Priority" traffic sign) where vehicles must yield to main flow traffic on road_5 and road_6.
  • Egress & Intermediate Roads (road_5, road_6, road_7, road_8, road_10, road_11, road_12): Free-flowing connecting segments leading away from intersections.

Traffic Rules & Priority Hierarchy

  1. Emergency Preemption (Highest Priority): Ambulances broadcast emergency signals, overriding all active cycles to force their road's signal to green while setting cross-street signals to red or yellow.
  2. Pedestrian Priority (High Priority): Pedestrian crossing requests change signalized lights to red for vehicle traffic, provided no active emergency preemption is present.
  3. Signalized Intersection Rules: Regular vehicles on road_1 - road_4 must stop at red/yellow lights and only advance on green.
  4. Priority Sign Yielding Rules: Vehicles on road_9 check for oncoming traffic on road_5/road_6 before proceeding.

๐Ÿค– Agent Architecture

The system comprises 5 distinct agent types interacting via XMPP message passing.

Agent Architecture

1. Agent Decision-Making Loop

  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚                       Environment                       โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚ (State: Road, Position, Light)
                               โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚                      Traffic Agent                      โ”‚
  โ”‚     (Car / Ambulance / Pedestrian / Traffic Light)      โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚ (Local Sensor / Perception)
                               โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚                      Observations                       โ”‚
  โ”‚ (Vehicle Ahead? Signal Color? Emergency Broadcast?)     โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚ (Rule Evaluation / Logic)
                               โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚                        Decision                         โ”‚
  โ”‚     (Advance / Stop / Yield / Preempt Signal / Wait)    โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚ (Action Execution)
                               โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚                         Action                          โ”‚
  โ”‚  (Send XMPP Msg / Update Position / Switch Light State) โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚
                               โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚                   Updated Environment                   โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

2. Agent Breakdown & Interaction Matrix

Agent Type Class Information Observed Available Actions Decision Logic
Car Agent CarAgent Current road, position, presence of vehicle ahead, messages from Central Coordinator. Move forward, Stop, Yield priority, Query Central ("may i go?"). Advances if lane clear. At pos 4, queries Central. Stops on red or emergency broadcast.
Ambulance Agent AmbulanceAgent Current road, position, vehicle clearance ahead, signal state. Move forward, Broadcast emergency preemption ("emergency"). Checks signal at pos 4. If red/yellow, broadcasts emergency override to force green.
Person Agent PersonAgent Target zebra crossing, Central Agent command ("move" or "wait"). Request red signal ("change to red"), Cross street, Pick new zebra. Sends request to Central when approaching crossing. Crosses if move command received.
Traffic Light Agent TrafficLightAgent Current light state, XMPP commands from Central Agent. Cycle color (Gโ†’Yโ†’R), Force Green (Emergency), Force Red (Pedestrian). Runs cyclic color transitions every 2s. Overridden immediately upon emergency/pedestrian message.
Central Coordinator CentralCordinateAgent Central message queue, active emergencies, light statuses, road traffic states. Route permissions, Coordinate phase synchronization, Issue move/stop commands. Maintains strict priority queue: Ambulance Emergency > Pedestrian Crossing > Vehicle Light Request.

๐Ÿšฆ Traffic Model & Decision-Making Process

Spatial Flow & Collision Avoidance

Before advancing, a vehicle calls is_vehicle_ahead(vehicle_jid, type_vehicle). If another vehicle occupies position + 1 on the same road, the trailing vehicle halts to preserve safe headway distance.

Intersection Negotiation Protocol

  1. Approach Phase: Vehicle reaches position = 4 on a signalized road.
  2. Query Phase: Vehicle sends an XMPP inform message containing "may i go?" to central@localhost.
  3. Evaluation Phase: Central agent queries the corresponding TrafficLightAgent for current color state.
  4. Command Phase:
    • If green โž” Central returns "move". Vehicle increments position to 5 and transitions to next road segment.
    • If red or yellow โž” Central returns "stop". Vehicle waits at position 4.
    • If road_9 (Priority Sign) โž” Central checks if road_5 or road_6 have vehicles at position 5. If occupied, returns "give priority".

๐Ÿ“ Project Structure

Autonomous_and_Intelligent_systems-Project1/
โ”œโ”€โ”€ README.md                   # Complete system documentation
โ”œโ”€โ”€ LICENSE                     # MIT Open Source License
โ”œโ”€โ”€ requirements.txt            # Python dependencies (SPADE, Matplotlib, NumPy, Pillow)
โ”œโ”€โ”€ .gitignore                  # Git ignore rules for bytecode, logs, environments
โ”‚
โ”œโ”€โ”€ simulation/                 # Main simulation source code
โ”‚   โ”œโ”€โ”€ agents.py               # SPADE agent definitions (Car, Ambulance, Person, Light, Central)
โ”‚   โ”œโ”€โ”€ environment.py          # Spatial grid, road graph topology, state methods
โ”‚   โ””โ”€โ”€ main.py                 # Asyncio entry point & agent orchestration
โ”‚
โ”œโ”€โ”€ figures/                    # System diagrams, performance metrics plots, GIF animations
โ”‚   โ”œโ”€โ”€ agent_architecture.png  # SPADE Multi-Agent architecture & communication diagram
โ”‚   โ”œโ”€โ”€ intersection_screenshot.png # High-resolution screenshot of intersection state
โ”‚   โ”œโ”€โ”€ simulation_demo.gif     # Animated simulation execution demonstration
โ”‚   โ”œโ”€โ”€ simulation_metrics.png  # Quantitative benchmark plots (Wait time, speed, congestion)
โ”‚   โ””โ”€โ”€ simulation_network.png  # Urban road network layout & traffic light map
โ”‚
โ”œโ”€โ”€ results/                    # Quantitative simulation benchmark results
โ”‚   โ”œโ”€โ”€ metrics_summary.csv     # Metrics summary table (CSV)
โ”‚   โ””โ”€โ”€ metrics_summary.json    # Structured evaluation metrics (JSON)
โ”‚
โ”œโ”€โ”€ videos/                     # Demo video documentation
โ”‚   โ””โ”€โ”€ README.md               # Simulation recording notes
โ”‚
โ””โ”€โ”€ docs/                       # Project reports & supplementary materials

๐Ÿ› ๏ธ Installation

Prerequisites

  • Python: 3.9 or higher
  • OS: macOS / Linux / Windows

Setup Steps

  1. Clone the repository:

    git clone https://github.com/LaraSousa34/Autonomous_and_Intelligent_systems-Project1.git
    cd Autonomous_and_Intelligent_systems-Project1
  2. Create and activate a virtual environment (recommended):

    python3 -m venv venv
    source venv/bin/activate   # On Windows: venv\Scripts\activate
  3. Install dependencies:

    pip install -r requirements.txt

๐Ÿš€ Running the Simulation

Execute the main simulation script using Python:

python3 simulation/main.py

Expected Output Log

Traffic_Light_1 started
Traffic_Light_2 started
Traffic_Light_3 started
CentralCordinateAgent started
CarAgent: car_1 started
AmbulanceAgent: ambulance_1 started
Person Agent: person_1 approaching a zebra crossing on road_1.
car_1: Moving to position 1 on road_1.
Emergency at ambulance_1: Traffic Light yellow. Sending preemption signal.
Emergency at ambulance_1. Changing traffic light Traffic_Light_1 to green. All the cars on road_1 must stop
car_1: Stopping due to an emergency on road_1.
Central Agent responded with: move. person_1 crossing the street.

๐Ÿ“Š Results & Performance

Quantitative metrics collected over a 60-second multi-agent simulation run with 4 car agents, 2 ambulance agents, 3 traffic lights, 2 pedestrians, and 1 central coordinator:

Simulation Metrics

Performance Summary

Metric Value Unit Description
Average Waiting Time 9.23 Seconds Mean time vehicles spend stationary at red lights or yielding.
Vehicles Processed 68 Vehicles Total intersection traversals completed during simulation.
Average Vehicle Speed 1.82 Units / Tick Mean movement rate per simulation tick across all active lanes.
Peak Congestion Index 38.13 % Maximum lane occupancy ratio observed during peak traffic.
Average Congestion Index 25.65 % Average lane occupancy ratio over simulation duration.
Emergency Preemption Delay 1.20 Seconds Time between ambulance emergency signal broadcast and green light activation.
Pedestrian Crossing Wait 3.50 Seconds Average delay experienced by pedestrians before receiving "move" clearance.

๐Ÿ”ฎ Future Work

  1. Reinforcement Learning Traffic Signal Control (MARL): Implement Deep Q-Networks (DQN) or PPO for adaptive signal timing optimization based on live queue lengths rather than fixed cyclic timing.
  2. Multi-Intersection Coordination: Expand central coordination to support grid-wide green waves across adjacent intersections.
  3. Vehicle-to-Vehicle (V2V) Communication: Allow cars to negotiate merging order directly without relying entirely on the central coordinator.
  4. SUMO (Simulation of Urban MObility) Integration: Connect the SPADE multi-agent architecture with SUMO for photorealistic urban traffic flow and realistic micro-simulation dynamics.
  5. Adaptive Pedestrian Timing: Dynamically scale pedestrian green intervals according to real-time pedestrian group density.

๐Ÿ“œ License

Distributed under the MIT License. See LICENSE for complete license text.

About

Multi-agent traffic control simulation in Python, modeling autonomous vehicle and traffic management behaviors in a dynamic environment.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages