Nonconvex constrained optimization
Loading...
Searching...
No Matches
results.hpp
Go to the documentation of this file.
1#pragma once
2
9
10#include <bit>
11#include <charconv>
12#include <chrono>
13#include <iomanip>
14#include <random>
15#include <string_view>
16#include <variant>
17
18namespace alpaqa::driver {
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;
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, const 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(projecting_difference_constraints);
74 EVAL(projection_multipliers);
75 EVAL(proximal_gradient_step);
76 EVAL(inactive_indices_res_lna);
77 EVAL(prox_jacobian_diag);
78 EVAL(nonsmooth_objective);
79 EVAL(objective);
80 EVAL(objective_gradient);
81 EVAL(objective_and_gradient);
82 EVAL(objective_and_constraints);
83 EVAL(objective_gradient_and_constraints_gradient_product);
84 EVAL(constraints);
85 EVAL(constraints_gradient_product);
86 EVAL(grad_gi);
87 EVAL(constraints_jacobian);
88 EVAL(lagrangian_gradient);
89 EVAL(lagrangian_hessian_product);
90 EVAL(lagrangian_hessian);
91 EVAL(augmented_lagrangian_hessian_product);
92 EVAL(augmented_lagrangian_hessian);
93 EVAL(augmented_lagrangian);
94 EVAL(augmented_lagrangian_gradient);
95 EVAL(augmented_lagrangian_and_gradient);
96#undef EVAL
97}
98
99inline void write_evaluations(std::ostream &os, const OCPEvalCounter &evals) {
100 auto dict_elem = [&os](std::string_view name, const auto &value) {
101 os << name << ": " << value << '\n';
102 };
103#define EVAL(name) dict_elem(#name, evals.name)
104 EVAL(f);
105 EVAL(jac_f);
106 EVAL(grad_f_prod);
107 EVAL(h);
108 EVAL(h_N);
109 EVAL(l);
110 EVAL(l_N);
111 EVAL(qr);
112 EVAL(q_N);
113 EVAL(add_Q);
114 EVAL(add_Q_N);
115 EVAL(add_R_masked);
116 EVAL(add_S_masked);
117 EVAL(add_R_prod_masked);
118 EVAL(add_S_prod_masked);
119 EVAL(constr);
120 EVAL(constr_N);
121 EVAL(grad_constr_prod);
122 EVAL(grad_constr_prod_N);
123 EVAL(add_gn_hess_constr);
124 EVAL(add_gn_hess_constr_N);
125#undef EVAL
126}
127
128namespace detail {
129template <class... Ts>
130struct overloaded : Ts... {
131 using Ts::operator()...;
132};
133template <class... Ts>
134overloaded(Ts...) -> overloaded<Ts...>;
135} // namespace detail
136
137inline void print_results(std::ostream &os, const BenchmarkResults &results) {
138 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_num_variables() << '\n'
151 << "num con: " << results.problem.problem.get_num_constraints() << '\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
183} // namespace alpaqa::driver
length_t get_num_constraints() const
[Required] Number of constraints.
length_t get_num_variables() const
[Required] Number of decision variables.
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
overloaded(Ts...) -> overloaded< Ts... >
void write_evaluations(std::ostream &os, const EvalCounter &evals)
Definition results.hpp:68
std::string random_hex_string(auto &&rng)
Definition results.hpp:52
auto timestamp_ms()
Definition results.hpp:61
void print_results(std::ostream &os, const BenchmarkResults &results)
Definition results.hpp:137
EigenConfigd DefaultConfig
Definition config.hpp:31
typename Conf::real_t real_t
Definition config.hpp:86
constexpr const auto NaN
Definition config.hpp:114
typename Conf::index_t index_t
Definition config.hpp:104
alpaqa::TypeErasedProblem< config_t > problem
typename Conf::vec vec
Definition config.hpp:88
#define EVAL(name)
static constexpr real_t NaN
Definition results.hpp:42
KKTError< config_t > error
Definition results.hpp:47
std::span< const std::string_view > options
Definition results.hpp:48
std::vector< std::pair< std::string, any_stat_t > > extra
Definition results.hpp:37
static constexpr real_t NaN
Definition results.hpp:22
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:35