中文
Diff-MPC 是一个基于 PyTorch 构建的端到端可微分模型预测控制框架,专为自动驾驶车辆设计。与传统基于 CasADi/Acados 的 MPC 方案不同,Diff-MPC 将整个车辆动力学模型与控制优化问题嵌入神经网络计算图中,实现了物理模型参数的在线自适应与控制序列的梯度优化。
English
Diff-MPC is an end-to-end differentiable Model Predictive Control framework built on PyTorch, designed specifically for autonomous vehicles. Unlike traditional MPC solutions based on CasADi/Acados, Diff-MPC embeds the entire vehicle dynamics model and control optimization problem into a neural network computational graph, enabling online adaptation of physical model parameters and gradient-based control sequence optimization.
这是本项目的核心创新点。 传统 MPC 在模型失配(如轮胎侧偏刚度变化、空气阻力系数漂移)时控制性能急剧下降,且难以在线修正物理参数。
Diff-MPC 将关键物理参数(如轮胎侧偏刚度 nn.Parameter,利用真实状态与预测状态之间的残差进行反向传播,实时更新参数估计值。这使得控制器具备天然的自适应能力,无需额外的系统辨识模块。
# 可学习物理参数示例
self.Cf = nn.Parameter(torch.tensor(80000.0)) # 前轮侧偏刚度
self.Cr = nn.Parameter(torch.tensor(80000.0)) # 后轮侧偏刚度
self.m = nn.Parameter(torch.tensor(1500.0)) # 车辆质量This is the core innovation of this project. Traditional MPC suffers severe performance degradation under model mismatch (e.g., tire cornering stiffness variation, aerodynamic drag coefficient drift), with no way to correct physical parameters online.
Diff-MPC declares key physical parameters (e.g., tire cornering stiffness nn.Parameter, performing backpropagation using residuals between actual and predicted states to update parameter estimates in real-time. This grants the controller inherent adaptive capability without requiring separate system identification modules.
摒弃传统串行 QP/SQP 求解器,Diff-MPC 将 MPC 问题转化为无约束优化问题(通过 Barrier 函数或软约束处理),定义综合损失函数:
对控制输入序列
Abandoning traditional serial QP/SQP solvers, Diff-MPC reformulates the MPC problem as an unconstrained optimization problem (via barrier functions or soft constraints), defining a composite loss function. The control input sequence is directly differentiated using PyTorch autograd, solved efficiently with optimizers like Adam/L-BFGS.
利用 PyTorch 的 Tensor 并行能力,Diff-MPC 可同时评估成百上千条候选轨迹:
- Batch 轨迹 Rollout: 一次性前向传播整批候选控制序列
- 向量化代价计算: 并行计算所有轨迹的跟踪误差与约束违反程度
- Top-K 精炼: 快速筛选最优候选,局部精细优化
相比传统 CPU 单线程求解器,在复杂场景下可获得 10-100x 加速比。
Leveraging PyTorch's tensor parallelism, Diff-MPC can simultaneously evaluate hundreds or thousands of candidate trajectories:
- Batch Trajectory Rollout: Forward propagate entire batches of candidate control sequences at once
- Vectorized Cost Computation: Compute tracking errors and constraint violations for all trajectories in parallel
- Top-K Refinement: Rapidly filter optimal candidates for local fine-tuning
Compared to traditional CPU single-threaded solvers, 10-100x speedup can be achieved in complex scenarios.
| 传统 MPC (模型失配) | Diff-MPC (自动修正) |
|---|---|
![]() |
![]() |
| 轮胎侧偏刚度降低 30% 后轨迹偏离 | 自动辨识参数并保持稳定跟踪 |
可见轮胎侧偏刚度
$C_f$ 在约 50 个控制周期内快速收敛至真实值附近。
Diff-MPC/
├── diff_mpc/ # 核心计算库 | Core Computation Library
│ ├── models/ # 可微动力学模型 | Differentiable Dynamics Models
│ │ ├── kinematics.py # 运动学单车模型 | Kinematic Bicycle Model
│ │ └── dynamics.py # 动力学单车模型 | Dynamic Bicycle Model
│ ├── solver/ # 基于梯度的优化器 | Gradient-Based Optimizers
│ │ └── mpc_solver.py # MPC 求解器主逻辑 | Main MPC Solver Logic
│ ├── identify/ # 在线参数辨识模块 | Online Parameter Identification
│ │ └── param_estimator.py # 参数估计器 | Parameter Estimator
│ └── utils/ # 几何运算与坐标转换 | Geometry & Coordinate Transforms
├── envs/ # 轻量级车辆仿真环境 | Lightweight Vehicle Simulation
├── scripts/ # 演示与实验脚本 | Demo & Experiment Scripts
│ └── run_demo.py # 快速演示入口 | Quick Demo Entry
├── tests/ # 单元测试 | Unit Tests
└── docs/ # 理论推导与 API 文档 | Theory Derivation & API Docs
# 克隆仓库 | Clone the repository
git clone https://github.com/your-username/Diff-MPC.git
cd Diff-MPC
# 创建虚拟环境 (推荐) | Create virtual environment (recommended)
conda create -n diff-mpc python=3.9
conda activate diff-mpc
# 安装依赖 | Install dependencies
pip install -r requirements.txtimport torch
from diff_mpc.models.dynamics import DynamicBicycleModel
from diff_mpc.solver.mpc_solver import DiffMPCSolver
# 初始化可微动力学模型 | Initialize differentiable dynamics model
model = DynamicBicycleModel(dt=0.1, learnable_params=['Cf', 'Cr'])
# 配置 MPC 求解器 | Configure MPC solver
solver = DiffMPCSolver(
model=model,
horizon=20,
Q=torch.diag(torch.tensor([1.0, 1.0, 0.1, 0.1])), # 状态权重
R=torch.diag(torch.tensor([0.1, 0.1])), # 控制权重
)
# 求解最优控制序列 | Solve for optimal control sequence
x0 = torch.tensor([0.0, 0.0, 0.0, 10.0]) # [x, y, yaw, v]
x_ref = torch.tensor([10.0, 5.0, 0.1, 10.0])
u_opt, x_pred = solver.solve(x0, x_ref)
# 在线参数辨识 | Online parameter identification
model.update_params(real_state, predicted_state) # 自动反向传播更新采用经典动力学单车模型 (Dynamic Bicycle Model):
其中
Using the classic Dynamic Bicycle Model, where
| 方法 | 求解时间 (ms) | 跟踪误差 RMSE (m) | 参数自适应 |
|---|---|---|---|
| CasADi MPC (CPU) | 15.2 | 0.42 | ❌ |
| Acados MPC (CPU) | 2.8 | 0.38 | ❌ |
| Diff-MPC (CPU) | 8.5 | 0.35 | ✅ |
| Diff-MPC (GPU) | 0.9 | 0.31 | ✅ |
测试环境: Intel i7-12700K, NVIDIA RTX 3080, 预测时域 N=20
欢迎提交 Issue 和 Pull Request!请确保:
- 代码风格符合 PEP 8 规范
- 新功能需附带单元测试
- 提交信息清晰描述改动内容
We welcome Issues and Pull Requests! Please ensure:
- Code style follows PEP 8
- New features include unit tests
- Commit messages clearly describe changes
本项目基于 MIT License 开源。
This project is open-sourced under the MIT License.
如果您在研究中使用了 Diff-MPC,请引用:
If you use Diff-MPC in your research, please cite:
@misc{diffmpc2024,
title={Diff-MPC: Differentiable Model Predictive Control for Autonomous Driving},
author={Your Name},
year={2024},
howpublished={\url{https://github.com/your-username/Diff-MPC}}
}⭐ 如果这个项目对您有帮助,请给一个 Star!⭐
⭐ If this project helps you, please give it a Star! ⭐



