16#include <gch/small_vector.hpp>
18#include "sleipnir/util/print.hpp"
19#include "sleipnir/util/setup_profiler.hpp"
20#include "sleipnir/util/solve_profiler.hpp"
27enum class IterationType : uint8_t {
40template <
typename Rep,
typename Period = std::ratio<1>>
41constexpr double to_ms(
const std::chrono::duration<Rep, Period>& duration) {
42 using std::chrono::duration_cast;
43 using std::chrono::microseconds;
44 return duration_cast<microseconds>(duration).count() / 1e3;
53template <
typename Scalar>
54std::string power_of_10(Scalar value) {
55 if (value == Scalar(0)) {
59 int exponent =
static_cast<int>(log10(value));
63 }
else if (exponent == 1) {
67 int n = std::abs(exponent);
68 gch::small_vector<int> digits;
70 digits.emplace_back(n % 10);
74 std::string output =
"10";
80 constexpr std::array strs = {
"⁰",
"¹",
"²",
"³",
"⁴",
81 "⁵",
"⁶",
"⁷",
"⁸",
"⁹"};
82 for (
const auto& digit : digits | std::views::reverse) {
83 output += strs[digit];
91#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
99template <
typename Scalar>
100void print_too_few_dofs_error(
101 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_e) {
102 slp::println(
"The problem has too few degrees of freedom.");
103 slp::println(
"Violated constraints (cₑ(x) = 0) in order of declaration:");
104 for (
int row = 0; row < c_e.rows(); ++row) {
105 if (c_e[row] < Scalar(0)) {
106 slp::println(
" {}/{}: {} = 0", row + 1, c_e.rows(), c_e[row]);
111#define print_too_few_dofs_error(...)
114#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
122template <
typename Scalar>
123void print_c_e_local_infeasibility_error(
124 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_e) {
126 "The problem is locally infeasible due to violated equality "
128 slp::println(
"Violated constraints (cₑ(x) = 0) in order of declaration:");
129 for (
int row = 0; row < c_e.rows(); ++row) {
130 if (c_e[row] < Scalar(0)) {
131 slp::println(
" {}/{}: {} = 0", row + 1, c_e.rows(), c_e[row]);
136#define print_c_e_local_infeasibility_error(...)
139#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
147template <
typename Scalar>
148void print_c_i_local_infeasibility_error(
149 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_i) {
151 "The problem is locally infeasible due to violated inequality "
153 slp::println(
"Violated constraints (cᵢ(x) ≥ 0) in order of declaration:");
154 for (
int row = 0; row < c_i.rows(); ++row) {
155 if (c_i[row] < Scalar(0)) {
156 slp::println(
" {}/{}: {} ≥ 0", row + 1, c_i.rows(), c_i[row]);
161#define print_c_i_local_infeasibility_error(...)
164#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
165inline void print_bound_constraint_global_infeasibility_error(
166 const std::span<
const std::pair<Eigen::Index, Eigen::Index>>
167 conflicting_lower_upper_bound_indices) {
169 "The problem is globally infeasible due to conflicting bound "
171 for (
const auto& [lower_bound_idx, upper_bound_idx] :
172 conflicting_lower_upper_bound_indices) {
174 " Inequality constraint {} gives a lower bound that is greater than "
175 "the upper bound given by inequality constraint {}",
176 lower_bound_idx, upper_bound_idx);
180#define print_bound_constraint_global_infeasibility_error(...)
183#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
203template <
typename Scalar,
typename Rep,
typename Period = std::ratio<1>>
204void print_iteration_diagnostics(
int iterations, IterationType type,
205 const std::chrono::duration<Rep, Period>& time,
206 Scalar error, Scalar cost,
207 Scalar infeasibility, Scalar complementarity,
208 Scalar μ, Scalar δ, Scalar primal_α,
209 Scalar primal_α_max, Scalar α_reduction_factor,
211 if (iterations % 20 == 0) {
212 if (iterations == 0) {
218 "{:━^4}┯{:━^4}┯{:━^9}┯{:━^12}┯{:━^13}┯{:━^12}┯{:━^12}┯{:━^8}┯{:━^5}┯"
219 "{:━^8}┯{:━^8}┯{:━^2}",
220 "",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"");
221 if (iterations == 0) {
227 "┃{:^4}│{:^4}│{:^9}│{:^12}│{:^13}│{:^12}│{:^12}│{:^8}│{:^5}│{:^8}│{:^8}"
229 "iter",
"type",
"time (ms)",
"error",
"cost",
"infeas.",
"complement.",
230 "μ",
"reg",
"primal α",
"dual α",
"↩");
232 "┡{:━^4}┷{:━^4}┷{:━^9}┷{:━^12}┷{:━^13}┷{:━^12}┷{:━^12}┷{:━^8}┷{:━^5}┷"
233 "{:━^8}┷{:━^8}┷{:━^2}┩",
234 "",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"");
249 static_cast<int>(log(primal_α / primal_α_max) / log(α_reduction_factor));
251 constexpr std::array ITERATION_TYPES = {
"norm",
"✓SOC",
"XSOC"};
253 "│{:4} {:4} {:9.3f} {:12e} {:13e} {:12e} {:12e} {:.2e} {:<5} {:.2e} "
255 iterations, ITERATION_TYPES[std::to_underlying(type)], to_ms(time), error,
256 cost, infeasibility, complementarity, μ, power_of_10(δ), primal_α, dual_α,
260#define print_iteration_diagnostics(...)
263#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
267inline void print_bottom_iteration_diagnostics() {
268 slp::println(
"└{:─^108}┘",
"");
271#define print_bottom_iteration_diagnostics(...)
282std::string histogram(
double value) {
283 value = std::clamp(value, 0.0, 1.0);
286 int fpart =
static_cast<int>(std::modf(value * Width, &ipart) * 8);
288 constexpr std::array strs = {
" ",
"▏",
"▎",
"▍",
"▌",
"▋",
"▊",
"▉",
"█"};
292 while (index < ipart) {
300 while (index < Width) {
308#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
314inline void print_solver_diagnostics(
315 const gch::small_vector<SolveProfiler>& solve_profilers) {
316 auto solve_duration = to_ms(solve_profilers[0].total_duration());
318 slp::println(
"┏{:━^23}┯{:━^18}┯{:━^10}┯{:━^9}┯{:━^4}┓",
"",
"",
"",
"",
"");
319 slp::println(
"┃{:^23}│{:^18}│{:^10}│{:^9}│{:^4}┃",
"solver trace",
"percent",
320 "total (ms)",
"each (ms)",
"runs");
321 slp::println(
"┡{:━^23}┷{:━^18}┷{:━^10}┷{:━^9}┷{:━^4}┩",
"",
"",
"",
"",
"");
323 for (
auto& profiler : solve_profilers) {
324 double norm = solve_duration == 0.0
325 ? (&profiler == &solve_profilers[0] ? 1.0 : 0.0)
326 : to_ms(profiler.total_duration()) / solve_duration;
327 slp::println(
"│{:<23} {:>6.2f}%▕{}▏ {:>10.3f} {:>9.3f} {:>4}│",
328 profiler.name(), norm * 100.0, histogram<9>(norm),
329 to_ms(profiler.total_duration()),
330 to_ms(profiler.average_duration()), profiler.num_solves());
333 slp::println(
"└{:─^68}┘",
"");
336#define print_solver_diagnostics(...)
339#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
345inline void print_autodiff_diagnostics(
346 const gch::small_vector<SetupProfiler>& setup_profilers) {
347 auto setup_duration = to_ms(setup_profilers[0].duration());
350 slp::println(
"┏{:━^23}┯{:━^18}┯{:━^10}┯{:━^9}┯{:━^4}┓",
"",
"",
"",
"",
"");
351 slp::println(
"┃{:^23}│{:^18}│{:^10}│{:^9}│{:^4}┃",
"autodiff trace",
352 "percent",
"total (ms)",
"each (ms)",
"runs");
353 slp::println(
"┡{:━^23}┷{:━^18}┷{:━^10}┷{:━^9}┷{:━^4}┩",
"",
"",
"",
"",
"");
356 for (
auto& profiler : setup_profilers) {
357 double norm = setup_duration == 0.0
358 ? (&profiler == &setup_profilers[0] ? 1.0 : 0.0)
359 : to_ms(profiler.duration()) / setup_duration;
360 slp::println(
"│{:<23} {:>6.2f}%▕{}▏ {:>10.3f} {:>9.3f} {:>4}│",
361 profiler.name(), norm * 100.0, histogram<9>(norm),
362 to_ms(profiler.duration()), to_ms(profiler.duration()),
"1");
365 slp::println(
"└{:─^68}┘",
"");
368#define print_autodiff_diagnostics(...)