Loading...
Searching...
No Matches
OuterIterCompositionTrace.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
6
7#include <cctype>
8#include <cmath>
9#include <fstream>
10#include <iomanip>
11#include <sstream>
12#include <stdexcept>
13
14namespace gpr {
15namespace neb {
16namespace {
17
18void writeBool(std::ostream& os, const char* key, bool v)
19{
20 os << key << ' ' << (v ? 1 : 0) << '\n';
21}
22
23void writeInt(std::ostream& os, const char* key, int v)
24{
25 os << key << ' ' << v << '\n';
26}
27
28void writeDouble(std::ostream& os, const char* key, double v)
29{
30 // 17 significant digits: enough for round-trip of IEEE-754 binary64 on
31 // the same platform; no locale dependency (classic "C" stream).
32 os << key << ' ' << std::scientific << std::setprecision(17) << v << '\n';
33 os << std::defaultfloat;
34}
35
36bool expectKey(std::istream& is, const std::string& want, std::string* error)
37{
38 std::string key;
39 if (!(is >> key)) {
40 if (error) *error = "unexpected EOF looking for key '" + want + "'";
41 return false;
42 }
43 if (key != want) {
44 if (error) *error = "expected key '" + want + "', got '" + key + "'";
45 return false;
46 }
47 return true;
48}
49
50bool readBool(std::istream& is, const std::string& key, bool& out,
51 std::string* error)
52{
53 if (!expectKey(is, key, error)) return false;
54 int v = 0;
55 if (!(is >> v) || (v != 0 && v != 1)) {
56 if (error) *error = "bad bool for " + key;
57 return false;
58 }
59 out = (v != 0);
60 return true;
61}
62
63bool readInt(std::istream& is, const std::string& key, int& out,
64 std::string* error)
65{
66 if (!expectKey(is, key, error)) return false;
67 if (!(is >> out)) {
68 if (error) *error = "bad int for " + key;
69 return false;
70 }
71 return true;
72}
73
74bool readDouble(std::istream& is, const std::string& key, double& out,
75 std::string* error)
76{
77 if (!expectKey(is, key, error)) return false;
78 if (!(is >> out)) {
79 if (error) *error = "bad double for " + key;
80 return false;
81 }
82 return true;
83}
84
85void writeRecord(std::ostream& os, const OuterIterCompositionTraceRecord& rec)
86{
87 os << "---\n";
88 writeInt(os, "outer_idx", rec.outer_idx);
89 writeInt(os, "oracle_target_image", rec.oracle_target_image);
90 writeDouble(os, "barrier_estimate", rec.barrier_estimate);
91 writeInt(os, "ci_index", rec.ci_index);
92 writeDouble(os, "max_oracle_force", rec.max_oracle_force);
93 writeInt(os, "n_uneval", rec.n_uneval);
94 writeBool(os, "ci_active", rec.ci_active);
95 writeBool(os, "cert.n_uneval_zero", rec.certificates.n_uneval_zero);
96 writeBool(os, "cert.mep_force_ok", rec.certificates.mep_force_ok);
97 writeBool(os, "cert.ci_force_ok", rec.certificates.ci_force_ok);
98 writeBool(os, "cert.true_saddle_energy",
99 rec.certificates.true_saddle_energy);
100 writeBool(os, "cert.ci_adjacent_perp", rec.certificates.ci_adjacent_perp);
101 writeBool(os, "cert.force_tolerance", rec.certificates.force_tolerance);
102 writeBool(os, "cert.certified_ci_only_branch",
103 rec.certificates.certified_ci_only_branch);
104 writeBool(os, "cert.certified_full_band_branch",
105 rec.certificates.certified_full_band_branch);
106 writeBool(os, "cert.certified_hook_branch",
107 rec.certificates.certified_hook_branch);
108 writeInt(os, "theta_n", static_cast<int>(rec.theta.size()));
109 for (std::size_t i = 0; i < rec.theta.size(); ++i) {
110 std::ostringstream key;
111 key << "theta_" << i;
112 writeDouble(os, key.str().c_str(), rec.theta[i]);
113 }
114}
115
116bool readRecord(std::istream& is, OuterIterCompositionTraceRecord& rec,
117 std::string* error)
118{
119 std::string sep;
120 if (!(is >> sep)) {
121 if (error) *error = "missing record separator";
122 return false;
123 }
124 if (sep != "---") {
125 if (error) *error = "expected '---', got '" + sep + "'";
126 return false;
127 }
128 if (!readInt(is, "outer_idx", rec.outer_idx, error)) return false;
129 if (!readInt(is, "oracle_target_image", rec.oracle_target_image, error))
130 return false;
131 if (!readDouble(is, "barrier_estimate", rec.barrier_estimate, error))
132 return false;
133 if (!readInt(is, "ci_index", rec.ci_index, error)) return false;
134 if (!readDouble(is, "max_oracle_force", rec.max_oracle_force, error))
135 return false;
136 if (!readInt(is, "n_uneval", rec.n_uneval, error)) return false;
137 if (!readBool(is, "ci_active", rec.ci_active, error)) return false;
138 if (!readBool(is, "cert.n_uneval_zero", rec.certificates.n_uneval_zero,
139 error))
140 return false;
141 if (!readBool(is, "cert.mep_force_ok", rec.certificates.mep_force_ok,
142 error))
143 return false;
144 if (!readBool(is, "cert.ci_force_ok", rec.certificates.ci_force_ok, error))
145 return false;
146 if (!readBool(is, "cert.true_saddle_energy",
147 rec.certificates.true_saddle_energy, error))
148 return false;
149 if (!readBool(is, "cert.ci_adjacent_perp",
150 rec.certificates.ci_adjacent_perp, error))
151 return false;
152 if (!readBool(is, "cert.force_tolerance", rec.certificates.force_tolerance,
153 error))
154 return false;
155 if (!readBool(is, "cert.certified_ci_only_branch",
156 rec.certificates.certified_ci_only_branch, error))
157 return false;
158 if (!readBool(is, "cert.certified_full_band_branch",
159 rec.certificates.certified_full_band_branch, error))
160 return false;
161 if (!readBool(is, "cert.certified_hook_branch",
162 rec.certificates.certified_hook_branch, error))
163 return false;
164 int theta_n = 0;
165 if (!readInt(is, "theta_n", theta_n, error)) return false;
166 if (theta_n < 0 || theta_n > 1'000'000) {
167 if (error) *error = "theta_n out of range";
168 return false;
169 }
170 rec.theta.resize(static_cast<std::size_t>(theta_n));
171 for (int i = 0; i < theta_n; ++i) {
172 std::ostringstream key;
173 key << "theta_" << i;
174 if (!readDouble(is, key.str(), rec.theta[static_cast<std::size_t>(i)],
175 error))
176 return false;
177 }
178 return true;
179}
180
181} // namespace
182
184 const std::vector<OuterIterCompositionTraceRecord>& records)
185{
186 std::ostringstream os;
187 os << "# gpridge outer-iter composition trace v"
189 os << "# Deterministic; no timestamps/pointers. Regenerate with "
190 "GPR_REGEN_GOLDEN=1.\n";
191 writeInt(os, "version", kCompositionTraceFormatVersion);
192 writeInt(os, "n_records", static_cast<int>(records.size()));
193 for (const auto& rec: records)
194 writeRecord(os, rec);
195 return os.str();
196}
197
198bool parseCompositionTrace(const std::string& text,
199 std::vector<OuterIterCompositionTraceRecord>& out,
200 std::string* error)
201{
202 out.clear();
203 std::istringstream is(text);
204 // Skip comment lines.
205 std::string line;
206 std::string body;
207 while (std::getline(is, line)) {
208 std::size_t i = 0;
209 while (i < line.size() &&
210 std::isspace(static_cast<unsigned char>(line[i])))
211 ++i;
212 if (i < line.size() && line[i] == '#') continue;
213 body += line;
214 body += '\n';
215 }
216 std::istringstream bs(body);
217 int version = 0;
218 int n_records = 0;
219 if (!readInt(bs, "version", version, error)) return false;
220 if (version != kCompositionTraceFormatVersion) {
221 if (error)
222 *error = "unsupported composition trace version " +
223 std::to_string(version);
224 return false;
225 }
226 if (!readInt(bs, "n_records", n_records, error)) return false;
227 if (n_records < 0 || n_records > 1'000'000) {
228 if (error) *error = "n_records out of range";
229 return false;
230 }
231 out.reserve(static_cast<std::size_t>(n_records));
232 for (int i = 0; i < n_records; ++i) {
234 if (!readRecord(bs, rec, error)) return false;
235 out.push_back(std::move(rec));
236 }
237 return true;
238}
239
241 const std::string& path,
242 const std::vector<OuterIterCompositionTraceRecord>& records)
243{
244 std::ofstream ofs(path, std::ios::binary | std::ios::trunc);
245 if (!ofs) return false;
246 ofs << formatCompositionTrace(records);
247 return static_cast<bool>(ofs);
248}
249
250bool readCompositionTraceFile(const std::string& path,
251 std::vector<OuterIterCompositionTraceRecord>& out,
252 std::string* error)
253{
254 std::ifstream ifs(path, std::ios::binary);
255 if (!ifs) {
256 if (error) *error = "cannot open " + path;
257 return false;
258 }
259 std::ostringstream ss;
260 ss << ifs.rdbuf();
261 return parseCompositionTrace(ss.str(), out, error);
262}
263
265 const std::vector<OuterIterCompositionTraceRecord>& a,
266 const std::vector<OuterIterCompositionTraceRecord>& b)
267{
269}
270
272 const std::vector<OuterIterCompositionTraceRecord>& got,
273 const std::vector<OuterIterCompositionTraceRecord>& expected, double rtol,
274 double atol, std::string* mismatch)
275{
276 auto fail = [&](const std::string& why) {
277 if (mismatch != nullptr) *mismatch = why;
278 return false;
279 };
280 if (got.size() != expected.size()) {
281 return fail("record count " + std::to_string(got.size()) + " vs " +
282 std::to_string(expected.size()));
283 }
284 auto close = [&](double g, double e, const char* field, std::size_t i) {
285 const double diff = std::abs(g - e);
286 const double bound = atol + rtol * std::abs(e);
287 if (diff <= bound) return true;
288 std::ostringstream os;
289 os << "record " << i << " field " << field << ": "
290 << std::setprecision(17) << g << " vs " << e << " (|diff|=" << diff
291 << " > " << bound << ")";
292 return fail(os.str());
293 };
294 for (std::size_t i = 0; i < got.size(); ++i) {
295 const auto& g = got[i];
296 const auto& e = expected[i];
297 // Structural / decision fields: exact.
298 auto eq_int = [&](long long a, long long b, const char* field) {
299 if (a == b) return true;
300 return fail("record " + std::to_string(i) + " field " + field +
301 ": " + std::to_string(a) + " vs " + std::to_string(b));
302 };
303 if (!eq_int(g.outer_idx, e.outer_idx, "outer_idx")) return false;
304 if (!eq_int(g.oracle_target_image, e.oracle_target_image,
305 "oracle_target_image"))
306 return false;
307 if (!eq_int(g.ci_index, e.ci_index, "ci_index")) return false;
308 if (!eq_int(g.n_uneval, e.n_uneval, "n_uneval")) return false;
309 if (!eq_int(g.ci_active, e.ci_active, "ci_active")) return false;
310 const auto& gc = g.certificates;
311 const auto& ec = e.certificates;
312 if (!eq_int(gc.n_uneval_zero, ec.n_uneval_zero, "cert.n_uneval_zero") ||
313 !eq_int(gc.mep_force_ok, ec.mep_force_ok, "cert.mep_force_ok") ||
314 !eq_int(gc.ci_force_ok, ec.ci_force_ok, "cert.ci_force_ok") ||
315 !eq_int(gc.true_saddle_energy, ec.true_saddle_energy,
316 "cert.true_saddle_energy") ||
317 !eq_int(gc.ci_adjacent_perp, ec.ci_adjacent_perp,
318 "cert.ci_adjacent_perp") ||
319 !eq_int(gc.force_tolerance, ec.force_tolerance,
320 "cert.force_tolerance") ||
321 !eq_int(gc.certified_ci_only_branch, ec.certified_ci_only_branch,
322 "cert.certified_ci_only_branch") ||
323 !eq_int(gc.certified_full_band_branch,
324 ec.certified_full_band_branch,
325 "cert.certified_full_band_branch") ||
326 !eq_int(gc.certified_hook_branch, ec.certified_hook_branch,
327 "cert.certified_hook_branch")) {
328 return false;
329 }
330 if (!eq_int(static_cast<long long>(g.theta.size()),
331 static_cast<long long>(e.theta.size()), "theta_n"))
332 return false;
333 // Continuous fields: tolerant.
334 if (!close(g.barrier_estimate, e.barrier_estimate, "barrier_estimate",
335 i))
336 return false;
337 if (!close(g.max_oracle_force, e.max_oracle_force, "max_oracle_force",
338 i))
339 return false;
340 for (std::size_t k = 0; k < g.theta.size(); ++k) {
341 if (!close(g.theta[k], e.theta[k], "theta", i)) return false;
342 }
343 }
344 return true;
345}
346
347} // namespace neb
348} // namespace gpr
Composition-level per-outer-iteration snapshot for gpNEB regression.
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
The nudged elastic band: path, GP-NEB driver, configuration and result, acquisition,...
bool writeCompositionTraceFile(const std::string &path, const std::vector< OuterIterCompositionTraceRecord > &records)
bool compositionTracesNumericallyClose(const std::vector< OuterIterCompositionTraceRecord > &got, const std::vector< OuterIterCompositionTraceRecord > &expected, double rtol, double atol, std::string *mismatch)
constexpr int kCompositionTraceFormatVersion
Bump when the on-disk golden format changes incompatibly.
bool compositionTracesTextuallyEqual(const std::vector< OuterIterCompositionTraceRecord > &a, const std::vector< OuterIterCompositionTraceRecord > &b)
True when canonical serializations are identical (golden check).
std::string formatCompositionTrace(const std::vector< OuterIterCompositionTraceRecord > &records)
Canonical text serialization used by golden files and exact-match asserts.
bool readCompositionTraceFile(const std::string &path, std::vector< OuterIterCompositionTraceRecord > &out, std::string *error)
bool parseCompositionTrace(const std::string &text, std::vector< OuterIterCompositionTraceRecord > &out, std::string *error)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
One outer-iteration composition snapshot.