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