A multi-agent reinforcement learning framework built on MetaDrive for autonomous racing environments. MARR extends the original MetaDrive simulator to support simultaneous training of multiple independent agents using Independent Proximal Policy Optimization (IPPO).
- Concurrent Agent Management: Supports N simultaneous agents in a single environment instance
- Direct Policy Mapping: Each agent (
agent0,agent1, ...,agentN) maintains its own independent actor-critic network - Decentralized Execution: No centralized coordination or communication between agents during execution
For N agents, each agent i maintains its own policy πi(ai|si) and value function Vi(si), where:
Policy Objective:
Ji(θi) = 𝔼[∑t=0T γt rit]
Surrogate Loss Function:
LCLIP,i(θi) = 𝔼t[min(ratioti(θi) Ait, clip(ratioti(θi), 1-ε, 1+ε) Ait)]
Where:
- ratioti(θi) = πiθ(ait|sit) / πiθ_old(ait|sit)
- Ait = Generalized Advantage Estimation for agent i
- ε = 0.2 = clipping parameter
Temporal Difference Error:
δit = rit + γVi(sit+1) - Vi(sit)
Generalized Advantage Estimation (GAE):
Ait = ∑l=0T-t-1 (γλ)l δit+l
Value Function Loss:
LVF,i = (Viθ(sit) - Vitarget,t)2
Where Vitarget,t = Ait + Vi(sit) and λ = 0.95
LTOTAL,i = LCLIP,i + c₁ LVF,i - c₂ Sπiθ
Where:
- c₁ = 0.5 = value function coefficient
- c₂ = 0.01 = entropy coefficient
- Sπiθ = -∑a πiθ(a|sit) log πiθ(a|sit) = entropy bonus
# Core IPPO approach
for agent_id in range(num_agents):
policy = PPOPolicy(
observation_space=obs_space,
action_space=action_space,
config=ppo_config
)
agent_policies[agent_id] = policyKey Properties:
- Independent Learning: Each agent i optimizes Ji(θi) independently
- Non-Stationarity: Other agents appear as environment dynamics in MDPi
- Scalable Training: O(N) complexity scaling with number of agents
- Lane Width Variability: Configurable
lane_widthparameter for diverse track geometries - Checkpoint System: Strategically placed checkpoint objects with position-based rewards
- Finish Line Detection: Dedicated finish-line objects with completion bonuses
track_config = {
"lane_width": [3.0, 4.0, 5.0], # meters
"checkpoint_interval": 100, # meters
"track_complexity": "medium"
}For agent i at timestep t, the observation vector is:
sit = [segot, snavt, slidart] ∈ ℝ260
Ego State Vector segot ∈ ℝ8:
segot = [θsteering, θheading, vx, vy, ω, dfront, dleft, dright]T
Where:
- θsteering ∈ [-1, 1] (steering angle)
- θheading ∈ [0, 2π] (heading angle)
- vx, vy, ω = velocity components & angular velocity
- dfront, dleft, dright ∈ [0, 1] = proximity distances
Navigation State snavt ∈ ℝ12:
snavt = [x₁, y₁, x₂, y₂, ..., x₆, y₆]T
Where (xj, yj) are ego-relative waypoint coordinates transformed by:
[x'j, y'j]T = R(-θheading) · ([xj, yj]T - [xego, yego]T)
Surrounding State (Lidar) slidart ∈ ℝ240:
slidart[k] = min(dmax, ray_distance(θk)) / dmax
Where θk = k · (2π/240) for k ∈ {0, 1, ..., 239} and dmax = 50m
Policy Output: ait = [a₁, a₂]T ∈ [-1, 1]²
Vehicle Control Mapping:
usteering = a₁ · Smax where Smax = 0.4 rad
uthrottle = { a₂ · Fmax, if a₂ ≥ 0 (Fmax = 2000 N) { 0, if a₂ < 0
ubrake = { 0, if a₂ ≥ 0 { |a₂| · Bmax, if a₂ < 0 (Bmax = 1000 N)
Control Constraints:
- Steering rate limit: |dusteering/dt| ≤ 2.0 rad/s
- Throttle/brake mutual exclusion: uthrottle · ubrake = 0
For agent i at timestep t, the total reward is:
rit = Rpositivet - Cpenaltyt
Collision Cost:
Ccollisiont = αcol · I(collision_detected) = 10.0 · I(collision_detected)
Off-Road Cost:
Coff-roadt = αoff · ∫₀Δt I(off_road(τ)) dτ / Δt = 0.1 · toff_road / Δt
Line Crossing Cost:
Clinet = αline · (I(yellow_cross) + I(white_cross)) = 0.05 · ncrossings
Wrong-Side Driving Cost:
Cwrongt = αwrong · I(wrong_lane) = 0.5 · I(wrong_lane)
Progress Reward:
Rprogresst = βprog · Δdgoal / Ltrack = 2.0 · (digoal,t - digoal,t-1) / Ltrack
Speed Maintenance Reward:
Rspeedt = βspeed · min(vicurrent / vtarget, 1.0) = 0.1 · min(||vit|| / 15.0, 1.0)
Competitive Leading Reward:
Rleadingt = βlead · I(digoal,t = maxj djgoal,t) = 1.0 · I(leading_position)
Checkpoint Completion Reward:
Rcheckpointt = βcheck · I(checkpoint_reached) · (1 + 0.5 · I(first_to_reach)) = 5.0 · I(checkpoint_reached) · (1 + 0.5 · I(first_to_reach))
Finish Line Reward:
Rfinisht = βfinish · I(finish_reached) · (1 + 2.0 · I(race_winner)) = 50.0 · I(finish_reached) · (1 + 2.0 · I(race_winner))
Reward Bounds: rit ∈ [-11.15, 155.6] per timestep
Expected Return: Git = 𝔼[∑k=0∞ γk rit+k] where γ = 0.99
Coefficient Rationale:
- Progress dominates: βprog >> αpenalties encourages forward movement
- Safety penalties: αcol >> other_costs heavily penalizes crashes
- Competition incentives: βfinish >> βcheck >> βlead creates racing hierarchy
For agent i, the neural network processes observations through:
sit ∈ ℝ260 → h₁ ∈ ℝ256 → h₂ ∈ ℝ256 → h₃ ∈ ℝ128 → LSTM → {Actor, Critic}
Base MLP Layers:
h₁ = ReLU(W₁sit + b₁) where W₁ ∈ ℝ256×260
h₂ = ReLU(W₂h₁ + b₂) where W₂ ∈ ℝ256×256
h₃ = ReLU(W₃h₂ + b₃) where W₃ ∈ ℝ128×256
LSTM Recurrence:
hilstm,t, cilstm,t = LSTM(h₃, hilstm,t-1, cilstm,t-1)
Where LSTM cell size = 256, sequence length = 32
Actor Network (Policy):
μit = tanh(Wπ hilstm,t + bπ) where Wπ ∈ ℝ2×256
πiθ(ait|sit) = 𝒩(μit, Σ) (Gaussian policy)
Where covariance Σ = diag(σ₁², σ₂²) with learnable log standard deviations.
Critic Network (Value Function):
Viθ(sit) = Wv hilstm,t + bv where Wv ∈ ℝ1×256
- Total Parameters per Agent: ~847,000
- Shared Parameters: None (fully independent)
- Memory Requirements: ~3.4MB per agent policy
config = PPOConfig()
config.training(
lr=1e-3,
gamma=0.99,
lambda_=0.95,
clip_param=0.2,
vf_loss_coeff=0.5,
entropy_coeff=0.01
)
config.rollouts(
num_rollout_workers=4,
rollout_fragment_length=200
)- CPU: 4× Apple M1 processors
- Memory: 16GB unified memory
- Batch Size: 50 episodes synchronous updates
- Training Duration: 10M timesteps per agent
- Update Frequency: Every 10,000 environment steps
metrics = {
"completion_rate": episodes_finished / total_episodes,
"collision_rate": total_collisions / total_episodes,
"avg_episode_length": mean(episode_steps),
"off_road_cost": mean(accumulated_off_road_penalties),
"checkpoint_efficiency": checkpoints_hit / checkpoints_available,
"speed_maintenance": mean(speed_consistency_score)
}- Progress Reward Removal: -23% completion rate
- Collision Cost Removal: +180% collision rate
- Leading Bonus Removal: -15% competitive behavior
- Checkpoint Rewards Removal: -31% navigation efficiency
git clone https://github.com/your-repo/MARR.git
cd MARR
pip install -e .from metadrive.envs.marl_envs import MultiAgentMetaDrive
config = {
"num_agents": 4,
"start_seed": 1000,
"environment_num": 100,
"traffic_density": 0.1,
"accident_prob": 0.0,
"use_render": False,
"map": "SSSSSSSS", # 8 straight segments
}
env = MultiAgentMetaDrive(config)python train_ippo.py --config configs/racing_4_agents.yaml@article{marr2024,
title={Multi-Agent Reinforced Racers: Decentralized Learning in Procedural Racing Environments},
author={Your Name},
journal={Conference/Journal},
year={2024}
}This project extends MetaDrive and is released under the same Apache 2.0 License.