Code Monkey home page Code Monkey logo

osqp-matlab's Introduction

The Operator Splitting QP Solver

CI Code coverage License

PyPI - downloads Conda - downloads

Visit our GitHub Discussions page for any questions related to the solver!

The documentation is available at osqp.org

The OSQP (Operator Splitting Quadratic Program) solver is a numerical optimization package for solving problems in the form

minimize        0.5 x' P x + q' x

subject to      l <= A x <= u

where x in R^n is the optimization variable. The objective function is defined by a positive semidefinite matrix P in S^n_+ and vector q in R^n. The linear constraints are defined by matrix A in R^{m x n} and vectors l and u so that l_i in R U {-inf} and u_i in R U {+inf} for all i in 1,...,m.

Citing OSQP

If you are using OSQP for your work, we encourage you to

We are looking forward to hearing your success stories with OSQP! Please share them with us.

Bug reports and support

Please report any issues via the Github issue tracker. All types of issues are welcome including bug reports, documentation typos, feature requests and so on.

Numerical benchmarks

Numerical benchmarks against other solvers are available here.

osqp-matlab's People

Contributors

abrandemuehl avatar bstellato avatar ebarnard avatar gbanjac avatar goulart-paul avatar imciner2 avatar traversaro avatar vineetbansal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

osqp-matlab's Issues

Code generation with kkt.c/.h

The code generation routines appear to always (on both vector and matrix updates) copy the kkt.c file into the generated code, instead of ignoring it for the vector updates as the comment suggests should happen. I traced this back to the comparison in osqp.m:468 :
~strcmp(cfiles(i).name, fullfile(cdir, 'kkt.c'))
which should actually be:
~strcmp(cfiles(i).name, 'kkt.c')
since dir returns only the name of the file, not the full path in that field. Was the use of fullfile() a compatibility fix for older MATLAB versions? I have been doing this recent work on 2019a.

Additionally, kkt.h is still being copied into the generated source when vector updates is requested, even though kkt.c is not. Is this expected behavior? It seems that it should be ignored as well.

If changes are needed, I will make them on my current dev branch in my fork since they tie in with some work I am doing related to the Simulink interface.

GNU Octave support

Has the MEX interface to OSQP been successfully built for GNU Octave? If not consider this a feature request.

I tried following the instructions for building the MATLAB interface under Octave and initially got an error about verlessThan not being supported in Octave. After manually eliminating those calls in make_osqp.m, I get ...

CMake Error at CMakeLists.txt:201 (message):
  You need Matlab libraries to build the Matlab interface



-- The C compiler identification is AppleClang 11.0.3.11030032
-- The CXX compiler identification is AppleClang 11.0.3.11030032
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- We are on a Darwin system
-- Embedded is OFF
-- Printing is ON
-- Profiling is ON
-- User interrupt is ON
-- Floats are OFF
-- Long integers (64bit) are ON
-- Code coverage is OFF
-- MKL Pardiso: ON
-- Could NOT find Matlab (missing: Matlab_INCLUDE_DIRS Matlab_MEX_LIBRARY Matlab_MEX_EXTENSION Matlab_MX_LIBRARY) (found version "NOTFOUND")
-- Configuring incomplete, errors occurred!

That was on macOS. I got essentially the same error when I tried it on Linux.

I have very little experience with Octave and MEX, but I do know that it is close enough to the MATLAB MEX interface that IPOPT's Matlab interface builds under both MATLAB and Octave.

I suspect that someone with experience could get the build scripts to work in Octave with little effort.

Failure to generate c code using "float" type instead of "double"

Hi, I wanna generate C code for my MCU, and use "float" instead of "double". The Matlab command is:

"m.codegen('Ccode1','FLOAT',true,'LONG',false)"

This command is able to generate c code. However the type of numerical variable is still double.

1> [CMake] -- Floats are OFF
1> [CMake] -- Long integers (64bit) are ON

what's wrong with it?

Failure to compile on MacBook Air M2 (and binary library for the M series is missing)

I am unable to start using OSQP within Matlab on MacBook Air M2:

  1. the binaries available on the GitHub were only generated for the Intel-version of Macs (osqp_mex.mexmaci64), not the M-series Macs (osqp_mex.mexmaca64 expected).

  2. I am failing to compile from source: While compiling qsqp-matlab (cloned from GitHub according to the instructions) I get the following outputs

>> make_osqp
Compiling OSQP solver...						[done]
Compiling and linking osqpmex...ld: warning: object file (libosqp.a[2](auxil.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[3](error.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[4](lin_alg.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[5](osqp.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[6](proj.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[7](scaling.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[8](util.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[9](kkt.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[10](cs.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[11](polish.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[12](lin_sys.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[13](ctrlc.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[14](lib_handler.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[15](amd_1.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[16](amd_2.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[17](amd_aat.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[21](amd_order.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[22](amd_post_tree.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[23](amd_postorder.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[24](amd_preprocess.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[25](amd_valid.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[26](SuiteSparse_config.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[27](qdldl_interface.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[28](qdldl.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[29](pardiso_interface.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)
ld: warning: object file (libosqp.a[30](pardiso_loader.c.o)) was built for newer 'macOS' version (14.0) than being linked (11.0)

					[done]
Copying source files for codegen...					[done]

I am not able to interpret the warnings. My system is up-to-date (macOS Sonoma 14.3, Xcode 15.2), Matlab mex configured with Xcode:

>> mex -setup
MEX configured to use 'Xcode with Clang' for C language compilation.

With the compilation of the QSQP library itself I did not have problems. It compiled with no problems, generating (above all) libosqp.dylib library.

Whole matrix update of A or P causes matlab to crash

The OSQP documentation indicates that there is a shortcut syntax for updating the whole of the A or P matrices when using embedded code generation features (see here), but attempting to do so causes Matlab to crash.

Example:

%run OSQP codegen on a minimal problem
P = speye(2);
q = ones(2,1);
A = speye(2);
l = -ones(2,1);
u =  ones(2,1);

m = osqp;
m.setup(P,q,A,l,u); 

%run codegen, solve once
%m.codegen('osqp_codegen','parameters','matrices');
[x,y,st,iter,run_time] = emosqp('solve');


%updating A all at once causes a crash
A = A.*2;
[~,~,Ax] = find(A);
%emosqp('update_A',Ax,[],0);              %crashes
emosqp('update_A',Ax,1:nnz(A),nnz(A));   %works

%updating P all at once causes a crash
P = P.*2;
[~,~,Px] = find(P);
%emosqp('update_P',Px,[],0);              %crashes
emosqp('update_P',Px,1:nnz(P),nnz(P));   %works

This issue relates also to #30.

Solver codegen generates incorrect csc matrix for linsys_solver_L

On the master branch the workspace.c file generated seems to set the linsys_solver_L nzmax variable to a random value at each codegen. My guess would be uninitialized memory usage but I haven't tracked down where it's coming from yet.

clc; clear all; close all;


% Load P, q, A, l, u.
P = zeros(500, 500);
A = zeros(500, 500);
q = zeros(1, 500);
l = zeros(500, 1);
u = zeros(500, 1);


problem = osqp;
settings = problem.default_settings();
settings.verbose = 1;
problem.setup(P, q, A, l, u, settings);

% Not using LONG means that the compiler should warn you that you're overflowing an integer
% if you get a very big random value
problem.codegen('osqp_solver', 'force_rewrite', true, 'FLOAT', true, 'LONG', false);

problem.solve()

Incomplete QDLDL installed headers prevent manual compilation of mex

This issue only relates to manual building of the OSQP mex interface. It does not affect the recommended build method using make_osqp.m. See #26 for the origin of this issue.

When installing OSQP from source, the QDLDL build does not export <includedir>/qdldl/qdldl_interface.h. This causes manual compilation of the OSQP mex interface to fail when using only the standard OSQP installed binaries and include paths.

Solution : we should either export this header as part of a standard install, or (preferably) make a better build option for Octave so that this is not necessary.

Different P and A sizes cause Matlab to crash

When passing matrices P ([n x n]) and A ([m x n_1]) to problem setup function, if one accidentally defines matrix A in a way that n_1 != n (false optimizer size), Matlab crashes instead of reporting an error. In case no one reported it earlier, I think this is something which could easily be fixed and would improve the usability of the software.

CPU timings for update step

When OSQP is used within an NMPC framework where the dynamics change at each iteration, it would be very useful to return the time required for the update step in the info struct. The setup_time would be an upper bound on this timing but an exact value would be better for benchmarking purposes.

Move Matlab-specific interface code from main library to Matlab interface repo

The merging of osqp/osqp#396 in the main OSQP library repo will allow us to move the Matlab-specific printing functions to the Matlab repo, removing the need to include the Matlab headers and code in the main library.

This is also possible to do with the memory allocation functions currently, and hopefully the timing/interrupt functions will move to this as well, so we can then move all Matlab-interface specific code to the interface and remove it from the main library repo - allowing us to then remove the Matlab CMake parts from the main library and only have it here in the interface.

Update to use new v1.0 API

Version 1.0 of the C library makes several changes to the API, so the mex file needs to be updated to use the new API.

Use OSQP as a custom solver for Matlab MPC

I am raising this issue to request for an interface to enable using OSQP as a custom solver with the MathWorks MPC tools for simulation and code generation. This interface will leverage the design capabilities of MPC Toolbox software and the computational performance of OSQP solver.

The following links explain the process and include some examples:

https://www.mathworks.com/help/mpc/ug/qp-solver.html?searchHighlight=QP%20solver&s_tid=srchtitle
and
https://www.mathworks.com/help/mpc/ug/generate-code-for-mpc-controller-with-custom-qp-solver.html
https://www.mathworks.com/help/mpc/ug/simulate-mpc-controller-with-a-custom-qp-solver.html

All needed would be an EML (that can be code generated) or MEX file using the OSQP code that can be called within a function [x,status] = mpcCustomSolverCodeGen(H,f,A,b,x0) signature.

Getting codegen to work in MATLAB

After troubleshooting getting codegen to work in MATLAB, I've got some errors to report and solutions for people struggling like I was. If these cannot be fixed, it would be helpful to have some messages in the docs about these specifics.

  • In MATLAB 2021a the supported minGW compiler (installed in the MATLAB gui as a support package) fails with error "Error configuring CMAKE environment" because "CMAKE_MAKE_PROGRAM" is not set. The osqp docs currently suggest installing this compiler through a url to matlab's supported compilers, so this error is bound to happen for others.

Solution:

  1. Install the minGW compiler from TDM-GCC at https://jmeubank.github.io/tdm-gcc/download/
  2. Add this line to your matlab code:
    setenv('MW_MINGW64_LOC', 'C:\TDM-GCC-64');
  3. Add this line to your matlab code:
    setenv('PATH', [getenv('PATH') ';C:\TDM-GCC-64\bin']);
    where C:\TDM-GCC-64 is replaced in 2 and 3 with the location of your TDM-GCC installation.
  • When installing osqp from the MATLAB terminal using the commands given in the osqp docs:
    >>websave('install_osqp.m', 'https://dl.bintray.com/bstellato/generic/OSQP/0.6.2/install_osqp.m');
    >>install_osqp
    the installation is saved to whatever directory is matlab's current working directory. If this path is too long, codegen will fail at some point due to mex.

Solution:

  1. Delete the osqp folder.
  2. Set matlab's working directory to a short path.
  3. Reinstall osqp using the same commands as before.

0.6.2 tag?

Hi everyone,
as usual, thanks a lot for the amazing work done on osqp and the related software.

It seems that OSQP Matlab binaries are distributed for the recent 0.6.2 release of osqp (from https://dl.bintray.com/bstellato/generic/OSQP/0.6.2/install_osqp.m), that I guess they have been generated from https://github.com/oxfordcontrol/osqp-matlab/tree/release-0.6.2 . Do you think it could make sense to add a v0.6.2 tag (similar to the tag that were done in the past: https://github.com/oxfordcontrol/osqp-matlab/releases) to the corresponding commit, to permit user to easily use the v0.6.2 version even if the compile osqp-matlab from source?

Thanks in advance.

tlc file error occurred while generating the code

Hello,
I created a new Simulink model following the steps of osqp/osqp#113 (comment) and the simulation worked fine,

image

but when I generated the code, I got the following error

Error:Error: File: C:\Program Files\MATLAB\R2023a\rtw\c\tlc\lib\blkiolib.tlc Line: 2429 Column: 13
The == and != operators can only be used to compare values of the same type

here is the model file and detail error message :
err_msg.txt
osqp_demo.zip

Can anyone give me some help or advice? Thanks!

BTW, I can generate the code for the example model quadcopter_example_codegen

Failing Tests on Windows 10 + MATLAB R2020a

I installed OSQP using the official binaries in bintray and the provided script as instructed in the guide.

I saw the run_osqp_tests.m file and ran it.
The result I got is:

Failure Summary:

     Name                                             Failed  Incomplete  Reason(s)
    ==============================================================================================
     codegen_mat_tests/test_solve                       X         X       Errored.
    ----------------------------------------------------------------------------------------------
     codegen_mat_tests/test_update_P                    X         X       Errored.
    ----------------------------------------------------------------------------------------------
     codegen_mat_tests/test_update_P_allind             X         X       Errored.
    ----------------------------------------------------------------------------------------------
     codegen_mat_tests/test_update_A                    X         X       Errored.
    ----------------------------------------------------------------------------------------------
     codegen_mat_tests/test_update_A_allind             X         X       Errored.
    ----------------------------------------------------------------------------------------------
     codegen_mat_tests/test_update_P_A_indP_indA        X         X       Errored.
    ----------------------------------------------------------------------------------------------
     codegen_mat_tests/test_update_P_A_indP             X         X       Errored.
    ----------------------------------------------------------------------------------------------
     codegen_mat_tests/test_update_P_A_indA             X         X       Errored.
    ----------------------------------------------------------------------------------------------
     codegen_mat_tests/test_update_P_A_allind           X         X       Errored.
    ----------------------------------------------------------------------------------------------
     codegen_vec_tests/test_solve                       X         X       Errored.
    ----------------------------------------------------------------------------------------------
     codegen_vec_tests/test_update_q                    X         X       Errored.
    ----------------------------------------------------------------------------------------------
     codegen_vec_tests/test_update_l                    X         X       Errored.
    ----------------------------------------------------------------------------------------------
     codegen_vec_tests/test_update_u                    X         X       Errored.
    ----------------------------------------------------------------------------------------------
     codegen_vec_tests/test_update_bounds               X         X       Errored.
    ----------------------------------------------------------------------------------------------
     feasibility_tests/test_feasibility_problem         X                 Failed by verification.
    ----------------------------------------------------------------------------------------------
     unconstrained_tests/test_unconstrained_problem     X                 Failed by verification.
    ----------------------------------------------------------------------------------------------
     update_matrices_tests/test_solve                   X                 Failed by verification.
    ----------------------------------------------------------------------------------------------
     update_matrices_tests/test_update_P                X                 Failed by verification.
    ----------------------------------------------------------------------------------------------
     update_matrices_tests/test_update_P_allind         X                 Failed by verification.
    ----------------------------------------------------------------------------------------------
     update_matrices_tests/test_update_A                X                 Failed by verification.
    ----------------------------------------------------------------------------------------------
     update_matrices_tests/test_update_A_allind         X                 Failed by verification.
    ----------------------------------------------------------------------------------------------
     update_matrices_tests/test_update_P_A_indP_indA    X                 Failed by verification.
    ----------------------------------------------------------------------------------------------
     update_matrices_tests/test_update_P_A_indP         X                 Failed by verification.
    ----------------------------------------------------------------------------------------------
     update_matrices_tests/test_update_P_A_indA         X                 Failed by verification.
    ----------------------------------------------------------------------------------------------
     update_matrices_tests/test_update_P_A_allind       X                 Failed by verification.

Looking at the folder tree:

image

It seems something is creating the folders one inside the other.

It seems the code generation procedure is failing.
Any idea?

Any information I can farther provide to assist fixing it?

Thank You.

MATLAB crashes when calling code-generated function

Hello,
I'm using the code-generation feature to speed up the solver. The code generation is done like shown in the documentation:

settings.verbose = false;
prob = osqp;
prob.setup(H, q, Aeq, beq, beq, settings); 
prob.codegen('osqp_codegen','parameters','matrices');

I then want to run the solver in a for loop where I update the bounds and the A-matrix accordingly

emosqp('update_bounds', beq, beq);
[~,~,Ax] = find(Aeq);
emosqp('update_A', Ax, [], 0);
res = emosqp('solve');

When the 'update_A' function is called, MATLAB crashes. I uploaded the crash report. I'm using MATLAB 2020b under Ubuntu 20.04 LTS.
matlab crash osqp2.txt

Note: At my first try to generate the mex-file I got the following errors in MATLAB

cmake: /usr/local/MATLAB/R2020b/bin/glnxa64/libcurl.so.4: no version information available (required by cmake)
cmake: /usr/local/MATLAB/R2020b/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.26' not found (required by cmake)
cmake: /usr/local/MATLAB/R2020b/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.26' not found (required by /usr/lib/x86_64-linux-gnu/libjsoncpp.so.1)

after some googling, I found a solution where I preload the library path as follows (executing the following command in the shell before I start matlab from the same shell): export LD_PRELOAD="/usr/lib/x86_64-linux-gnu/libstdc++.so.6"

workspace.h should not initialise values for QDLDL workspace.

I think it is not necessary to explicitly initialise the fwork/bwork/iwork vectors that are used by QDLDL in workspace.h, since these are only used for temporary internal calculations by QDLDL. They should instead just be initialised as arrays of the correct size and type.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.