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