Convert Stable Baselines3 reinforcement learning agents to Simulink S-Functions using LibTorch.
SB3toSFunction enables you to deploy trained RL agents from Stable Baselines3 directly in Simulink for hardware-in-the-loop simulation, rapid prototyping, or integration with existing control systems.
Workflow:
SB3 Model (.zip) → Python Export → TorchScript (.pt) → C++ S-Function → Simulink
- Multi-algorithm support: SAC, TD3, PPO, A2C, DQN
- Auto-detection: Automatically detects algorithm type and dimensions
- MATLAB GUI: User-friendly interface for the complete workflow
- Command-line tools: Python script and MATLAB functions for automation
- Real-time ready: Optimized inference with
torch::NoGradGuard
| Software | Version | Notes |
|---|---|---|
| MATLAB | R2020a+ | With Simulink |
| Python | 3.8+ | With pip |
| Visual Studio | 2019+ | Windows, C++ build tools |
| LibTorch | 2.0+ | CPU version (download separately) |
pip install stable-baselines3>=2.0.0 torch>=2.0.0 gymnasium numpygit clone https://github.com/shyney7/SB3toSFunction.git
cd SB3toSFunctionpip install -r python/requirements.txtDownload LibTorch from pytorch.org:
- Select: LibTorch → C++/Java → CPU → Release
- Extract to the
libtorch/folder
mex -setup C++Note: LibTorch for Windows is built with MSVC (Visual Studio) and is not compatible with MinGW/GCC due to ABI differences.
cd path/to/SB3toSFunction
compile_sfunction()Before running Simulink simulations:
setenv('PATH', [fullfile(pwd, 'libtorch', 'lib') pathsep getenv('PATH')]);addpath('gui')
SB3toSFunction()- Select your SB3 model (.zip file)
- Click Export Model to create TorchScript
- Click Compile S-Function to build MEX file
- Click Test Model to verify
Export model (Python):
python python/export_model.py --input my_agent.zip --output my_agent.pt --verboseCompile S-Function (MATLAB):
compile_sfunction()Create Simulink test model:
addpath('examples')
create_test_model('test_model', 4, 1, 'my_agent.pt') % obs_dim=4, act_dim=1SB3toSFunction/
├── python/
│ ├── export_model.py # SB3 to TorchScript export
│ └── requirements.txt # Python dependencies
├── src/
│ └── libtorch_sfun.cpp # C++ S-Function source
├── gui/
│ └── SB3toSFunction.m # MATLAB GUI
├── examples/
│ ├── create_test_model.m # Simulink model generator
│ └── test_libtorch_sfun.slx
├── docs/
│ └── user_guide.md # Detailed documentation
├── libtorch/ # LibTorch library
├── compile_sfunction.m # MEX compilation helper
└── README.md
| Algorithm | Policy Extraction | Action Type |
|---|---|---|
| SAC | model.policy.actor |
Continuous [-1, 1] |
| TD3 | model.policy.actor |
Continuous [-1, 1] |
| PPO | model.policy.action_net |
Continuous (mean) |
| A2C | model.policy.action_net |
Continuous (mean) |
| DQN | model.policy.q_net |
Discrete (argmax) |
The S-Function block accepts three parameters:
| Parameter | Type | Description |
|---|---|---|
| Model Path | string | Path to the .pt TorchScript file |
| Observation Dim | integer | Number of observation inputs |
| Action Dim | integer | Number of action outputs |
Example:
'C:/models/my_agent.pt', 4, 1
┌──────────────┐ ┌─────────────────┐ ┌─────────┐
│ Constant │───▶│ libtorch_sfun │───▶│ Scope │
│ (obs_dim=4) │ │ S-Function │ │ │
└──────────────┘ └─────────────────┘ └─────────┘
Input Inference Output
observations (RL agent) actions
Add LibTorch to PATH:
setenv('PATH', ['C:\path\to\libtorch\lib' pathsep getenv('PATH')]);- Verify the
.ptfile path in S-Function parameters - Use forward slashes or escaped backslashes in paths
- Ensure Visual Studio 2019+ is installed
- Run
mex -setup C++to configure compiler - Verify LibTorch paths are correct
- Check
obs_dimandact_dimmatch your model - Inspect the
.jsonmetadata file created during export
# 1. Train in Python
from stable_baselines3 import SAC
model = SAC("MlpPolicy", "Pendulum-v1")
model.learn(10000)
model.save("pendulum_agent")# 2. Export to TorchScript
python python/export_model.py -i pendulum_agent.zip -o pendulum.pt -v% 3. Use in MATLAB/Simulink
compile_sfunction()
addpath('examples')
create_test_model('pendulum_sim', 3, 1, 'pendulum.pt')
sim('pendulum_sim')- Model loads once at simulation start (
mdlStart) - Inference uses
torch::NoGradGuardfor speed - CPU-only execution avoids CUDA overhead
- Suitable for real-time applications with proper tuning
Contributions are welcome! Please feel free to submit issues or pull requests.
GPLv2 License - see LICENSE file for details.
- Stable Baselines3 - RL training framework
- PyTorch/LibTorch - Deep learning framework
- MathWorks - MATLAB/Simulink platform