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(projecting_difference_constraints);
76 EVAL(projection_multipliers);
77 EVAL(proximal_gradient_step);
78 EVAL(inactive_indices_res_lna);
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,
100 const alpaqa::OCPEvalCounter &evals) {
101 auto dict_elem = [&os](std::string_view name, const auto &value) {
102 os << name << ": " << value << '\n';
103 };
104#define EVAL(name) dict_elem(#name, evals.name)
105 EVAL(f);
106 EVAL(jac_f);
107 EVAL(grad_f_prod);
108 EVAL(h);
109 EVAL(h_N);
110 EVAL(l);
111 EVAL(l_N);
112 EVAL(qr);
113 EVAL(q_N);
114 EVAL(add_Q);
115 EVAL(add_Q_N);
116 EVAL(add_R_masked);
117 EVAL(add_S_masked);
118 EVAL(add_R_prod_masked);
119 EVAL(add_S_prod_masked);
120 EVAL(constr);
121 EVAL(constr_N);
122 EVAL(grad_constr_prod);
123 EVAL(grad_constr_prod_N);
124 EVAL(add_gn_hess_constr);
125 EVAL(add_gn_hess_constr_N);
126#undef EVAL
127}
128
129namespace detail {
130template <class... Ts>
131struct overloaded : Ts... {
132 using Ts::operator()...;
133};
134template <class... Ts>
135overloaded(Ts...) -> overloaded<Ts...>;
136} // namespace detail
137
138inline void print_results(std::ostream &os, const BenchmarkResults &results) {
139 USING_ALPAQA_CONFIG(BenchmarkResults::config_t);
140 using alpaqa::float_to_str;
141 const auto &solstats = results.solver_results;
142 const auto &kkterr = results.error;
143 auto obj = results.objective;
144 auto time_s = std::chrono::duration<double>(solstats.duration);
145 os << '\n'
146 << solstats.evals << '\n'
147 << "solver: " << solstats.solver << '\n'
148 << "problem: " << results.problem.name << " (from "
149 << results.problem.path << ")" << '\n'
150 << "status: " << (solstats.success ? "\033[0;32m" : "\033[0;31m")
151 << solstats.status << "\033[0m" << '\n'
152 << "num var: " << results.problem.problem.get_num_variables() << '\n'
153 << "num con: " << results.problem.problem.get_num_constraints() << '\n'
154 << "ε = " << float_to_str(solstats.ε) << '\n'
155 << "δ = " << float_to_str(solstats.δ) << '\n'
156 << "final step size = " << float_to_str(solstats.γ) << '\n'
157 << "penalty norm = " << float_to_str(solstats.Σ) << '\n'
158 << "nonsmooth objective = " << float_to_str(solstats.h) << '\n'
159 << "smooth objective = " << float_to_str(obj) << '\n'
160 << "objective = " << float_to_str(obj + solstats.h) << '\n'
161 << "stationarity = " << float_to_str(kkterr.stationarity) << '\n'
162 << "violation = " << float_to_str(kkterr.constr_violation) << '\n'
163 << "complementarity = " << float_to_str(kkterr.complementarity) << '\n'
164 << "bounds violation = " << float_to_str(kkterr.bounds_violation) << '\n'
165 << "time: " << float_to_str(time_s.count(), 3) << " s\n"
166 << "outer iter: " << std::setw(6) << solstats.outer_iter << '\n'
167 << "iter: " << std::setw(6) << solstats.inner_iter << '\n'
168 << std::endl;
169 for (const auto &[key, value] : solstats.extra) {
170 auto print_key = [&os, k{key}](const auto &v) {
171 os << k << ": " << v << '\n';
172 };
173 auto print = detail::overloaded{
174 [&print_key](const auto &v) { print_key(v); },
175 [&print_key](real_t v) { print_key(float_to_str(v)); },
176 [&print_key](bool v) { print_key(v ? "yes" : "no"); },
177 [](const std::vector<real_t> &) {},
178 [](const vec &) {},
179 };
180 std::visit(print, value);
181 }
182 os << std::endl;
183}
184
185inline void write_results(std::ostream &os, const BenchmarkResults &results) {
186 // TODO
187 (void)os;
188 (void)results;
189}
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
EigenConfigd DefaultConfig
Definition config.hpp:31
constexpr const auto NaN
Definition config.hpp:114
overloaded(Ts...) -> overloaded< Ts... >
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:185
#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:138
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
alpaqa::EvalCounter evals
Definition results.hpp:27
index_t outer_iter
Definition results.hpp:35
std::variant< index_t, real_t, std::string, bool, vec, std::vector< real_t > > any_stat_t
Definition results.hpp:36
vec multipliers_bounds
Definition results.hpp:33