![]() |
Home | Libraries | People | FAQ | More |
#include <boost/math/optimization/trust_region_newton.hpp> namespace boost { namespace math { namespace optimization { template<typename ArgumentContainer, typename RealType, class Objective, class InitializationPolicy, class ObjectiveEvalPolicy, class GradEvalPolicy, class HessianVectorPolicy> class trust_region_newton { public: void step(); RealType radius() const; }; /* x holds rdiff::rvar<RealType, 2> for the default policies */ template<class Objective, typename ArgumentContainer, class InitializationPolicy = tape_initializer_rvar<RealType, 2>, class FunctionEvalPolicy = reverse_mode_function_eval_policy<RealType, 2>, class GradientEvalPolicy = reverse_mode_gradient_evaluation_policy<RealType, 2>, class HessianVectorPolicy = reverse_mode_hessian_vector_product_policy<RealType>> auto make_trust_region_newton(Objective&& obj, ArgumentContainer& x, RealType initial_radius = 1, InitializationPolicy&& ip = InitializationPolicy{}, FunctionEvalPolicy&& fep = FunctionEvalPolicy{}, GradientEvalPolicy&& gep = GradientEvalPolicy{}, HessianVectorPolicy&& hvp = HessianVectorPolicy{}); } // namespace optimization } // namespace math } // namespace boost
The trust-region Newton method minimizes the quadratic model m(p) = f
+ g.p +
p.Hp/2
built from the exact Hessian H,
over a ball |p| <= radius,
then moves only if the objective agrees with the model. Near a minimum it
converges quadratically, and unlike a line search it needs no positive definite
Hessian: on negative curvature, at a saddle or a maximum, the step goes to
the boundary of the ball along a descent direction.
The variables are rvar<RealType, 2>, and
the objective is evaluated on them. The Hessian is never formed: each Hessian-vector
product is one reverse sweep through the tape that records the gradient,
so the memory is that of the tape, and the method suits large n.
At each step:
g
at x, recording it on
the first order tape.
|p| <= radius
by the Steihaug-Toint truncated conjugate gradient method (Nocedal and
Wright, Algorithm 7.2), stopping at the boundary, on negative curvature,
or when the residual is below min(0.1,
sqrt(|g|)) |g|.
f(x + p),
and the ratio rho of
the actual to the predicted reduction. The radius becomes |p|/4 if
rho <
1/4, and doubles if rho
> 3/4 and
the step reached the boundary (Nocedal and Wright, Algorithm 4.1).
rho > 1e-4;
otherwise x, the objective
value and the gradient are unchanged. A change in f
within its rounding error counts as rho
= 1.
If a constraint policy of minimize
moves x, the next step evaluates
there.
A step costs one objective-and-gradient evaluation, two after a rejected
step, and one Hessian-vector product per conjugate gradient iteration, at
most n. Use the default
gradient_norm_convergence_policy
with minimize: since a rejected
step leaves the objective unchanged, a convergence policy on the change in
objective would stop there.
Objective&&
obj : objective function to
minimize, on rvar<RealType,
2>.
ArgumentContainer&
x : variables to optimize over,
a container of rvar<RealType,
2>.
Updated in-place.
RealType initial_radius
: radius of the first trust region, positive and finite, else std::domain_error is thrown. Default is
1. It adapts, so it need only be of the right order of magnitude; radius()
returns the current one.
InitializationPolicy&& ip
: by default tape_initializer_rvar<RealType, 2>,
which adds checkpoints to the tapes of order 1 and 2.
ObjectiveEvalPolicy&&
oep, GradEvalPolicy&& gep
: by default reverse_mode_function_eval_policy<RealType, 2>
and reverse_mode_gradient_evaluation_policy<RealType, 2>.
HessianVectorPolicy&&
hvp : hvp(objective, x, v, Hv) sets Hv
to the Hessian at x times
v. It is called only
after a gradient evaluation at the same x.
By default reverse_mode_hessian_vector_product_policy<RealType>.
#include <boost/math/differentiation/autodiff_reverse.hpp> #include <boost/math/optimization/minimizer.hpp> #include <boost/math/optimization/trust_region_newton.hpp> #include <iostream> #include <vector> namespace rdiff = boost::math::differentiation::reverse_mode; namespace bopt = boost::math::optimization; template<class Real> Real rosenbrock(std::vector<Real> const& x) { Real f = Real(0); for (size_t i = 0; i + 1 < x.size(); ++i) { f += 100 * (x[i + 1] - x[i] * x[i]) * (x[i + 1] - x[i] * x[i]) + (1 - x[i]) * (1 - x[i]); } return f; } int main() { using rvar = rdiff::rvar<double, 2>; std::vector<double> x0(10, -1.2); std::vector<rvar> x(x0.begin(), x0.end()); // each element its own variable auto opt = bopt::make_trust_region_newton(&rosenbrock<rvar>, x); auto result = bopt::minimize(opt, bopt::unconstrained_policy<std::vector<rvar>>{}, bopt::gradient_norm_convergence_policy<double>(1e-10)); std::cout << result.num_iter << " steps, f = " << result.objective_value << "\n"; }
Per step, one gradient evaluation on rvar<RealType, 2>,
whose tape is many times that of rvar<RealType, 1>,
and up to n Hessian-vector
products, each a sweep of that tape. It takes fewer steps than L-BFGS, and
converges quadratically at the end, but each step costs more: minimizing
the Rosenbrock function from (-1.2, 1, ...), L-BFGS
is 3 to 20 times faster in wall time for n
from 2 to 100 (reporting/performance/trust_region_newton_performance.cpp).
Prefer it where the Hessian is indefinite, or where steps, not evaluations,
are what cost.