A parallel control system simulation and control library based on PyTorch.
Project description
torchcontrol
Introduction
torchcontrol is a parallel control system simulation and control library based on PyTorch. It supports batch simulation, classical and modern control, nonlinear systems, GPU acceleration, and visualization. The library is modular and extensible, suitable for both research and teaching.
Features
- Batch simulation: Simulate multiple environments in parallel (vectorized, GPU-friendly)
- Classical & modern control: Built-in PID controller, state-space, transfer function, and nonlinear system support
- Custom plants: Easily define linear or nonlinear plants (systems) with custom dynamics
- GPU acceleration: All computations support CUDA (if available)
- Visualization: Example scripts for step response, PID control, and nonlinear systems with matplotlib output
- Extensible: Modular design for adding new controllers, observers, or plants
Installation
From the project root, run:
pip install .
Or for development mode (auto-reload on code change):
pip install -e .
Directory Structure
- torchcontrol/ # Main package (controllers, plants, system, observers)
- examples/ # Example scripts (PID, nonlinear, batch, visualization)
- results/ # Output results (figures, logs, etc.)
- README.md
- setup.py
- pyproject.toml
How to Run Examples
After installation, run example scripts from the project root:
python3 examples/pid_with_internal_plant.py
python3 examples/pid_with_external_plant.py
python3 examples/second_order_plant_step_response.py
python3 examples/nonlinear_plant_step_response.py
- All import paths use package-level imports (e.g.,
from torchcontrol.controllers import PID). - Output files will be automatically saved in
examples/results/. - All examples support both CPU and GPU (CUDA) if available.
Example: Batch PID Control of Second-Order System
from torchcontrol.controllers import PID, PIDCfg
from torchcontrol.plants import InputOutputSystem, InputOutputSystemCfg
import torch
# System: G(s) = 1/(s^2 + 2s + 1)
dt = 0.01
num_envs = 16
num = [1.0]
den = [1.0, 2.0, 1.0]
initial_states = torch.rand(num_envs, 1) * 2
plant_cfg = InputOutputSystemCfg(numerator=num, denominator=den, dt=dt, num_envs=num_envs, initial_state=initial_states)
plant = InputOutputSystem(plant_cfg)
pid_cfg = PIDCfg(Kp=120.0, Ki=600.0, Kd=30.0, dt=dt, num_envs=num_envs, state_dim=1, action_dim=1, plant=plant)
pid = PID(pid_cfg)
# ...simulate and visualize...
Dependencies
- Python 3.8+
- torch
- torchdiffeq
- scipy
- numpy
- matplotlib
For GPU support, please ensure you have installed the CUDA version of PyTorch.
Customization & Extension
- To add new controllers, inherit from
ControllerBaseand register a config. - To add new plants, inherit from
PlantBaseor useInputOutputSystem/NonlinearSystem. - See
examples/for advanced usage and batch simulation.
For batch simulation, RL interface, or custom controllers/systems, please refer to the code structure in the torchcontrol/ directory.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torchcontrol-0.1.0.tar.gz.
File metadata
- Download URL: torchcontrol-0.1.0.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80528a93239cf39f5924d05bf756b76c9dd1327514593b9edd71d30a24fc6575
|
|
| MD5 |
6e83b364f623bf585e15f14812d3e9e3
|
|
| BLAKE2b-256 |
66aae95901a6441f01e6dc7e7902c2e249f069ece7afd52bdca66acb1617868c
|
Provenance
The following attestation bundles were made for torchcontrol-0.1.0.tar.gz:
Publisher:
python-publish.yml on TangLongbin/torchcontrol
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchcontrol-0.1.0.tar.gz -
Subject digest:
80528a93239cf39f5924d05bf756b76c9dd1327514593b9edd71d30a24fc6575 - Sigstore transparency entry: 217635494
- Sigstore integration time:
-
Permalink:
TangLongbin/torchcontrol@c5720e9aa7af94ce06cb93cfdb0676fd04ac62fd -
Branch / Tag:
refs/heads/main - Owner: https://github.com/TangLongbin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@c5720e9aa7af94ce06cb93cfdb0676fd04ac62fd -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file torchcontrol-0.1.0-py3-none-any.whl.
File metadata
- Download URL: torchcontrol-0.1.0-py3-none-any.whl
- Upload date:
- Size: 19.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b245bc8d8271c278da52f5c1b9d8c60c7ff8302eff9ca100c52b88523efc9ef4
|
|
| MD5 |
c2b2a72a97ecb1c3620136250e573a29
|
|
| BLAKE2b-256 |
c2bb2d68cc3bba6222d8998a6dfd37119908330b45ca7c8d5c94be24e3404d1d
|
Provenance
The following attestation bundles were made for torchcontrol-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on TangLongbin/torchcontrol
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchcontrol-0.1.0-py3-none-any.whl -
Subject digest:
b245bc8d8271c278da52f5c1b9d8c60c7ff8302eff9ca100c52b88523efc9ef4 - Sigstore transparency entry: 217635500
- Sigstore integration time:
-
Permalink:
TangLongbin/torchcontrol@c5720e9aa7af94ce06cb93cfdb0676fd04ac62fd -
Branch / Tag:
refs/heads/main - Owner: https://github.com/TangLongbin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@c5720e9aa7af94ce06cb93cfdb0676fd04ac62fd -
Trigger Event:
workflow_dispatch
-
Statement type: