[Paper Review] Learning for CasADi: Data-driven Models in Numerical Optimization
This paper introduces L4CasADi, a framework that enables seamless integration of PyTorch-trained deep learning models into CasADi-based numerical optimization workflows. By compiling learned models into efficient C++ code and supporting hardware acceleration, L4CasADi allows for high-performance, differentiable optimization involving complex data-driven dynamics—demonstrated through energy-efficient fish trajectory planning in turbulent flows and collision-free motion planning using Neural Radiance Fields (NeRFs).
While real-world problems are often challenging to analyze analytically, deep learning excels in modeling complex processes from data. Existing optimization frameworks like CasADi facilitate seamless usage of solvers but face challenges when integrating learned process models into numerical optimizations. To address this gap, we present the Learning for CasADi (L4CasADi) framework, enabling the seamless integration of PyTorch-learned models with CasADi for efficient and potentially hardware-accelerated numerical optimization. The applicability of L4CasADi is demonstrated with two tutorial examples: First, we optimize a fish's trajectory in a turbulent river for energy efficiency where the turbulent flow is represented by a PyTorch model. Second, we demonstrate how an implicit Neural Radiance Field environment representation can be easily leveraged for optimal control with L4CasADi. L4CasADi, along with examples and documentation, is available under MIT license at https://github.com/Tim-Salzmann/l4casadi
Motivation & Objective
- Address the lack of native support for learned models in CasADi, a leading numerical optimization framework.
- Enable efficient, hardware-accelerated integration of deep learning models into optimization pipelines without sacrificing differentiability or performance.
- Bridge the gap between data-driven modeling (via PyTorch) and numerical optimization (via CasADi) for complex real-world problems.
- Support end-to-end optimization workflows that combine learned dynamics with constrained, nonlinear programming.
- Facilitate deployment of optimized models in standalone C/C++ applications for embedded or high-performance environments.
Proposed method
- Leverage PyTorch models as input and compile them into optimized C++ code via L4CasADi’s runtime compilation pipeline.
- Expose the compiled model as a differentiable symbolic function within CasADi’s MX graph, enabling seamless integration into NLPs.
- Support GPU and CPU inference through device-aware model compilation, enabling hardware acceleration during optimization.
- Generate standalone shared libraries (e.g., .so files) for deployment in external environments like MATLAB or C++.
- Use CasADi’s nonlinear programming (NLP) solvers (e.g., IPOPT) to solve optimization problems involving learned models.
- Represent complex environment dynamics—such as turbulent flows or NeRF-based scene densities—using trained neural networks within the optimization framework.

Experimental results
Research questions
- RQ1How can learned neural network models from PyTorch be efficiently and differentiably integrated into CasADi-based numerical optimization frameworks?
- RQ2To what extent can hardware acceleration (e.g., GPU) be leveraged within optimization workflows that include data-driven components?
- RQ3Can complex, non-convex optimal control problems involving implicit, high-dimensional environment representations (e.g., NeRFs) be solved using a hybrid learning-optimization pipeline?
- RQ4How does the performance and accuracy of optimization with learned models compare to analytical formulations in real-world scenarios?
- RQ5What is the practical feasibility of deploying optimized, learned models in standalone C/C++ applications for embedded or real-time systems?
Key findings
- L4CasADi successfully enables end-to-end differentiable optimization involving PyTorch models, with full support for GPU acceleration and C++ code generation.
- The framework allows for the optimization of a fish’s trajectory in a turbulent river using a PyTorch-based flow model, resulting in energy-efficient paths that exploit vortices.
- The fish trajectory demonstrates effective use of positive and negative vortices to minimize energy expenditure, with velocity commands remaining near zero in the y-direction.
- L4CasADi enables collision-free trajectory planning through NeRF-embedded 3D environments by treating NeRF density as an implicit obstacle function.
- The optimization of a 9th-degree polynomial trajectory under NeRF-based collision constraints is achieved using CasADi’s IPOPT solver, with the learned model integrated as a differentiable constraint.
- The framework supports deployment of optimized models as standalone shared libraries (e.g., libl4casadi_f.so), enabling use in MATLAB and C++ environments.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.