alpaqa 1.0.0a18
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) {
25 auto t = std::chrono::steady_clock::now();
26 if (progress_info.outer_iter == 0 && progress_info.k == 0)
27 t0 = t;
28 Record r{
29 .outer_iter = progress_info.outer_iter,
30 .inner_iter = progress_info.k,
31 .time = std::chrono::duration<double>{t - t0}.count(),
32 };
33 if constexpr (requires { progress_info.γ; })
34 r.gamma = progress_info.γ;
35 if constexpr (requires { progress_info.ε; })
36 r.eps = progress_info.ε;
37 if constexpr (requires { progress_info.ψ; })
38 r.psi = progress_info.ψ;
39 if constexpr (requires { progress_info.ψ_hat; })
40 r.psi_hat = progress_info.ψ_hat;
41 if constexpr (requires { progress_info.φγ; })
42 r.fbe = progress_info.φγ;
43 if constexpr (requires { progress_info.τ; })
44 r.tau = progress_info.τ;
45 if constexpr (requires { progress_info.Δ; })
46 r.radius = progress_info.Δ;
47 if constexpr (requires { progress_info.ρ; })
48 r.rho = progress_info.ρ;
49 if constexpr (requires {
50 progress_info.y;
51 progress_info.ŷ;
52 progress_info.Σ;
53 })
54 r.delta = norm_inf((progress_info.ŷ - progress_info.y)
55 .cwiseQuotient(progress_info.Σ));
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:77
auto norm_inf(const Eigen::MatrixBase< Derived > &v)
Get the maximum or infinity-norm of the given vector.
Definition config.hpp:202
constexpr const auto inf
Definition config.hpp:112
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