Loading...
Searching...
No Matches
TiedLengthScaleObjective.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 GPR_ML_GP_TIEDLENGTHSCALEOBJECTIVE_H_
5#define GPR_ML_GP_TIEDLENGTHSCALEOBJECTIVE_H_
6
7#include <Eigen/Core>
8
10
11namespace gpr {
12namespace detail {
13
21template <class Inner>
22struct TiedLengthScaleObjective {
23 Inner& inner;
24 GaussianProcessRegression& gp;
25
26private:
27 struct GradientScope {
28 GaussianProcessRegression& gp;
29 bool previous;
30 explicit GradientScope(GaussianProcessRegression& model)
31 : gp(model), previous(model.tied_length_scale_gradient_)
32 {
33 gp.tied_length_scale_gradient_ = true;
34 }
35 ~GradientScope()
36 {
37 gp.tied_length_scale_gradient_ = previous;
38 }
39 GradientScope(const GradientScope&) = delete;
40 GradientScope& operator=(const GradientScope&) = delete;
41 };
42
43public:
44 [[nodiscard]] Eigen::VectorXd tied(const Eigen::VectorXd& w) const
45 {
46 Eigen::VectorXd point = w;
47 gp.tieLengthScaleParameters(point);
48 return point;
49 }
50
51 void evaluateEnergyAndGradient(const Eigen::VectorXd& w,
52 const EigenMatrix& x,
53 const Eigen::VectorXd& x_ind,
54 const Eigen::VectorXd& y,
55 EnergyAndGradient& energy_and_gradient)
56 {
57 const GradientScope scope(gp);
58 inner.evaluateEnergyAndGradient(tied(w), x, x_ind, y,
59 energy_and_gradient);
60 gp.reduceTiedLengthScaleGradient(energy_and_gradient.gradient);
61 }
62
63 double evaluateEnergy(const Eigen::VectorXd& w, const EigenMatrix& x,
64 const Eigen::VectorXd& x_ind,
65 const Eigen::VectorXd& y)
66 {
67 if constexpr (requires(Inner& o) {
68 o.evaluateEnergy(w, x, x_ind, y);
69 }) {
70 return inner.evaluateEnergy(tied(w), x, x_ind, y);
71 } else {
72 double energy = 0.0;
73 Eigen::VectorXd gradient;
74 EnergyAndGradient energy_and_gradient(energy, gradient);
75 const GradientScope scope(gp);
76 inner.evaluateEnergyAndGradient(tied(w), x, x_ind, y,
77 energy_and_gradient);
78 return energy;
79 }
80 }
81
82 bool evaluateGradientAfterEnergy(const Eigen::VectorXd& w,
83 const EigenMatrix& x,
84 const Eigen::VectorXd& x_ind,
85 const Eigen::VectorXd& y,
86 Eigen::VectorXd& gradient)
87 {
88 if constexpr (requires(Inner& o) {
89 o.evaluateGradientAfterEnergy(w, x, x_ind, y,
90 gradient);
91 }) {
92 const GradientScope scope(gp);
93 const bool ok = inner.evaluateGradientAfterEnergy(tied(w), x, x_ind,
94 y, gradient);
95 if (ok) gp.reduceTiedLengthScaleGradient(gradient);
96 return ok;
97 } else {
98 return false;
99 }
100 }
101
102 bool evaluateDirectionalCurvature(const Eigen::VectorXd& x_ind,
103 const EigenMatrix& x,
104 const Eigen::VectorXd& y,
105 const Eigen::VectorXd& direction,
106 double& curvature)
107 {
108 if constexpr (requires(Inner& o) {
109 o.evaluateDirectionalCurvature(x_ind, x, y, direction,
110 curvature);
111 }) {
112 // A step of the leader moves every member with it.
113 return inner.evaluateDirectionalCurvature(
114 x_ind, x, y, tied(direction), curvature);
115 } else {
116 return false;
117 }
118 }
119
120 [[nodiscard]] bool exactNlmlHessianVectorAvailable() const
121 {
122 if constexpr (requires(const Inner& o) {
123 o.exactNlmlHessianVectorAvailable();
124 }) {
125 return inner.exactNlmlHessianVectorAvailable();
126 } else {
127 return false;
128 }
129 }
130
131 bool evaluateNlmlHessianVector(const Eigen::VectorXd& x_ind,
132 const EigenMatrix& x,
133 const Eigen::VectorXd& y,
134 const Eigen::VectorXd& direction,
135 Eigen::VectorXd& product)
136 {
137 if constexpr (requires(Inner& o) {
138 o.evaluateNlmlHessianVector(x_ind, x, y, direction,
139 product);
140 }) {
141 // A leader step moves every member. The reduced product is
142 // J^T (H J v), which is what the tied gradient's derivative is.
143 const bool ok = inner.evaluateNlmlHessianVector(
144 x_ind, x, y, tied(direction), product);
145 if (!ok) return false;
146 gp.reduceTiedLengthScaleGradient(product);
147 return product.size() == direction.size() && product.allFinite();
148 } else {
149 return false;
150 }
151 }
152};
153
154} // namespace detail
155} // namespace gpr
156
157#endif // GPR_ML_GP_TIEDLENGTHSCALEOBJECTIVE_H_
@ x
Definition Enums.h:28
@ y
Definition Enums.h:28
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20