archimedes.qpsolΒΆ
- archimedes.qpsol(obj: Callable, constr: Callable, x0: ArrayLike, lba: ArrayLike | None = None, uba: ArrayLike | None = None, lam_a0: ArrayLike | None = None, args: Sequence = (), verbose: bool = False, name: str | None = None, warm_start: bool = True, **options) QPSolution ΒΆ
Solve a quadratic programming problem
This function solves a quadratic problem of the form:
minimize (1/2) x^T Q(p) x + c(p)^T x subject to lba <= A(p)x <= uba
where
x
represents decision variables andp
represents parameters. The arraysQ
,c
, andA
that define the convex quadratic program are determined by automatically differentiating the provided objective and constraint functions. That is, if the objective and constraint functions do not define a convex quadratic program, the solver will solve the convex quadratic approximation to the provided problem, determined by linearization about the initial guess.- Parameters:
obj (callable) β Objective function to minimize with signature
obj(x, *args)
. Must return a scalar value.constr (callable) β Constraint function with signature
constr(x, *args)
. Must return a vector of constraint values where the constraints are interpreted aslba <= constr(x, *args) <= uba
.x0 (array-like) β Initial guess for the decision variables. This is used to determine the linearization point for the convex approximation and to warm-start the QP solver. If None, the initial guess will be set to zero.
lba (array-like, optional) β Lower bounds for the constraints. If None, the lower bounds will be set to negative infinity.
uba (array-like, optional) β Upper bounds for the constraints. If None, the upper bounds will be set to positive infinity.
lam_a0 (array-like, optional) β Initial guess for the dual variables associated with the constraints. This is used to warm-start the QP solver.
verbose (bool, default=False) β Print output from the solver, including number of iterations and convergence.
name (str, optional) β Name for the resulting solver function. If None, a name will be generated based on the objective function name.
warm_start (bool, default=True) β Whether to enable warm starting. Default is True.
options (dict) β Additional options passed to the underlying QP solver (OSQP).
- Returns:
solution β
- A named tuple containing the solution to the QP problem, including:
x
: The optimal decision variables.lam_a
: The dual variables associated with the constraints.
- Return type:
QPSolution
Notes
When to use this function: - For solving convex quadratic optimization problems efficiently - For embedding QP solvers in larger computational graphs - As part of model predictive control (MPC) or trajectory optimization
The solution to the quadratic program is unique, so the initial guess is less important than for more general nonlinear programming. The exception is when the QP is the convex approximation to a nonlinear program, in which case the initial guess is used as the linearization point.
This function supports code generation, but requires linking the OSQP C library to the generated code.
Edge cases: - If the objective function is not convex (i.e., the Hessian is not positive semidefinite), OSQP may fail to find a solution. - For problems with equality constraints, set the same value for both the lower and upper bounds. - Currently only supports scalar and vector decision variables, not matrices.
Examples
>>> import numpy as np >>> import archimedes as arc >>> >>> # Define a simple QP: minimize x^2 + y^2 subject to x + y >= 1 >>> def obj(z): ... return np.dot(z, z) ... >>> def constr(z): ... return z[0] + z[1] ... >>> # Create initial guess and constraint bounds >>> z0 = np.array([0.0, 0.0]) >>> lba = 1.0 # Lower bound for x + y >= 1 >>> >>> # Create and solve the QP >>> sol = arc.qpsol(obj, constr, z0, lba=lba) >>> print(f"Optimal solution: x = {sol.x}") >>> print(f"Dual variables: Ξ» = {sol.lam_a}")