Loading...
Searching...
No Matches
gpr::optim::RgminStep Class Reference

#include <gpr/optimization/RgminStep.h>

Classes

struct  PairRecord

Public Types

enum class  Objective : int { Unspecified = 0 , Surrogate = 1 , Exact = 2 }

Public Member Functions

 RgminStep ()=default
 ~RgminStep ()
 RgminStep (const RgminStep &)=delete
RgminStep & operator= (const RgminStep &)=delete
void forget ()
bool ready () const noexcept
std::size_t pairCount () const noexcept
 Curvature pairs the session currently holds.
bool step (Eigen::VectorXd &x, const Eigen::VectorXd &gradient, double max_move, double empty_scale, Objective objective=Objective::Unspecified)

Static Public Member Functions

static std::vector< PairRecord > *& pairTrace () noexcept

Detailed Description

One bounded quasi-Newton step from a persistent rgmin session.

The removed in-tree LBFGS returned a direction from its own two-loop and the caller applied it as the step, rescaling afterwards only when the result was longer than max_step_length. Nothing bounded a step that was shorter than that but wrong, and a non-finite direction reached the geometry before anything checked it.

The caller owns the curvature pairs. A dimer supplies one gradient per outer iteration, and a stepping entry that evaluates a trial point is handed that same frozen gradient, so it records y = 0 and the two-loop degenerates. push_pair takes the pair the caller already knows from its own previous outer, and search_direction returns d = -H g without evaluating anything.

Definition at line 41 of file RgminStep.h.

Member Enumeration Documentation

◆ Objective

enum class gpr::optim::RgminStep::Objective : int
strong

The function whose gradient a step is taken from. A curvature pair (s, y) is a secant of one function, so a pair whose two gradients come from different objectives, such as the surrogate and an exact oracle evaluation, describes neither. The history is dropped instead. Unspecified is its own objective: callers that never name one keep a single history.

Enumerator
Unspecified 
Surrogate 
Exact 

Definition at line 49 of file RgminStep.h.

Constructor & Destructor Documentation

◆ RgminStep() [1/2]

gpr::optim::RgminStep::RgminStep ( )
default

◆ ~RgminStep()

gpr::optim::RgminStep::~RgminStep ( )
inline

Definition at line 67 of file RgminStep.h.

68 {
69 release();
70 }

◆ RgminStep() [2/2]

gpr::optim::RgminStep::RgminStep ( const RgminStep & )
delete

Member Function Documentation

◆ forget()

void gpr::optim::RgminStep::forget ( )
inline

Drop the curvature history; the next step starts from steepest descent.

Definition at line 76 of file RgminStep.h.

77 {
78 if (solver_ != nullptr) rgmin_solver_forget(solver_);
79 resetHistory();
80 }

◆ operator=()

RgminStep & gpr::optim::RgminStep::operator= ( const RgminStep & )
delete

◆ pairCount()

std::size_t gpr::optim::RgminStep::pairCount ( ) const
inlinenodiscardnoexcept

Curvature pairs the session currently holds.

Definition at line 88 of file RgminStep.h.

89 {
90 return solver_ != nullptr ? rgmin_solver_pair_count(solver_) : 0;
91 }

◆ pairTrace()

std::vector< PairRecord > *& gpr::optim::RgminStep::pairTrace ( )
inlinestaticnoexcept

Receives a PairRecord for every pair pushed on this thread while it is set. For tests; null in production.

Definition at line 60 of file RgminStep.h.

61 {
62 thread_local std::vector<PairRecord>* trace = nullptr;
63 return trace;
64 }

◆ ready()

bool gpr::optim::RgminStep::ready ( ) const
inlinenodiscardnoexcept

Definition at line 82 of file RgminStep.h.

83 {
84 return solver_ != nullptr;
85 }

◆ step()

bool gpr::optim::RgminStep::step ( Eigen::VectorXd & x,
const Eigen::VectorXd & gradient,
double max_move,
double empty_scale,
Objective objective = Objective::Unspecified )
inline

x is the geometry, read and written in place: an L-BFGS history is built from successive iterates, so restarting x at a fixed point would pair curvature across unrelated geometries.

max_move bounds the norm of the displacement, matching what the dimer's max_step_length means; a per-atom bound lets the total exceed it. Non-positive leaves it unbounded.

Returns false and leaves x untouched when the session refuses the step, so the caller can fall back rather than propagate a non-finite geometry. One bounded quasi-Newton step from the gradient at x.

x is the geometry, read and written in place. empty_scale scales the first step taken on empty memory, where the two-loop is plain steepest descent: an unscaled -g overshoots the move bound on every outer and the search cycles instead of settling.

Returns false and leaves x untouched when the direction is unusable, so the caller falls back rather than propagating a non-finite geometry. A refusal after this call pushed a pair forgets the whole session: rgmin cannot pop a single pair, so every stored pair and the previous-point anchor are cleared and the next step starts from empty memory.

objective names the function gradient belongs to. When it differs from the objective of the previous step the curvature history is dropped and no pair is formed, so the history only ever holds pairs whose two gradients are of one objective.

Definition at line 122 of file RgminStep.h.

125 {
126 if (x.size() <= 0 || gradient.size() != x.size()) return false;
127 if (!x.allFinite() || !gradient.allFinite()) return false;
128 ensure(static_cast<std::size_t>(x.size()));
129 if (solver_ == nullptr) return false;
130 const std::size_t n = static_cast<std::size_t>(x.size());
131
132 if (have_previous_ && previous_objective_ != objective) forget();
133
134 // The pair from the caller's own previous outer. Recording it
135 // here rather than letting the session evaluate for it is what
136 // keeps a frozen gradient from writing y = 0. A step that is
137 // not applied forgets the whole session: every stored pair is
138 // cleared, not only the pair just pushed, and the previous-point
139 // anchor is cleared with them. Otherwise a retry pushes the
140 // same (s, y) and two-loops a step the caller did not take.
141 const bool pushed_pair = have_previous_;
142 if (pushed_pair) {
143 const Eigen::VectorXd s_vec = x - previous_x_;
144 const Eigen::VectorXd y_vec = gradient - previous_g_;
145 rgmin_solver_push_pair(solver_, s_vec.data(), y_vec.data(), n);
146 if (auto* trace = pairTrace()) {
147 trace->push_back({previous_objective_, objective});
148 }
149 }
150
151 const auto refuse = [&]() {
152 if (pushed_pair) forget();
153 return false;
154 };
155
156 Eigen::VectorXd direction(x.size());
157 const rgmin_status_t st = rgmin_solver_search_direction(
158 solver_, gradient.data(), direction.data(), n);
159 if (st != RGMIN_SUCCESS || !direction.allFinite()) return refuse();
160
161 if (rgmin_solver_pair_count(solver_) == 0) direction *= empty_scale;
162
163 // norm() squares each entry, so a finite component near 1e308
164 // has an infinite length. The length itself is finite; the sum
165 // with x is what can overflow.
166 const double length = direction.stableNorm();
167 if (!std::isfinite(length) || !(length > 0.0)) return refuse();
168 if (max_move > 0.0 && length > max_move) {
169 direction *= max_move / length;
170 }
171
172 // Finite addends can still overflow. Refuse before x or the
173 // anchor is written, so the caller keeps the point it offered.
174 const Eigen::VectorXd stepped = x + direction;
175 if (!stepped.allFinite()) return refuse();
176
177 previous_x_ = x;
178 previous_g_ = gradient;
179 previous_objective_ = objective;
180 have_previous_ = true;
181 x = stepped;
182 return true;
183 }
@ x
Definition Enums.h:28
static std::vector< PairRecord > *& pairTrace() noexcept
Definition RgminStep.h:60

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