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