After a solve, DAQP can differentiate the solution with respect to the problem data, e.g. for sensitivities in MPC or for using a QP as a layer in a learned model. The exact derivative is that of the KKT system of the final working set \(\mathcal{W}\):
\[\begin{bmatrix} H & A_\mathcal{W}^T \\ A_\mathcal{W} & -P_\mathcal{W} \end{bmatrix} \begin{bmatrix} dx \\ d\lambda_\mathcal{W} \end{bmatrix} = \begin{bmatrix} -df \\ db_\mathcal{W} \end{bmatrix},\]where \(P_\mathcal{W}\) holds the reciprocal weights \(\rho\) of active soft constraints with a nonzero slack. Inactive constraints have zero multiplier derivative. The solution is piecewise affine in \(f\) and \(b\), so this is exact while the working set is unchanged (one-sided at weakly active constraints).
Dense problems
daqp_solve(&res, &work);
// dx (n) and dlam (m, may be NULL) for the perturbation (df, dbu, dbl) of (f, bupper, blower); NULL: zero
int flag = daqp_sensitivity(&work, df, dbu, dbl, dx, dlam);
The factorization of the working set from the solve is reused. Give equality constraints the same seed for both bounds. After proximal-point iterations (singular \(H\), LPs), the derivative is refined to that of \(H\). Returns 1, or 0 if unavailable: no solution, a non-unique solution, branch and bound, hierarchical problems, AVIs, eliminated equalities, an LP with an active soft constraint, or data updated since the solve.
Multistage problems
// Example: w.r.t. component j of x_0, fixed by bound k at stage 0
c_float d0[NB0] = {0}, *dbnd[N+1] = {d0}; // (stages with NULL: zero)
d0[k] = 1;
DAQPMultistage seed = {0}; // q, r, b, lb, ub, lg, ug of the perturbation (NULL: zero)
seed.lb = seed.ub = dbnd;
daqp_multistage_sensitivity(&work, tau, &seed, dz, dlam, dpi);
Set prob.sensitivities = 1 before the setup: the memory for sensitivities (about a fifth of the workspace) is otherwise left out, and they are unavailable. The seed has the layout of the problem (its dimensions and matrices are not read); give equalities, such as the bounds fixing \(x_0\), the same seed in lb and ub. dz, dlam and dpi (each may be NULL) are laid out as res.x and the output of daqp_multistage_duals. The cost is one weighted Riccati factorization, shared by seeds with the same tau, and per seed one backward and one forward recursion.
tau = 0: the exact derivative.tau > 0: a smoothed derivative. The exact one is zero for a variable at a bound, which gives no learning signal (e.g. for policy gradients through an MPC with saturated inputs). Withtau > 0, each constraint (multiplier \(\lambda\), slack \(s\)) acts as a penalty with weight \(\tilde\lambda^2/\tau\), \(\tilde\lambda = \tfrac{1}{2}\big((\lambda - s) + \sqrt{(\lambda - s)^2 + 4\tau}\big)\): large for active constraints, small for inactive ones near their bound. Equalities stay exact, and the solution itself is not changed (unlike an interior-point method stopped at barrier parameter \(\tau\)). As \(\tau \to 0\) it tends to the exact derivative; very small \(\tau\) is ill-conditioned.
Returns 1, or 0 if unavailable: no solution, branch and bound, proximal-point iterations, coupling constraints, or a numerical breakdown (e.g. nearly dependent active constraints). The data must be unchanged since the solve.
A solution from elsewhere (e.g. of an NLP solver) can be differentiated without a solve: after the update with its data, daqp_multistage_sensitivity_point(&work, x, lam) takes it (laid out as res.x and the multipliers of daqp_multistage_duals), with the constraints that have a nonzero multiplier at their bound as working set. This also works if the QP is not convex (e.g. an exact Hessian that is positive definite only on the null space of the active constraints); with setup_daqp_multistage_mem, such a setup returns DAQP_EXIT_NONCONVEX but keeps the workspace.
Adjoint (reverse mode)
The KKT matrix is symmetric, so the gradient of a scalar loss \(\ell(x)\) with respect to all of \(f\) and \(b\) takes one call, with the seed \(df = -\bar{x}\) for \(\bar{x} = \partial \ell / \partial x\) (and \(dbu = dbl = \bar{\lambda}\) if \(\ell\) also depends on \(\lambda\)):
| Gradient | Dense | Multistage (seed q, r \(= -\bar{x}\)) |
|---|---|---|
\(\partial\ell/\partial f\) (q, r) | -dx | -dz |
| \(\partial\ell/\partial b\) of the active bound | dlam | dlam |
| \(\partial\ell/\partial b_t\) (dynamics) | -dpi |
The sign of \(\lambda_i\) tells which bound of constraint \(i\) is active (positive: upper).
Derivatives with respect to the matrices
A perturbation of \(H\) or \(A\) is equivalent to one of \(f\) and \(b\) at the solution \((x, \lambda)\):
- forward: \(dH\) gives \(df = dH\,x\), and \(dA\) gives \(df = dA^T\lambda\), \(db = -dA\,x\) (both bounds);
- adjoint, with \(g = \partial\ell/\partial f\) and \(h = \partial\ell/\partial b\) from above: \(\partial\ell/\partial H = \tfrac{1}{2}(g x^T + x g^T)\) and \(\partial\ell/\partial A = \lambda g^T - h x^T\).
For multistage problems the same holds stage by stage, and for the dynamics \(x_{t+1} = A_t x_t + B_t u_t + b_t\) with costate \(\pi_t\): \(dA_t\) gives \(dq_t = dA_t^T \pi_t\) and \(db_t = dA_t x_t\) (\(B_t\) likewise, with \(r_t\) and \(u_t\)).