HPIPM Documentation

repository·master·Indexed 20 days ago

https://github.com/giaf/hpipm

A high-performance interior-point method solver for dense, optimal control- and tree-structured convex quadratic programs (QP) and quadratically-constrained quadratic programs (QCQP). Optimized for small to medium scale problems in model predictive control and embedded optimization, HPIPM provides interfaces for C, Python, MATLAB, Octave, Julia, and Simulink, relying on the BLASFEO library for high-performance linear algebra.

Tokens
3.2K
Snippets
12
Records
16
Agent score
70%

What's inside HPIPM

  1. Understand HPIPM Matrix Formats and Memory Layout

    master

    HPIPM uses internal matrix and vector formats based on BLASFEO structs for high performance. The exact memory layout is architecture-dependent and cannot be inspected directly.

    Using C Interface

    • Column-major (Default): Use standard setter/getter routines. These expect matrices in column-major order (standard for Matlab, BLAS, and LAPACK).
    • Row-major: Use routines suffixed with _rowmaj. These expect input matrices in row-major order.

    Language-specific Layout Pitfalls

    When passing data to HPIPM, be aware of how your language stores matrices in memory:

    • Matlab: Dense matrices are column-major.
    • C: Arrays of arrays are row-major.
    • Python (NumPy): Arrays of arrays are row-major.

    If you use row-major routines, the input matrix is transposed while being packed/unpacked into the internal format.

  2. Install HPIPM for MATLAB and Octave (Linux)

    master

    The MATLAB/Octave interface uses MEX files.

    1. Clone BLASFEO: git clone https://github.com/giaf/blasfeo.git
    2. Build and install BLASFEO as a shared library: From the BLASFEO root, run make shared_library -j 4 && sudo make install_shared.
    3. Build and install HPIPM as a shared library: From the HPIPM root, run make shared_library -j 4 && sudo make install_shared.
    4. Compile the interface:
      • Navigate to hpipm/interfaces/matlab_octave.
      • Run source env.sh (ensure BLASFEO_MAIN_FOLDER matches your BLASFEO_PATH).
      • For Octave: make all -j 4.
      • For Matlab: make compile_mex_with_matlab.
    5. Run examples: Navigate to hpipm/examples/matlab_octave, run source env.sh, and launch Matlab/Octave from that same terminal.
    # 1. Clone BLASFEO
    git clone https://github.com/giaf/blasfeo.git
    
    # 2. Install BLASFEO shared
    cd blasfeo
    make shared_library -j 4 && sudo make install_shared
    
    # 3. Install HPIPM shared
    cd ..
    cd hpipm
    make shared_library -j 4 && sudo make install_shared
    
    # 4. Compile Interface
    cd interfaces/matlab_octave
    source env.sh
    # For Matlab:
    make compile_mex_with_matlab
    # For Octave:
    make all -j 4
    
    # 5. Run Examples
    cd ../../examples/matlab_octave
    source env.sh
    matlab
  3. Install the hpipm_python module

    master

    To use HPIPM in Python, you must first ensure that the shared libraries libblasfeo.so and libhpipm.so are installed on your system. Once the libraries are present, install the Python interface using pip from the hpipm/interface/python/ directory:

    pip install .

    Note: Depending on your system configuration, you may need to use pip3 instead of pip.

  4. Compile shared libraries for HPIPM and BLASFEO

    master

    Before using the Matlab/Octave interface, you must compile the required shared libraries libblasfeo.so and libhpipm.so. This is done from the root folders of blasfeo and hpipm respectively.

    make shared_library -j 4
    # Optionally install them:
    make install_shared
  5. Configure LD_LIBRARY_PATH for HPIPM shared libraries

    master

    After installing the shared libraries, you must ensure the system can locate libblasfeo.so and libhpipm.so by adding their installation paths to the LD_LIBRARY_PATH environment variable. By default, these are located in /opt/blasfeo/lib and /opt/hpipm/lib.

    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/blasfeo/lib:/opt/hpipm/lib
  6. Install HPIPM for MATLAB (Windows)

    master

    On Windows, the interface requires Microsoft Visual C++ and CMake.

    1. Install Microsoft Visual C++.
    2. Build BLASFEO: In the BLASFEO root, create a build directory, run cmake .., then cmake --build .. Copy blasfeo.lib from build/Debug/ to lib/.
    3. Build HPIPM: In the HPIPM root, create a build directory, run cmake .. (use -DBLASFEO_PATH=/path/to/blasfeo if necessary), then cmake --build .. Copy hpipm.lib from build/Debug/ to lib/.
    4. Compile MEX: In Matlab, navigate to hpipm/interfaces/matlab_octave. Run env.m (update BLASFEO_MAIN_FOLDER if needed), then run compile_mex_all.m.

    Note: Ensure Matlab is configured to use the same compiler (e.g., Visual Studio) used in step 1 to avoid runtime dependency errors.

    # BLASFEO Build
    cd blasfeo
    mkdir build && cd build
    cmake ..
    cmake --build .
    copy build\Debug\blasfeo.lib lib\
    
    # HPIPM Build
    cd ..
    cd hpipm
    mkdir build && cd build
    cmake -DBLASFEO_PATH=C:\path\to\blasfeo ..
    cmake --build .
    copy build\Debug\hpipm.lib lib\
  7. Run HPIPM Matlab/Octave examples

    master

    To run the examples provided in the examples/matlab_octave folder:

    1. Open a terminal and navigate to the examples folder.
    2. Run source env.sh to configure the environment.
    3. Launch Matlab or Octave from that same terminal.
    4. Run the example scripts within the Matlab/Octave environment.
    cd examples/matlab_octave
    source env.sh
    matlab
  8. Use HPIPM with Simulink

    master

    Simulink uses a QP model read from qp_data.c, which can be generated via C, Matlab, or Python interfaces.

    1. Complete the MATLAB interface installation steps.
    2. Compile S-function: Navigate to hpipm/examples/simulink and run make_sfun.m.
    3. Load parameters: Run load_paramaters.m to load parameters (horizon length, inputs, states) from qp_data.c.
    4. Run simulation: Open hpipm_simulink_getting_started.slx in Simulink and start the simulation.
    % Inside hpipm/examples/simulink
    make_sfun;
    load_paramaters;
    % Then open hpipm_simulink_getting_started.slx in Simulink
  9. Use HPIPM in Simulink via S-Function

    master

    To solve Quadratic Programs (QPs) within Simulink, you must compile the HPIPM S-Function into MEX binaries using code-generated QP data.

    Setup Steps:

    1. Prepare QP Data: Copy the C file containing your code-generated QP data into your current working directory.
    2. Configure Path: Open the make_sfun.m script and update the path to point to your copied C file.
    3. Environment Setup: Ensure the environment variables required by the make script are set (typically by sourcing env.sh in your shell before launching MATLAB/Simulink).
    4. Compile and Load: Run the following scripts in MATLAB:
      • make_sfun.m (to compile the S-Function into MEX binaries)
      • load_parameters.m (to load necessary parameters)
    5. Simulink Integration: Open the hpipm_simulink_getting_started.slx model to use the compiled HPIPM block.

    Important Considerations:

    • Signal Dimensions: If you update the QP data with different dimensions, you must manually adjust the signal dimensions within the Simulink block to match the new data.
  10. Use the HPIPM Simulink interface

    master
    The HPIPM Simulink interface is implemented as a single C file that defines an S-Function. To use HPIPM within Simulink, you must use this S-Function in conjunction with the generated C code that contains your specific Quadratic Program (QP) data.
  11. Install HPIPM and BLASFEO shared libraries

    master

    Before installing the Python module, you must build and install the required shared libraries (libblasfeo.so and libhpipm.so) from the blasfeo and hpipm root folders using make:

    make shared_library -j4 && sudo make install_shared
  12. Set up HPIPM for Julia

    master

    To run HPIPM examples in Julia (version > 0.5), you must install the required shared libraries and configure the Julia environment to interface with Python and the HPIPM modules.

    1. Install Shared Libraries

    From the blasfeo and hpipm root folders, run the following commands to build and install libblasfeo.so and libhpipm.so:

    make shared_library -j4 && sudo make install_shared

    Ensure the system can find these libraries by adding their locations to your LD_LIBRARY_PATH:

    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/blasfeo/lib:/opt/hpipm/lib

    2. Configure Julia and PyCall

    Julia requires the PyCall package to call the Python interpreter.

    If you need to use a specific Python interpreter, set the PYTHON environment variable in Julia before building PyCall:

    ENV["PYTHON"] = "path_to_python_interpreter"
    using Pkg
    Pkg.build("PyCall")

    3. Include HPIPM Julia Module

    Finally, include the HPIPM Julia interface file and ensure hpipm_python is installed for the Python version used by Julia:

    include("<path_to_hpipm_julia.jl>")
    make shared_library -j4 && sudo make install_shared