API Reference¶
The whole public surface is three names, importable from the top-level package:
| Export | What it is | Start here when… |
|---|---|---|
solve_qp |
Solves a strictly convex QP by the Goldfarb/Idnani dual method | you have a QP |
Solution |
What solve_qp returns — minimiser, objective, multipliers, active set |
you want to read the result |
Sweep |
Keeps one factorisation across a family of QPs differing only in a |
you have many related QPs |
The problem¶
$$\min_x \tfrac{1}{2} x^T G x - a^T x \quad \text{subject to} \quad C^T x \ge b$$
with G symmetric positive definite, and the first meq constraints treated as
equalities.
Two conventions are inherited from the original quadprog and are easy to trip over:
- the linear term is subtracted, not added;
- constraints are column-wise —
Cisn × m, one column per constraint — and stated as>=.
Errors¶
Everything raises ValueError: inconsistent shapes, an out-of-range meq, a G
that is not positive definite, and constraints that admit no solution. Nothing
returns a status code, so a result is always a solution.
Many related problems¶
Sweep is for the case where G, C, b and meq are fixed and only the
linear term moves — an efficient frontier, a rolling rebalance, a scenario grid.
It reuses the factorisation when the cached active set still satisfies the KKT
conditions, and repairs it when it does not, so it returns what solve_qp would
return and never something else.
A faster path for one problem¶
solve_qp(..., fast=True) tries a primal-dual active set before the exact walk,
and keeps its answer only if that answer passes the KKT conditions. It is off by
default because two reported fields change when it answers — see the argument's
own documentation below.
Drop-in compatibility¶
Solution is a NamedTuple yielding its six fields in the same order as the
plain tuple quadprog.solve_qp returns, so existing unpacking keeps working:
solve_qp¶
cvx.quadprog.solve_qp(G, a, C=None, b=None, meq=0, factorized=False, check_finite=False, fast=False, blas_threads=None)
¶
Solve a strictly convex quadratic program.
Minimises :math:\tfrac{1}{2} x^T G x - a^T x subject to
:math:C^T x \ge b, with the first meq constraints held as equalities.
The example below is chosen so its answer can be written down rather than
discovered: with G the identity the objective separates, and each
coordinate reduces to minimising :math:\tfrac{1}{2} x_i^2 - a_i x_i over
:math:x_i \ge 0. That is the unconstrained minimiser with its negative
entries clipped to zero -- the projection of a onto the non-negative
orthant.
import numpy as np from cvx.quadprog import solve_qp G = np.eye(3) a = np.array([1.0, -2.0, 3.0]) C = np.eye(3) b = np.zeros(3) solution = solve_qp(G, a, C, b) bool(np.allclose(solution.x, [1.0, 0.0, 3.0])) True
The other fields describe the same solve. xu is the unconstrained
minimiser :math:G^{-1} a, which here is a itself; iact names the
constraints that ended up binding, 1-based -- only the second, since it is
the only one xu violates; and f is the objective at x.
bool(np.allclose(solution.xu, a)) True solution.iact.tolist() [2] round(solution.f, 12) -5.0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
ndarray
|
See :func: |
required |
a
|
ndarray
|
See :func: |
required |
C
|
ndarray | None
|
See :func: |
None
|
b
|
ndarray | None
|
See :func: |
None
|
meq
|
int
|
See :func: |
0
|
factorized
|
bool
|
See :func: |
False
|
check_finite
|
bool
|
See :func: |
False
|
fast
|
bool
|
Offer the problem to the primal-dual active-set path in
:mod: It is not uniformly faster, which is the other reason it is opt-in. Where the exact walk happens to converge in one or two iterations -- a box-constrained problem whose unconstrained minimum is nearly feasible, say -- there is nothing to save, and the factorisation and certificate this path pays for anyway make it up to 20% slower. Those are also the cheapest solves there are, so the loss is a handful of microseconds against the hundreds this saves elsewhere. Two reported fields differ when the fast path answers, which is why
this is off by default. |
False
|
blas_threads
|
int | None
|
Cap the BLAS thread count for the duration of this call, via
a scoped Left unset, threading is touched only where it has been measured to
be catastrophic: on Linux, against an OpenBLAS build, with more
threads configured than there are physical cores, and only once Set it explicitly to override that, in either direction: an explicit
count is used as given and the automatic gate is not consulted. Worth
doing on MKL, where more threads than cores is not the trap it is on
OpenBLAS, or to pin a solve to 1. Not worth doing around a small solve:
|
None
|
Returns:
| Type | Description |
|---|---|
Solution
|
The solution. |
This is a thin wrapper over :func:_solve_with_factors, which additionally
returns the factorisation it ends on. Nothing about the solve differs; the
factors are simply discarded here, because for a single problem they are dead
state and J alone is n^2 doubles -- 15.7 MB at n = 1400, against
the 33 KB of the :class:Solution itself. :class:~cvx.quadprog.Sweep keeps
them instead, which is the whole reason the split exists.
Setting fast additionally offers the problem to :mod:._pdas first. See
the argument's own documentation for what that changes and what it does not.
Raises:
| Type | Description |
|---|---|
ValueError
|
As :func: |
ImportError
|
If |
Source code in src/cvx/quadprog/_solve.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
Solution¶
cvx.quadprog.Solution
¶
Bases: NamedTuple
The outcome of a quadratic program.
Iterating over an instance yields the same six values, in the same order, as
the tuple returned by quadprog.solve_qp, so it is a drop-in replacement.
That ordering is what the example below pins down, on a problem small enough
to check by hand: with G the identity the objective separates into
x_i**2 / 2 - a_i x_i per coordinate, so subject to x >= 0 the answer
is a with its negative entries clipped to zero.
import numpy as np from cvx.quadprog import solve_qp solution = solve_qp(np.eye(2), np.array([1.0, -1.0]), np.eye(2), np.zeros(2)) x, f, xu, iterations, lagrangian, iact = solution x.tolist() [1.0, 0.0] xu.tolist() [1.0, -1.0] round(f, 12) -0.5
Only the second constraint binds, so iact names it -- 1-based -- and only
its multiplier is non-zero. iterations is a pair, additions then removals,
and its first entry counts outer iterations: it therefore exceeds the number
of constraints that ended up active, one iteration having brought x_2 onto
its bound and a final one confirming there was nothing left to add.
iact.tolist() [2] lagrangian.tolist() [0.0, 1.0] iterations.tolist() [2, 0]
Attributes:
| Name | Type | Description |
|---|---|---|
x |
ndarray
|
|
f |
float
|
Value of the objective at |
xu |
ndarray
|
|
iterations |
ndarray
|
|
lagrangian |
ndarray
|
|
iact |
ndarray
|
1-based indices of the constraints active at the solution. |
Source code in src/cvx/quadprog/_base.py
Sweep¶
cvx.quadprog.Sweep
¶
Solve a family of QPs sharing G, C, b and meq.
Only the linear term changes between calls. The first call solves from scratch; later ones reuse the factorisation when the active set still holds.
import numpy as np from cvx.quadprog import Sweep, solve_qp G = np.eye(3) C = np.array([[-4.0, 2.0, 0.0], [-3.0, 1.0, -2.0], [0.0, 0.0, 1.0]]) b = np.array([-8.0, 2.0, 0.0]) sweep = Sweep(G, C, b) a = np.array([0.0, 5.0, 0.0]) bool(np.allclose(sweep.solve(a).x, solve_qp(G, a, C, b).x)) True bool(np.allclose(sweep.solve(1.01 * a).x, solve_qp(G, 1.01 * a, C, b).x)) True
Source code in src/cvx/quadprog/_sweep.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | |
__init__(G, C=None, b=None, meq=0, check_finite=False, blas_threads=None)
¶
Fix the part of the problem that does not vary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
ndarray
|
|
required |
C
|
ndarray | None
|
|
None
|
b
|
ndarray | None
|
|
None
|
meq
|
int
|
Number of leading constraints to treat as equalities. |
0
|
check_finite
|
bool
|
Whether to reject NaN and infinity in |
False
|
blas_threads
|
int | None
|
Cap the BLAS thread count for the expensive parts of this
sweep, as :func: Decided once here rather than per call, because |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the shapes are inconsistent, if |
ImportError
|
If |
Source code in src/cvx/quadprog/_sweep.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
solve(a)
¶
Solve for a new linear term.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ndarray
|
|
required |
Returns:
| Type | Description |
|---|---|
Solution
|
The same :class: |
Solution
|
func: |
Solution
|
that |
Solution
|
reused outright -- no active-set iteration was performed. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |