16#include <gch/small_vector.hpp>
18#include "sleipnir/util/print.hpp"
19#include "sleipnir/util/profiler.hpp"
24enum class IterationType : uint8_t {
35template <
typename Rep,
typename Period = std::ratio<1>>
36constexpr double to_ms(
const std::chrono::duration<Rep, Period>& duration) {
37 using std::chrono::duration_cast;
38 using std::chrono::microseconds;
39 return duration_cast<microseconds>(duration).count() / 1e3;
46template <
typename Scalar>
47std::string power_of_10(Scalar value) {
48 if (value == Scalar(0)) {
52 int exponent =
static_cast<int>(log10(value));
56 }
else if (exponent == 1) {
60 int n = std::abs(exponent);
61 gch::small_vector<int> digits;
63 digits.emplace_back(n % 10);
67 std::string output =
"10";
73 constexpr std::array strs = {
"⁰",
"¹",
"²",
"³",
"⁴",
74 "⁵",
"⁶",
"⁷",
"⁸",
"⁹"};
75 for (
const auto& digit : digits | std::views::reverse) {
76 output += strs[digit];
84#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
90template <
typename Scalar>
91void print_too_few_dofs_error(
92 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_e) {
93 slp::println(
"The problem has too few degrees of freedom.");
94 slp::println(
"Violated constraints (cₑ(x) = 0) in order of declaration:");
95 for (
int row = 0; row < c_e.rows(); ++row) {
96 if (c_e[row] < Scalar(0)) {
97 slp::println(
" {}/{}: {} = 0", row + 1, c_e.rows(), c_e[row]);
102#define print_too_few_dofs_error(...)
105#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
111template <
typename Scalar>
112void print_c_e_local_infeasibility_error(
113 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_e) {
115 "The problem is locally infeasible due to violated equality "
117 slp::println(
"Violated constraints (cₑ(x) = 0) in order of declaration:");
118 for (
int row = 0; row < c_e.rows(); ++row) {
119 if (c_e[row] < Scalar(0)) {
120 slp::println(
" {}/{}: {} = 0", row + 1, c_e.rows(), c_e[row]);
125#define print_c_e_local_infeasibility_error(...)
128#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
134template <
typename Scalar>
135void print_c_i_local_infeasibility_error(
136 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_i) {
138 "The problem is locally infeasible due to violated inequality "
140 slp::println(
"Violated constraints (cᵢ(x) ≥ 0) in order of declaration:");
141 for (
int row = 0; row < c_i.rows(); ++row) {
142 if (c_i[row] < Scalar(0)) {
143 slp::println(
" {}/{}: {} ≥ 0", row + 1, c_i.rows(), c_i[row]);
148#define print_c_i_local_infeasibility_error(...)
151#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
152inline void print_bound_constraint_global_infeasibility_error(
153 const std::span<
const std::pair<Eigen::Index, Eigen::Index>>
154 conflicting_lower_upper_bound_indices) {
156 "The problem is globally infeasible due to conflicting bound "
158 for (
const auto& [lower_bound_idx, upper_bound_idx] :
159 conflicting_lower_upper_bound_indices) {
161 " Inequality constraint {} gives a lower bound that is greater than "
162 "the upper bound given by inequality constraint {}",
163 lower_bound_idx, upper_bound_idx);
167#define print_bound_constraint_global_infeasibility_error(...)
170#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
188template <
typename Scalar,
typename Rep,
typename Period = std::ratio<1>>
189void print_iteration_diagnostics(
int iterations, IterationType type,
190 const std::chrono::duration<Rep, Period>& time,
191 Scalar error, Scalar cost,
192 Scalar infeasibility, Scalar complementarity,
193 Scalar μ, Scalar δ, Scalar primal_α,
194 Scalar primal_α_max, Scalar α_reduction_factor,
196 if (iterations % 20 == 0) {
197 if (iterations == 0) {
203 "{:━^4}┯{:━^4}┯{:━^9}┯{:━^12}┯{:━^13}┯{:━^12}┯{:━^12}┯{:━^8}┯{:━^5}┯"
204 "{:━^8}┯{:━^8}┯{:━^2}",
205 "",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"");
206 if (iterations == 0) {
212 "┃{:^4}│{:^4}│{:^9}│{:^12}│{:^13}│{:^12}│{:^12}│{:^8}│{:^5}│{:^8}│{:^8}"
214 "iter",
"type",
"time (ms)",
"error",
"cost",
"infeas.",
"complement.",
215 "μ",
"reg",
"primal α",
"dual α",
"↩");
217 "┡{:━^4}┷{:━^4}┷{:━^9}┷{:━^12}┷{:━^13}┷{:━^12}┷{:━^12}┷{:━^8}┷{:━^5}┷"
218 "{:━^8}┷{:━^8}┷{:━^2}┩",
219 "",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"");
234 static_cast<int>(log(primal_α / primal_α_max) / log(α_reduction_factor));
236 constexpr std::array ITERATION_TYPES = {
"norm",
"✓SOC",
"XSOC"};
238 "│{:4} {:4} {:9.3f} {:12e} {:13e} {:12e} {:12e} {:.2e} {:<5} {:.2e} "
240 iterations, ITERATION_TYPES[std::to_underlying(type)], to_ms(time), error,
241 cost, infeasibility, complementarity, μ, power_of_10(δ), primal_α, dual_α,
245#define print_iteration_diagnostics(...)
248#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
250inline void print_bottom_iteration_diagnostics() {
251 slp::println(
"└{:─^108}┘",
"");
254#define print_bottom_iteration_diagnostics(...)
263std::string histogram(
double value) {
264 value = std::clamp(value, 0.0, 1.0);
267 int fpart =
static_cast<int>(std::modf(value * Width, &ipart) * 8);
269 constexpr std::array strs = {
" ",
"▏",
"▎",
"▍",
"▌",
"▋",
"▊",
"▉",
"█"};
273 while (index < ipart) {
281 while (index < Width) {
289#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
293inline void print_solver_diagnostics(
294 const gch::small_vector<SolveProfiler>& solve_profilers) {
295 auto solve_duration = to_ms(solve_profilers[0].total_duration());
297 slp::println(
"┏{:━^23}┯{:━^18}┯{:━^10}┯{:━^9}┯{:━^4}┓",
"",
"",
"",
"",
"");
298 slp::println(
"┃{:^23}│{:^18}│{:^10}│{:^9}│{:^4}┃",
"solver trace",
"percent",
299 "total (ms)",
"each (ms)",
"runs");
300 slp::println(
"┡{:━^23}┷{:━^18}┷{:━^10}┷{:━^9}┷{:━^4}┩",
"",
"",
"",
"",
"");
302 for (
auto& profiler : solve_profilers) {
303 double norm = solve_duration == 0.0
304 ? (&profiler == &solve_profilers[0] ? 1.0 : 0.0)
305 : to_ms(profiler.total_duration()) / solve_duration;
306 slp::println(
"│{:<23} {:>6.2f}%▕{}▏ {:>10.3f} {:>9.3f} {:>4}│",
307 profiler.name(), norm * 100.0, histogram<9>(norm),
308 to_ms(profiler.total_duration()),
309 to_ms(profiler.average_duration()), profiler.num_solves());
312 slp::println(
"└{:─^68}┘",
"");
315#define print_solver_diagnostics(...)
318#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
322inline void print_autodiff_diagnostics(
323 const gch::small_vector<SetupProfiler>& setup_profilers) {
324 auto setup_duration = to_ms(setup_profilers[0].duration());
327 slp::println(
"┏{:━^23}┯{:━^18}┯{:━^10}┯{:━^9}┯{:━^4}┓",
"",
"",
"",
"",
"");
328 slp::println(
"┃{:^23}│{:^18}│{:^10}│{:^9}│{:^4}┃",
"autodiff trace",
329 "percent",
"total (ms)",
"each (ms)",
"runs");
330 slp::println(
"┡{:━^23}┷{:━^18}┷{:━^10}┷{:━^9}┷{:━^4}┩",
"",
"",
"",
"",
"");
333 for (
auto& profiler : setup_profilers) {
334 double norm = setup_duration == 0.0
335 ? (&profiler == &setup_profilers[0] ? 1.0 : 0.0)
336 : to_ms(profiler.duration()) / setup_duration;
337 slp::println(
"│{:<23} {:>6.2f}%▕{}▏ {:>10.3f} {:>9.3f} {:>4}│",
338 profiler.name(), norm * 100.0, histogram<9>(norm),
339 to_ms(profiler.duration()), to_ms(profiler.duration()),
"1");
342 slp::println(
"└{:─^68}┘",
"");
345#define print_autodiff_diagnostics(...)