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.
- Overview
- Motivation
- Simulation Environment
- Agent Architecture
- Traffic Model & Decision-Making
- Project Structure
- Installation
- Running the Simulation
- Results & Performance
- Future Work
- License
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.
- Emergency Preemption: Emergency vehicles (ambulances) must navigate dense urban networks with zero latency, overriding normal traffic signal cycles to prevent life-threatening delays.
- Pedestrian Safety: Pedestrians at zebra crossings require instant, safe passage requests without creating sudden vehicle collisions or gridlock.
- Decentralized & Coordinated Control: Evaluating how message passing between specialized agents (CarAgent, AmbulanceAgent, TrafficLightAgent, PersonAgent, and CentralCordinateAgent) resolves complex bottleneck scenarios in real time.
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.
- Grid / Discrete Spatial Positions: Each road segment consists of discrete position slots (
position0 through 5). Reachingposition = 4triggers an intersection request, whileposition = 5marks 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).
- Signalized Roads (
road_1toroad_4): Equipped with traffic lights (Traffic_Light_1,Traffic_Light_2,Traffic_Light_3) and pedestrian zebra crossings (Zebra_Crossing_1toZebra_Crossing_3). - Unsignalized Priority Road (
road_9): Governed by priority yielding rules ("Priority" traffic sign) where vehicles must yield to main flow traffic onroad_5androad_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.
- Emergency Preemption (Highest Priority): Ambulances broadcast emergency signals, overriding all active cycles to force their road's signal to
greenwhile setting cross-street signals toredoryellow. - Pedestrian Priority (High Priority): Pedestrian crossing requests change signalized lights to
redfor vehicle traffic, provided no active emergency preemption is present. - Signalized Intersection Rules: Regular vehicles on
road_1-road_4must stop at red/yellow lights and only advance ongreen. - Priority Sign Yielding Rules: Vehicles on
road_9check for oncoming traffic onroad_5/road_6before proceeding.
The system comprises 5 distinct agent types interacting via XMPP message passing.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 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 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| 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. |
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.
- Approach Phase: Vehicle reaches
position = 4on a signalized road. - Query Phase: Vehicle sends an XMPP
informmessage containing"may i go?"tocentral@localhost. - Evaluation Phase: Central agent queries the corresponding
TrafficLightAgentfor current color state. - Command Phase:
- If
greenโ Central returns"move". Vehicle increments position to 5 and transitions to next road segment. - If
redoryellowโ Central returns"stop". Vehicle waits at position 4. - If
road_9(Priority Sign) โ Central checks ifroad_5orroad_6have vehicles at position 5. If occupied, returns"give priority".
- If
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
- Python:
3.9or higher - OS: macOS / Linux / Windows
-
Clone the repository:
git clone https://github.com/LaraSousa34/Autonomous_and_Intelligent_systems-Project1.git cd Autonomous_and_Intelligent_systems-Project1 -
Create and activate a virtual environment (recommended):
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
Execute the main simulation script using Python:
python3 simulation/main.pyTraffic_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.
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:
| 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. |
- 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.
- Multi-Intersection Coordination: Expand central coordination to support grid-wide green waves across adjacent intersections.
- Vehicle-to-Vehicle (V2V) Communication: Allow cars to negotiate merging order directly without relying entirely on the central coordinator.
- SUMO (Simulation of Urban MObility) Integration: Connect the SPADE multi-agent architecture with SUMO for photorealistic urban traffic flow and realistic micro-simulation dynamics.
- Adaptive Pedestrian Timing: Dynamically scale pedestrian green intervals according to real-time pedestrian group density.
Distributed under the MIT License. See LICENSE for complete license text.



