UnconstrainedOptimizationAdjoint() differentiates the solution u* of
an unconstrained OptimizationProblem() with respect to the parameters
by the implicit function theorem on the optimality condition
grad f(u*, p) = 0: with the Hessian H, the adjoint lambda = H^-1 g_u
gives dG/dp = g_p - lambda' d(grad f)/dp, the SteadyStateAdjoint() of
the stationarity condition.
Usage
UnconstrainedOptimizationAdjoint(
chunk_size = 0,
autodiff = TRUE,
diff_type = "central",
objective_ad = TRUE,
autojacvec = NULL,
linsolve = NULL,
linsolve_kwargs = list()
)
OptimizationAdjoint(
chunk_size = 0,
autodiff = NULL,
diff_type = "forward",
autojacvec = NULL,
linsolve = NULL,
linsolve_kwargs = list(),
active_tol = NULL
)Arguments
- chunk_size, autodiff, diff_type, objective_ad
Options of the Julia signatures (the automatic differentiation choices).
- autojacvec
FALSEtakes central differences also for problems built from a System.- linsolve, linsolve_kwargs
Linear solver options (unused; the KKT system is solved directly).
- active_tol
Tolerance of the active set detection;
sqrt(.Machine$double.eps)whenNULL.
Details
OptimizationAdjoint() differentiates a constrained problem through its
KKT conditions. Equality constraints (lcons == ucons), inequality
constraints lcons <= cons(u, p) <= ucons within active_tol of a bound
and bounds lb <= u <= ub within active_tol are active; the
multipliers are the least squares solution of the stationarity
condition, and constraints whose multiplier has the wrong sign are
dropped from the active set. One solve of the symmetric KKT system
[L_xx J'; J 0] lambda = [g_u; 0] then gives dG/dp = -lambda' dF/dp
for the KKT residual F = (grad_x L, active constraints).
Both are chosen automatically by solve_gradient() for an
OptimizationProblem (the KKT adjoint when the problem has constraints or
bounds), and adjoint_sensitivities() applies them to an optimization
solution with dgdu. The derivatives come from the System the problem
was built from (symbolic), or from the grad, hess, cons_j and
cons_h of the OptimizationFunction and central differences. Julia
requires the stored gradient of the OptimizationFunction; here a
missing gradient is taken by central differences, the counterpart of its
automatic differentiation backend.
Examples
f <- function(x, p) (x[1] - p[1])^2 + p[2] * (x[2] - x[1]^2)^2
prob <- OptimizationProblem(f, c(0, 0), c(1.5, 3))
# u* = (p1, p1^2): the gradient of u1 + u2 is (1 + 2 p1, 0)
solve_gradient(function(sol) sum(sol$u), prob, BFGS(),
sensealg = UnconstrainedOptimizationAdjoint())$p
#> [1] 4.000000e+00 -9.920885e-10