alpaqa 1.0.0a16
Nonconvex constrained optimization
Loading...
Searching...
No Matches
extra-stats.hpp
Go to the documentation of this file.
1#pragma once
2
5#include "solver-driver.hpp"
6
7#include <chrono>
8#include <memory>
9
10template <alpaqa::Config Conf>
13 static constexpr real_t NaN = alpaqa::NaN<Conf>;
14 struct Record {
16 double time;
17 real_t gamma = NaN, eps = NaN, delta = NaN, psi = NaN, psi_hat = NaN,
18 fbe = NaN, tau = NaN, radius = NaN, rho = NaN;
19 };
20 std::vector<Record> stats{};
21 std::chrono::steady_clock::time_point t0;
22
23 void update_iter(const auto &progress_info) {
24 auto t = std::chrono::steady_clock::now();
25 if (progress_info.outer_iter == 0 && progress_info.k == 0)
26 t0 = t;
27 Record r{
28 .outer_iter = progress_info.outer_iter,
29 .inner_iter = progress_info.k,
30 .time = std::chrono::duration<double>{t - t0}.count(),
31 };
32 if constexpr (requires { progress_info.γ; })
33 r.gamma = progress_info.γ;
34 if constexpr (requires { progress_info.ε; })
35 r.eps = progress_info.ε;
36 if constexpr (requires { progress_info.ψ; })
37 r.psi = progress_info.ψ;
38 if constexpr (requires { progress_info.ψ_hat; })
39 r.psi_hat = progress_info.ψ_hat;
40 if constexpr (requires { progress_info.φγ; })
41 r.fbe = progress_info.φγ;
42 if constexpr (requires { progress_info.τ; })
43 r.tau = progress_info.τ;
44 if constexpr (requires { progress_info.Δ; })
45 r.radius = progress_info.Δ;
46 if constexpr (requires { progress_info.ρ; })
47 r.rho = progress_info.ρ;
48 if constexpr (requires {
49 progress_info.y;
50 progress_info.ŷ;
51 progress_info.Σ;
52 })
53 r.delta = (progress_info.Σ.asDiagonal().inverse() *
54 (progress_info.ŷ - progress_info.y))
55 .template lpNorm<Eigen::Infinity>();
56 stats.push_back(r);
57 }
58};
59
60template <alpaqa::Config Conf>
64 std::shared_ptr<const AlpaqaSolverStatsCollector<config_t>>;
68 [[nodiscard]] bool has_statistics() const override {
69 return collector && !collector->stats.empty();
70 }
71 void write_statistics_to_stream(std::ostream &os) override {
72 std::array<char, 64> buf;
73 os << "outer_iter,inner_iter,time,gamma,eps,delta,psi,psi_hat,fbe,tau,"
74 "radius,rho\n";
75 for (const auto &r : collector->stats) {
76 os << r.outer_iter << ',' << r.inner_iter << ','
77 << alpaqa::float_to_str_vw(buf, r.time) << ','
78 << alpaqa::float_to_str_vw(buf, r.gamma) << ','
79 << alpaqa::float_to_str_vw(buf, r.eps) << ','
80 << alpaqa::float_to_str_vw(buf, r.delta) << ','
81 << alpaqa::float_to_str_vw(buf, r.psi) << ','
82 << alpaqa::float_to_str_vw(buf, r.psi_hat) << ','
83 << alpaqa::float_to_str_vw(buf, r.fbe) << ','
84 << alpaqa::float_to_str_vw(buf, r.tau) << ','
85 << alpaqa::float_to_str_vw(buf, r.radius) << ','
86 << alpaqa::float_to_str_vw(buf, r.rho) << '\n';
87 }
88 }
89};
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:63
constexpr const auto inf
Definition config.hpp:98
std::string_view float_to_str_vw(auto &buf, double value, int precision=std::numeric_limits< double >::max_digits10)
Definition print.tpp:39
std::function< solver_free_func_t > solver_func_t
void update_iter(const auto &progress_info)
static constexpr real_t NaN
std::vector< Record > stats
std::chrono::steady_clock::time_point t0
AlpaqaSolverWrapperStats(solver_func_t run, collector_t collector)
void write_statistics_to_stream(std::ostream &os) override
std::shared_ptr< const AlpaqaSolverStatsCollector< config_t > > collector_t
bool has_statistics() const override
solver_func_t run