Skip to main content

Parallelized differentiable IDM computation layer for traffic simulation

Project description

DiffIDM [Arxiv]

This is the official code of our paper "Gradient-based Trajectory Optimization with Parallelized Traffic Simulation", which has been accepted to ICRA 2025. Here we provide the code for parallelized differentiable IDM computation layer, which could be easily adopted for larger scale traffic simulators. The computation layer can handle upto 2 million vehicles in real time using either CPU or GPU. Please see our paper for more details.

Install

You need to install pytorch to use our computation layer. Then, you can install our computation layer using pip.

pip install diffidm

After installation, you can use the computation layer as follows.

import torch
from diffidm import IDMLayer

device = 'cuda' if torch.cuda.is_available() else 'cpu'
num_vehicles = int(1e6)   # 1M vehicles

print("Device:", device)
print("Number of vehicles:", num_vehicles)

### randomly generate IDM variables
a_max = torch.rand(num_vehicles, device=device) * 5 + 5             # [5, 10], maximum acceleration
a_min = torch.rand(num_vehicles, device=device) * 5 - 10            # [-10, -5], minimum acceleration
a_pref = torch.rand(num_vehicles, device=device) * 4.9 + 0.1        # [0.1, 5], preferred acceleration
v_curr = torch.rand(num_vehicles, device=device) * 40 + 20          # [20, 60], current velocity
v_target = torch.rand(num_vehicles, device=device) * 40 + 20        # [20, 60], target velocity
pos_delta = torch.rand(num_vehicles, device=device) * 10 + 5        # [5, 15], headway distance to the leading vehicle
vel_delta = torch.rand(num_vehicles, device=device) * 20 + 10       # [10, 30], relative velocity to the leading vehicle
min_space = torch.rand(num_vehicles, device=device) * 9 + 1         # [1, 10], minimum space headway
time_pref = torch.rand(num_vehicles, device=device) * 4.9 + 0.1     # [0.1, 5], desired time headway
delta_time = torch.full((num_vehicles,), 0.01, device=device)       # 0.01, simulation time step

a_max = a_max.requires_grad_()
a_min = a_min.requires_grad_()
a_pref = a_pref.requires_grad_()
v_curr = v_curr.requires_grad_()
v_target = v_target.requires_grad_()
pos_delta = pos_delta.requires_grad_()
vel_delta = vel_delta.requires_grad_()
min_space = min_space.requires_grad_()
time_pref = time_pref.requires_grad_()
delta_time = delta_time.requires_grad_()
print("IDM variables generated.")

### forward pass: compute acceleration using IDM
start_event = torch.cuda.Event(enable_timing=True)
end_event = torch.cuda.Event(enable_timing=True)
start_event.record()

acc = IDMLayer.apply(
    a_max,
    a_min,
    a_pref,
    v_curr,
    v_target,
    pos_delta,
    vel_delta,
    min_space,
    time_pref,
    delta_time,
)

end_event.record()
torch.cuda.synchronize()
elapsed_time = start_event.elapsed_time(end_event)
print(f"Forward pass time: {elapsed_time} ms")

### backward pass: compute gradients of IDM variables
start_event.record()
acc.sum().backward()
end_event.record()
torch.cuda.synchronize()
elapsed_time = start_event.elapsed_time(end_event)
print(f"Backward pass time: {elapsed_time} ms")

If installed correctly, it would print as follows.

Device: cuda
Number of vehicles: 1000000
IDM variables generated.
Forward pass time: 1.222208023071289 ms
Backward pass time: 3.7359039783477783 ms

Now you can use it in your code!

Citation

If you found our work to be useful, please consider citing our work.

@article{son2024gradient,
  title={Gradient-based Trajectory Optimization with Parallelized Differentiable Traffic Simulation},
  author={Son, Sanghyun and Zheng, Laura and Clipp, Brian and Greenwell, Connor and Philip, Sujin and Lin, Ming C},
  journal={arXiv preprint arXiv:2412.16750},
  year={2024}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

diffidm-0.0.3.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

diffidm-0.0.3-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file diffidm-0.0.3.tar.gz.

File metadata

  • Download URL: diffidm-0.0.3.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for diffidm-0.0.3.tar.gz
Algorithm Hash digest
SHA256 de39ad0e8d926cab6bf517b7a015932abcb500bf93a7198b7ae268bd8dc98f7c
MD5 8f62d14b3a24ff8853254eec6e10aaf6
BLAKE2b-256 df57959544432184d4448766309b7a678c288d32965b12cbd21b6420a7030db9

See more details on using hashes here.

File details

Details for the file diffidm-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: diffidm-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for diffidm-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cdf4f79126c56a880aa7c8d2303f0341e92af79c2b6c5751326f82891b6c8ed4
MD5 2b9ebd3da79d60fc699b584c1f9cfff6
BLAKE2b-256 daf29422afbdd168e6af46a91906d5b68ad5b0035db2cc8bf5f11b1ecd494dfa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page