From d0a6cc2f9f52c0960331bf917883011bc1a0a1f9 Mon Sep 17 00:00:00 2001 From: Jonathan Frey Date: Thu, 30 May 2019 11:53:04 -0400 Subject: [PATCH 1/3] README: add Matlab instructions, minor change matlab example --- README.md | 59 +++++++++++++++++++++++-------- examples/matlab/getting_started.m | 4 +-- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 67a74e79..670d7c20 100644 --- a/README.md +++ b/README.md @@ -2,23 +2,55 @@ This is HPIPM, a high-performance interior-point method solver for dense, optima -------------------------------------------------- -Getting Started: +## Getting Started: +The best way to get started with HPIPM is to check out the examples in `/hpipm/examples/`. +HPIPM can be directly used from `C`, but there are also interfaces to Python and Matlab. +Depending on which level you want to use HPIPM, check out the following section below. +The QP notation used in HPIPM can be found in the `doc` folder. -The best way to get started with HPIPM is to check out the examples in /hpipm/examples/c/ and /hpipm/examples/python/. In order to run the C example, follow the steps below: - -1) clone BLASFEO on your machine: 'git clone https://github.com/giaf/blasfeo.git' -2) from the BLASFEO root folder, run 'make static_library & sudo make install_static' -3) from the HPIPM root folder, run 'make static_library & make examples' +### C +In order to run the C examples in `/hpipm/examples/C/` follow the steps below: +1) clone BLASFEO on your machine: `git clone https://github.com/giaf/blasfeo.git` +2) from the BLASFEO root folder, run `make static_library & sudo make install_static` +3) from the HPIPM root folder, run `make static_library & make examples` 4) cd to /hpipm/examples/c/ and run getting_started.out to solve a simple OCP-structured QP. -If you would like to try out the Python interface, you will need to proceed as follows: -1) clone BLASFEO on your machine: 'git clone https://github.com/giaf/blasfeo.git' -2) from the BLASFEO root folder, run 'make shared_library & sudo make install_shared' -3) from the HPIPM root folder, run 'make shared_library & sudo make install_shared' -4) make sure that the location of the installed shared libraries is known to the system by running 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/blasfeo/lib:/opt/hpipm/lib'. If you would like +### Python +If you would like to try out the Python interface, check out the examples in `/hpipm/examples/python/` after going through the following steps: +1) clone BLASFEO on your machine: `git clone https://github.com/giaf/blasfeo.git` +2) from the BLASFEO root folder, run `make shared_library & sudo make install_shared` +3) from the HPIPM root folder, run `make shared_library & sudo make install_shared` +4) make sure that the location of the installed shared libraries is known to the system by running `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/blasfeo/lib:/opt/hpipm/lib`. If you would like to avoid running this command whenever opening a new shell. You can add the commands above to your .bashrc. -5) cd to /hpipm/interfaces/python/hpipm_python and run 'pip3 install .' -6) cd to /hpipm/examples/python and run 'python3 getting_started.py' to solve a simple OCP-structured QP. +5) cd to /hpipm/interfaces/python/hpipm_python and run `pip3 install .` +6) cd to /hpipm/examples/python and run `python3 getting_started.py` to solve a simple OCP-structured QP. + + +### MATLAB +Since the HPIPM MATLAB interface is based on the HPIPM Python interface, please first follow the steps in the previous paragraph, "Python". + +In order to be able to run the examples in `/hpipm/examples/matlab/`, it is necessary to install the shared libraries libblasfeo.so and libhpipm.so. +The required shared libraries can be installed by running +``` +make shared_library -j4 & sudo make install_shared +``` +from the blasfeo and hpipm root folder. +Make sure that the location of libblasfeo.so and libhpipm.so is known to the system by adding /opt/blasfeo/lib and /opt/hpipm/lib to LD_LIBRARY_PATH as +``` +export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/blasfeo/lib:/opt/hpipm/lib +``` +(notice that, under some OSs, the value environment variable LD_LIBRARY_PATH within MATLAB can be different from the system's value. As a quick workaround, try to start MATLAB from a terminal). + +Finally you will need to make sure that hpipm/interfaces/matlab/hpipm_matlab is in your MATLAB path and that hpipm_python has been installed for the Python version used by MATLAB. +You can check which version is used by running the command +``` +py.sys.path +``` +from the MATLAB command line and change it by running +``` +pyversion +``` + -------------------------------------------------- @@ -43,4 +75,3 @@ This license has been chosen to avoid the issues that the LGPL license implies i Unformally speaking, the GPL+CE license allows the HPIPM library to be linked to both statically and dynamically to produce an executable, and the resulting executable can be released under terms of your choice. Please refer to the LICENSE.txt file for the legally binding statement of the license. - diff --git a/examples/matlab/getting_started.m b/examples/matlab/getting_started.m index 3f6287ad..224e8b9a 100644 --- a/examples/matlab/getting_started.m +++ b/examples/matlab/getting_started.m @@ -1,7 +1,7 @@ % pyversion /usr/bin/python3 % py.sys.path -clear all +clearvars close all clc @@ -44,8 +44,6 @@ Jx = [1, 0; 0, 1]; x0 = [1; 1]; -Jsx = [1, 0; 0, 1]; -Zl = [1e5, 0; 0, 1e5]; Zu = [1e5, 0; 0, 1e5]; zl = [1e5; 1e5]; zu = [1e5; 1e5]; From 1ccc0bba53007a0fa7bc6c586b4ff9a3dd3e1922 Mon Sep 17 00:00:00 2001 From: Jonathan Frey Date: Tue, 18 Jun 2019 13:14:52 +0200 Subject: [PATCH 2/3] installation instructions for Matlab without duplication and structured into steps --- README.md | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 670d7c20..76f46ba4 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,8 @@ In order to run the C examples in `/hpipm/examples/C/` follow the steps below: ### Python If you would like to try out the Python interface, check out the examples in `/hpipm/examples/python/` after going through the following steps: 1) clone BLASFEO on your machine: `git clone https://github.com/giaf/blasfeo.git` -2) from the BLASFEO root folder, run `make shared_library & sudo make install_shared` -3) from the HPIPM root folder, run `make shared_library & sudo make install_shared` +2) from the BLASFEO root folder, run `make shared_library -j4 & sudo make install_shared` +3) from the HPIPM root folder, run `make shared_library -j4 & sudo make install_shared` 4) make sure that the location of the installed shared libraries is known to the system by running `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/blasfeo/lib:/opt/hpipm/lib`. If you would like to avoid running this command whenever opening a new shell. You can add the commands above to your .bashrc. 5) cd to /hpipm/interfaces/python/hpipm_python and run `pip3 install .` @@ -28,28 +28,15 @@ to avoid running this command whenever opening a new shell. You can add the comm ### MATLAB Since the HPIPM MATLAB interface is based on the HPIPM Python interface, please first follow the steps in the previous paragraph, "Python". - -In order to be able to run the examples in `/hpipm/examples/matlab/`, it is necessary to install the shared libraries libblasfeo.so and libhpipm.so. -The required shared libraries can be installed by running -``` -make shared_library -j4 & sudo make install_shared -``` -from the blasfeo and hpipm root folder. -Make sure that the location of libblasfeo.so and libhpipm.so is known to the system by adding /opt/blasfeo/lib and /opt/hpipm/lib to LD_LIBRARY_PATH as -``` -export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/blasfeo/lib:/opt/hpipm/lib -``` -(notice that, under some OSs, the value environment variable LD_LIBRARY_PATH within MATLAB can be different from the system's value. As a quick workaround, try to start MATLAB from a terminal). - -Finally you will need to make sure that hpipm/interfaces/matlab/hpipm_matlab is in your MATLAB path and that hpipm_python has been installed for the Python version used by MATLAB. -You can check which version is used by running the command -``` -py.sys.path -``` -from the MATLAB command line and change it by running -``` -pyversion -``` +In order to be able to run HPIPM from Matlab the following steps are additionally required: +1) Make sure that the location of the shared libraries `libblasfeo.so` and `libhpipm.so` is known to the system, see step 4) for Python. +2) Check the paths are defined in the terminal using: `echo $LD_LIBRARY_PATH` +3) Start Matlab from a terminal in which the `LD_LIBRARY_PATH` is defined correctly +4) Make sure that hpipm_python has been installed for the Python version used by MATLAB. +You can check which version is used by running the Matlab command `py.sys.path`. +Use the Matlab command `pyversion ` to change the Python interpreter used by Matlab. +5) Add `hpipm/interfaces/matlab/hpipm_matlab` to your MATLAB path. +6) Get started using HPIPM from Matlab by looking at the examples in `/hpipm/examples/matlab/`. -------------------------------------------------- @@ -74,4 +61,3 @@ Notes: This license has been chosen to avoid the issues that the LGPL license implies in case of static linking. Unformally speaking, the GPL+CE license allows the HPIPM library to be linked to both statically and dynamically to produce an executable, and the resulting executable can be released under terms of your choice. Please refer to the LICENSE.txt file for the legally binding statement of the license. - From 771052df4429254e767b8e8123387d46cdffc942 Mon Sep 17 00:00:00 2001 From: Jonathan Frey Date: Tue, 25 Jun 2019 13:54:57 +0200 Subject: [PATCH 3/3] matlab/python interface: fixed issue with empty shape input --- .../hpipm_python/hpipm_python/wrapper/hpipm_ocp_qp.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/interfaces/python/hpipm_python/hpipm_python/wrapper/hpipm_ocp_qp.py b/interfaces/python/hpipm_python/hpipm_python/wrapper/hpipm_ocp_qp.py index 999602ec..c05f2091 100644 --- a/interfaces/python/hpipm_python/hpipm_python/wrapper/hpipm_ocp_qp.py +++ b/interfaces/python/hpipm_python/hpipm_python/wrapper/hpipm_ocp_qp.py @@ -167,6 +167,17 @@ def __init__(self, dim): def set(self, field, value, idx=None): + # cast to np array + if type(value) is not np.ndarray: + if (type(value) is int) or (type(value) is float): + value_ = value + value = np.array((1,)) + value[0] = value_ + # reshape np arrays with empty shape, that occur when using hpipm_matlab + if type(value) is np.ndarray and value.shape == (): + value_ = value + value = np.array((1,)) + value[0] = value_ # non-native setters (not implemented as C APIs) setter_map = { "Jx" : self.set_Jx,