Home

Awesome

Quadratic Programming Solvers in Python

CI Documentation Coverage Conda version PyPI version PyPI downloads

This library provides a one-stop shop solve_qp function to solve convex quadratic programs:

$$ \begin{split} \begin{array}{ll} \underset{x}{\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 apply coordinate by coordinate. The function returns the primal solution $x^*$ found by the backend QP solver, or None in case of failure/unfeasible problem. All solvers require the problem to be convex, meaning the matrix $P$ should be positive semi-definite. Some solvers further require the problem to be strictly convex, meaning $P$ should be positive definite.

Dual multipliers: there is also a solve_problem function that returns not only the primal solution, but also its dual multipliers and all other relevant quantities computed by the backend solver.

Example

To solve a quadratic program, build the matrices that define it and call solve_qp, selecting the backend QP solver via the solver keyword argument:

import numpy as np
from qpsolvers import solve_qp

M = np.array([[1.0, 2.0, 0.0], [-8.0, 3.0, 2.0], [0.0, 1.0, 1.0]])
P = M.T @ M  # this is a positive definite matrix
q = np.array([3.0, 2.0, 3.0]) @ M
G = np.array([[1.0, 2.0, 1.0], [2.0, 0.0, 1.0], [-1.0, 2.0, -1.0]])
h = np.array([3.0, 2.0, -2.0])
A = np.array([1.0, 1.0, 1.0])
b = np.array([1.0])

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

This example outputs the solution [0.30769231, -0.69230769, 1.38461538]. It is also possible to get dual multipliers at the solution, as shown in this example.

Installation

From conda-forge

conda install -c conda-forge qpsolvers

From PyPI

To install the library with open source QP solvers:

pip install qpsolvers[open_source_solvers]

This one-size-fits-all installation may not work immediately on all systems (for instance if a solver tries to compile from source). If you run into any issue, check out the following variants:

When imported, qpsolvers loads all the solvers it can find and lists them in qpsolvers.available_solvers.

Solvers

SolverKeywordAlgorithmAPILicenseWarm-start
ClarabelclarabelInterior pointSparseApache-2.0✖️
CVXOPTcvxoptInterior pointDenseGPL-3.0✔️
DAQPdaqpActive setDenseMIT✖️
ECOSecosInterior pointSparseGPL-3.0✖️
GurobigurobiInterior pointSparseCommercial✖️
HiGHShighsActive setSparseMIT✖️
HPIPMhpipmInterior pointDenseBSD-2-Clause✔️
MOSEKmosekInterior pointSparseCommercial✔️
NPPronpproActive setDenseCommercial✔️
OSQPosqpAugmented LagrangianSparseApache-2.0✔️
PIQPpiqpProximal interior pointDense & SparseBSD-2-Clause✖️
ProxQPproxqpAugmented LagrangianDense & SparseBSD-2-Clause✔️
QPALMqpalmAugmented LagrangianSparseLGPL-3.0✔️
qpaxqpaxInterior pointDenseMIT✖️
qpOASESqpoasesActive setDenseLGPL-2.1
qpSWIFTqpswiftInterior pointSparseGPL-3.0✖️
quadprogquadprogActive setDenseGPL-2.0✖️
SCSscsAugmented LagrangianSparseMIT✔️

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

Frequently Asked Questions

Benchmark

QP solvers come with their strengths and weaknesses depending on the algorithmic choices they make. To help you find the ones most suited to your problems, you can check out the results from qpbenchmark, a benchmark for QP solvers in Python. The benchmark is divided into test sets, each test set representing a different distribution of quadratic programs with specific dimensions and structure (large sparse problems, optimal control problems, ...):

Citing qpsolvers

If you find this project useful, please consider giving it a :star: or citing it if your work is scientific:

@software{qpsolvers2024,
  title = {{qpsolvers: Quadratic Programming Solvers in Python}},
  author = {Caron, Stéphane and Arnström, Daniel and Bonagiri, Suraj and Dechaume, Antoine and Flowers, Nikolai and Heins, Adam and Ishikawa, Takuma and Kenefake, Dustin and Mazzamuto, Giacomo and Meoli, Donato and O'Donoghue, Brendan and Oppenheimer, Adam A. and Pandala, Abhishek and Quiroz Omaña, Juan José and Rontsis, Nikitas and Shah, Paarth and St-Jean, Samuel and Vitucci, Nicola and Wolfers, Soeren and Yang, Fengyu and @bdelhaisse and @MeindertHH and @rimaddo and @urob and @shaoanlu and Khalil, Ahmed and Kozlov, Lev},
  license = {LGPL-3.0},
  url = {https://github.com/qpsolvers/qpsolvers},
  version = {4.4.0},
  year = {2024}
}

Contributing

We welcome contributions! 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.

See also