diffidm


Namediffidm JSON
Version 0.0.3 PyPI version JSON
download
home_pageNone
SummaryParallelized differentiable IDM computation layer for traffic simulation
upload_time2025-02-18 02:08:15
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseMIT License Copyright (c) 2025 Sanghyun Son, Laura Zheng Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords traffic parallelized differentiable trajectory optimization
VCS
bugtrack_url
requirements pandas shapely scipy tqdm matplotlib
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # DiffIDM [[Arxiv](https://arxiv.org/abs/2412.16750)]

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](https://pytorch.org/) to use our computation layer. Then, you can install our computation layer using `pip`.
```bash
pip install diffidm
```

After installation, you can use the computation layer as follows.
```python
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.
```bash
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.
```bibtex
@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}
}
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "diffidm",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "traffic, parallelized, differentiable, trajectory, optimization",
    "author": null,
    "author_email": "Sanghyun Son <shh1295@umd.edu>, Laura Zheng <lyzheng@umd.edu>",
    "download_url": "https://files.pythonhosted.org/packages/df/57/959544432184d4448766309b7a678c288d32965b12cbd21b6420a7030db9/diffidm-0.0.3.tar.gz",
    "platform": null,
    "description": "# DiffIDM [[Arxiv](https://arxiv.org/abs/2412.16750)]\n\nThis 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.\n\n# Install\n\nYou need to install [pytorch](https://pytorch.org/) to use our computation layer. Then, you can install our computation layer using `pip`.\n```bash\npip install diffidm\n```\n\nAfter installation, you can use the computation layer as follows.\n```python\nimport torch\nfrom diffidm import IDMLayer\n\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'\nnum_vehicles = int(1e6)   # 1M vehicles\n\nprint(\"Device:\", device)\nprint(\"Number of vehicles:\", num_vehicles)\n\n### randomly generate IDM variables\na_max = torch.rand(num_vehicles, device=device) * 5 + 5             # [5, 10], maximum acceleration\na_min = torch.rand(num_vehicles, device=device) * 5 - 10            # [-10, -5], minimum acceleration\na_pref = torch.rand(num_vehicles, device=device) * 4.9 + 0.1        # [0.1, 5], preferred acceleration\nv_curr = torch.rand(num_vehicles, device=device) * 40 + 20          # [20, 60], current velocity\nv_target = torch.rand(num_vehicles, device=device) * 40 + 20        # [20, 60], target velocity\npos_delta = torch.rand(num_vehicles, device=device) * 10 + 5        # [5, 15], headway distance to the leading vehicle\nvel_delta = torch.rand(num_vehicles, device=device) * 20 + 10       # [10, 30], relative velocity to the leading vehicle\nmin_space = torch.rand(num_vehicles, device=device) * 9 + 1         # [1, 10], minimum space headway\ntime_pref = torch.rand(num_vehicles, device=device) * 4.9 + 0.1     # [0.1, 5], desired time headway\ndelta_time = torch.full((num_vehicles,), 0.01, device=device)       # 0.01, simulation time step\n\na_max = a_max.requires_grad_()\na_min = a_min.requires_grad_()\na_pref = a_pref.requires_grad_()\nv_curr = v_curr.requires_grad_()\nv_target = v_target.requires_grad_()\npos_delta = pos_delta.requires_grad_()\nvel_delta = vel_delta.requires_grad_()\nmin_space = min_space.requires_grad_()\ntime_pref = time_pref.requires_grad_()\ndelta_time = delta_time.requires_grad_()\nprint(\"IDM variables generated.\")\n\n### forward pass: compute acceleration using IDM\nstart_event = torch.cuda.Event(enable_timing=True)\nend_event = torch.cuda.Event(enable_timing=True)\nstart_event.record()\n\nacc = IDMLayer.apply(\n    a_max,\n    a_min,\n    a_pref,\n    v_curr,\n    v_target,\n    pos_delta,\n    vel_delta,\n    min_space,\n    time_pref,\n    delta_time,\n)\n\nend_event.record()\ntorch.cuda.synchronize()\nelapsed_time = start_event.elapsed_time(end_event)\nprint(f\"Forward pass time: {elapsed_time} ms\")\n\n### backward pass: compute gradients of IDM variables\nstart_event.record()\nacc.sum().backward()\nend_event.record()\ntorch.cuda.synchronize()\nelapsed_time = start_event.elapsed_time(end_event)\nprint(f\"Backward pass time: {elapsed_time} ms\")\n```\n\nIf installed correctly, it would print as follows.\n```bash\nDevice: cuda\nNumber of vehicles: 1000000\nIDM variables generated.\nForward pass time: 1.222208023071289 ms\nBackward pass time: 3.7359039783477783 ms\n```\n\nNow you can use it in your code!\n\n# Citation\n\nIf you found our work to be useful, please consider citing our work.\n```bibtex\n@article{son2024gradient,\n  title={Gradient-based Trajectory Optimization with Parallelized Differentiable Traffic Simulation},\n  author={Son, Sanghyun and Zheng, Laura and Clipp, Brian and Greenwell, Connor and Philip, Sujin and Lin, Ming C},\n  journal={arXiv preprint arXiv:2412.16750},\n  year={2024}\n}\n```\n",
    "bugtrack_url": null,
    "license": "MIT License\n        \n        Copyright (c) 2025 Sanghyun Son, Laura Zheng\n        \n        Permission is hereby granted, free of charge, to any person obtaining a copy\n        of this software and associated documentation files (the \"Software\"), to deal\n        in the Software without restriction, including without limitation the rights\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n        copies of the Software, and to permit persons to whom the Software is\n        furnished to do so, subject to the following conditions:\n        \n        The above copyright notice and this permission notice shall be included in all\n        copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n        SOFTWARE.",
    "summary": "Parallelized differentiable IDM computation layer for traffic simulation",
    "version": "0.0.3",
    "project_urls": {
        "Homepage": "https://github.com/SonSang/diffidm"
    },
    "split_keywords": [
        "traffic",
        " parallelized",
        " differentiable",
        " trajectory",
        " optimization"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "daf29422afbdd168e6af46a91906d5b68ad5b0035db2cc8bf5f11b1ecd494dfa",
                "md5": "2b9ebd3da79d60fc699b584c1f9cfff6",
                "sha256": "cdf4f79126c56a880aa7c8d2303f0341e92af79c2b6c5751326f82891b6c8ed4"
            },
            "downloads": -1,
            "filename": "diffidm-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2b9ebd3da79d60fc699b584c1f9cfff6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 5427,
            "upload_time": "2025-02-18T02:08:13",
            "upload_time_iso_8601": "2025-02-18T02:08:13.856468Z",
            "url": "https://files.pythonhosted.org/packages/da/f2/9422afbdd168e6af46a91906d5b68ad5b0035db2cc8bf5f11b1ecd494dfa/diffidm-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "df57959544432184d4448766309b7a678c288d32965b12cbd21b6420a7030db9",
                "md5": "8f62d14b3a24ff8853254eec6e10aaf6",
                "sha256": "de39ad0e8d926cab6bf517b7a015932abcb500bf93a7198b7ae268bd8dc98f7c"
            },
            "downloads": -1,
            "filename": "diffidm-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "8f62d14b3a24ff8853254eec6e10aaf6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 5707,
            "upload_time": "2025-02-18T02:08:15",
            "upload_time_iso_8601": "2025-02-18T02:08:15.474772Z",
            "url": "https://files.pythonhosted.org/packages/df/57/959544432184d4448766309b7a678c288d32965b12cbd21b6420a7030db9/diffidm-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-18 02:08:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "SonSang",
    "github_project": "diffidm",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "pandas",
            "specs": [
                [
                    "==",
                    "2.2.3"
                ]
            ]
        },
        {
            "name": "shapely",
            "specs": [
                [
                    "==",
                    "2.0.7"
                ]
            ]
        },
        {
            "name": "scipy",
            "specs": [
                [
                    "==",
                    "1.15.1"
                ]
            ]
        },
        {
            "name": "tqdm",
            "specs": [
                [
                    "==",
                    "4.67.1"
                ]
            ]
        },
        {
            "name": "matplotlib",
            "specs": [
                [
                    "==",
                    "3.10.0"
                ]
            ]
        }
    ],
    "lcname": "diffidm"
}
        
Elapsed time: 1.25668s