Quadratic programming solvers in Python with a unified API
Project description
QP Solvers for Python
Installation  Documentation  Example  Solvers  FAQ  Benchmark
Unified interface to Quadratic Programming (QP) solvers available in Python.
Installation
pip install qpsolvers
Check out the documentation for Python 2 or Windows instructions.
Usage
The library provides a onestop shop solve_qp(P, q, G, h, A, b, lb, ub)
function with a solver
keyword argument to select the backend solver. It solves convex quadratic programs in standard form:
Vector inequalities are taken coordinate by coordinate. For most solvers, the matrix P should be positive definite.
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) 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:
Solver  Keyword  Type  License  Warmstart 

CVXOPT  cvxopt 
Dense  GPL3.0  ✔️ 
ECOS  ecos 
Sparse  GPL3.0  ✖️ 
Gurobi  gurobi 
Sparse  Commercial  ✖️ 
MOSEK  mosek 
Sparse  Commercial  ✔️ 
OSQP  osqp 
Sparse  Apache2.0  ✔️ 
qpOASES  qpoases 
Dense  LGPL2.1  ➖ 
qpSWIFT  qpswift 
Sparse  GPL3.0  ✖️ 
quadprog  quadprog 
Dense  GPL2.0  ✖️ 
SCS  scs 
Sparse  MIT  ✔️ 
Frequently Asked Questions
 Can I print the list of solvers available on my machine?
 Absolutely:
print(qpsolvers.available_solvers)
 Absolutely:
 Is it possible to solve a least squares rather than a quadratic program?
 Yes,
qpsolvers
also provides a solve_ls function.
 Yes,
 I have a squared norm in my cost function, how can I apply a QP solver to my problem?
 You can cast squared norms to QP matrices and feed the result to
solve_qp
.
 You can cast squared norms to QP matrices and feed the result to
 I have a nonconvex quadratic program. Is there a solver I can use?
 I get the following build error on Windows when running
pip install qpsolvers
. You will need to install the Visual C++ Build Tools to build all package dependencies.
Benchmark
On a dense problem, the performance of all solvers (as measured by IPython's %timeit
on an Intel(R) Core(TM) i76700K 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 
cvxpy  Sparse  5.71 
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 
cvxpy  Sparse  11 
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 
cvxpy  Sparse  7.02 
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 SecondOrder 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
Release history Release notifications  RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for qpsolvers1.9.1py3noneany.whl
Algorithm  Hash digest  

SHA256  ffc274a622343332e740d8eddaaf42e65889a2889465b5a214cc6da4298c5dcb 

MD5  2647efc1e6c03f12396efbd9bd9cab2c 

BLAKE2256  8f34126a51ca56748ae6a5fec66ebdb6a8f46a15b07c9cc35fdb32566c069d22 