Austin Schuh | 9049e20 | 2022-02-20 17:34:16 -0800 | [diff] [blame] | 1 | Python |
| 2 | ====== |
| 3 | |
| 4 | Before generating code for a parametric problem, the problem should be first |
| 5 | specified in the setup phase. See :ref:`python_setup` for more details. |
| 6 | |
| 7 | |
| 8 | Codegen |
| 9 | ------- |
| 10 | The code is generated by running |
| 11 | |
| 12 | .. code:: python |
| 13 | |
| 14 | m.codegen(dir_name, **opts) |
| 15 | |
| 16 | The argument :code:`dir_name` is the name of a directory where the generated |
| 17 | code is stored. |
| 18 | Additional codegen options are shown in the following table |
| 19 | |
| 20 | +-------------------------+---------------------------------------------------+--------------------------------+ |
| 21 | | Option | Description | Allowed values | |
| 22 | +=========================+===================================================+================================+ |
| 23 | | :code:`project_type` | Build environment | | :code:`''` (default) | |
| 24 | | | | | :code:`'Makefile'` | |
| 25 | | | | | :code:`'MinGW Makefiles'` | |
| 26 | | | | | :code:`'Unix Makefiles'` | |
| 27 | | | | | :code:`'CodeBlocks'` | |
| 28 | | | | | :code:`'Xcode'` | |
| 29 | +-------------------------+---------------------------------------------------+--------------------------------+ |
| 30 | | :code:`parameters` | Problem parameters | | :code:`'vectors'` (default) | |
| 31 | | | | | :code:`'matrices'` | |
| 32 | +-------------------------+---------------------------------------------------+--------------------------------+ |
| 33 | | :code:`python_ext_name` | Name of the generated Python module | | :code:`'emosqp'` (default) | |
| 34 | | | | | Nonempty string | |
| 35 | +-------------------------+---------------------------------------------------+--------------------------------+ |
| 36 | | :code:`force_rewrite` | Rewrite existing directory | | :code:`False` (default) | |
| 37 | | | | | :code:`True` | |
| 38 | +-------------------------+---------------------------------------------------+--------------------------------+ |
| 39 | | :code:`FLOAT` | Use :code:`float` type instead of :code:`double` | | :code:`False` (default) | |
| 40 | | | | | :code:`True` | |
| 41 | +-------------------------+---------------------------------------------------+--------------------------------+ |
| 42 | | :code:`LONG` | Use :code:`long long` type instead of :code:`int` | | :code:`True` (default) | |
| 43 | | | | | :code:`False` | |
| 44 | +-------------------------+---------------------------------------------------+--------------------------------+ |
| 45 | |
| 46 | The options are passed using named arguments, e.g., |
| 47 | |
| 48 | .. code:: python |
| 49 | |
| 50 | m.codegen('code', parameters='matrices', python_ext_name='emosqp') |
| 51 | |
| 52 | If the :code:`project_type` argument is not passed or is set to :code:`''`, |
| 53 | then no build files are generated. |
| 54 | |
| 55 | |
| 56 | |
| 57 | Extension module API |
| 58 | -------------------- |
| 59 | Once the code is generated, you can import a light python wrapper with |
| 60 | |
| 61 | .. code:: python |
| 62 | |
| 63 | import emosqp |
| 64 | |
| 65 | where :code:`emosqp` is the extension name given in the previous section. The module imports the following functions |
| 66 | |
| 67 | .. py:function:: solve() |
| 68 | :noindex: |
| 69 | |
| 70 | Solve the problem. |
| 71 | |
| 72 | :returns: tuple (x, y, status_val, iter, run_time) |
| 73 | |
| 74 | - **x** (*ndarray*) - Primal solution |
| 75 | - **y** (*ndarray*) - Dual solution |
| 76 | - **status_val** (*int*) - Status value as in :ref:`status_values` |
| 77 | - **iter** (*int*) - Number of iterations |
| 78 | - **run_time** (*double*) - Run time |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | .. py:function:: update_lin_cost(q_new) |
| 85 | :noindex: |
| 86 | |
| 87 | Update linear cost. |
| 88 | |
| 89 | :param ndarray q_new: New linear cost vector |
| 90 | |
| 91 | |
| 92 | .. py:function:: update_lower_bound(l_new) |
| 93 | :noindex: |
| 94 | |
| 95 | Update lower bound in the constraints. |
| 96 | |
| 97 | :param ndarray l_new: New lower bound vector |
| 98 | |
| 99 | |
| 100 | .. py:function:: update_upper_bound(u_new) |
| 101 | :noindex: |
| 102 | |
| 103 | Update upper bound in the constraints. |
| 104 | |
| 105 | :param ndarray u_new: New upper bound vector |
| 106 | |
| 107 | |
| 108 | .. py:function:: update_bounds(l_new, u_new) |
| 109 | :noindex: |
| 110 | |
| 111 | Update lower and upper bounds in the constraints. |
| 112 | |
| 113 | :param ndarray l_new: New lower bound vector |
| 114 | :param ndarray u_new: New upper bound vector |
| 115 | |
| 116 | |
| 117 | If the code is generated with the option :code:`parameters` set to |
| 118 | :code:`'matrices'`, the following functions are also provided |
| 119 | |
| 120 | |
| 121 | .. py:function:: update_P(Px, Px_idx, Px_n) |
| 122 | :noindex: |
| 123 | |
| 124 | Update nonzero entries of the quadratic cost matrix (only upper triangular) without changing sparsity structure. |
| 125 | |
| 126 | :param ndarray Px: Values of entries to be updated |
| 127 | :param ndarray Px_idx: Indices of entries to be updated. Pass :code:`None` if |
| 128 | all the indices are to be updated |
| 129 | :param int Px_n: Number of entries to be updated. Used only if Px_idx is not |
| 130 | :code:`None`. |
| 131 | |
| 132 | |
| 133 | .. py:function:: update_A(Ax, Ax_idx, Ax_n) |
| 134 | :noindex: |
| 135 | |
| 136 | Update nonzero entries of the constraint matrix. |
| 137 | |
| 138 | :param ndarray Ax: Values of entries to be updated |
| 139 | :param ndarray Ax_idx: Indices of entries to be updated. Pass :code:`None` if |
| 140 | all the indices are to be updated |
| 141 | :param int Ax_n: Number of entries to be updated. Used only if Ax_idx is not |
| 142 | :code:`None`. |
| 143 | |
| 144 | |
| 145 | .. py:function:: update_P_A(Px, Px_idx, Px_n, Ax, Ax_idx, Ax_n) |
| 146 | :noindex: |
| 147 | |
| 148 | Update nonzero entries of the quadratic cost and constraint matrices. It considers only the upper-triangular part of P. |
| 149 | |
| 150 | :param ndarray Px: Values of entries to be updated |
| 151 | :param ndarray Px_idx: Indices of entries to be updated. Pass :code:`None` if |
| 152 | all the indices are to be updated |
| 153 | :param int Px_n: Number of entries to be updated. Used only if Px_idx is not |
| 154 | :code:`None`. |
| 155 | :param ndarray Ax: Values of entries to be updated |
| 156 | :param ndarray Ax_idx: Indices of entries to be updated. Pass :code:`None` if |
| 157 | all the indices are to be updated |
| 158 | :param int Ax_n: Number of entries to be updated. Used only if Ax_idx is not |
| 159 | :code:`None`. |
| 160 | |
| 161 | |
| 162 | You can update all the nonzero entries in matrix :math:`A` by running |
| 163 | |
| 164 | .. code:: python |
| 165 | |
| 166 | emosqp.update_A(Ax_new, None, 0); |
| 167 | |
| 168 | See C :ref:`C_sublevel_API` for more details on the input arguments. |