|
autogenu-jupyter
An automatic code generator and the continuation/GMRES (C/GMRES) based numerical solvers for nonlinear MPC
|
This project provides the continuation/GMRES method (C/GMRES method) based solvers for nonlinear model predictive control (NMPC) and an automatic code generator for NMPC, called AutoGenU.
The following C/GMRES based solvers are provided:
MultipleShootingCGMRESSolver : The multiple shooting based C/GMRES method with condensing of the state and costate directions.SingleShootingCGMRESSolver : The original C/GMRES method (single shooting).Please confirm that you clone this repository as
Otherwise, please do the following command:
Move to the local repository:
In the local repository, create and activate a virtual environment, then install the Python package via
The default installation is intentionally minimal and installs only NumPy and SymPy. Choose an extra for the environment you use:
In VS Code connected to WSL, select .venv/bin/python with Notebook: Select Notebook Kernel. Confirm the selected kernel from a notebook cell with:
AutoGenU.ipynb generates the following source files under your setting state equation, constraints, cost function, and parameters:
ocp.hpp : A definition of the optimal control problem (OCP).main.cpp : An executablb of the closed-loop simulation.CMakeLists.txt : Scripts to build C++ projects.python directory : Source files of Python interface via pybind11.You can generate these files, run simulations, plot results, and install the Python interfaces through AutoGenU.ipynb.
The build API uses CMake consistently on Linux, macOS, and Windows:
The legacy MSYS and MinGW generator names remain available. Build failures raise subprocess.CalledProcessError, and ag.get_executable_path() locates executables produced by both single- and multi-configuration generators.
The supported top-level API is explicitly defined by autogenu.__all__ and contains only problem-independent functionality: AutoGenU, NLPType, integration and logging helpers, documentation helpers, and the generic Plotter. Internal CMake helpers and example-specific animators are not exported at the package top level.
Example-specific animation helpers remain available from their module when needed by the bundled examples:
Advanced users can access the low-level, cross-platform build primitives from the dedicated module:
Application code should normally use AutoGenU.build_main() and AutoGenU.build_python_interface() instead.
The stable structure of generated C++, pybind11, Python package, and CMake files lives in autogenu/templates. autogenu.template_renderer renders the templates using explicit {{name}} placeholders and always writes UTF-8 files with LF line endings. Problem-specific symbolic expressions in ocp.hpp continue to be generated programmatically.
A minimal generated project is protected by a SHA-256 snapshot manifest in tests/snapshots/minimal_generation.json. Run the normal test suite to detect unintended changes:
After reviewing an intentional generator change, update the snapshot explicitly:
AutoGenU validates problem names, dimensions, finite numeric settings, vector lengths, control bounds, and generation prerequisites before writing or building generated code. Invalid types raise TypeError, invalid values or dimensions raise ValueError, and missing setup steps raise RuntimeError. Errors name the affected argument and include the expected and received values, which makes configuration mistakes directly actionable in a notebook. For example:
The installed package includes the PEP 561 py.typed marker and annotations for the public AutoGenU, integration, logging, plotting, installation, and build APIs. VS Code/Pylance can therefore report invalid argument types and provide return-type-aware completion without additional stub packages.
Run the same Pyright check used by CI with:
Generated simulations and Python bindings can enable compiler warnings as errors through the cross-platform build API:
This maps to /W4 /WX with MSVC and to -Wall -Wextra -Wpedantic -Werror with GCC and Clang. Unused callback parameters are excluded because generated OCP callbacks intentionally retain a stable signature even when a particular symbolic expression does not use every argument. The E2E CI matrix enables this policy on Linux, macOS, and Windows.
CI runs clang-tidy on the project C++ headers and a representative C++ example. Third-party Eigen and pybind11 headers are excluded. The enabled checks focus on compiler static analysis, use-after-move and loop defects, and unnecessary copies; every reported diagnostic fails the job.
Generated code can be built with AddressSanitizer and UndefinedBehaviorSanitizer when using GCC or Clang:
The equivalent CMake option is -DCGMRES_ENABLE_SANITIZERS=ON. The sanitizer CI job builds and runs a minimal generated simulation so that runtime memory and undefined-behavior findings fail the workflow.
The root project provides matching configure, build, and test presets for local development, VS Code CMake Tools, and CI:
Replace dev with strict, clang-tidy, or sanitizers to run the same quality mode used by CI. The clang-tidy preset expects clang-tidy-18 on PATH, while the sanitizers preset requires GCC or Clang. Every test preset runs the fast cgmres.smoke CTest, which exercises public headers, the horizon and solver defaults, and RK4 integration.
Python bindings are built and installed via .ipynb files. Activate the virtual environment before starting Jupyter; the bindings are installed into that environment's site-packages directory by default:
No manual PYTHONPATH setting is required. The interfaces can be imported as
cgmres C++ libraryAside from the notebook for the code-generation, the C++ cgmres library, which is a header-only library, can be installed by running
at the project root directory of autogenu-jupyter.
Then you can build the NMPC code with the generated ocp.hpp file and without .ipynb notebook files.
The examples are found in examples/cpp directory.
autogenu Python moduleThe Python module autogenu can be installed by running
at the project root directory of autogenu-jupyter. Further, if you install have installed header-only cgmres C++ library as step 4, then you can run .ipynb files for the code generation in everywhere.
C++ API documentation of cgmres library is found at https://ohtsukalab.github.io/autogenu-jupyter/.
Python interfaces are almost the same as the C++ API, so please refere to https://ohtsukalab.github.io/autogenu-jupyter/ even for Python interfaces as well as the tips for conversions between C++ and Python.
Demos are presented in cartpole.ipynb, pendubot.ipynb, hexacopter.ipynb, and mobilerobot.ipynb. You can obtain the following simulation results jusy by runnig these .ipynb files. The details of the each OCP formulations are described in each .ipynb files.


MIT
We'd appriciate if you use cite the following conference paper: