Quadratic Programming Solvers

4.11.0 · active · verified Mon Apr 13

qpsolvers is a Python library that provides a unified API to various quadratic programming (QP) solvers. It simplifies the process of solving convex QPs by abstracting away solver-specific APIs and matrix format requirements. Currently at version 4.11.0, the library maintains an active development pace with frequent minor releases to integrate new solvers and provide updates for existing ones.

Warnings

Install

Imports

Quickstart

This example demonstrates how to define a quadratic programming problem using NumPy arrays and solve it with an available backend solver. It automatically selects 'proxqp' if available, otherwise it tries the first available solver, or prompts the user to install one if no solvers are found. The problem is formulated in the standard quadratic program form.

import numpy as np
from qpsolvers import solve_qp, available_solvers

# Define the QP problem in standard form: minimize 0.5 * x.T * P * x + q.T * x 
# subject to G * x <= h, A * x == b, lb <= x <= ub

M = np.array([[1., 2., 0.], [-8., 3., 2.], [0., 1., 1.]])
P = M.T @ M  # This results in a positive definite matrix
q = np.array([3., 2., 3.]) @ M

G = np.array([[1., 2., 1.], [2., 0., 1.], [-1., 2., -1.]])
h = np.array([3., 2., -2.])

A = np.array([1., 1., 1.])
b = np.array([1.])

# Try to use a common open-source solver, or fallback if not available
solver_to_use = "proxqp" if "proxqp" in available_solvers else available_solvers[0] if available_solvers else None

if solver_to_use:
    print(f"Using solver: {solver_to_use}")
    x = solve_qp(P, q, G, h, A, b, solver=solver_to_use)
    print(f"QP solution: {x = }")
else:
    print("No QP solvers found. Please install one, e.g., pip install qpsolvers[proxqp]")

view raw JSON →