osqp-eigen Documentation

repository·master·Indexed 19 days ago

https://github.com/gbionics/osqp-eigen

A C++ wrapper for the OSQP (Operator Splitting Quadratic Program) solver designed for use with the Eigen linear algebra library. It provides tools to instantiate solvers, configure settings, and solve QP problems, including support for updating problem data dynamically for applications like Model Predictive Control (MPC). Supports installation via conda, CMake, and Bazel.

Tokens
1.7K
Snippets
6
Records
9
Agent score
17%

What's inside osqp-eigen

  1. Convert MPC into a QP problem

    master

    Since osqp-eigen only handles standard Quadratic Programming (QP) problems, users implementing Model Predictive Control (MPC) must manually cast the MPC formulation into a QP form.

    An MPC problem involving a linear system tracking a reference state $x_r$ with state/input constraints can be mapped to the standard QP form:

    Minimize: $\frac{1}{2} x^T P x + q^T x$ Subject to: $l \leq A_c x \leq u$

    Where:

    • Hessian ($P$): A diagonal matrix of the cost matrices (e.g., $Q$ and $R$).
    • Gradient ($q$): Derived from the reference state $x_r$ and cost matrices.
    • Constraint Matrix ($A_c$): Encodes the system dynamics ($x_{k+1} = Ax_k + Bu_k$) and initial state.
    • Bounds ($l, u$): Encodes state/input limits and the initial state constraint.

    Reference implementations for these conversion functions are available in the repository's example code.

    // Reference implementation functions for casting MPC to QP
    castMPCToQPHessian(Q, R, mpcWindow, hessian);
    castMPCToQPGradient(Q, xRef, mpcWindow, gradient);
    castMPCToQPConstraintMatrix(a, b, mpcWindow, linearMatrix);
    castMPCToQPConstraintVectors(xMax, xMin, uMax, uMin, x0, mpcWindow, lowerBound, upperBound);
  2. Build osqp-eigen from source

    master

    For advanced users, you can build the library from source using CMake. After installation, you must set the OsqpEigen_DIR environment variable to the path where the library was installed.

    # 1. Clone the repository
    git clone https://github.com/gbionics/osqp-eigen.git
    
    # 2. Build it
    cd osqp-eigen
    mkdir build
    cd build
    cmake -DCMAKE_INSTALL_PREFIX:PATH=<custom-folder> ../
    make
    make install
    
    # 3. Add the following environmental variable
    export OsqpEigen_DIR=/path/where/you/installed/
  3. Initialize and solve an OSQP problem

    master

    To solve a QP problem using osqp-eigen, follow these steps:

    1. Instantiate the solver: Create an OsqpEigen::Solver instance.
    2. Configure settings: Use solver.settings()->set<Setting>() to modify default behaviors (e.g., enabling warm start).
    3. Set problem data: Define the number of variables/constraints and provide the Hessian, gradient, constraint matrix, and bounds using the solver.data()->set... methods. These methods return true on success.
    4. Initialize: Call solver.initSolver() to prepare the underlying OSQP structure.
    5. Solve: Call solver.solveProblem() and check the return flag against OsqpEigen::ErrorExitFlag::NoError.
    6. Retrieve solution: Use solver.getSolution() to get the resulting Eigen::VectorXd.
    // 1. Instantiate
    OsqpEigen::Solver solver;
    
    // 2. Configure settings
    solver.settings()->setWarmStart(true);
    
    // 3. Set data
    solver.data()->setNumberOfVariables(numberOfVariable);
    solver.data()->setNumberOfConstraints(numberOfConstraints);
    if(!solver.data()->setHessianMatrix(hessian)) return 1;
    if(!solver.data()->setGradient(gradient)) return 1;
    if(!solver.data()->setLinearConstraintMatrix(linearMatrix)) return 1;
    if(!solver.data()->setLowerBound(lowerBound)) return 1;
    if(!solver.data()->setUpperBound(upperBound)) return 1;
    
    // 4. Initialize
    if(!solver.initSolver()) return 1;
    
    // 5. Solve
    if(solver.solveProblem() != OsqpEigen::ErrorExitFlag::NoError) return 1;
    
    // 6. Get solution
    Eigen::VectorXd QPSolution = solver.getSolution();
  4. Integrate osqp-eigen into a CMake project

    master

    The library provides native CMake support. It exports the target OsqpEigen::OsqpEigen, which you can consume using find_package and target_link_libraries.

    cmake_minimum_required(VERSION 3.0)
    project(myproject)
    
    find_package(OsqpEigen REQUIRED)
    
    add_executable(example example.cpp)
    target_link_libraries(example OsqpEigen::OsqpEigen)
  5. Update QP problem data dynamically

    master

    If you need to solve a sequence of related problems (common in MPC) without re-initializing the entire solver, use the following methods to update specific components of the optimization problem:

    • OsqpEigen::Solver::updateBounds: Updates both upper and lower bounds simultaneously.
    • OsqpEigen::Solver::updateLowerBound: Updates only the lower bounds.
    • OsqpEigen::Solver::updateUpperBound: Updates only the upper bounds.
    • OsqpEigen::Solver::updateGradient: Updates the gradient vector $q$.
  6. Configure OSQP solver settings

    master

    The OsqpEigen::Settings class allows you to modify the default solver behavior. You access these settings through the solver instance using the set<Setting>() pattern.

    Example: Enabling warm start to speed up subsequent solves in a sequence.

    Note: The specific available settings are defined in the OsqpEigen::Settings class.

    solver.settings()->setWarmStart(true);