QP Solvers for Python
Wrapper around Quadratic Programming (QP) solvers in Python, with a unified interface.
The simplest way to install this module is:
pip install qpsolvers
You can add the
--user parameter for a user-only installation. See also the
wiki page for
advanced installation instructions.
solve_qp(P, q, G, h, A, b) is called with the
keyword argument to select the backend solver. The quadratic program it solves
is, in standard form:
Vector inequalities are taken coordinate by coordinate.
The list of supported solvers currently includes:
To solve a quadratic program, simply build the matrices that define it and call
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) # quick way to build a symmetric 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,)) print "QP solution:", solve_qp(P, q, G, h)
This example outputs the solution
[-0.49025721 -1.57755261 -0.66484801].
On the dense example above, the performance of all solvers (as measured by
%timeit on my machine) is:
Meanwhile, on the sparse.py example, these performances become:
Finally, here are the results on a benchmark of random problems generated with the randomized.py example (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.