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.
pip install osqpVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define and solve a simple Quadratic Program using OSQP. It initializes problem matrices (P, A as sparse CSC matrices) and vectors (q, l, u as NumPy arrays), sets up the solver, and then solves the problem, printing the solution status and optimal variable `x`.
Consult the official 'Migration guide from v0.6.x' for a complete list of changes and updated API calls. Update `setup`, `update` method calls, and setting parameters accordingly.
Ensure MKL or CUDA runtime libraries are correctly installed and configured on your system or within your Python environment (e.g., via `conda install cudatoolkit` or manual installation and `LD_LIBRARY_PATH` setup).
Always verify that your problem formulation results in a convex QP before passing it to OSQP. Consider adding regularization if the convexity is borderline or uncertain.
If the sparsity pattern of `P` or `A` needs to change, a new `osqp.OSQP()` object must be created and `m.setup()` called again with the new matrices.
Set `raise_error=True` in `m.solve(raise_error=True)` to explicitly raise an exception if the solver status is not `OSQP_SOLVED`. Alternatively, always check `res.info.status` to inspect the solver's outcome.
Install CMake on your system. On Ubuntu, use `sudo apt-get install cmake`. On macOS, use `brew install cmake`. On Windows, download and install from the official CMake website, ensuring it's added to your PATH.
Install the 'Build Tools for Visual Studio' from Microsoft's website. During installation, select the 'Desktop development with C++' workload. Ensure your pip is up-to-date: `python -m pip install --upgrade pip`.
First, create an OSQP solver object, then call its `solve` method. Example: `import osqp; prob = osqp.OSQP(); prob.setup(P, q, A, l, u); results = prob.solve()`.
Ensure your quadratic program is convex by verifying that the matrix `P` is positive semi-definite. Double-check all input matrices (`P`, `A`) and vectors (`q`, `l`, `u`) for correct dimensions and valid numerical values, especially for `numpy.inf` where appropriate.
Install `numpy` before attempting to install `osqp`: `pip install numpy` then `pip install osqp`.