Skip to main content

Robust and nonlinear Direct Data-Driven MPC controllers for LTI and nonlinear systems in Python

Project description

Direct Data-Driven Model Predictive Control (MPC)

GitHub Actions status codecov Ruff Python License: MIT

Robust Data-Driven MPC Nonlinear Data-Driven MPC
Robust Data-Driven MPC Animation Nonlinear Data-Driven MPC Animation
Robust controller applied to an LTI system. Nonlinear controller applied to a Nonlinear system.

This repository provides a Python implementation of Direct Data-Driven Model Predictive Control (MPC) controllers for Linear Time-Invariant (LTI) and Nonlinear systems using CVXPY. It includes robust and nonlinear controllers implemented based on the Data-Driven MPC schemes presented in the papers "Data-Driven Model Predictive Control With Stability and Robustness Guarantees" and "Linear Tracking MPC for Nonlinear Systems—Part II: The Data-Driven Case" by J. Berberich et al.

A direct data-driven controller maps measured input-output data from an unknown system directly onto the controller without requiring an explicit system identification step. This approach is particularly useful in applications where the system dynamics are too complex to be modeled accurately or where traditional system identification methods are impractical or difficult to apply.


Disclaimer: This is an independent project based on the referenced papers and does not contain the official implementations from the authors.


Table of Contents

Requirements

This package requires the following:

  • Python (>=3.10, <3.13). Python 3.13 is not fully supported, as some dependencies have not been compiled for this version yet. We recommend using Python 3.10 to 3.12.
  • FFmpeg: Required for saving animations (e.g., GIF or MP4).
    • On Windows: You can download FFmpeg from the official FFmpeg website. Ensure it's correctly added to your system's PATH.
    • On Unix: You can install it using your package manager. For Debian/Ubuntu:
      sudo apt install ffmpeg
      
    Verify the installation by running this command:
    ffmpeg -version
    

Installation

Follow these steps to create a virtual environment and install this package:

[!NOTE] On some Unix-based systems (like Ubuntu/Debian), you may need to install venv manually if it's not included with Python:

  • On Ubuntu/Debian: sudo apt install python3-venv
  • On macOS and Windows: venv should be included with Python 3 by default. Make sure to check if Python is installed correctly.
  1. Clone this repository.
    git clone https://github.com/pavelacamposp/direct-data-driven-mpc.git
    
  2. Navigate to the project directory:
    cd direct-data-driven-mpc
    
  3. Create a virtual environment in the project directory:
    • Unix/macOS:
      python3 -m venv .venv
      
    • Windows:
      python -m venv venv
      
  4. Activate the virtual environment:
    • Unix/macOS:
      source .venv/bin/activate
      
    • Windows:
      venv\Scripts\activate
      
  5. Install direct-data-driven-mpc (Data-Driven MPC controller package):
    pip install -e .
    

[!NOTE] If you plan to contribute to or develop the project, you can install optional development dependencies by running:

pip install -e ".[dev]"

This will install tools like pre-commit and mypy. To enable automatic checks before each commit using pre-commit hooks, run:

pre-commit install

Usage

The example scripts lti_dd_mpc_example.py and nonlinear_dd_mpc_example.py demonstrate the setup, simulation, and data visualization of the Data-Driven MPC controllers applied to LTI and Nonlinear systems, respectively.

To run the example scripts, use the following commands:

  • Data-Driven MPC for LTI systems:

    Run the example script with a seed of 18, a simulation length of 400 steps, a verbosity level of 1, and save the generated animation to a file:

    python examples/lti_control/lti_dd_mpc_example.py --seed 18 --t_sim 400 --verbose 1 --save_anim
    
  • Data-Driven MPC for Nonlinear systems:

    Run the example script with a seed of 0, a simulation length of 3000 steps, a verbosity level of 1, and save the generated animation to a file:

    python examples/nonlinear_control/nonlinear_dd_mpc_example.py --seed 0 --t_sim 3000 --verbose 1 --save_anim
    

[!NOTE] The --save_anim flag requires FFmpeg to be installed. See the Requirements section for more details.

Customizing Controller Parameters

To use different controller parameters, modify the configuration files in examples/config/controllers/ for each controller, or specify a custom configuration file using the --controller_config_path argument.

Customizing System Models

Example system parameters are defined in examples/config/models/.

Customizing Plots

Matplotlib properties for input-output plots can be customized by modifying plot_params.yaml.

Additional Information

Some key arguments used in the scripts are listed below:

Argument Type Description
--controller_config_path str Path to the YAML file containing Data-Driven MPC controller parameters.
--t_sim int Simulation length in time steps.
--save_anim flag If passed, saves the generated animation to a file using FFmpeg.
--anim_path str Path where the generated animation file will be saved. Includes the file name and its extension (e.g., data-driven_mpc_sim.gif).
--verbose int Verbosity level: 0 = no output, 1 = minimal output, 2 = detailed output.

To get the full list of arguments, run each script with the --help flag.


For a deeper understanding of the project and how the controllers operate, we recommend reading through the scripts and the docstrings of the implemented utility functions and classes. The documentation includes detailed descriptions of how the implementations follow the Data-Driven MPC controller schemes and algorithms described in the referenced papers.

Paper Reproduction

Reproduction scripts are provided to validate our implementations by comparing them with the results presented in the referenced papers.

  • Data-Driven MPC for LTI systems:

    The reproduction is implemented in robust_lti_dd_mpc_reproduction.py. This script closely follows the example presented in Section V of [1], which demonstrates various Robust Data-Driven MPC controller schemes applied to a four-tank system model.

    To run the script, execute the following command:

    python examples/lti_control/robust_lti_dd_mpc_reproduction.py
    
  • Data-Driven MPC for Nonlinear systems:

    The reproduction is included in the example script nonlinear_dd_mpc_example.py, which closely follows the example presented in Section V of [2] for the control of a nonlinear continuous stirred tank reactor (CSTR) system.

    To run the script, execute the following command:

    python examples/nonlinear_control/nonlinear_dd_mpc_example.py --seed 0
    

The figures below show the expected output from executing these scripts. The graphs from our results closely resemble those shown in Fig. 2 of [1] and Fig. 2 of [2], with minor differences due to randomization.

LTI Data-Driven MPC Nonlinear Data-Driven MPC
Robust Data-Driven MPC Animation Nonlinear Data-Driven MPC Animation
Reproduction of results from [1] Reproduction of results from [2]

Code Structure

Direct Data-Driven MPC Controller

The project is structured as a Python package, encapsulating the main Data-Driven MPC controllers logic within the following modules:

The utility module direct_data_driven_mpc/utilities/hankel_matrix.py is used for constructing Hankel matrices and evaluating whether data sequences are persistently exciting of a given order.

Model Simulation

The following utility modules have been implemented to simulate LTI and Nonlinear systems:

Controller Creation

To modularize the creation of Data-Driven MPC controllers, the following utility modules are provided:

Data-Driven Controller Simulation

The direct_data_driven_mpc/utilities/controller/data_driven_mpc_sim.py module implements the main control loops for both Data-Driven MPC controllers, following Algorithms 1 and 2 of [1] for LTI systems and Algorithm 1 of [2] for Nonlinear systems.

Visualization (Static and Animated Plots)

Custom functions are implemented in direct_data_driven_mpc/utilities/visualization/ to display input-output data in static and animated plots. These functions use Matplotlib for visualization and FFmpeg for saving animations in various formats (e.g., GIF, MP4).

Examples

The examples directory contains scripts that demonstrate the operation of the Data-Driven MPC controller and reproduce the results presented in the referenced papers.

Configuration Files

The system and controller parameters used in the example scripts are defined in YAML configuration files in examples/config/. These parameters are based on the examples from Section V of [1] for LTI systems, and from Section V of [2] for Nonlinear systems.

A YAML loading function is provided in direct_data_driven_mpc/utilities/yaml_config_loading.py.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Citation

If you use the controller implementations in your research, please cite the original papers:

Data-Driven MPC Control for Linear Time-Invariant (LTI) systems

[1] J. Berberich, J. Köhler, M. A. Müller and F. Allgöwer, "Data-Driven Model Predictive Control With Stability and Robustness Guarantees," in IEEE Transactions on Automatic Control, vol. 66, no. 4, pp. 1702-1717, April 2021, doi: 10.1109/TAC.2020.3000182.

BibTex entry:

@article{Berberich_2021,
   title={Data-Driven Model Predictive Control With Stability and Robustness Guarantees},
   volume={66},
   ISSN={2334-3303},
   url={http://dx.doi.org/10.1109/TAC.2020.3000182},
   DOI={10.1109/tac.2020.3000182},
   number={4},
   journal={IEEE Transactions on Automatic Control},
   publisher={Institute of Electrical and Electronics Engineers (IEEE)},
   author={Berberich, Julian and Kohler, Johannes and Muller, Matthias A. and Allgower, Frank},
   year={2021},
   month=apr, pages={1702–1717}}

Data-Driven MPC Control for Nonlinear systems

[2] J. Berberich, J. Köhler, M. A. Müller and F. Allgöwer, "Linear Tracking MPC for Nonlinear Systems—Part II: The Data-Driven Case," in IEEE Transactions on Automatic Control, vol. 67, no. 9, pp. 4406-4421, Sept. 2022, doi: 10.1109/TAC.2022.3166851.

BibTex entry:

@article{Berberich_2022,
   title={Linear Tracking MPC for Nonlinear Systems—Part II: The Data-Driven Case},
   volume={67},
   ISSN={2334-3303},
   url={http://dx.doi.org/10.1109/TAC.2022.3166851},
   DOI={10.1109/tac.2022.3166851},
   number={9},
   journal={IEEE Transactions on Automatic Control},
   publisher={Institute of Electrical and Electronics Engineers (IEEE)},
   author={Berberich, Julian and Kohler, Johannes and Muller, Matthias A. and Allgower, Frank},
   year={2022},
   month=sep, pages={4406–4421} }

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

direct_data_driven_mpc-1.2.0.tar.gz (67.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

direct_data_driven_mpc-1.2.0-py3-none-any.whl (72.9 kB view details)

Uploaded Python 3

File details

Details for the file direct_data_driven_mpc-1.2.0.tar.gz.

File metadata

  • Download URL: direct_data_driven_mpc-1.2.0.tar.gz
  • Upload date:
  • Size: 67.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for direct_data_driven_mpc-1.2.0.tar.gz
Algorithm Hash digest
SHA256 511428f0ca24a62d8cdd05237008a6ceca8a8524f5754b3e1b594ed2b35404af
MD5 2f0c5e135356e66e6e4b9a7e4dc2ac64
BLAKE2b-256 36299c071479f2aedf1af7b3fc174d8117f8e5bae31370e86d6b75bc0ca4558d

See more details on using hashes here.

Provenance

The following attestation bundles were made for direct_data_driven_mpc-1.2.0.tar.gz:

Publisher: release.yml on pavelacamposp/direct-data-driven-mpc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file direct_data_driven_mpc-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for direct_data_driven_mpc-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98ad851817bcbc8715e3035c63c05e6b1224304b17364f95294d76dfccb52ec3
MD5 9a67fcb9648dbf4b63fe750e4929809f
BLAKE2b-256 7ffab201b3f093f3adc6335b518ee44f04e171ec4367c61a144f2e39b6302795

See more details on using hashes here.

Provenance

The following attestation bundles were made for direct_data_driven_mpc-1.2.0-py3-none-any.whl:

Publisher: release.yml on pavelacamposp/direct-data-driven-mpc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page