Skip to main content

Quadratic programming solvers in Python with a unified API

Project description

QP Solvers for Python

build PyPI package Documentation Status

Wrapper around Quadratic Programming (QP) solvers in Python, with a unified interface.

Installation

sudo apt install python3-dev
pip3 install qpsolvers

Check out the documentation for Python 2 or Windows instructions.

Usage

The function solve_qp(P, q, G, h, A, b, lb, ub) is called with the solver keyword argument to select the backend solver. The convex quadratic program it solves is, in standard form:

Equation of Quadratic Program

Vector inequalities are taken coordinate by coordinate. The matrix P should be positive definite.

Example

To solve a quadratic program, simply 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).reshape((3,))
G = array([[1., 2., 1.], [2., 0., 1.], [-1., 2., -1.]])
h = array([3., 2., -2.]).reshape((3,))
A = array([1., 1., 1.])
b = array([1.])

x = solve_qp(P, q, G, h, A, b)
print("QP solution: x = {}".format(x))

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

Solvers

The list of supported solvers currently includes:

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, qpsolvers also provides 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.

Performances

On a dense problem, the performance of all solvers (as measured by IPython's %timeit on my machine) is:

Solver Type Time (ms)
quadprog Dense 0.02
qpoases Dense 0.03
osqp Sparse 0.04
ecos Sparse 0.34
cvxopt Dense 0.46
gurobi Sparse 0.84
cvxpy Sparse 3.40
mosek Sparse 7.17

On a sparse problem, these performances become:

Solver Type Time (ms)
osqp Sparse 1
mosek Sparse 17
ecos Sparse 21
cvxopt Dense 186
gurobi Sparse 221
quadprog Dense 550
cvxpy Sparse 654
qpoases Dense 2250

Finally, here are the results on a benchmark of random 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-1.7.0.tar.gz (35.6 kB view details)

Uploaded Source

Built Distribution

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

qpsolvers-1.7.0-py3-none-any.whl (35.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for qpsolvers-1.7.0.tar.gz
Algorithm Hash digest
SHA256 86a10a3984df6d8abc263da8870b3af14a7149e31ec7565ce569a52175b9cf2f
MD5 ad1005f9ca6276dcf67a3a3f00d0a2a4
BLAKE2b-256 e71cbfba9a2737f7a8b3acd09fac68dde9c0d9039ac926b0a3ea849d6a950781

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for qpsolvers-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f5f0685aa5dbf0b23ef99ddeb28bc5b33cf82fcf2b12d64ace44d281ae7b12b0
MD5 5a29975954d9def30c3c99ccf2027ab9
BLAKE2b-256 e3f0a32449d2590a7f2577fc377d900f1b6e4823df6adf4990d3eae7c937f864

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