proxsuite-nlp  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear programming on manifolds.
Loading...
Searching...
No Matches
logger.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include <vector>
6#include <array>
7#include <fmt/color.h>
8#include <fmt/ranges.h>
9
10namespace proxsuite {
11namespace nlp {
12
13constexpr int NUM_KEYS = 10;
14const std::array<std::string, NUM_KEYS> BASIC_KEYS = {
15 "iter", "step_size", "inner_crit", "prim_err", "dual_err",
16 "xreg", "dphi0", "merit", "dM", "al_iter"};
17constexpr char int_format[] = "{: >{}d}";
18constexpr char sci_format[] = "{: > {}.{}e}";
19constexpr char dbl_format[] = "{: > {}.{}g}";
20
21struct LogRecord {
22 size_t iter;
23 double step_size;
24 double inner_crit;
25 double prim_err;
26 double dual_err;
27 double xreg;
28 double dphi0;
29 double merit;
30 double dM;
31 size_t al_iter;
32};
33
35struct BaseLogger {
36 unsigned int COL_WIDTH_0 = 4;
37 unsigned int COL_WIDTH = 10;
38 bool active = true;
39
40 constexpr static std::size_t print_outline_every() { return 25; }
41
42 const std::string join_str = "|";
43
44 void start() const {
45 if (!active)
46 return;
47 static constexpr char fstr[] = "{:^{}s}";
48 std::array<std::string, NUM_KEYS> v;
49 v[0] = fmt::format(fstr, BASIC_KEYS[0], COL_WIDTH_0);
50 for (std::size_t i = 1; i < BASIC_KEYS.size(); ++i) {
51 v[i] = fmt::format(fstr, BASIC_KEYS[i], COL_WIDTH);
52 }
53 fmt::print(fmt::emphasis::bold, "{}\n", fmt::join(v, join_str));
54 }
55
56 void log(const LogRecord &values) const {
57 if (!active)
58 return;
59 std::vector<std::string> v;
60 int sci_prec = 3;
61 int dbl_prec = 3;
62 using fmt::format;
63 if (values.iter % print_outline_every() == 0)
64 start();
65 v.push_back(format(int_format, values.iter, COL_WIDTH_0));
66 v.push_back(format(sci_format, values.step_size, COL_WIDTH, sci_prec));
67 v.push_back(format(sci_format, values.inner_crit, COL_WIDTH, sci_prec));
68 v.push_back(format(sci_format, values.prim_err, COL_WIDTH, sci_prec));
69 v.push_back(format(sci_format, values.dual_err, COL_WIDTH, sci_prec));
70 v.push_back(format(sci_format, values.xreg, COL_WIDTH, sci_prec));
71 v.push_back(format(sci_format, values.dphi0, COL_WIDTH, dbl_prec));
72 v.push_back(format(sci_format, values.merit, COL_WIDTH, sci_prec));
73 v.push_back(format(dbl_format, values.dM, COL_WIDTH, dbl_prec));
74 v.push_back(format(int_format, values.al_iter, COL_WIDTH));
75
76 fmt::print("{}\n", fmt::join(v, join_str));
77 }
78};
79
80} // namespace nlp
81} // namespace proxsuite
constexpr char sci_format[]
Definition logger.hpp:18
constexpr char int_format[]
Definition logger.hpp:17
const std::array< std::string, NUM_KEYS > BASIC_KEYS
Definition logger.hpp:14
constexpr int NUM_KEYS
Definition logger.hpp:13
constexpr char dbl_format[]
Definition logger.hpp:19
Main package namespace.
Definition bcl-params.hpp:5
A logging utility.
Definition logger.hpp:35
const std::string join_str
Definition logger.hpp:42
static constexpr std::size_t print_outline_every()
Definition logger.hpp:40
void log(const LogRecord &values) const
Definition logger.hpp:56
unsigned int COL_WIDTH_0
Definition logger.hpp:36