25#include "neml2/solvers/NewtonWithTrustRegion.h"
26#include "neml2/misc/math.h"
39 "A trust-region Newton solver. The step size and direction are modified by solving a "
40 "constrained minimization problem using the local quadratic approximation. The default "
41 "solver parameters are chosen based on a limited set of problems we tested on and are "
42 "expected to be tuned.";
44 options.
set<
Real>(
"delta_0") = 1.0;
45 options.
set(
"delta_0").doc() =
"Initial trust region radius";
47 options.
set<
Real>(
"delta_max") = 10.0;
48 options.
set(
"delta_max").doc() =
"Maximum trust region radius";
50 options.
set<
Real>(
"reduce_criteria") = 0.25;
51 options.
set(
"reduce_criteria").doc() =
"The trust region radius is reduced when the merit "
52 "function reduction is below this threshold";
54 options.
set<
Real>(
"expand_criteria") = 0.75;
55 options.
set(
"expand_criteria").doc() =
"The trust region radius is increased when the merit "
56 "function reduction is above this threshold";
58 options.
set<
Real>(
"reduce_factor") = 0.25;
59 options.
set(
"reduce_factor").doc() =
"Factor to apply when reducing the trust region radius";
61 options.
set<
Real>(
"expand_factor") = 2.0;
62 options.
set(
"expand_factor").doc() =
"Factor to apply when increasing the trust region radius";
64 options.
set<
Real>(
"accept_criteria") = 0.1;
65 options.
set(
"accept_criteria").doc() =
66 "Reject the current step when the merit function reduction is below this threshold";
68 options.
set<
Real>(
"subproblem_rel_tol") = 1
e-6;
69 options.
set(
"subproblem_rel_tol").doc() =
"Relative tolerance used for the quadratic sub-problem";
71 options.
set<
Real>(
"subproblem_abs_tol") = 1
e-8;
72 options.
set(
"subproblem_abs_tol").doc() =
"Absolute tolerance used for the quadratic sub-problem";
74 options.
set<
unsigned int>(
"subproblem_max_its") = 10;
75 options.
set(
"subproblem_max_its").doc() =
76 "Maximum number of allowable iterations when solving the quadratic sub-problem";
83 _subproblem(subproblem_options(options)),
84 _subproblem_solver(subproblem_solver_options(options)),
85 _delta_0(options.get<
Real>(
"delta_0")),
86 _delta_max(options.get<
Real>(
"delta_max")),
87 _reduce_criteria(options.get<
Real>(
"reduce_criteria")),
88 _expand_criteria(options.get<
Real>(
"expand_criteria")),
89 _reduce_factor(options.get<
Real>(
"reduce_factor")),
90 _expand_factor(options.get<
Real>(
"expand_factor")),
91 _accept_criteria(options.get<
Real>(
"accept_criteria"))
108 solver_options.set<
unsigned int>(
"max_its") = options.
get<
unsigned int>(
"subproblem_max_its");
131 auto red_a = 0.5 * torch::pow(
nR, 2.0) - 0.5 * torch::pow(
nRp, 2.0);
150 std::cout <<
" RHO MIN/MAX : " << std::scientific << torch::min(
rho).item<
Real>()
151 <<
"/" << std::scientific << torch::max(
rho).item<
Real>() << std::endl;
152 std::cout <<
" ACCEPTANCE RATE : " << torch::sum(
accept).item<
TorchSize>() <<
"/"
154 std::cout <<
" ADJUSTED DELTA MIN/MAX : " << std::scientific
155 << torch::min(
_delta).item<
Real>() <<
"/" << std::scientific
156 << torch::max(
_delta).item<
Real>() << std::endl;
159 x.variable_data().copy_(torch::where(
accept,
xp,
x));
183 std::cout <<
" TRUST-REGION ITERATIONS: " <<
iters << std::endl;
184 std::cout <<
" ACTIVE CONSTRAINTS : " << torch::sum(
s > 0).item<
TorchSize>() <<
"/"
TorchShapeRef batch_sizes() const
Return the batch size.
Definition BatchTensorBase.cxx:149
Derived batch_index(TorchSlice indices) const
Get a batch.
Definition BatchTensorBase.cxx:184
void batch_index_put(TorchSlice indices, const torch::Tensor &other)
Set a index sliced on the batch dimensions to a value.
Definition BatchTensorBase.cxx:202
Definition BatchTensor.h:32
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
static Scalar full(Real init, const torch::TensorOptions &options=default_tensor_options())
Unbatched tensor filled with a given value given base shape.
Definition FixedDimTensor.h:176
The nonlinear solver solves a nonlinear system of equations.
Definition NewtonWithTrustRegion.h:47
Newton _subproblem_solver
Solver used to solver the trust-region subproblem.
Definition NewtonWithTrustRegion.h:73
virtual BatchTensor solve_direction(const NonlinearSystem &system) override
Find the current update direction.
Definition NewtonWithTrustRegion.cxx:164
Real _reduce_criteria
Criteria for reducing the trust region.
Definition NewtonWithTrustRegion.h:85
Real _reduce_factor
Cutback factor if we do reduce the trust region.
Definition NewtonWithTrustRegion.h:91
TrustRegionSubProblem _subproblem
Trust-region subproblem.
Definition NewtonWithTrustRegion.h:70
OptionSet subproblem_solver_options(const OptionSet &) const
Extract options for the subproblem solver.
Definition NewtonWithTrustRegion.cxx:103
Real _delta_max
Maximum size of the trust region.
Definition NewtonWithTrustRegion.h:82
Scalar merit_function_reduction(const NonlinearSystem &system, const BatchTensor &p) const
Reduction in the merit function.
Definition NewtonWithTrustRegion.cxx:193
Real _delta_0
Initial size of the trust region.
Definition NewtonWithTrustRegion.h:79
OptionSet subproblem_options(const OptionSet &) const
Extract options for the subproblem.
Definition NewtonWithTrustRegion.cxx:96
NewtonWithTrustRegion(const OptionSet &options)
Definition NewtonWithTrustRegion.cxx:81
Real _expand_factor
Expansion factor if we do increase the trust region.
Definition NewtonWithTrustRegion.h:94
Scalar _delta
The trust region radius.
Definition NewtonWithTrustRegion.h:76
Real _accept_criteria
Acceptance criteria for a step.
Definition NewtonWithTrustRegion.h:97
virtual void update(NonlinearSystem &system, BatchTensor &x) override
Update trial solution.
Definition NewtonWithTrustRegion.cxx:119
static OptionSet expected_options()
Definition NewtonWithTrustRegion.cxx:35
Real _expand_criteria
Criteria for expanding the trust region.
Definition NewtonWithTrustRegion.h:88
virtual void prepare(const NonlinearSystem &system, const BatchTensor &x) override
Prepare solver internal data before the iterative update.
Definition NewtonWithTrustRegion.cxx:113
The nonlinear solver solves a nonlinear system of equations.
Definition Newton.h:39
virtual std::tuple< bool, size_t > solve(NonlinearSystem &system, BatchTensor &x) override
Solve the given nonlinear system.
Definition Newton.cxx:48
static OptionSet expected_options()
Definition Newton.cxx:34
virtual BatchTensor solve_direction(const NonlinearSystem &system)
Find the current update direction.
Definition Newton.cxx:121
Definition of a nonlinear system of equations.
Definition NonlinearSystem.h:37
static OptionSet expected_options()
Definition NonlinearSystem.cxx:31
virtual BatchTensor solution() const
Get the solution vector.
Definition NonlinearSystem.h:66
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:59
const std::string & doc() const
A readonly reference to the option set's docstring.
Definition OptionSet.h:91
const T & get(const std::string &) const
Definition OptionSet.h:422
T & set(const std::string &)
Definition OptionSet.h:436
The (logical) scalar.
Definition Scalar.h:38
const bool verbose
Whether to print additional (debugging) information during the solve.
Definition Solver.h:49
virtual void reinit(const NonlinearSystem &system, const Scalar &delta)
Definition TrustRegionSubProblem.cxx:36
BatchTensor preconditioned_direction(const Scalar &s) const
Definition TrustRegionSubProblem.cxx:74
BatchTensor vector_norm(const BatchTensor &v)
Vector norm of a vector. Falls back to math::abs is v is a Scalar.
Definition math.cxx:317
BatchTensor bvv(const BatchTensor &a, const BatchTensor &b)
Batched vector-vector (dot) product.
Definition BatchTensor.cxx:137
BatchTensor bmv(const BatchTensor &a, const BatchTensor &v)
Batched matrix-vector product.
Definition BatchTensor.cxx:122
Derived sqrt(const Derived &a)
Definition BatchTensorBase.h:439
TorchSize storage_size(TorchShapeRef shape)
The flattened storage size of a tensor with given shape.
Definition utils.cxx:32
Definition CrossRef.cxx:32