Supported solvers

CVXOPT

qpsolvers.cvxopt_solve_qp(P, q, G=None, h=None, A=None, b=None, solver=None, initvals=None, verbose=False)

Solve a Quadratic Program defined as:

\[\begin{split}\begin{split}\begin{array}{ll} \mbox{minimize} & \frac{1}{2} x^T P x + q^T x \\ \mbox{subject to} & G x \leq h \\ & A x = h \end{array}\end{split}\end{split}\]

using CVXOPT.

Parameters
  • P (Union[ndarray, matrix, spmatrix]) – Symmetric quadratic-cost matrix.

  • q (Union[ndarray, matrix, spmatrix]) – Quadratic-cost vector.

  • G (Union[ndarray, matrix, spmatrix, None]) – Linear inequality matrix.

  • h (Union[ndarray, matrix, spmatrix, None]) – Linear inequality vector.

  • A (Union[ndarray, matrix, spmatrix, None]) – Linear equality constraint matrix.

  • b (Union[ndarray, matrix, spmatrix, None]) – Linear equality constraint vector.

  • solver (Optional[str]) – Set to ‘mosek’ to run MOSEK rather than CVXOPT.

  • initvals (Optional[ndarray]) – Warm-start guess vector.

  • verbose (bool) – Set to True to print out extra information.

Return type

Optional[ndarray]

Returns

Solution to the QP, if found, otherwise None.

Note

CVXOPT only considers the lower entries of P, therefore it will use a different cost than the one intended if a non-symmetric matrix is provided.

ECOS

qpsolvers.ecos_solve_qp(P, q, G=None, h=None, A=None, b=None, initvals=None, verbose=False)

Solve a Quadratic Program defined as:

\[\begin{split}\begin{split}\begin{array}{ll} \mbox{minimize} & \frac{1}{2} x^T P x + q^T x \\ \mbox{subject to} & G x \leq h \\ & A x = h \end{array}\end{split}\end{split}\]

using ECOS.

Parameters
  • P (ndarray) – Primal quadratic cost matrix.

  • q (ndarray) – Primal quadratic cost vector.

  • G (Optional[ndarray]) – Linear inequality constraint matrix.

  • h (Optional[ndarray]) – Linear inequality constraint vector.

  • A (Optional[ndarray]) – Linear equality constraint matrix.

  • b (Optional[ndarray]) – Linear equality constraint vector.

  • initvals (Optional[ndarray]) – Warm-start guess vector (not used).

  • verbose (bool) – Set to True to print out extra information.

Return type

Optional[ndarray]

Returns

Solution to the QP, if found, otherwise None.

Gurobi

See the installation page for additional instructions on installing this solver.

qpsolvers.gurobi_solve_qp(P, q, G=None, h=None, A=None, b=None, initvals=None, verbose=False)

Solve a Quadratic Program defined as:

\[\begin{split}\begin{split}\begin{array}{ll} \mbox{minimize} & \frac{1}{2} x^T P x + q^T x \\ \mbox{subject to} & G x \leq h \\ & A x = h \end{array}\end{split}\end{split}\]

using Gurobi.

Parameters
  • P (ndarray) – Primal quadratic cost matrix.

  • q (ndarray) – Primal quadratic cost vector.

  • G (Optional[ndarray]) – Linear inequality constraint matrix.

  • h (Optional[ndarray]) – Linear inequality constraint vector.

  • A (Optional[ndarray]) – Linear equality constraint matrix.

  • b (Optional[ndarray]) – Linear equality constraint vector.

  • initvals (Optional[ndarray]) – Warm-start guess vector (not used).

  • verbose (bool) – Set to True to print out extra information.

Return type

Optional[ndarray]

Returns

Solution to the QP, if found, otherwise None.

MOSEK

qpsolvers.mosek_solve_qp(P, q, G, h, A=None, b=None, initvals=None, verbose=False)

Solve a Quadratic Program defined as:

\[\begin{split}\begin{split}\begin{array}{ll} \mbox{minimize} & \frac{1}{2} x^T P x + q^T x \\ \mbox{subject to} & G x \leq h \\ & A x = h \\ & lb \leq x \leq ub \end{array}\end{split}\end{split}\]

using the MOSEK interface from CVXOPT.

Parameters
  • P (Union[ndarray, matrix, spmatrix]) – Symmetric quadratic-cost matrix.

  • q (Union[ndarray, matrix, spmatrix]) – Quadratic-cost vector.

  • G (Union[ndarray, matrix, spmatrix]) – Linear inequality constraint matrix.

  • h (Union[ndarray, matrix, spmatrix]) – Linear inequality constraint vector.

  • A (Union[ndarray, matrix, spmatrix, None]) – Linear equality constraint matrix.

  • b (Union[ndarray, matrix, spmatrix, None]) – Linear equality constraint vector.

  • initvals (Union[ndarray, matrix, spmatrix, None]) – Warm-start guess vector.

  • verbose (bool) – Set to True to print out extra information.

Return type

Optional[ndarray]

Returns

Solution to the QP, if found, otherwise None.

OSQP

qpsolvers.osqp_solve_qp(P, q, G=None, h=None, A=None, b=None, initvals=None, verbose=False, eps_abs=0.0001, eps_rel=0.0001, polish=True, **kwargs)

Solve a Quadratic Program defined as:

\[\begin{split}\begin{split}\begin{array}{ll} \mbox{minimize} & \frac{1}{2} x^T P x + q^T x \\ \mbox{subject to} & G x \leq h \\ & A x = h \end{array}\end{split}\end{split}\]

using OSQP.

Parameters
  • P (Union[ndarray, csc_matrix]) – Symmetric quadratic-cost matrix.

  • q (Union[ndarray, csc_matrix]) – Quadratic cost vector.

  • G (Union[ndarray, csc_matrix, None]) – Linear inequality constraint matrix.

  • h (Union[ndarray, csc_matrix, None]) – Linear inequality constraint vector.

  • A (Union[ndarray, csc_matrix, None]) – Linear equality constraint matrix.

  • b (Union[ndarray, csc_matrix, None]) – Linear equality constraint vector.

  • initvals (Union[ndarray, csc_matrix, None]) – Warm-start guess vector.

  • verbose (bool) – Set to True to print out extra information.

  • eps_abs (float) – Absolute convergence tolerance of the solver. Lower values yield more precise solutions at the cost of computation time.

  • eps_rel (float) – Relative convergence tolerance of the solver. Lower values yield more precise solutions at the cost of computation time.

  • polish (bool) – Perform polishing, an additional step where the solver tries to improve the accuracy of the solution. Default is True.

Return type

Optional[ndarray]

Returns

Solution to the QP, if found, otherwise None.

Note

OSQP requires a symmetric P and won’t check for errors otherwise. Check out this point if you get nan values in your solutions.

Notes

As of OSQP 0.6.1, the default values for both absolute and relative tolerances are set to 1e-3, which results in low solver times but imprecise solutions compared to the other QP solvers. We lower them to 1e-5 so that OSQP behaves closer to the other solvers in terms of numerical accuracy.

All other keyword arguments are forwarded to the OSQP solver. For instance, you can call osqp_solve_qp(P, q, G, h, u, alpha=1.42). See the solver documentation for details.

qpOASES

See the installation page for additional instructions on installing this solver.

qpsolvers.qpoases_solve_qp(P, q, G=None, h=None, A=None, b=None, initvals=None, verbose=False, max_wsr=1000)

Solve a Quadratic Program defined as:

\[\begin{split}\begin{split}\begin{array}{ll} \mbox{minimize} & \frac{1}{2} x^T P x + q^T x \\ \mbox{subject to} & G x \leq h \\ & A x = h \end{array}\end{split}\end{split}\]

using qpOASES.

Parameters
  • P (ndarray) – Symmetric quadratic-cost matrix.

  • q (ndarray) – Quadratic-cost vector.

  • G (Optional[ndarray]) – Linear inequality constraint matrix.

  • h (Optional[ndarray]) – Linear inequality constraint vector.

  • A (Optional[ndarray]) – Linear equality constraint matrix.

  • b (Optional[ndarray]) – Linear equality constraint vector.

  • initvals (Optional[ndarray]) – Warm-start guess vector.

  • verbose (bool) – Set to True to print out extra information.

  • max_wsr (int) – Maximum number of Working-Set Recalculations given to qpOASES.

Return type

Optional[ndarray]

Returns

Solution to the QP, if found, otherwise None.

Notes

This function relies on some updates from the standard distribution of qpOASES. See the installation instructions for details.

Empty bounds (lb, ub, lbA or ubA) are allowed. This is possible in the C++ API but not by the Python API of qpOASES (as of version 3.2.0). If using them, be sure to update the Cython file (qpoases.pyx) in your distribution of qpOASES to convert None to the null pointer.

quadprog

qpsolvers.quadprog_solve_qp(P, q, G=None, h=None, A=None, b=None, initvals=None, verbose=False, **kwargs)

Solve a Quadratic Program defined as:

\[\begin{split}\begin{split}\begin{array}{ll} \mbox{minimize} & \frac{1}{2} x^T P x + q^T x \\ \mbox{subject to} & G x \leq h \\ & A x = h \end{array}\end{split}\end{split}\]

using quadprog.

Parameters
  • P (ndarray) – Symmetric quadratic-cost matrix.

  • q (ndarray) – Quadratic-cost vector.

  • G (Optional[ndarray]) – Linear inequality constraint matrix.

  • h (Optional[ndarray]) – Linear inequality constraint vector.

  • A (Optional[ndarray]) – Linear equality constraint matrix.

  • b (Optional[ndarray]) – Linear equality constraint vector.

  • initvals (Optional[ndarray]) – Warm-start guess vector (not used).

  • verbose (bool) – Set to True to print out extra information.

Return type

Optional[ndarray]

Returns

Solution to the QP, if found, otherwise None.

Note

The quadprog solver only considers the lower entries of \(P\), therefore it will use a different cost than the one intended if a non-symmetric matrix is provided.

Notes

All other keyword arguments are forwarded to the quadprog solver. For instance, you can call quadprog_solve_qp(P, q, G, h, factorized=True). See the solver documentation for details.

SCS

qpsolvers.scs_solve_qp(P, q, G=None, h=None, A=None, b=None, initvals=None, verbose=False, eps=1e-07, use_indirect=True, **kwargs)

Solve a Quadratic Program defined as:

\[\begin{split}\begin{split}\begin{array}{ll} \mbox{minimize} & \frac{1}{2} x^T P x + q^T x \\ \mbox{subject to} & G x \leq h \\ & A x = h \end{array}\end{split}\end{split}\]

using SCS.

Parameters
  • P (ndarray) – Primal quadratic cost matrix.

  • q (ndarray) – Primal quadratic cost vector.

  • G (Optional[ndarray]) – Linear inequality constraint matrix.

  • h (Optional[ndarray]) – Linear inequality constraint vector.

  • A (Optional[ndarray]) – Linear equality constraint matrix.

  • b (Optional[ndarray]) – Linear equality constraint vector.

  • initvals (Optional[ndarray]) – Warm-start guess vector (not used).

  • verbose (bool) – Set to True to print out extra information.

  • eps (float) – Convergence tolerange.

  • use_indirect (bool) – Solve linear systems either “directly” via a sparse LDL factorization or “indirectly” by means of a conjugate gradient method.

Return type

Optional[ndarray]

Returns

Solution to the QP, if found, otherwise None.

Notes

As of SCS 2.1.2, the default convergence tolerance eps is set to 1e-5, resulting in inequality constraints that are violated by more than 1e-6 as opposed to 1e-10 for other solvers e.g. on the README problem. We lower it to 1e-7 and switch use_indirect=True so that SCS behaves closer to the other solvers on this example.

All other keyword arguments are forwarded as is to SCS. For instance, you can call scs_solve_qp(P, q, G, h, use_indirect=True, normalize=True). See the solver documentation for details.