alpaqa pantr
Nonconvex constrained optimization
Loading...
Searching...
No Matches
results.hpp
Go to the documentation of this file.
1#pragma once
2
8
9#include <bit>
10#include <charconv>
11#include <chrono>
12#include <iomanip>
13#include <map>
14#include <numeric>
15#include <string_view>
16#include <variant>
17
18#include "problem.hpp"
19
22 static constexpr real_t NaN = alpaqa::NaN<config_t>;
23
24 std::string_view status;
25 bool success = false;
27 std::chrono::nanoseconds duration{};
28 std::string solver;
29 real_t h = NaN, δ = NaN, ε = NaN, γ = NaN, Σ = NaN;
30 vec solution{};
33 index_t outer_iter = -1, inner_iter = -1;
34 using any_stat_t = std::variant<index_t, real_t, std::string, bool, vec,
35 std::vector<real_t>>;
36 std::vector<std::pair<std::string, any_stat_t>> extra{};
37};
38
41 static constexpr real_t NaN = alpaqa::NaN<config_t>;
42
47 std::span<const std::string_view> options;
48 int64_t timestamp = 0;
49};
50
51inline std::string random_hex_string(auto &&rng) {
52 auto rnd = std::uniform_int_distribution<uint32_t>()(rng);
53 auto rnd_str = std::string(8, '0');
54 std::to_chars(rnd_str.data() + std::countl_zero(rnd) / 4,
55 rnd_str.data() + rnd_str.size(), rnd, 16);
56 return rnd_str;
57}
58
59template <class Clk>
61 using ms = std::chrono::milliseconds;
62 auto now = Clk::now();
63 auto now_ms = std::chrono::duration_cast<ms>(now.time_since_epoch());
64 return now_ms;
65}
66
67inline void write_evaluations(std::ostream &os,
68 const alpaqa::EvalCounter &evals) {
69 auto dict_elem = [&os](std::string_view name, const auto &value) {
70 os << name << ": " << value << '\n';
71 };
72#define EVAL(name) dict_elem(#name, evals.name)
73 EVAL(proj_diff_g);
74 EVAL(proj_multipliers);
75 EVAL(prox_grad_step);
76 EVAL(f);
77 EVAL(grad_f);
78 EVAL(f_grad_f);
79 EVAL(f_g);
80 EVAL(grad_f_grad_g_prod);
81 EVAL(g);
82 EVAL(grad_g_prod);
83 EVAL(grad_gi);
84 EVAL(grad_L);
85 EVAL(hess_L_prod);
86 EVAL(hess_L);
87 EVAL(hess_ψ_prod);
88 EVAL(hess_ψ);
89 EVAL(ψ);
90 EVAL(grad_ψ);
91 EVAL(ψ_grad_ψ);
92#undef EVAL
93}
94
95inline void write_evaluations(std::ostream &os,
96 const alpaqa::OCPEvalCounter &evals) {
97 auto dict_elem = [&os](std::string_view name, const auto &value) {
98 os << name << ": " << value << '\n';
99 };
100#define EVAL(name) dict_elem(#name, evals.name)
101 EVAL(f);
102 EVAL(jac_f);
103 EVAL(grad_f_prod);
104 EVAL(h);
105 EVAL(h_N);
106 EVAL(l);
107 EVAL(l_N);
108 EVAL(qr);
109 EVAL(q_N);
110 EVAL(add_Q);
111 EVAL(add_Q_N);
112 EVAL(add_R_masked);
113 EVAL(add_S_masked);
114 EVAL(add_R_prod_masked);
115 EVAL(add_S_prod_masked);
116 EVAL(constr);
117 EVAL(constr_N);
118 EVAL(grad_constr_prod);
119 EVAL(grad_constr_prod_N);
120 EVAL(add_gn_hess_constr);
121 EVAL(add_gn_hess_constr_N);
122#undef EVAL
123}
124
125namespace detail {
126template <class... Ts>
127struct overloaded : Ts... {
128 using Ts::operator()...;
129};
130template <class... Ts>
131overloaded(Ts...) -> overloaded<Ts...>;
132} // namespace detail
133
134inline void print_results(std::ostream &os, const BenchmarkResults &results) {
135 USING_ALPAQA_CONFIG(BenchmarkResults::config_t);
137 const auto &solstats = results.solver_results;
138 const auto &kkterr = results.error;
139 auto time_s = std::chrono::duration<double>(solstats.duration);
140 os << '\n'
141 << solstats.evals << '\n'
142 << "solver: " << solstats.solver << '\n'
143 << "problem: " << results.problem.path.filename().c_str() << " (from "
144 << results.problem.path.parent_path() << ")" << '\n'
145 << "status: " << (solstats.success ? "\033[0;32m" : "\033[0;31m")
146 << solstats.status << "\033[0m" << '\n'
147 << "num var: " << results.problem.problem.get_n() << '\n'
148 << "num con: " << results.problem.problem.get_m() << '\n'
149 << "ε = " << float_to_str(solstats.ε) << '\n'
150 << "δ = " << float_to_str(solstats.δ) << '\n'
151 << "final step size = " << float_to_str(solstats.γ) << '\n'
152 << "penalty norm = " << float_to_str(solstats.Σ) << '\n'
153 << "nonsmooth objective = " << float_to_str(solstats.h) << '\n'
154 << "smooth objective = " << float_to_str(results.objective) << '\n'
155 << "objective = " << float_to_str(results.objective) << '\n'
156 << "stationarity = " << float_to_str(kkterr.stationarity) << '\n'
157 << "violation = " << float_to_str(kkterr.constr_violation) << '\n'
158 << "complementarity = " << float_to_str(kkterr.complementarity) << '\n'
159 << "time: " << float_to_str(time_s.count(), 3) << " s\n"
160 << "outer iter: " << std::setw(6) << solstats.outer_iter << '\n'
161 << "iter: " << std::setw(6) << solstats.inner_iter << '\n'
162 << std::endl;
163 for (const auto &[key, value] : solstats.extra) {
164 auto print_key = [&os, k{key}](const auto &v) {
165 os << k << ": " << v << '\n';
166 };
167 auto print = detail::overloaded{
168 [&print_key](const auto &v) { print_key(v); },
169 [&print_key](real_t v) { print_key(float_to_str(v)); },
170 [&print_key](bool v) { print_key(v ? "yes" : "no"); },
171 [](const std::vector<real_t> &) {},
172 [](const vec &) {},
173 };
174 std::visit(print, value);
175 }
176 os << std::endl;
177}
178
179inline void write_results(std::ostream &os, const BenchmarkResults &results) {
180 // TODO
181 (void)os;
182 (void)results;
183}
length_t get_n() const
[Required] Number of decision variables.
length_t get_m() const
[Required] Number of constraints.
#define USING_ALPAQA_CONFIG(Conf)
Definition: config.hpp:42
std::string float_to_str(F value, int precision)
Definition: print.tpp:66
alpaqa::TypeErasedProblem< config_t > problem
Definition: problem.hpp:15
fs::path path
Definition: problem.hpp:17
void write_results(std::ostream &os, const BenchmarkResults &results)
Definition: results.hpp:179
#define EVAL(name)
std::string random_hex_string(auto &&rng)
Definition: results.hpp:51
void write_evaluations(std::ostream &os, const alpaqa::EvalCounter &evals)
Definition: results.hpp:67
auto timestamp_ms()
Definition: results.hpp:60
void print_results(std::ostream &os, const BenchmarkResults &results)
Definition: results.hpp:134
real_t smooth_objective
Definition: results.hpp:45
static constexpr real_t NaN
Definition: results.hpp:41
LoadedProblem & problem
Definition: results.hpp:43
int64_t timestamp
Definition: results.hpp:48
alpaqa::KKTError< config_t > error
Definition: results.hpp:46
SolverResults solver_results
Definition: results.hpp:44
std::span< const std::string_view > options
Definition: results.hpp:47
real_t objective
Definition: results.hpp:45
index_t inner_iter
Definition: results.hpp:33
real_t h
Definition: results.hpp:29
std::vector< std::pair< std::string, any_stat_t > > extra
Definition: results.hpp:36
real_t δ
Definition: results.hpp:29
static constexpr real_t NaN
Definition: results.hpp:22
std::string solver
Definition: results.hpp:28
real_t γ
Definition: results.hpp:29
std::chrono::nanoseconds duration
Definition: results.hpp:27
std::string_view status
Definition: results.hpp:24
bool success
Definition: results.hpp:25
std::variant< index_t, real_t, std::string, bool, vec, std::vector< real_t > > any_stat_t
Definition: results.hpp:35
real_t ε
Definition: results.hpp:29
alpaqa::EvalCounter evals
Definition: results.hpp:26
real_t Σ
Definition: results.hpp:29
index_t outer_iter
Definition: results.hpp:33
vec multipliers
Definition: results.hpp:31
vec multipliers_bounds
Definition: results.hpp:32