OSQP: The Operator Splitting QP Solver
OSQP (Operator Splitting Quadratic Program) is an optimization solver for Quadratic Programs (QPs) using the Alternating Direction Method of Multipliers (ADMM). It's primarily written in C, providing high-level language interfaces for Python, Julia, Matlab, and R. The current Python library version is 1.1.1, with an active development and release cadence of several updates per year, including major versions that introduce breaking changes.
Warnings
- breaking Migration from v0.6.x to v1.0 introduced significant API changes, including modified `osqp_setup` function signature, new update functions (`osqp_update_data_vec`, `osqp_update_data_mat` replacing old vector/matrix updates), and renamed settings (e.g., `polish` to `polishing`, `warm_start` to `warm_starting`).
- gotcha Installing optional algebra backends (e.g., `osqp[mkl]` or `osqp[cu12]`) only installs the Python binding; it does NOT automatically install the underlying MKL or CUDA runtime libraries. These libraries must be installed separately and made available to your Python environment for the backends to function.
- gotcha OSQP is designed for convex Quadratic Programs. While the solver includes heuristics to detect non-convexity, it might fail to reliably identify non-convex problems, especially those with slightly negative eigenvalues of P. Providing non-convex problems can lead to unexpected or incorrect results.
- gotcha When updating problem matrices `P` or `A` using `m.update()`, only the *values* of existing nonzero entries can be changed. Their sparsity pattern (i.e., which entries are zero and non-zero) cannot be modified without a full problem re-setup.
- gotcha By default, the `problem.solve()` method in v1.x has `raise_error=False`. This means if the solver does not reach an optimal solution (e.g., due to reaching max iterations or being primal/dual infeasible), it will return a result object without raising an exception, potentially hiding issues.
Install
-
pip install osqp
Imports
- osqp
import osqp
- numpy
import numpy as np
- sparse
from scipy import sparse
Quickstart
import osqp
import numpy as np
from scipy import sparse
# Define problem data
P = sparse.csc_matrix([[4, 1], [1, 2]])
q = np.array([1, 1])
A = sparse.csc_matrix([[1, 1], [1, 0], [0, 1]])
l = np.array([1, 0, 0])
u = np.array([1, 0.7, 0.7])
# Create an OSQP object
m = osqp.OSQP()
# Setup workspace
m.setup(P=P, q=q, A=A, l=l, u=u, verbose=False)
# Solve problem
res = m.solve()
# Print results
print(f"Problem status: {res.info.status}")
print(f"Optimal x: {res.x}")