Matlab¶
Setup¶
The solver is initialized by creating an OSQP object
m = osqp;
The problem is specified in the setup phase by running
m.setup(P, q, A, l, u, varargin)
The arguments q
, l
and u
are arrays. The elements of l
and u
can be \(\pm \infty\) ( using Inf
). The arguments P
and A
are sparse matrices.
Matrix P
can be either complete or just the upper triangular
part. OSQP will make use of only the upper triangular part.
There is no need to specify all the problem data. They can be omitted by writing []
.
The last argument varargin
specifies the solver options. You can pass the options in two ways. You can either set the individual parameters as fieldvalue pairs, e.g.,
m.setup(P, q, A, l, u, 'eps_abs', 1e04, 'eps_rel', 1e04);
Alternatively, you can create a structure containing all the settings, change some of the fields and then pass it as the last argument
settings = m.default_settings();
settings.eps_abs = 1e04;
settings.eps_rel = 1e04;
m.setup(P, q, A, l, u, settings);
The allowed settings are defined in Solver settings.
Solve¶
The problem can be solved by
results = m.solve();
The results
structure contains the primal solution x
, the dual solution y
, certificate of primal infeasibility prim_inf_cert
, certificate of dual infeasibility dual_inf_cert
and the info
structure containing the solver statistics defined in the following table
Member 
Description 


Number of iterations 

Solver status 

Solver status value as in Status values 

Polishing status 

Objective value 

Primal residual 

Dual residual 

Setup time 

Solve time 

Update time 

Polish time 

Total run time: setup/update + solve + polish 

Optimal rho estimate 

Number of rho updates 
Note that if multiple solves are executed from single setup, then after the
first one run_time
includes update_time
+ solve_time
+ polish_time
.
Update¶
Part of problem data and settings can be updated without requiring a new problem setup.
Update problem vectors¶
Vectors q
, l
and u
can be updated with new values q_new
, l_new
and u_new
by just running
m.update('q', q_new, 'l', l_new, 'u', u_new);
The user does not have to specify all the arguments.
Update problem matrices¶
Matrices A
and P
can be updated by changing the value of their elements but not their sparsity pattern.
The interface is designed to mimic the C/C++ counterpart with the Matlab 1based indexing.
Note that the new values of P
represent only the upper triangular part while A
is always represented as a full matrix.
You can update the values of all the elements of P
by executing
m.update('Px', Px_new)
If you want to update only some elements, you can pass
m.update('Px', Px_new, 'Px_idx', Px_new_idx)
where Px_new_idx
is the vector of indices of mapping the elements of Px_new
to the original vector Px
representing the data of the sparse matrix P
.
Matrix A
can be changed in the same way. You can also change both matrices at the same time by running, for example
m.update('Px', Px_new, 'Px_idx', Px_new_idx, 'Ax' Ax_new, 'Ax', Ax_new_idx)
Update settings¶
Settings can be updated by running
m.update_settings(varargin);
where varargin
argument is described in Setup. The allowed settings that can be updated are marked with an * in Solver settings.
Warm start¶
OSQP automatically warm starts primal and dual variables from the previous QP solution. If you would like to warm start their values manually, you can use
m.warm_start('x', x0, 'y', y0)
where x0
and y0
are the new primal and dual variables.