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(dir_name, **opts)

The argument dir_name 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

project_type

Build environment

'' (default)
'Makefile'
'MinGW Makefiles'
'Unix Makefiles'
'CodeBlocks'
'Xcode'

parameters

Problem parameters

'vectors' (default)
'matrices'

python_ext_name

Name of the generated Python module

'emosqp' (default)
Nonempty string

force_rewrite

Rewrite existing directory

False (default)
True

FLOAT

Use float type instead of double

False (default)
True

LONG

Use long long type instead of int

True (default)
False

The options are passed using named arguments, e.g.,

m.codegen('code', parameters='matrices', python_ext_name='emosqp')

If the project_type argument is not passed or is set to '', then no build files are generated.

Extension module API

Once the code is generated, you can import a light python wrapper with

import emosqp

where emosqp 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 updated

  • Px_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 updated

  • Ax_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 updated

  • Px_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 updated

  • Ax_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

emosqp.update_A(Ax_new, None, 0);

See C Sublevel API for more details on the input arguments.