Python
Before generating code for a parametric problem, the problem should be first specified in the setup phase. See Setup for more details.
Codegen
The code is generated by running
m.codegen(folder, **opts)
The argument folder
is the name of a directory where the generated
code is stored.
Additional codegen options are shown in the following table
Option |
Description |
Allowed values |
---|---|---|
parameters |
Problem parameters
|
'vectors' (default)'matrices' |
extension_name |
Name of the generated Python module
|
'emosqp' (default)Nonempty string
|
force_rewrite |
Rewrite existing directory
|
False (default)True |
use_float |
Use
float type instead of double |
False (default)True |
printing_enable |
Enable printing
|
False (default)True |
profiling_enable |
Enable profiling
|
False (default)True |
interrupt_enable |
Enable interrupts
|
False (default)True |
include_codegen_src |
Include codegen source code (this includes
headers/sources/Makefile in the output folder,
creating a self-contained compilable folder.
|
True (default)False |
prefix |
Prefix for filenames and C variables,
useful if generating multiple problems.
|
'' (default)string
|
compile |
Compile the above python extension into an
importable module, allowing “import emosqp”
|
False (default)True |
The options are passed using named arguments, e.g.,
m.codegen('code', parameters='matrices', extension_name='myosqpext')
Extension module API
Once the code is generated, you can import a light python wrapper with
import myosqpext
where myosqpext
is the extension name given in the previous section. The module imports the following functions
- solve()
Solve the problem.
- Returns:
tuple (x, y, status_val, iter, run_time)
x (ndarray) - Primal solution
y (ndarray) - Dual solution
status_val (int) - Status value as in Status values and errors
iter (int) - Number of iterations
run_time (double) - Run time
- update_lin_cost(q_new)
Update linear cost.
- Parameters:
q_new (ndarray) – New linear cost vector
- update_lower_bound(l_new)
Update lower bound in the constraints.
- Parameters:
l_new (ndarray) – New lower bound vector
- update_upper_bound(u_new)
Update upper bound in the constraints.
- Parameters:
u_new (ndarray) – New upper bound vector
- update_bounds(l_new, u_new)
Update lower and upper bounds in the constraints.
- Parameters:
l_new (ndarray) – New lower bound vector
u_new (ndarray) – New upper bound vector
If the code is generated with the option parameters
set to
'matrices'
, the following functions are also provided
- update_P(Px, Px_idx, Px_n)
Update nonzero entries of the quadratic cost matrix (only upper triangular) without changing sparsity structure.
- Parameters:
Px (ndarray) – Values of entries to be updated
Px_idx (ndarray) – Indices of entries to be updated. Pass
None
if all the indices are to be updatedPx_n (int) – Number of entries to be updated. Used only if Px_idx is not
None
.
- update_A(Ax, Ax_idx, Ax_n)
Update nonzero entries of the constraint matrix.
- Parameters:
Ax (ndarray) – Values of entries to be updated
Ax_idx (ndarray) – Indices of entries to be updated. Pass
None
if all the indices are to be updatedAx_n (int) – Number of entries to be updated. Used only if Ax_idx is not
None
.
- update_P_A(Px, Px_idx, Px_n, Ax, Ax_idx, Ax_n)
Update nonzero entries of the quadratic cost and constraint matrices. It considers only the upper-triangular part of P.
- Parameters:
Px (ndarray) – Values of entries to be updated
Px_idx (ndarray) – Indices of entries to be updated. Pass
None
if all the indices are to be updatedPx_n (int) – Number of entries to be updated. Used only if Px_idx is not
None
.Ax (ndarray) – Values of entries to be updated
Ax_idx (ndarray) – Indices of entries to be updated. Pass
None
if all the indices are to be updatedAx_n (int) – Number of entries to be updated. Used only if Ax_idx is not
None
.
You can update all the nonzero entries in matrix \(A\) by running
myosqpext.update_A(Ax_new, None, 0);