Austin Schuh | 9049e20 | 2022-02-20 17:34:16 -0800 | [diff] [blame^] | 1 | .. _matlab_interface: |
| 2 | |
| 3 | Matlab |
| 4 | ====== |
| 5 | |
| 6 | .. _matlab_setup: |
| 7 | |
| 8 | Setup |
| 9 | ----- |
| 10 | The solver is initialized by creating an OSQP object |
| 11 | |
| 12 | .. code:: matlab |
| 13 | |
| 14 | m = osqp; |
| 15 | |
| 16 | The 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 | |
| 23 | The 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. |
| 24 | Matrix :code:`P` can be either complete or just the upper triangular |
| 25 | part. OSQP will make use of only the upper triangular part. |
| 26 | |
| 27 | There is no need to specify all the problem data. They can be omitted by writing :code:`[]`. |
| 28 | |
| 29 | The 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 | |
| 36 | Alternatively, 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 | |
| 45 | The allowed settings are defined in :ref:`solver_settings`. |
| 46 | |
| 47 | |
| 48 | Solve |
| 49 | ----- |
| 50 | |
| 51 | The problem can be solved by |
| 52 | |
| 53 | .. code:: matlab |
| 54 | |
| 55 | results = m.solve(); |
| 56 | |
| 57 | The :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 | |
| 92 | Note that if multiple solves are executed from single setup, then after the |
| 93 | first one :code:`run_time` includes :code:`update_time` + :code:`solve_time` |
| 94 | + :code:`polish_time`. |
| 95 | |
| 96 | |
| 97 | Update |
| 98 | ------ |
| 99 | Part of problem data and settings can be updated without requiring a new problem setup. |
| 100 | |
| 101 | |
| 102 | |
| 103 | Update problem vectors |
| 104 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 105 | |
| 106 | Vectors :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 | |
| 113 | The user does not have to specify all the arguments. |
| 114 | |
| 115 | |
| 116 | Update problem matrices |
| 117 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 118 | Matrices :code:`A` and :code:`P` can be updated by changing the value of their elements but not their sparsity pattern. |
| 119 | The interface is designed to mimic the :ref:`C counterpart <c_cpp_update_data>` with the Matlab 1-based indexing. |
| 120 | Note that the new values of :code:`P` represent only the upper triangular part while :code:`A` is always represented as a full matrix. |
| 121 | |
| 122 | You 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 | |
| 129 | If 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 | |
| 135 | where :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 | |
| 137 | Matrix :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 | |
| 145 | Update settings |
| 146 | ^^^^^^^^^^^^^^^ |
| 147 | |
| 148 | Settings can be updated by running |
| 149 | |
| 150 | .. code:: python |
| 151 | |
| 152 | m.update_settings(varargin); |
| 153 | |
| 154 | |
| 155 | where :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 | |
| 160 | Warm start |
| 161 | ---------- |
| 162 | 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 |
| 163 | |
| 164 | .. code:: matlab |
| 165 | |
| 166 | m.warm_start('x', x0, 'y', y0) |
| 167 | |
| 168 | where :code:`x0` and :code:`y0` are the new primal and dual variables. |