Implementation:CARLA simulator Carla InvertedAI Traffic Example
| Knowledge Sources | |
|---|---|
| Domains | Simulation, Traffic Generation, AI Integration |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
Example script that generates realistic traffic using the InvertedAI API integrated with CARLA, demonstrating external AI-driven traffic simulation with configurable vehicles, walkers, and simulation parameters.
Description
This script (invertedai_traffic.py) demonstrates integration of the InvertedAI external API for realistic traffic generation within CARLA. It imports invertedai and uses its AgentProperties, AgentState, and TrafficLightState classes. The argument_parser() function defines extensive configuration options including: vehicle count (-n, default 30), walker count (-w, default 10), vehicle/walker generation filters (--generationv/--generationw), safe spawning mode (--safe), hero vehicle designation (--hero), InvertedAI API key (--iai-key), recording toggle (--record), simulation length (--sim-length, default 120s), IAI location format (--location, default 'carla:Town10HD'), quadtree capacity (--capacity, default 100), and area dimensions (--width/--height, default 250). The script uses carla.command.SpawnActor for batch vehicle spawning and integrates InvertedAI's initialize and drive APIs for realistic agent behavior.
Usage
Use this example to learn how to integrate external AI traffic generation services with CARLA. It demonstrates a production-grade traffic simulation pipeline with the InvertedAI API, suitable for creating realistic test scenarios for autonomous driving development.
Code Reference
Source Location
- Repository: CARLA
- File: PythonAPI/examples/invertedai_traffic.py
Signature
import invertedai as iai
from invertedai.common import AgentProperties, AgentState, TrafficLightState
SpawnActor = carla.command.SpawnActor
def argument_parser():
"""Define argument parser with vehicle, walker, IAI, and simulation params."""
# Main integration flow:
# 1. Connect to CARLA and configure synchronous mode
# 2. Initialize InvertedAI with API key and location
# 3. Spawn vehicles using batch commands
# 4. Run simulation loop calling iai.drive() each tick
# 5. Apply AI-computed controls to CARLA vehicles
Import
# Run as a command-line tool (requires InvertedAI API key):
python invertedai_traffic.py --iai-key YOUR_API_KEY --location carla:Town10HD -n 30
# Requires: invertedai package
I/O Contract
| Argument | Type | Default | Description |
|---|---|---|---|
| --host | str | 127.0.0.1 | CARLA server IP address |
| -p, --port | int | 2000 | CARLA server TCP port |
| -n, --number-of-vehicles | int | 30 | Number of InvertedAI-controlled vehicles |
| -w, --number-of-walkers | int | 10 | Number of walkers |
| --safe | bool | True | Avoid accident-prone vehicle types |
| --filterv | str | vehicle.* | Vehicle blueprint filter pattern |
| --generationv | str | 3 | Vehicle generation (2, 3, All) |
| --filterw | str | walker.pedestrian.* | Walker blueprint filter |
| --iai-key | str | None | InvertedAI API key |
| --record | flag | False | Enable CARLA recording |
| --sim-length | int | 120 | Simulation duration in seconds |
| --location | str | carla:Town10HD | IAI-formatted map location |
| --capacity | int | 100 | Quadtree leaf capacity before splitting |
| --width | int | 250 | Full width of initialization area |
| --height | int | 250 | Full height of initialization area |
| --hero | flag | False | Designate one vehicle as hero |
| -s, --seed | int | None | Random seed for reproducibility |
Usage Examples
# Basic InvertedAI traffic generation
python invertedai_traffic.py --iai-key MY_KEY -n 50 --location carla:Town10HD
# With recording and hero vehicle
python invertedai_traffic.py --iai-key MY_KEY --record --hero --sim-length 300
# Custom area and capacity
python invertedai_traffic.py --iai-key MY_KEY --width 500 --height 500 --capacity 200
# Reproducible simulation with seed
python invertedai_traffic.py --iai-key MY_KEY -s 42 -n 30