multiagent-simulation

Multi-Agent Traffic Simulation System

The Multi-Agent Traffic Simulation System is a sophisticated traffic management simulation built to model autonomous vehicle behavior in a complex urban environment. Developed as part of my AI coursework at Tecnológico de Monterrey, this project demonstrates the intersection of artificial intelligence, computer graphics, and distributed systems architecture.

The simulation features autonomous vehicles (agents) that navigate through a grid-based city environment filled with roads, traffic lights, obstacles, and destination points. Each vehicle independently calculates optimal routes, makes real-time decisions to avoid collisions, changes lanes when necessary, and obeys traffic signals—all while being visualized in an interactive 3D environment.

Key Features & Technical Highlights

01. Intelligent Pathfinding with Memoization

The core of each vehicle's intelligence lies in its pathfinding algorithm. Each car agent uses Breadth-First Search (BFS) to calculate the shortest path from its current position to its randomly assigned destination.

  • Route Caching: Routes are stored in a global memo dictionary with a key combining start position and destination.
  • Performance Gains: The system tracks memoization hits vs. misses, showing significant computational savings as the simulation runs.
  • Dynamic Recalculation: When vehicles change lanes, new routes are calculated from the new position and cached for future use.
02. Custom CautionScheduler for Collision Prevention

One of the most challenging aspects was preventing collisions at intersections. I developed a custom scheduler that manages agent activation order based on spatial awareness of 'danger zones.'

  • Danger Square Detection: The scheduler identifies intersection points and lane merge areas as danger squares.
  • Priority-Based Activation: Vehicles in danger zones are activated first, along with vehicles ahead of them.
  • Pre-Danger Buffers: Vehicles approaching danger zones are held back to maintain safe following distances.
03. Advanced Vehicle Behavior

Each vehicle exhibits realistic autonomous behavior:

  • Traffic Light Compliance: Cars detect when they're on a traffic light cell and stop when the light is red.
  • Dynamic Lane Changing: When stuck behind another vehicle for more than one step, cars attempt to change lanes.
  • Collision Avoidance: Vehicles check adjacent cells before moving and recalculate routes when obstacles are detected.
traffic-visualization
04. Real-Time 3D Visualization with WebGL

The simulation features a sophisticated 3D visualization system built with WebGL:

  • Custom Phong Shading: Implemented vertex and fragment shaders for realistic lighting and material properties.
  • Smooth Interpolation: Vehicle movements are interpolated over 200ms for fluid motion between grid cells.
  • Dynamic Traffic Lights: Traffic lights emit red/green glow using emission colors in the shader.
webgl-visualization
05. Flask REST API Architecture

The backend exposes a clean REST API that separates simulation logic from visualization. Key endpoints include /init, /getCars, /getTrafficLights, /getBuildings, /getRoads, and /update.

  • Decoupled Architecture: The simulation runs independently from the visualization.
  • Real-time Updates: The frontend polls for updates every 10 frames maintaining smooth animations.
06. Graph-Based City Representation

The city map is represented as a directed adjacency graph that's built automatically from a text file:

  • Text Map Parsing: Roads, traffic lights, obstacles, and destinations are encoded with ASCII characters.
  • Graph Generation: A BFS traversal builds the graph structure, detecting valid moves and lane changes.
07. Comprehensive Data Collection

The system tracks extensive simulation metrics:

  • Active vehicle count over time and total vehicles that successfully reached destinations.
  • Step count distribution showing how many steps vehicles took to reach destinations.
  • Memoization efficiency tracking cache hits vs. misses.
simulation-metrics

Challenges & Solutions

01. Preventing Deadlocks at Intersections

Initial implementations caused vehicles to collide or deadlock at intersections when multiple cars tried to occupy the same cell simultaneously. The solution was developing the CautionScheduler that identifies intersection danger zones at initialization and manages agent activation order.

02. Lane Changing Logic

When vehicles got stuck behind slower traffic, they would wait indefinitely, creating unrealistic traffic jams. I implemented intelligent lane-changing behavior that activates after a vehicle has been stopped for more than one step.

03. Performance Optimization

Recalculating routes with BFS for every vehicle at every step was computationally expensive. The memoization system caches calculated routes, and since many vehicles travel between the same start-destination pairs, this reduced redundant calculations significantly.

04. Smooth 3D Animation

The discrete grid-based simulation caused jerky vehicle movements in the 3D visualization. I implemented client-side interpolation where vehicle positions are smoothly animated over 200ms between grid cells.

traffic-simulation

Results & Impact

  • Autonomous Agent Coordination: Multiple vehicles navigate independently while avoiding collisions.
  • Realistic Traffic Patterns: Traffic lights create natural stopping patterns, and lane changing reduces congestion.
  • Performance Efficiency: Memoization reduces computational overhead by caching frequently-used routes.
  • Scalable Architecture: The REST API design allows the simulation to run independently from the visualization.

Project Info

  • Type:School Project
  • Date: October 2024 - November 2024
  • Location:Mexico City, Mexico
  • Stack:Python, Mesa, Flask, WebGL, Three.js, JavaScript