Registry / ai-ml / quadprog

quadprog

JSON →
library0.1.13pypypi✓ verified 87d ago

quadprog is a Python wrapper for a C++ library that efficiently solves quadratic programming problems. It minimizes `0.5 * x^T G x + a^T x` subject to `C^T x >= b` and an optional number of equality constraints (`meq`). The library is currently at version 0.1.13 and receives active, though somewhat irregular, maintenance.

pip install quadprog
INSTALL
IMPORT
SIG · QUADPROG
Q
quadprog
ai-mlpythonv0.1.13
Install
3.6s avg
Import
261ms
Disk
89MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.13 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
build_error
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.6s · import 0.261s · 87MB
89MB installed
● package 89MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

solve_qp
from quadprog import solve_qp

This example demonstrates how to set up and solve a simple quadratic programming problem with inequality constraints using `quadprog.solve_qp`. The problem minimizes `x^2 + y^2 - x - y` subject to `x >= 0`, `y >= 0`, and `x + y >= 1`.

import numpy as np from quadprog import solve_qp # Define the quadratic program: # Minimize 0.5 * x^T G x + a^T x # Subject to C^T x >= b # Example: Minimize x^2 + y^2 - x - y # This means G = [[2, 0], [0, 2]] and a = [-1, -1] G = np.array([[2., 0.], [0., 2.]]) a = np.array([-1., -1.]) # Constraints: x >= 0, y >= 0, x + y >= 1 # In quadprog, C's columns are the normal vectors of the constraints. # C^T x >= b => [[1, 0, 1], [0, 1, 1]]^T x >= [0, 0, 1]^T # Which means: # 1*x + 0*y >= 0 # 0*x + 1*y >= 0 # 1*x + 1*y >= 1 C = np.array([[1., 0., 1.], [0., 1., 1.]]) b = np.array([0., 0., 1.]) # Number of equality constraints (first 'meq' rows of C and b) meq = 0 # Solve the QP. It returns (x, fval, xu, l) # x: solution vector # fval: objective function value at x # xu: unconstrained solution (unused in this example) # l: Lagrange multipliers (unused in this example) solution, _, _, _ = solve_qp(G, a, C, b, meq) print(f"Optimal solution x: {solution}") # Expected output for this problem: Optimal solution x: [0.5 0.5]
Debug
Known issues
breakingVersion 0.1.10 is explicitly marked as 'not recommended for use' due to a bug related to Lagrange multipliers for equality constraints.
fix
Upgrade to version 0.1.11 or later to avoid this critical bug.
affects: 0.1.10
gotchaThe matrix `G` must be symmetric and positive definite. If `G` is not positive definite, the solver may fail or return incorrect results. Ensure numerical stability for your problem.
fix
Verify that your `G` matrix is symmetric and strictly positive definite. For indefinite problems, consider other QP solvers or reformulate the problem.
affects: All
gotchaThe `C` matrix requires constraint vectors as its columns. If you define constraints as `A x >= b`, then `C` in `solve_qp` should be `A.T`.
fix
Always transpose your constraint matrix `A` (where `A x >= b`) when passing it as `C` to `solve_qp`, i.e., `C = A.T`.
affects: All
gotchaThe `meq` parameter specifies the number of *equality* constraints, and these constraints *must* be the first `meq` rows in both `C` and `b`. Mixing equality and inequality constraints out of order will lead to incorrect solutions.
fix
Ensure all equality constraints are placed at the beginning of your `C` and `b` arrays, and set `meq` accordingly.
affects: All
gotchaThe `factorized` parameter (default `False`) determines if `G` is expected as the Cholesky factor `R` (such that `R^T R = G`). Setting it to `True` when `G` is the original quadratic matrix will lead to incorrect results.
fix
Only set `factorized=True` if you are providing the Cholesky decomposition `R` of `G`. Otherwise, keep the default `factorized=False` and provide the original `G` matrix.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'quadprog'
The 'quadprog' library has not been installed in the current Python environment or is not accessible.
fix
Install the library using pip: `pip install quadprog`
ValueError: array must be 1-dimensional
The 'a' vector (linear term in the QP objective) must be a 1D NumPy array (shape `(n,)`), but a 2D array (e.g., shape `(n, 1)`) was provided.
fix
Ensure the 'a' vector has a shape of `(n,)` by using methods like `a = a.flatten()` or `a = a.ravel()`.
ValueError: Expects G to be a (n, n) matrix
One or more of the input matrices ('G', 'C') or vectors ('a', 'b') do not have the precise dimensions required by the `solve_qp` function for the given number of variables (n) and constraints (m).
fix
Verify that 'G' is `(n, n)`, 'a' is `(n,)`, 'C' is `(n, m)`, and 'b' is `(m,)` where `n` is the number of variables and `m` is the number of constraints, reshaping them if necessary.
ValueError: quadprog.solve_qp: failed to solve problem
The underlying C++ solver failed to converge or find a solution, typically because the quadratic programming problem is ill-posed, infeasible, or the 'G' matrix (Hessian) is not strictly positive definite.
fix
Check if the 'G' matrix is strictly positive definite and well-conditioned; ensure the problem is feasible by reviewing your problem formulation, constraints, and objective function coefficients, and consider adding a small regularization term (e.g., `G = G + 1e-6 * np.eye(n)`) if 'G' is only positive semi-definite or nearly singular.
Upgrade
Version history
0.1.13latest on PyPI · released Oct 24, 2024
Audit
Dependencies
numpyrequiredRequired for numerical array operations and input/output formats.
Agent activity
13 hits · last 30 days
node
10
Amazon
1
OpenAI (training)
1
Resources
quadprog — pip install quadprog · libregistry