Loading...
Searching...
No Matches
SpectralTrustRegion.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#ifndef GPRIDGEIZATION_SPECTRAL_TRUST_REGION_H_
5#define GPRIDGEIZATION_SPECTRAL_TRUST_REGION_H_
6
7#include <Eigen/Dense>
8#include <cmath>
9#include <limits>
10#include <utility>
11#include <vector>
12
13namespace gpr {
14namespace optim {
15
19 bool valid = false;
20 Eigen::VectorXd step;
21 double sigma = 0.0;
22 double length = 0.0;
24 Eigen::Index flat = 0;
27 double flat_residual_sq = 0.0;
28 double kept_residual_sq = 0.0;
30 Eigen::Index request = -1;
32 bool descends_measured = false;
33};
34
46 const Eigen::VectorXd& curvatures, const Eigen::MatrixXd& modes,
47 Eigen::Index mode_index, const Eigen::VectorXd& force, double radius,
48 const std::vector<std::pair<Eigen::VectorXd, double>>& measured_negative)
49{
51 const Eigen::Index m = curvatures.size();
52 if (m == 0 || modes.cols() != m || modes.rows() != force.size() ||
53 mode_index < 0 || mode_index >= m || !(radius > 0.0) ||
54 !std::isfinite(radius) || !force.allFinite() ||
55 !(curvatures(mode_index) < 0.0))
56 return out;
57 Eigen::VectorXd mu = curvatures;
58 mu(mode_index) = -mu(mode_index);
59 Eigen::VectorXd c = modes.transpose() * force;
60 c(mode_index) = -c(mode_index);
61 if (!c.allFinite()) return out;
62 double requested_residual = -1.0;
63 for (Eigen::Index k = 0; k < m; ++k) {
64 if (k == mode_index || mu(k) > 0.0) {
65 out.kept_residual_sq += c(k) * c(k);
66 continue;
67 }
68 bool measured = false;
69 for (const auto& [direction, measured_curvature]: measured_negative) {
70 if (direction.size() != modes.rows()) continue;
71 const double overlap = direction.dot(modes.col(k));
72 if (overlap * overlap > 0.5 * direction.squaredNorm()) {
73 mu(k) = measured_curvature;
74 measured = true;
75 break;
76 }
77 }
78 if (measured) {
79 out.kept_residual_sq += c(k) * c(k);
80 if (c(k) != 0.0) out.descends_measured = true;
81 continue;
82 }
83 out.flat_residual_sq += c(k) * c(k);
84 if (std::abs(c(k)) > requested_residual) {
85 out.request = k;
86 requested_residual = std::abs(c(k));
87 }
88 c(k) = 0.0;
89 mu(k) = 0.0;
90 ++out.flat;
91 }
92 Eigen::Index minimum_mode;
93 const double mu_min = mu.minCoeff(&minimum_mode);
94 const double sigma_floor = std::max(0.0, -mu_min);
95 auto step_norm = [&](double sigma) {
96 double sum = 0.0;
97 for (Eigen::Index k = 0; k < m; ++k) {
98 const double d = mu(k) + sigma;
99 if (!(d > 0.0)) {
100 if (c(k) != 0.0) return std::numeric_limits<double>::infinity();
101 continue;
102 }
103 sum += (c(k) / d) * (c(k) / d);
104 }
105 return std::sqrt(sum);
106 };
107 double sigma = sigma_floor;
108 double length = step_norm(sigma);
109 if (!(length <= radius)) {
110 // Newton on 1/|s(sigma)| - 1/radius, monotone and nearly linear
111 // in sigma; bisection safeguards the bracket.
112 double lower = sigma_floor;
113 double upper = sigma_floor + c.norm() / radius;
114 while (step_norm(upper) > radius)
115 upper = 2.0 * upper + c.norm() / radius;
116 sigma = upper;
117 for (int iteration = 0; iteration < 200; ++iteration) {
118 const double s_norm = step_norm(sigma);
119 if (std::isfinite(s_norm) && s_norm > 0.0) {
120 double derivative = 0.0;
121 for (Eigen::Index k = 0; k < m; ++k) {
122 const double d = mu(k) + sigma;
123 if (d > 0.0) derivative += (c(k) * c(k)) / (d * d * d);
124 }
125 derivative /= (s_norm * s_norm * s_norm);
126 const double phi = 1.0 / s_norm - 1.0 / radius;
127 if (std::abs(phi) * radius <= 1.0e-10) break;
128 if (s_norm > radius)
129 lower = sigma;
130 else
131 upper = sigma;
132 double next = sigma - phi / derivative;
133 if (!(next > lower) || !(next < upper) || !std::isfinite(next))
134 next = 0.5 * (lower + upper);
135 sigma = next;
136 } else {
137 lower = sigma;
138 sigma = 0.5 * (lower + upper);
139 }
140 if (upper - lower <= 1.0e-14 * std::max(1.0, upper)) break;
141 }
142 length = step_norm(sigma);
143 }
144 Eigen::VectorXd step = Eigen::VectorXd::Zero(modes.rows());
145 for (Eigen::Index k = 0; k < m; ++k) {
146 const double d = mu(k) + sigma;
147 if (d > 0.0) step += (c(k) / d) * modes.col(k);
148 }
149 if (sigma_floor > 0.0 && sigma == sigma_floor && length < radius) {
150 // In the hard case the force is orthogonal to the most negative
151 // eigenspace. The shifted solution needs its nullspace component
152 // to reach the boundary and minimize the indefinite quadratic.
153 const double relative_length = length / radius;
154 const double remaining =
155 radius * std::sqrt(std::max(0.0, (1.0 - relative_length) *
156 (1.0 + relative_length)));
157 step += remaining * modes.col(minimum_mode);
158 out.descends_measured = true;
159 }
160 if (!step.allFinite() || !(step.norm() > 0.0)) return out;
161 out.valid = true;
162 out.step = step;
163 out.sigma = sigma;
164 out.length = step.norm();
165 return out;
166}
167
168} // namespace optim
169} // namespace gpr
170
171#endif // GPRIDGEIZATION_SPECTRAL_TRUST_REGION_H_
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
SpectralTrustRegionStep spectralTrustRegionStep(const Eigen::VectorXd &curvatures, const Eigen::MatrixXd &modes, Eigen::Index mode_index, const Eigen::VectorXd &force, double radius, const std::vector< std::pair< Eigen::VectorXd, double > > &measured_negative)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
bool descends_measured
The step descends along a measured negative transverse direction.
Eigen::Index flat
Transverse modes with nonpositive curvature the step left out.
Eigen::Index request
The left-out mode carrying the most residual, or -1.