libmpc++ is a C++ header-only library to solve linear and non-linear MPC

Overview

libmpc++

libmpc++ is a C++ library to solve linear and non-linear MPC. The library is written in modern C++17 and it is tested to work on Linux, macOS and Windows.

The libmpc++ website can be found at the following link: https://altairlab.gitlab.io/libmpc/

Dependecies

The library depends on the following external libraries which must be installed on the machine before using libmpc++

If you are a developer, to setup the debug environment you also need to install:

Usage

The latest version of libmpc++ is available from GitHub https://github.com/nicolapiccinelli/libmpc/releases and does not require any installation process other than the one required by its dependecies.

Comments
  • Is this lab able to support output reference has ten sets of different value in each steps? If so, which file should I refer to? Thanks

    Is this lab able to support output reference has ten sets of different value in each steps? If so, which file should I refer to? Thanks

    I'd like to make a path planning with your lib. However, in your examples, it only has a constant output reference. However in my case, each step should has differenct values for output. Could you please help me to figure out how to do it?

    opened by ZhYGu 10
  • Working with multiple dynamically allocated controllers

    Working with multiple dynamically allocated controllers

    Hi @nicolapiccinelli I think this might be more of a question than an issue... (Unfortunately, I am no cpp guru) I've been having trouble instantiating multiple LMPC objects for problems of different dimensions, when I run the lines below the LMPC template captures the problem dimensions of the last statement only. For example when I try to set the matrices for latController, I get Eigen resize errors on setStateSpaceModel because the controller instance thinks that the dimensions of the problem are those of yawController instead of latController. Is there a way to instantiate multiple dynamically allocated problems like I'm trying to do? I tried the static allocation but my matrices are too big.

    // MPC LATERAL CONTROLLER
    mpc::LMPC<8, 2, 2, 5, 50, 3> latController;
    auto latCtrlInput = latController.getLastResult();
    
    // MPC LONGITUDINAL CONTROLLER
    mpc::LMPC<8, 2, 2, 5, 50, 3> longController;
    auto longCtrlInput = longController.getLastResult();
    
    // MPC VERTICAL CONTROLLER
    mpc::LMPC<4, 1, 1, 3, 50, 3> vertController;
    auto vertCtrlInput = vertController.getLastResult();
    
    // MPC YAW CONTROLLER
    mpc::LMPC<4, 1, 1, 2, 50, 3> yawController;
    auto yawCtrlInput = yawController.getLastResult();
    
    opened by camilogonzalez97 7
  • Solving with cuOSQP

    Solving with cuOSQP

    I am trying to implement a controller that needs to run at 100Hz but after a lot of tuning I can only get solutions in ~55ms. There is this version of OSQP that is CUDA accelerated (cuOSQP) and apparently it can speed up execution by up to two orders of magnitude for some problems.

    I thought this looked pretty promising so I tried to quickly glue it into your library by replacing the OSQPData struct with a custom struct that is compatible with what libmpc and cuOSQP expect. cuOSQP expects the data struct in the picture below which is internally generated from the csc* and c_float* that are in use on the normal OSQP.

    image

    So I just added the struct below to LOptimizer.hpp and replaced data with this new type.

    image

    I think that should make all of libmpc keep its current behaviour and the code actually compiles and runs but the solver always returns NaN for some reason and the objective function is 0 on the first evaluation.

    image

    I don't think this is an issue for libmpc but I thought I'd post here and ask if you have any ideas as to what might be causing it. It would certainly be awesome if this library could be even better and faster by using cuOSQP.

    opened by camilogonzalez97 6
  • Linking library from external cmake project

    Linking library from external cmake project

    Hello,

    I have an issue trying to use this library, i might be doing something wrong in my CMakeLists.txt file, but i have no idea if that's the underlying issue.

    Setup: Compiling with C++20 Using target_link_library(my_application mpc++)

    Its throwing the following errors: In file included from /usr/local/include/mpc/NLMPC.hpp:3: In file included from /usr/local/include/mpc/IMPC.hpp:3: /usr/local/include/mpc/IDimensionable.hpp:22:23: error: a non-type template parameter cannot have type 'mpc::MPCSize' template ^ /usr/local/include/mpc/IDimensionable.hpp:87:19: error: expected '(' for function-style cast or type construction double)>; ~~~~~~^ /usr/local/include/mpc/IDimensionable.hpp:90:31: error: expected expression cvec<sizer.ineq> &, ^ /usr/local/include/mpc/IDimensionable.hpp:94:19: error: expected '(' for function-style cast or type construction double)>; ~~~~~~^ /usr/local/include/mpc/IDimensionable.hpp:97:29: error: expected expression cvec<sizer.eq> &, ^ /usr/local/include/mpc/IDimensionable.hpp:102:29: error: expected expression cvec<sizer.nx> &, ^ /usr/local/include/mpc/IDimensionable.hpp:107:29: error: expected expression cvec<sizer.ny> &, ^ In file included from /SAM/Workspace/src/control_stack/path_tracking/src/mpc_node.cpp:1: In file included from /SAM/Workspace/src/control_stack/path_tracking/include/mpc/mpc.hpp:5: In file included from /usr/local/include/mpc/NLMPC.hpp:3: In file included from /usr/local/include/mpc/IMPC.hpp:4: In file included from /usr/local/include/mpc/IOptimizer.hpp:3: /usr/local/include/mpc/IComponent.hpp:21:23: error: a non-type template parameter cannot have type 'mpc::MPCSize' template ^ In file included from /SAM/Workspace/src/control_stack/path_tracking/src/mpc_node.cpp:1: In file included from /SAM/Workspace/src/control_stack/path_tracking/include/mpc/mpc.hpp:5: In file included from /usr/local/include/mpc/NLMPC.hpp:3: In file included from /usr/local/include/mpc/IMPC.hpp:4: /usr/local/include/mpc/IOptimizer.hpp:20:23: error: a non-type template parameter cannot have type 'mpc::MPCSize' template ^ In file included from /SAM/Workspace/src/control_stack/path_tracking/src/mpc_node.cpp:1: In file included from /SAM/Workspace/src/control_stack/path_tracking/include/mpc/mpc.hpp:5: In file included from /usr/local/include/mpc/NLMPC.hpp:3: /usr/local/include/mpc/IMPC.hpp:23:23: error: a non-type template parameter cannot have type 'mpc::MPCSize' template ^ In file included from /SAM/Workspace/src/control_stack/path_tracking/src/mpc_node.cpp:1: In file included from /SAM/Workspace/src/control_stack/path_tracking/include/mpc/mpc.hpp:5: In file included from /usr/local/include/mpc/NLMPC.hpp:5: In file included from /usr/local/include/mpc/NLMPC/Constraints.hpp:3: In file included from /usr/local/include/mpc/NLMPC/Base.hpp:4: /usr/local/include/mpc/NLMPC/Mapping.hpp:20:23: error: a non-type template parameter cannot have type 'mpc::MPCSize' template ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:46:29: error: use of undeclared identifier 'ph' Iz2uMat.resize((ph() * nu()), (nu() * ch())); ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:46:36: error: use of undeclared identifier 'nu' Iz2uMat.resize((ph() * nu()), (nu() * ch())); ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:46:44: error: use of undeclared identifier 'nu' Iz2uMat.resize((ph() * nu()), (nu() * ch())); ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:46:51: error: use of undeclared identifier 'ch' Iz2uMat.resize((ph() * nu()), (nu() * ch())); ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:47:29: error: use of undeclared identifier 'nu' Iu2zMat.resize((nu() * ch()), (ph() * nu())); ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:47:36: error: use of undeclared identifier 'ch' Iu2zMat.resize((nu() * ch()), (ph() * nu())); ^ In file included from /SAM/Workspace/src/control_stack/path_tracking/src/mpc/mpc.cpp:1: In file included from /SAM/Workspace/src/control_stack/path_tracking/include/mpc/mpc.hpp:5: In file included from /usr/local/include/mpc/NLMPC.hpp:3: In file included from /usr/local/include/mpc/IMPC.hpp:3: /usr/local/include/mpc/IDimensionable.hpp:22:23: error: a non-type template parameter cannot have type 'mpc::MPCSize' template ^ /usr/local/include/mpc/IDimensionable.hpp:87:19: error: expected '(' for function-style cast or type construction double)>; ~~~~~~^ /usr/local/include/mpc/IDimensionable.hpp:90:31: error: expected expression cvec<sizer.ineq> &, ^ /usr/local/include/mpc/IDimensionable.hpp:94:19: error: expected '(' for function-style cast or type construction double)>; ~~~~~~^ /usr/local/include/mpc/IDimensionable.hpp:97:29: error: expected expression cvec<sizer.eq> &, ^ /usr/local/include/mpc/IDimensionable.hpp:102:29: error: expected expression cvec<sizer.nx> &, ^ /usr/local/include/mpc/IDimensionable.hpp:107:29: error: expected expression cvec<sizer.ny> &, ^ In file included from /SAM/Workspace/src/control_stack/path_tracking/src/mpc/mpc.cpp:1: In file included from /SAM/Workspace/src/control_stack/path_tracking/include/mpc/mpc.hpp:5: In file included from /usr/local/include/mpc/NLMPC.hpp:3: In file included from /usr/local/include/mpc/IMPC.hpp:4: In file included from /usr/local/include/mpc/IOptimizer.hpp:3: /usr/local/include/mpc/IComponent.hpp:21:23: error: a non-type template parameter cannot have type 'mpc::MPCSize' template ^ In file included from /SAM/Workspace/src/control_stack/path_tracking/src/mpc/mpc.cpp:1: In file included from /SAM/Workspace/src/control_stack/path_tracking/include/mpc/mpc.hpp:5: In file included from /usr/local/include/mpc/NLMPC.hpp:3: In file included from /usr/local/include/mpc/IMPC.hpp:4: /usr/local/include/mpc/IOptimizer.hpp:20:23: error: a non-type template parameter cannot have type 'mpc::MPCSize' template ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:47:44: error: use of undeclared identifier 'ph' Iu2zMat.resize((nu() * ch()), (ph() * nu())); ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:47:51: error: use of undeclared identifier 'nu' Iu2zMat.resize((nu() * ch()), (ph() * nu())); ^ fatal error: too many errors emitted, stopping now [-ferror-limit=] In file included from /SAM/Workspace/src/control_stack/path_tracking/src/mpc/mpc.cpp:1: In file included from /SAM/Workspace/src/control_stack/path_tracking/include/mpc/mpc.hpp:5: In file included from /usr/local/include/mpc/NLMPC.hpp:3: /usr/local/include/mpc/IMPC.hpp:23:23: error: a non-type template parameter cannot have type 'mpc::MPCSize' template ^ In file included from /SAM/Workspace/src/control_stack/path_tracking/src/mpc/mpc.cpp:1: In file included from /SAM/Workspace/src/control_stack/path_tracking/include/mpc/mpc.hpp:5: In file included from /usr/local/include/mpc/NLMPC.hpp:5: In file included from /usr/local/include/mpc/NLMPC/Constraints.hpp:3: In file included from /usr/local/include/mpc/NLMPC/Base.hpp:4: /usr/local/include/mpc/NLMPC/Mapping.hpp:20:23: error: a non-type template parameter cannot have type 'mpc::MPCSize' template ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:46:29: error: use of undeclared identifier 'ph' Iz2uMat.resize((ph() * nu()), (nu() * ch())); ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:46:36: error: use of undeclared identifier 'nu' Iz2uMat.resize((ph() * nu()), (nu() * ch())); ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:46:44: error: use of undeclared identifier 'nu' Iz2uMat.resize((ph() * nu()), (nu() * ch())); ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:46:51: error: use of undeclared identifier 'ch' Iz2uMat.resize((ph() * nu()), (nu() * ch())); ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:47:29: error: use of undeclared identifier 'nu' Iu2zMat.resize((nu() * ch()), (ph() * nu())); ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:47:36: error: use of undeclared identifier 'ch' Iu2zMat.resize((nu() * ch()), (ph() * nu())); ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:47:44: error: use of undeclared identifier 'ph' Iu2zMat.resize((nu() * ch()), (ph() * nu())); ^ /usr/local/include/mpc/NLMPC/Mapping.hpp:47:51: error: use of undeclared identifier 'nu' Iu2zMat.resize((nu() * ch()), (ph() * nu())); ^ fatal error: too many errors emitted, stopping now [-ferror-limit=]

    opened by Veng97 1
  • test_lmpc fails with static assertion OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG

    test_lmpc fails with static assertion OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG

    Hi,

    Not sure if I got the execution right,

      g++ test_main.cpp test_lmpc.cpp -I./include -lCatch2
    

    but the test_lmpc fails with the following error, any suggestions? Thanks

    /usr/include/Eigen/src/Core/DenseStorage.h:33:3: error: static assertion failed: OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG
    /usr/include/Eigen/src/Core/DenseStorage.h: In instantiation of ‘void Eigen::internal::check_static_allocation_size() [with T = double; int Size = 30976]’:
    /usr/include/Eigen/src/Core/DenseStorage.h:110:41:   required from ‘Eigen::internal::plain_array<T, Size, MatrixOrArrayOptions, 16>::plain_array() [with T = double; int Size = 30976; int MatrixOrArrayOptions = 0]’
    /usr/include/Eigen/src/Core/DenseStorage.h:187:38:   required from ‘Eigen::DenseStorage<T, Size, _Rows, _Cols, _Options>::DenseStorage() [with T = double; int Size = 30976; int _Rows = 176; int _Cols = 176; int _Options = 0]’
    /usr/include/Eigen/src/Core/PlainObjectBase.h:484:55:   required from ‘Eigen::PlainObjectBase<Derived>::PlainObjectBase() [with Derived = Eigen::Matrix<double, 176, 176, 0, 176, 176>]’
    /usr/include/Eigen/src/Core/Matrix.h:292:5:   required from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const T&) [with T = Eigen::ReturnByValue<Eigen::KroneckerProduct<Eigen::Matrix<double, 11, 11, 0, 11, 11>, Eigen::CwiseUnaryOp<Eigen::internal::scalar_opposite_op<double>, const Eigen::Matrix<double, 16, 16, 0, 16, 16> > > >; _Scalar = double; int _Rows = 176; int _Cols = 176; int _Options = 0; int _MaxRows = 176; int _MaxCols = 176]’
    /usr/include/Eigen/src/Core/DenseBase.h:406:48:   required from ‘Eigen::DenseBase<Derived>::EvalReturnType Eigen::DenseBase<Derived>::eval() const [with Derived = Eigen::ReturnByValue<Eigen::KroneckerProduct<Eigen::Matrix<double, 11, 11, 0, 11, 11>, Eigen::CwiseUnaryOp<Eigen::internal::scalar_opposite_op<double>, const Eigen::Matrix<double, 16, 16, 0, 16, 16> > > >; Eigen::DenseBase<Derived>::EvalReturnType = const Eigen::Matrix<double, 176, 176, 0, 176, 176>]’
    /usr/local/include/mpc/ProblemBuilder.hpp:417:61:   required from ‘bool mpc::ProblemBuilder<Tnx, Tnu, Tndu, Tny, Tph, Tch>::buildTITerms() [with int Tnx = 12; int Tnu = 4; int Tndu = 4; int Tny = 12; int Tph = 10; int Tch = 10]’
    /usr/local/include/mpc/ProblemBuilder.hpp:173:16:   required from ‘bool mpc::ProblemBuilder<Tnx, Tnu, Tndu, Tny, Tph, Tch>::setStateModel(mpc::mat<Tnx, Tnx>&, mpc::mat<Tnx, Tnu>&, mpc::mat<Tny, Tnx>&) [with int Tnx = 12; int Tnu = 4; int Tndu = 4; int Tny = 12; int Tph = 10; int Tch = 10; mpc::mat<Tnx, Tnx> = Eigen::Matrix<double, 12, 12, 0, 12, 12>; mpc::mat<Tnx, Tnu> = Eigen::Matrix<double, 12, 4, 0, 12, 4>; mpc::mat<Tny, Tnx> = Eigen::Matrix<double, 12, 12, 0, 12, 12>]’
    /usr/local/include/mpc/LMPC.hpp:190:45:   required from ‘bool mpc::LMPC<Tnx, Tnu, Tndu, Tny, Tph, Tch>::setStateSpaceModel(mpc::mat<Tnx, Tnx>&, mpc::mat<Tnx, Tnu>&, mpc::mat<Tny, Tnx>&) [with int Tnx = 12; int Tnu = 4; int Tndu = 4; int Tny = 12; int Tph = 10; int Tch = 10; mpc::mat<Tnx, Tnx> = Eigen::Matrix<double, 12, 12, 0, 12, 12>; mpc::mat<Tnx, Tnu> = Eigen::Matrix<double, 12, 4, 0, 12, 4>; mpc::mat<Tny, Tnx> = Eigen::Matrix<double, 12, 12, 0, 12, 12>]’
    test_lmpc.cpp:62:44:   required from here
    
    opened by vkotaru 1
  • Future references for NLMPC

    Future references for NLMPC

    The NLMPC doesn't seem to have a way of setting the future reference output. I guess something similar to the implementation in LMPC, that adds the ability to set a reference output, yr, and reference input, ur. Maybe we can add these two vectors directly in the definition of the ObjectiveFunction handle.

    opened by Veng97 0
  • Future variables for NLMPC

    Future variables for NLMPC

    For my task i need to be able to specify online parameters in some way within the model itself. Specifically i need the ability to set the curvature for a cross-track-error model. This requires adding an additional horizon variable, e.g:

    auto stateEq = [&](mpc::cvec<Tnx>& dx,
                       mpc::cvec<Tnx> x,
                       mpc::cvec<Tnu> u,
                      mpc::cvec<Tnv> v) {
    ...
    };
    

    The new variable, v, can be used for online parameter settings along the entire prediction horizon. This would also allow updating parameters with online estimates of disturbances or model parameters.

    opened by Veng97 0
Releases(0.1.0)
  • 0.1.0(Nov 12, 2022)

    Added

    • Added support in linear mpc to define the references, weights, constraints and exogenous inputs different in each prediction step
    • Added general support to the retrival of the optimal sequence (state, input and output)

    Changed

    • The API to set the references, weights constraints and exogenous inputs using vector now requires a span of the horizon (Breaking changes)
    • Added new APIs to define the references, weights, constraints and exogenous inputs matrices to the whole horizon
    • Internal structure of the library has been refactored to separate non-linear and linear classes
    Source code(tar.gz)
    Source code(zip)
  • 0.0.9(Jul 6, 2022)

  • 0.0.8.2(Jun 20, 2022)

  • 0.0.8.1(Jun 15, 2022)

Owner
Nicola Piccinelli
Research fellow at Altair Laboratory
Nicola Piccinelli
Seidel's Algorithm: Linear-Complexity Linear Programming for Small-Dimensional Variables

SDLP Seidel's Algorithm: Linear-Complexity Linear Programming (LP) for Small-Dimensions About This solver is super efficient for small-dimensional LP

ZJU FAST Lab 43 Dec 19, 2022
a lean linear math library, aimed at graphics programming. Supports vec3, vec4, mat4x4 and quaternions

linmath.h -- A small library for linear math as required for computer graphics linmath.h provides the most used types required for programming compute

datenwolf 729 Jan 9, 2023
nml is a simple matrix and linear algebra library written in standard C.

nml is a simple matrix and linear algebra library written in standard C.

Andrei Ciobanu 45 Dec 9, 2022
C++ library for solving large sparse linear systems with algebraic multigrid method

AMGCL AMGCL is a header-only C++ library for solving large sparse linear systems with algebraic multigrid (AMG) method. AMG is one of the most effecti

Denis Demidov 578 Dec 11, 2022
Header only, single file, simple and efficient C++ library to compute the signed distance function to a triangle mesh

TriangleMeshDistance Header only, single file, simple and efficient C++11 library to compute the signed distance function to a triangle mesh. The dist

Interactive Computer Graphics 100 Dec 28, 2022
A work-in-progress C++20/23 header-only maths library for game development, embedded, kernel and general-purpose that works in constant context.

kMath /kmæθ/ A work-in-progress general-purpose C++20/23 header-only maths library that works in constant context Abstract The kMath Project aims to p

The λ Project 13 Sep 5, 2022
A C++ header-only library of statistical distribution functions.

StatsLib StatsLib is a templated C++ library of statistical distribution functions, featuring unique compile-time computing capabilities and seamless

Keith O'Hara 423 Jan 3, 2023
RcppFastFloat: Rcpp Bindings for the fastfloat C++ Header-Only Library

Converting ascii text into (floating-point) numeric values is a very common problem. The fast_float header-only C++ library by Daniel Lemire does this very well, and very fast at up to or over to 1 gigabyte per second as described in more detail in a recent arXiv paper.

Dirk Eddelbuettel 18 Nov 15, 2022
Header only FFT library

dj_fft: Header-only FFT library Details This repository provides a header-only library to compute fourier transforms in 1D, 2D, and 3D. Its goal is to

Jonathan Dupuy 134 Dec 29, 2022
C++ header-only fixed-point math library

fpm A C++ header-only fixed-point math library. "fpm" stands for "fixed-point math". It is designed to serve as a drop-in replacement for floating-poi

Mike Lankamp 392 Jan 7, 2023
C++ header-only library with methods to efficiently encode/decode Morton codes in/from 2D/3D coordinates

Libmorton v0.2.7 Libmorton is a C++ header-only library with methods to efficiently encode/decode 64, 32 and 16-bit Morton codes and coordinates, in 2

Jeroen Baert 488 Dec 31, 2022
Extremely simple yet powerful header-only C++ plotting library built on the popular matplotlib

matplotlib-cpp Welcome to matplotlib-cpp, possibly the simplest C++ plotting library. It is built to resemble the plotting API used by Matlab and matp

Benno Evers 3.6k Dec 30, 2022
A modern, C++20-native, single-file header-only dense 2D matrix library.

A modern, C++20-native, single-file header-only dense 2D matrix library. Contents Example usage creating matrices basic operations row, col, size, sha

feng wang 62 Dec 17, 2022
A header-only C++ library for large scale eigenvalue problems

NOTE: Spectra 1.0.0 is released, with a lot of API-breaking changes. Please see the migration guide for a smooth transition to the new version. NOTE:

Yixuan Qiu 609 Jan 2, 2023
Header-only C++11 library to handle physical measures

cpp-measures Header-only C++11 library to handle physical measures License: This project is released under the Mozilla Public License 2.0. Purpose Thi

Carlo Milanesi 20 Jun 28, 2018
A C++ header only library for decomposition of spectra into a sum of response functions whose weights are positive definite.

DecompLib A C++ header only library for decomposition of spectra into a sum of response functions whose weights are positive definite. Introduction Bu

James T. Matta 2 Jan 22, 2022
A matrix header-only library, uses graphs internally, helpful when your matrix is part of a simulation where it needs to grow many times (or auto expand)

GraphMat Header-only Library Matrix implemented as a graph, specially for the use case when it should be auto expanding at custom rate, specially in s

Aditya Gupta 3 Oct 25, 2021
Linear Algebra in C

Linear Algebra in C Quick Start Grab la.h and use it as an stb-style header-only library. For more info on such libraries see: https://github.com/noth

Tsoding 74 Nov 26, 2022
linalg.h is a single header, public domain, short vector math library for C++

linalg.h linalg.h is a single header, public domain, short vector math library for C++. It is inspired by the syntax of popular shading and compute la

Sterling Orsten 758 Jan 7, 2023