Skip to main content

Quadratic programming solvers in Python with a unified API

Project description

QP Solvers for Python

Build Coverage Documentation Downloads/month PyPI version

Unified interface to Quadratic Programming (QP) solvers available in Python.

Installation

To install both the library and a starter set of free QP solvers:

pip install qpsolvers[open_source_solvers]

To only install the library:

pip install qpsolvers

Check out the documentation for Python 2 or Windows instructions.

Usage

The library provides a one-stop shop solve_qp function with a solver keyword argument to select the backend solver. It solves convex quadratic programs in standard form:

$$ \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 = b \ & lb \leq x \leq ub \end{array} \end{split} $$

Vector inequalities are taken coordinate by coordinate. For most solvers, the matrix $P$ should be positive definite.

📢 With v2.5, tolerances for OSQP and SCS have been reverted to the solvers' defaults. For the previous setting, pass the additional keyword arguments eps_abs=1e-4, eps_rel=1e-4 to OSQP or eps_abs=1e-7, eps_rel=1e-7 to SCS. Changes to the API are reported in the Announcements.

Example

To solve a quadratic program, build the matrices that define it and call the solve_qp function:

from numpy import array, dot
from qpsolvers import solve_qp

M = array([[1., 2., 0.], [-8., 3., 2.], [0., 1., 1.]])
P = dot(M.T, M)  # this is a positive definite matrix
q = dot(array([3., 2., 3.]), M)
G = array([[1., 2., 1.], [2., 0., 1.], [-1., 2., -1.]])
h = array([3., 2., -2.])
A = array([1., 1., 1.])
b = array([1.])

x = solve_qp(P, q, G, h, A, b, solver="osqp")
print(f"QP solution: x = {x}")

This example outputs the solution [0.30769231, -0.69230769, 1.38461538].

Solvers

Solver Keyword Algorithm Matrices License Warm-start
CVXOPT cvxopt Interior point Dense GPL-3.0 ✔️
ECOS ecos Interior point Sparse GPL-3.0 ✖️
Gurobi gurobi Interior point Sparse Commercial ✖️
HiGHS highs Active set Sparse MIT ✖️
MOSEK mosek Interior point Sparse Commercial ✔️
OSQP osqp Augmented Lagrangian Sparse Apache-2.0 ✔️
ProxQP proxqp Augmented Lagrangian Dense & Sparse BSD-2-Clause ✔️
qpOASES qpoases Active set Dense LGPL-2.1
qpSWIFT qpswift Interior point Sparse GPL-3.0 ✖️
quadprog quadprog Active set Dense GPL-2.0 ✖️
SCS scs Augmented Lagrangian Sparse MIT ✔️

Matrix arguments are NumPy arrays for dense solvers and SciPy Compressed Sparse Column (CSC) matrices for sparse ones.

Frequently Asked Questions

  • Can I print the list of solvers available on my machine?
    • Absolutely: print(qpsolvers.available_solvers)
  • Is it possible to solve a least squares rather than a quadratic program?
    • Yes, there is also a solve_ls function.
  • I have a squared norm in my cost function, how can I apply a QP solver to my problem?
  • I have a non-convex quadratic program. Is there a solver I can use?
    • Unfortunately most available QP solvers are designed for convex problems.
    • If your cost matrix P is semi-definite rather than definite, try OSQP.
    • If your problem has concave components, go for a nonlinear solver such as IPOPT e.g. using CasADi.
  • I get the following build error on Windows when running pip install qpsolvers.
  • Can I help?
    • Absolutely! The first step is to install the library and use it. Report any bug in the issue tracker.
    • If you're a developer looking to hack on open source, check out the contribution guidelines for suggestions.

Benchmark

On a dense problem, the performance of all solvers (as measured by IPython's %timeit on an Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz) is:

Solver Type Time (ms)
qpswift Dense 0.008
quadprog Dense 0.01
qpoases Dense 0.02
osqp Sparse 0.03
scs Sparse 0.03
ecos Sparse 0.27
cvxopt Dense 0.44
gurobi Sparse 1.74
mosek Sparse 7.17

On a sparse problem with n = 500 optimization variables, these performances become:

Solver Type Time (ms)
osqp Sparse 1
qpswift Dense 2
scs Sparse 4
mosek Sparse 17
ecos Sparse 33
cvxopt Dense 51
gurobi Sparse 221
quadprog Dense 427
qpoases Dense 1560

On a model predictive control problem for robot locomotion, we get:

Solver Type Time (ms)
quadprog Dense 0.03
qpswift Dense 0.08
qpoases Dense 0.36
osqp Sparse 0.48
ecos Sparse 0.69
scs Sparse 0.76
cvxopt Dense 2.75

Finally, here is a small benchmark of random dense problems (each data point corresponds to an average over 10 runs):

Note that performances of QP solvers largely depend on the problem solved. For instance, MOSEK performs an automatic conversion to Second-Order Cone Programming (SOCP) which the documentation advises bypassing for better performance. Similarly, ECOS reformulates from QP to SOCP and works best on small problems.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qpsolvers-2.6.0.tar.gz (60.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qpsolvers-2.6.0-py3-none-any.whl (50.8 kB view details)

Uploaded Python 3

File details

Details for the file qpsolvers-2.6.0.tar.gz.

File metadata

  • Download URL: qpsolvers-2.6.0.tar.gz
  • Upload date:
  • Size: 60.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.22.0

File hashes

Hashes for qpsolvers-2.6.0.tar.gz
Algorithm Hash digest
SHA256 f700bde679e3525677454dc753703a0b39169a2799d20ef1018667edab380f67
MD5 94739bf44ac458d2bbc538baa759d984
BLAKE2b-256 ff861a8d84d190d4fdb3093ce002e13f10f482d323ead2b83e360ddcc3c8f37b

See more details on using hashes here.

File details

Details for the file qpsolvers-2.6.0-py3-none-any.whl.

File metadata

  • Download URL: qpsolvers-2.6.0-py3-none-any.whl
  • Upload date:
  • Size: 50.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.22.0

File hashes

Hashes for qpsolvers-2.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11b21cd696fe1ffbdc977c8681f5a00377afd5bf48fb91611e09c66bd6a99c67
MD5 2441b2cde025ac7fce0523fe4418dd4d
BLAKE2b-256 a9502671ee7e190b32d03b8f146321855cf6d2c051646e2d2f28fc335a6d7a5f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page