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

#include <gpr/optimization/SecantHessian.h>

Public Member Functions

 SecantHessian (SecantHessianMethod method=SecantHessianMethod::Bofill)
void reset ()
bool observe (const Eigen::VectorXd &position, const Eigen::VectorXd &gradient)
bool observe (const Eigen::VectorXd &position, const Eigen::VectorXd &gradient, double energy)
void noteEnergy (double energy)
bool hasReferenceEnergy () const noexcept
double referenceEnergy () const
bool available () const noexcept
std::size_t pairs () const noexcept
const Eigen::MatrixXd & hessian () const noexcept
const Eigen::VectorXd & referencePosition () const noexcept
const Eigen::VectorXd & referenceGradient () const noexcept
const Eigen::VectorXd & lastDirection () const noexcept
const Eigen::MatrixXd & measuredSteps () const noexcept
SecantConsistency consistency (const Eigen::VectorXd &posterior_action, double relative_tolerance) const
double meanEnergy (const Eigen::VectorXd &position, double reference_energy) const
Eigen::VectorXd meanGradient (const Eigen::VectorXd &position) const

Static Public Member Functions

static bool update (Eigen::MatrixXd &matrix, SecantHessianMethod method, const Eigen::VectorXd &step, const Eigen::VectorXd &gradient_change)

Detailed Description

Dense symmetric Hessian estimate carried by consecutive true gradients.

The first usable pair supplies the isotropic BFGS scale. SR1 and Bofill start from zero, which is already a valid symmetric prior. Failed updates are reported to the caller; methods are never substituted silently.

Definition at line 65 of file SecantHessian.h.

Constructor & Destructor Documentation

◆ SecantHessian()

gpr::optim::SecantHessian::SecantHessian ( SecantHessianMethod method = SecantHessianMethod::Bofill)
inlineexplicit

Definition at line 67 of file SecantHessian.h.

69 : method_(method)
70 {
71 }

Member Function Documentation

◆ available()

bool gpr::optim::SecantHessian::available ( ) const
inlinenodiscardnoexcept

Definition at line 194 of file SecantHessian.h.

195 {
196 return pairs_ > 0 && hessian_.rows() > 0;
197 }

◆ consistency()

SecantConsistency gpr::optim::SecantHessian::consistency ( const Eigen::VectorXd & posterior_action,
double relative_tolerance ) const
inlinenodiscard

Definition at line 226 of file SecantHessian.h.

229 {
230 SecantConsistency result;
231 if (!available() || last_step_.size() == 0 ||
232 posterior_action.size() != last_step_.size() ||
233 !posterior_action.allFinite() || !(relative_tolerance >= 0.0) ||
234 !std::isfinite(relative_tolerance))
235 return result;
236 const double length = last_step_.norm();
237 if (!(length > 0.0) || !std::isfinite(length)) return result;
238 result.direction = last_step_ / length;
239 const Eigen::VectorXd measured = last_gradient_change_ / length;
240 const double scale = std::max({measured.norm(), posterior_action.norm(),
241 std::numeric_limits<double>::min()});
242 result.relative_error = (posterior_action - measured).norm() / scale;
243 result.available = std::isfinite(result.relative_error);
244 result.trusted =
245 result.available && result.relative_error <= relative_tolerance;
246 return result;
247 }
bool available() const noexcept
Half measured(const GradientOracle &oracle, const Eigen::VectorXd &x, Kind kind, const Options &options)
Full central difference of the oracle gradient: 2 d calls.

◆ hasReferenceEnergy()

bool gpr::optim::SecantHessian::hasReferenceEnergy ( ) const
inlinenodiscardnoexcept

Definition at line 181 of file SecantHessian.h.

182 {
183 return has_reference_energy_;
184 }

◆ hessian()

const Eigen::MatrixXd & gpr::optim::SecantHessian::hessian ( ) const
inlinenodiscardnoexcept

Definition at line 202 of file SecantHessian.h.

203 {
204 return hessian_;
205 }

◆ lastDirection()

const Eigen::VectorXd & gpr::optim::SecantHessian::lastDirection ( ) const
inlinenodiscardnoexcept

Definition at line 214 of file SecantHessian.h.

215 {
216 return last_step_;
217 }

◆ meanEnergy()

double gpr::optim::SecantHessian::meanEnergy ( const Eigen::VectorXd & position,
double reference_energy ) const
inlinenodiscard

Definition at line 249 of file SecantHessian.h.

251 {
252 requireMeanPoint(position, reference_energy);
253 const Eigen::VectorXd displacement = position - previous_position_;
254 return reference_energy + previous_gradient_.dot(displacement) +
255 0.5 * displacement.dot(hessian_ * displacement);
256 }

◆ meanGradient()

Eigen::VectorXd gpr::optim::SecantHessian::meanGradient ( const Eigen::VectorXd & position) const
inlinenodiscard

Definition at line 258 of file SecantHessian.h.

260 {
261 requireMeanPoint(position, 0.0);
262 return previous_gradient_ + hessian_ * (position - previous_position_);
263 }

◆ measuredSteps()

const Eigen::MatrixXd & gpr::optim::SecantHessian::measuredSteps ( ) const
inlinenodiscardnoexcept

Orthonormal basis of all independent accepted step directions. Step lengths are removed before membership is tested, so the span is invariant to coordinate units and contains at most one column per degree of freedom.

Definition at line 222 of file SecantHessian.h.

223 {
224 return measured_steps_;
225 }

◆ noteEnergy()

void gpr::optim::SecantHessian::noteEnergy ( double energy)
inline

Energy at the latest observed geometry. The quadratic mean expands about that point.

Definition at line 171 of file SecantHessian.h.

172 {
173 if (!std::isfinite(energy)) {
174 has_reference_energy_ = false;
175 return;
176 }
177 reference_energy_ = energy;
178 has_reference_energy_ = true;
179 }

◆ observe() [1/2]

bool gpr::optim::SecantHessian::observe ( const Eigen::VectorXd & position,
const Eigen::VectorXd & gradient )
inlinenodiscard

Definition at line 86 of file SecantHessian.h.

88 {
89 if (position.size() == 0 || gradient.size() != position.size() ||
90 !position.allFinite() || !gradient.allFinite())
91 throw std::invalid_argument(
92 "secant observation requires finite matching position and "
93 "gradient");
94 if (previous_position_.size() == 0) {
95 previous_position_ = position;
96 previous_gradient_ = gradient;
97 hessian_ = Eigen::MatrixXd::Zero(position.size(), position.size());
98 return false;
99 }
100 if (position.size() != previous_position_.size())
101 throw std::invalid_argument("secant observation dimension changed");
102
103 const Eigen::VectorXd step = position - previous_position_;
104 const Eigen::VectorXd gradient_change = gradient - previous_gradient_;
105 if (!(step.squaredNorm() > 0.0) || !step.allFinite() ||
106 !gradient_change.allFinite())
107 return false;
108
109 Eigen::MatrixXd candidate = hessian_;
110 if (method_ == SecantHessianMethod::BFGS && pairs_ == 0) {
111 const double ss = step.squaredNorm();
112 const double ys = gradient_change.dot(step);
113 if (!bfgsCurvatureResolved(gradient_change, step, ys) ||
114 !std::isfinite(ss))
115 return false;
116 candidate.diagonal().setConstant(ys / ss);
117 }
118
119 // A repeated secant relation is an accepted no-op. Its resolution
120 // is relative to the two competing Hessian actions: a dimensionless
121 // absolute floor would classify small, exactly represented pairs by
122 // their units. Do not bypass BFGS's positive-curvature condition.
123 const Eigen::VectorXd action = candidate * step;
124 const Eigen::VectorXd residual = gradient_change - action;
125 const double action_scale =
126 std::max(gradient_change.norm(), action.norm());
127 const double resolution =
128 std::sqrt(std::numeric_limits<double>::epsilon()) * action_scale;
129 const double ys = gradient_change.dot(step);
130 if (pairs_ > 0 && residual.norm() <= resolution &&
131 (method_ != SecantHessianMethod::BFGS ||
132 (ys > 0.0 && std::isfinite(ys)))) {
133 previous_position_ = position;
134 previous_gradient_ = gradient;
135 last_step_ = step;
136 last_gradient_change_ = gradient_change;
137 appendMeasuredStep(step);
138 ++pairs_;
139 return true;
140 }
141
142 const bool updated = update(candidate, method_, step, gradient_change);
143 if (!updated || !candidate.allFinite()) return false;
144 hessian_ = (0.5 * (candidate + candidate.transpose())).eval();
145 previous_position_ = position;
146 previous_gradient_ = gradient;
147 last_step_ = step;
148 last_gradient_change_ = gradient_change;
149 appendMeasuredStep(step);
150 ++pairs_;
151 return true;
152 }
static bool update(Eigen::MatrixXd &matrix, SecantHessianMethod method, const Eigen::VectorXd &step, const Eigen::VectorXd &gradient_change)

◆ observe() [2/2]

bool gpr::optim::SecantHessian::observe ( const Eigen::VectorXd & position,
const Eigen::VectorXd & gradient,
double energy )
inlinenodiscard

Observe a geometry together with its energy. The quadratic mean expands about the reference geometry, so the energy is kept only when this geometry becomes that reference: the first geometry, or an accepted pair. A refused pair leaves both the reference and its energy alone, and an accepted geometry whose energy is not finite leaves no reference energy rather than the previous geometry's.

Definition at line 160 of file SecantHessian.h.

162 {
163 const bool first = previous_position_.size() == 0;
164 const bool accepted = observe(position, gradient);
165 if (accepted || first) noteEnergy(energy);
166 return accepted;
167 }
void noteEnergy(double energy)
bool observe(const Eigen::VectorXd &position, const Eigen::VectorXd &gradient)

◆ pairs()

std::size_t gpr::optim::SecantHessian::pairs ( ) const
inlinenodiscardnoexcept

Definition at line 198 of file SecantHessian.h.

199 {
200 return pairs_;
201 }

◆ referenceEnergy()

double gpr::optim::SecantHessian::referenceEnergy ( ) const
inlinenodiscard

Definition at line 186 of file SecantHessian.h.

187 {
188 if (!has_reference_energy_)
189 throw std::logic_error(
190 "quadratic secant mean requires a recorded reference energy");
191 return reference_energy_;
192 }

◆ referenceGradient()

const Eigen::VectorXd & gpr::optim::SecantHessian::referenceGradient ( ) const
inlinenodiscardnoexcept

Definition at line 210 of file SecantHessian.h.

211 {
212 return previous_gradient_;
213 }

◆ referencePosition()

const Eigen::VectorXd & gpr::optim::SecantHessian::referencePosition ( ) const
inlinenodiscardnoexcept

Definition at line 206 of file SecantHessian.h.

207 {
208 return previous_position_;
209 }

◆ reset()

void gpr::optim::SecantHessian::reset ( )
inline

Definition at line 73 of file SecantHessian.h.

74 {
75 hessian_.resize(0, 0);
76 previous_position_.resize(0);
77 previous_gradient_.resize(0);
78 last_step_.resize(0);
79 last_gradient_change_.resize(0);
80 measured_steps_.resize(0, 0);
81 reference_energy_ = 0.0;
82 has_reference_energy_ = false;
83 pairs_ = 0;
84 }

◆ update()

bool gpr::optim::SecantHessian::update ( Eigen::MatrixXd & matrix,
SecantHessianMethod method,
const Eigen::VectorXd & step,
const Eigen::VectorXd & gradient_change )
inlinestatic

Apply one named direct-Hessian update to caller-owned state. False means that method's own denominator/curvature condition failed.

Definition at line 267 of file SecantHessian.h.

270 {
271 if (matrix.rows() == 0 || matrix.rows() != matrix.cols() ||
272 step.size() != matrix.rows() ||
273 gradient_change.size() != matrix.rows() || !matrix.allFinite() ||
274 !step.allFinite() || !gradient_change.allFinite())
275 return false;
276 switch (method) {
278 return updateBfgs(matrix, step, gradient_change);
280 return updateSr1(matrix, step, gradient_change);
282 return updateBofill(matrix, step, gradient_change);
283 }
284 return false;
285 }

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