blob: 81e70275f67bc46887117b43d2f4db138d814e4d [file] [log] [blame]
Austin Schuh9049e202022-02-20 17:34:16 -08001.. _matlab_interface:
2
3Matlab
4======
5
6.. _matlab_setup:
7
8Setup
9-----
10The solver is initialized by creating an OSQP object
11
12.. code:: matlab
13
14 m = osqp;
15
16The problem is specified in the setup phase by running
17
18.. code:: matlab
19
20 m.setup(P, q, A, l, u, varargin)
21
22
23The arguments :code:`q`, :code:`l` and :code:`u` are arrays. The elements of :code:`l` and :code:`u` can be :math:`\pm \infty` ( using :code:`Inf`). The arguments :code:`P` and :code:`A` are sparse matrices.
24Matrix :code:`P` can be either complete or just the upper triangular
25part. OSQP will make use of only the upper triangular part.
26
27There is no need to specify all the problem data. They can be omitted by writing :code:`[]`.
28
29The last argument :code:`varargin` specifies the solver options. You can pass the options in two ways. You can either set the individual parameters as field-value pairs, e.g.,
30
31.. code:: matlab
32
33 m.setup(P, q, A, l, u, 'eps_abs', 1e-04, 'eps_rel', 1e-04);
34
35
36Alternatively, you can create a structure containing all the settings, change some of the fields and then pass it as the last argument
37
38.. code:: matlab
39
40 settings = m.default_settings();
41 settings.eps_abs = 1e-04;
42 settings.eps_rel = 1e-04;
43 m.setup(P, q, A, l, u, settings);
44
45The allowed settings are defined in :ref:`solver_settings`.
46
47
48Solve
49-----
50
51The problem can be solved by
52
53.. code:: matlab
54
55 results = m.solve();
56
57The :code:`results` structure contains the primal solution :code:`x`, the dual solution :code:`y`, certificate of primal infeasibility :code:`prim_inf_cert`, certificate of dual infeasibility :code:`dual_inf_cert` and the :code:`info` structure containing the solver statistics defined in the following table
58
59
60+-----------------------+------------------------------------------------+
61| Member | Description |
62+=======================+================================================+
63| :code:`iter` | Number of iterations |
64+-----------------------+------------------------------------------------+
65| :code:`status` | Solver status |
66+-----------------------+------------------------------------------------+
67| :code:`status_val` | Solver status value as in :ref:`status_values` |
68+-----------------------+------------------------------------------------+
69| :code:`status_polish` | Polishing status |
70+-----------------------+------------------------------------------------+
71| :code:`obj_val` | Objective value |
72+-----------------------+------------------------------------------------+
73| :code:`pri_res` | Primal residual |
74+-----------------------+------------------------------------------------+
75| :code:`dua_res` | Dual residual |
76+-----------------------+------------------------------------------------+
77| :code:`setup_time` | Setup time |
78+-----------------------+------------------------------------------------+
79| :code:`solve_time` | Solve time |
80+-----------------------+------------------------------------------------+
81| :code:`update_time` | Update time |
82+-----------------------+------------------------------------------------+
83| :code:`polish_time` | Polish time |
84+-----------------------+------------------------------------------------+
85| :code:`run_time` | Total run time: setup/update + solve + polish |
86+-----------------------+------------------------------------------------+
87| :code:`rho_estimate` | Optimal rho estimate |
88+-----------------------+------------------------------------------------+
89| :code:`rho_updates` | Number of rho updates |
90+-----------------------+------------------------------------------------+
91
92Note that if multiple solves are executed from single setup, then after the
93first one :code:`run_time` includes :code:`update_time` + :code:`solve_time`
94+ :code:`polish_time`.
95
96
97Update
98------
99Part of problem data and settings can be updated without requiring a new problem setup.
100
101
102
103Update problem vectors
104^^^^^^^^^^^^^^^^^^^^^^^^
105
106Vectors :code:`q`, :code:`l` and :code:`u` can be updated with new values :code:`q_new`, :code:`l_new` and :code:`u_new` by just running
107
108.. code:: python
109
110 m.update('q', q_new, 'l', l_new, 'u', u_new);
111
112
113The user does not have to specify all the arguments.
114
115
116Update problem matrices
117^^^^^^^^^^^^^^^^^^^^^^^^
118Matrices :code:`A` and :code:`P` can be updated by changing the value of their elements but not their sparsity pattern.
119The interface is designed to mimic the :ref:`C counterpart <c_cpp_update_data>` with the Matlab 1-based indexing.
120Note that the new values of :code:`P` represent only the upper triangular part while :code:`A` is always represented as a full matrix.
121
122You can update the values of all the elements of :code:`P` by executing
123
124.. code:: matlab
125
126 m.update('Px', Px_new)
127
128
129If you want to update only some elements, you can pass
130
131.. code:: matlab
132
133 m.update('Px', Px_new, 'Px_idx', Px_new_idx)
134
135where :code:`Px_new_idx` is the vector of indices of mapping the elements of :code:`Px_new` to the original vector :code:`Px` representing the data of the sparse matrix :code:`P`.
136
137Matrix :code:`A` can be changed in the same way. You can also change both matrices at the same time by running, for example
138
139
140.. code:: matlab
141
142 m.update('Px', Px_new, 'Px_idx', Px_new_idx, 'Ax' Ax_new, 'Ax', Ax_new_idx)
143
144
145Update settings
146^^^^^^^^^^^^^^^
147
148Settings can be updated by running
149
150.. code:: python
151
152 m.update_settings(varargin);
153
154
155where :code:`varargin` argument is described in :ref:`matlab_setup`. The allowed settings that can be updated are marked with an * in :ref:`solver_settings`.
156
157
158
159
160Warm start
161----------
162OSQP 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
163
164.. code:: matlab
165
166 m.warm_start('x', x0, 'y', y0)
167
168where :code:`x0` and :code:`y0` are the new primal and dual variables.