Loading...
Searching...
No Matches
funcmin::RgminScg Class Reference

#include <gpr/ml/RgminScg.h>

Public Member Functions

void setAlgorithmSettings (const gpr::ScgOptimizationSettings &s)
template<typename ClassName, typename FuncName>
void optimize (const gpr::EigenMatrix &x, const Eigen::VectorXd &x_ind, const Eigen::VectorXd &y, Eigen::VectorXd &w, FuncName func_to_min, ClassName &holder, double, const Eigen::VectorXd &lengthscale_rho0=Eigen::VectorXd(), const MapObjectiveConfig *objective_config=nullptr)

Public Attributes

bool failedOptim = false
bool exhaustedIterationBudget = false
int completedIterations = 0
bool hessianVectorFallback = false

Detailed Description

SCG waist through rgmin. The loop lives in Rust. SCG mode selects this optimizer.

Definition at line 29 of file RgminScg.h.

Member Function Documentation

◆ optimize()

template<typename ClassName, typename FuncName>
void funcmin::RgminScg::optimize ( const gpr::EigenMatrix & x,
const Eigen::VectorXd & x_ind,
const Eigen::VectorXd & y,
Eigen::VectorXd & w,
FuncName func_to_min,
ClassName & holder,
double current_barrier_strength,
const Eigen::VectorXd & lengthscale_rho0 = Eigen::VectorXd(),
const MapObjectiveConfig * objective_config = nullptr )

Definition at line 16 of file RgminScg.inl.

22{
23 failedOptim = false;
27 if (settings.common.stop_requested && settings.common.stop_requested()) {
28 failedOptim = true;
29 return;
30 }
31
32 // Without the product nlml_hessian_vector runs as finite_difference:
33 // no curvature callback, so rgmin probes the gradient itself.
34 bool hessian_vector_curvature =
35 settings.curvature_mode == gpr::ScgCurvatureMode::NLML_HESSIAN_VECTOR;
36 if (hessian_vector_curvature && !holderHasNlmlHessianVector(holder)) {
37 hessian_vector_curvature = false;
39 if (settings.common.report_level >= 1) {
40 gpr::io::LogManager log_man;
41 log_man << " nlml_hessian_vector: this objective has no exact NLML "
42 "Hessian-vector product; finite_difference curvature\n";
43 }
44 }
45
46 const MapObjectiveConfig map_cfg =
47 objective_config != nullptr
48 ? *objective_config
49 : makeMapObjectiveConfig(x, y, w, current_barrier_strength,
50 lengthscale_rho0, settings);
51
52 using Ctx = RgminNlmlCtx<ClassName, FuncName>;
53 Ctx ctx(x, x_ind, y, holder, func_to_min, map_cfg,
54 settings.curvature_mode == gpr::ScgCurvatureMode::EXACT, false,
55 settings.common.stop_requested);
56 ctx.trace_evaluations = settings.common.report_level >= 3;
57
58 std::vector<double> wbuf(static_cast<std::size_t>(w.size()));
59 Eigen::Map<Eigen::VectorXd>(wbuf.data(), w.size()) = w;
61 const RgminTensorHandle xt(
62 rgmin_tensor_borrow_cpu_f64(wbuf.data(), wbuf.size()));
63 if (!xt) {
64 failedOptim = true;
65 return;
66 }
67
68 rgmin_control_t ctrl{};
69 // rgmin_minimize_scg still reads 0 as unlimited and stops its own
70 // plateau inside that one-shot. A positive budget is the caller's.
71 ctrl.maxiter = static_cast<size_t>(settings.common.max_iter);
72 // Rust run_scg also stops on ||g||_2 < gtol. Leave gtol inert so
73 // only ScgParams.tol_sol / tol_func apply.
74 ctrl.gtol = 0.0;
75 ctrl.istep = 1.0;
76 ctrl.memory = 0;
77 // Hyperparameters, not atoms: no step cap of any kind.
78 ctrl.maxmove = -1.0;
79
80 rgmin_scg_params_t params{};
81 params.sigma0 = 1e-4;
82 params.lambda = settings.lambda;
83 params.lambda_limit = settings.lambda_limit;
84 params.tol_sol = settings.common.tolerance_sol;
85 params.tol_func = settings.common.tolerance_func;
86 params.conjugacy = RGMIN_CONJUGACY_LIU_STOREY;
87
88 rgmin_scg_options_t options{};
89 options.step_tolerance = settings.stationarity_step_stop
90 ? RGMIN_SCG_RELATIVE_EUCLIDEAN
91 : RGMIN_SCG_ABSOLUTE_INFINITY;
92
93 rgmin_report_t report{};
94 const rgmin_status_t st = rgmin_minimize_scg_with_options(
96 hessian_vector_curvature ? &rgminNlmlHessianVectorCurv<Ctx>
97 : ctx.exact_curvature ? &rgminCurv<Ctx>
98 : nullptr,
99 &ctx, xt.get(), &ctrl, &params, &options, &report);
100 completedIterations = static_cast<int>(report.steps);
101 if (settings.common.stop_requested && settings.common.stop_requested()) {
102 failedOptim = true;
103 return;
104 }
105 if (ctx.hessian_vector_fallback) {
107 if (settings.common.report_level >= 1) {
108 gpr::io::LogManager log_man;
109 log_man << " nlml_hessian_vector: the exact NLML Hessian-vector "
110 "product failed during the fit; rgmin's "
111 "finite_difference probe took the later curvatures\n";
112 }
113 }
114 if (ctx.failure.failed()) {
115 if (settings.common.report_level >= 1) ctx.failure.report();
116 failedOptim = true;
117 return;
118 }
119 w = Eigen::Map<Eigen::VectorXd>(wbuf.data(), w.size());
120
121 if (st != RGMIN_SUCCESS) {
122 failedOptim = true;
123 return;
124 }
125 if (ctrl.maxiter > 0 && report.steps >= ctrl.maxiter) {
126 failedOptim = true;
128 }
129}
@ x
Definition Enums.h:28
@ y
Definition Enums.h:28
bool exhaustedIterationBudget
Definition RgminScg.h:32
int completedIterations
Definition RgminScg.h:33
bool hessianVectorFallback
Definition RgminScg.h:36
MapObjectiveConfig makeMapObjectiveConfig(const gpr::EigenMatrix &x, const Eigen::VectorXd &y, const Eigen::VectorXd &w, double current_barrier_strength, const Eigen::VectorXd &lengthscale_rho0, const gpr::ScgOptimizationSettings &settings, bool robust_signal_anchor=true)
bool holderHasNlmlHessianVector(Holder &holder)
rgmin_status_t rgminNlmlHessianVectorCurv(void *user, const DLManagedTensorVersioned *x, const DLManagedTensorVersioned *d, double *curv_out) noexcept
rgmin_status_t rgminCurv(void *user, const DLManagedTensorVersioned *x, const DLManagedTensorVersioned *d, double *curv_out) noexcept
std::unique_ptr< DLManagedTensorVersioned, RgminTensorDeleter > RgminTensorHandle
rgmin_status_t rgminEval(void *user, const DLManagedTensorVersioned *x, double *value_out) noexcept
rgmin_status_t rgminGrad(void *user, const DLManagedTensorVersioned *x, DLManagedTensorVersioned *grad_out) noexcept
void requireCompatibleLibrary()
Definition RgminAbi.h:90

◆ setAlgorithmSettings()

void funcmin::RgminScg::setAlgorithmSettings ( const gpr::ScgOptimizationSettings & s)
inline

Definition at line 38 of file RgminScg.h.

39 {
40 settings = s;
41 }

Member Data Documentation

◆ completedIterations

int funcmin::RgminScg::completedIterations = 0

Definition at line 33 of file RgminScg.h.

◆ exhaustedIterationBudget

bool funcmin::RgminScg::exhaustedIterationBudget = false

Definition at line 32 of file RgminScg.h.

◆ failedOptim

bool funcmin::RgminScg::failedOptim = false

Definition at line 31 of file RgminScg.h.

◆ hessianVectorFallback

bool funcmin::RgminScg::hessianVectorFallback = false

True when nlml_hessian_vector continued with rgmin's finite-difference curvature probe.

Definition at line 36 of file RgminScg.h.


The documentation for this class was generated from the following files: