Loading...
Searching...
No Matches
AnnealStage.h
Go to the documentation of this file.
1
19#ifndef GPR_ML_ANNEAL_STAGE_H_
20#define GPR_ML_ANNEAL_STAGE_H_
21
22#ifdef USE_ANNEAL
23
24#include <anneal-core.h>
25#include <rgmin.h>
26
27#include <Eigen/Dense>
28#include <cmath>
29#include <cstdint>
30#include <cstdio>
31#include <limits>
32#include <vector>
33
34#include "MapObjective.h"
35#include "RgminAbi.h"
36#include "RgminObjective.h"
37
38namespace funcmin {
39
40struct AnnealStageSettings {
42 std::size_t budget = 0;
44 std::size_t replicas = 4;
46 std::uint64_t seed = 0;
49 double box_halfwidth = std::log(8.0);
52 double coverage_height = 0.1;
53 double coverage_radius = 0.05;
54};
55
56struct AnnealStageReport {
57 bool ran = false;
58 bool failed = false;
59 double start_value = std::numeric_limits<double>::quiet_NaN();
60 double value = std::numeric_limits<double>::quiet_NaN();
61 std::size_t evaluations = 0;
62 std::size_t gradients = 0;
63 std::size_t hops = 0;
65 bool improved = false;
66};
67
70static_assert(static_cast<int>(RGMIN_SUCCESS) ==
71 static_cast<int>(anneal::ANNEAL_SUCCESS) &&
72 static_cast<int>(RGMIN_INVALID_PARAMETER) ==
73 static_cast<int>(anneal::ANNEAL_INVALID_PARAMETER) &&
74 static_cast<int>(RGMIN_INTERNAL_ERROR) ==
75 static_cast<int>(anneal::ANNEAL_INTERNAL_ERROR),
76 "rgmin and anneal status values must coincide");
77template <typename Ctx>
78anneal::anneal_status_t annealEval(void* user,
79 const DLManagedTensorVersioned* x,
80 double* value_out) noexcept
81{
82 return static_cast<anneal::anneal_status_t>(
83 static_cast<int>(rgminEval<Ctx>(user, x, value_out)));
84}
85
86template <typename Ctx>
87anneal::anneal_status_t annealGrad(void* user,
88 const DLManagedTensorVersioned* x,
89 DLManagedTensorVersioned* grad_out) noexcept
90{
91 return static_cast<anneal::anneal_status_t>(
92 static_cast<int>(rgminGrad<Ctx>(user, x, grad_out)));
93}
94
98template <typename ClassName, typename FuncName>
99AnnealStageReport annealMapStage(
100 const gpr::EigenMatrix& x, const Eigen::VectorXd& x_ind,
101 const Eigen::VectorXd& y, Eigen::VectorXd& w, ClassName& holder,
102 FuncName func, const MapObjectiveConfig& map_cfg,
103 const Eigen::VectorXd& box_lo, const Eigen::VectorXd& box_hi,
104 const AnnealStageSettings& settings, int report_level)
105{
106 AnnealStageReport report;
107 const Eigen::Index n = w.size();
108 if (settings.budget == 0 || n == 0 || !w.allFinite()) return report;
109 Eigen::VectorXd lower = w.array() - settings.box_halfwidth;
110 Eigen::VectorXd upper = w.array() + settings.box_halfwidth;
111 const auto intersect = [&](const Eigen::VectorXd& side, bool is_lower) {
112 if (side.size() == 0) return true;
113 if (side.size() != n || side.array().isNaN().any()) return false;
114 if (is_lower)
115 lower = lower.cwiseMax(side);
116 else
117 upper = upper.cwiseMin(side);
118 return true;
119 };
120 if (!intersect(box_lo, true) || !intersect(box_hi, false) ||
121 !intersect(map_cfg.parameter_lower, true) ||
122 !intersect(map_cfg.parameter_upper, false)) {
123 report.failed = true;
124 return report;
125 }
126 for (Eigen::Index i = 0; i < n; ++i) {
127 // The start lies inside its box; a pinned coordinate stays put.
128 lower(i) = std::min(lower(i), w(i));
129 upper(i) = std::max(upper(i), w(i));
130 if (!std::isfinite(lower(i)) || !std::isfinite(upper(i))) {
131 report.failed = true;
132 return report;
133 }
134 }
135 // The amplitude ceiling holds over the box even when the start
136 // stands above it; the start is then clipped to it.
137 if (map_cfg.use_magn_ceiling && n > 0) {
138 upper(0) = std::min(upper(0), map_cfg.max_log_magnSigma2);
139 lower(0) = std::min(lower(0), upper(0));
140 w(0) = std::min(w(0), upper(0));
141 }
143 Ctx ctx(x, x_ind, y, holder, func, map_cfg, false);
144 ctx.trace_evaluations = report_level >= 3;
145 std::vector<double> wbuf(static_cast<std::size_t>(n));
146 Eigen::Map<Eigen::VectorXd>(wbuf.data(), n) = w;
148 const RgminTensorHandle xt(
149 rgmin_tensor_borrow_cpu_f64(wbuf.data(), wbuf.size()));
150 if (!xt) {
151 report.failed = true;
152 return report;
153 }
154 double start_value = std::numeric_limits<double>::infinity();
155 if (annealEval<Ctx>(&ctx, xt.get(), &start_value) !=
156 anneal::ANNEAL_SUCCESS) {
157 report.failed = true;
158 return report;
159 }
160 report.start_value = start_value;
161 anneal::anneal_box_config_t config{};
162 if (anneal::anneal_box_config_default(&config) != anneal::ANNEAL_SUCCESS) {
163 report.failed = true;
164 return report;
165 }
166 config.replicas = settings.replicas;
167 config.budget = settings.budget;
168 config.seed = settings.seed;
169 config.coverage_height = settings.coverage_height;
170 config.coverage_radius = settings.coverage_radius;
171 anneal::anneal_box_report_t out{};
172 const anneal::anneal_status_t status = anneal::anneal_box_minimize(
173 &annealEval<Ctx>, &annealGrad<Ctx>, &ctx, xt.get(), lower.data(),
174 upper.data(), &config, &out);
175 report.ran = true;
176 if (status != anneal::ANNEAL_SUCCESS || ctx.failure.failed()) {
177 report.failed = true;
178 if (report_level >= 1) {
179 const char* message = anneal::anneal_last_error();
180 std::fprintf(stderr, "Anneal stage failed: %s\n",
181 message != nullptr ? message : "unknown error");
182 }
183 return report;
184 }
185 report.value = out.value;
186 report.evaluations = out.n_evals;
187 report.gradients = out.n_grads;
188 report.hops = out.hops;
189 const Eigen::Map<const Eigen::VectorXd> best(wbuf.data(), n);
190 report.improved = std::isfinite(out.value) && best.allFinite() &&
191 out.value < start_value &&
192 mapParametersInDomain(best, map_cfg);
193 if (report.improved) w = best;
194 return report;
195}
196
197} // namespace funcmin
198
199#endif // USE_ANNEAL
200#endif // GPR_ML_ANNEAL_STAGE_H_
@ x
Definition Enums.h:28
@ y
Definition Enums.h:28
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
int status
eon_relax_band_t eon_relax_surface_fn void * user
std::unique_ptr< DLManagedTensorVersioned, RgminTensorDeleter > RgminTensorHandle
rgmin_status_t rgminEval(void *user, const DLManagedTensorVersioned *x, double *value_out) noexcept
bool mapParametersInDomain(const Eigen::VectorXd &w, const MapObjectiveConfig &cfg)
rgmin_status_t rgminGrad(void *user, const DLManagedTensorVersioned *x, DLManagedTensorVersioned *grad_out) noexcept
void requireCompatibleLibrary()
Definition RgminAbi.h:90
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20