16#include <gch/small_vector.hpp>
18#include "sleipnir/util/print.hpp"
19#include "sleipnir/util/profiler.hpp"
24enum class IterationType : uint8_t {
28 SECOND_ORDER_CORRECTION,
30 FEASIBILITY_RESTORATION
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
191template <
typename Scalar,
typename Rep,
typename Period = std::ratio<1>>
192void print_iteration_diagnostics(
int iterations, IterationType type,
193 const std::chrono::duration<Rep, Period>& time,
194 Scalar error, Scalar cost,
195 Scalar infeasibility, Scalar complementarity,
196 Scalar μ, Scalar δ, Scalar γ,
197 Scalar full_primal_step_inf_norm,
198 Scalar full_dual_step_inf_norm,
199 Scalar primal_α, Scalar primal_α_max,
200 Scalar α_reduction_factor, Scalar dual_α) {
201 if (iterations % 20 == 0) {
202 if (iterations == 0) {
203 slp::println(
"┏{:━^119}┓",
"");
205 slp::println(
"┢{:━^119}┪",
"");
208 "┃{:^4} {:^9} {:^10} {:^11} {:^10} {:^8} {:^8} {:^5} {:^5} {:^8} "
209 "{:^8} {:^8} {:^8} {:^2}┃",
210 "iter",
"duration",
"error",
"cost",
"infeas.",
"complem.",
"μ",
"δ",
211 "γ",
"|p_pr|",
"|p_du|",
"α_pr",
"α_du",
"↩");
212 slp::println(
"┡{:━^119}┩",
"");
227 static_cast<int>(log(primal_α / primal_α_max) / log(α_reduction_factor));
229 constexpr std::array ITERATION_TYPES{
" ",
"s",
"r"};
231 "│{:4} {:1} {:9.3f} {:.4e} {:11.4e} {:.4e} {:.2e} {:.2e} {:<5} {:<5} "
232 "{:.2e} {:.2e} {:.2e} {:.2e} {:2d}│",
233 iterations, ITERATION_TYPES[std::to_underlying(type)], to_ms(time), error,
234 cost, infeasibility, complementarity, μ, power_of_10(δ), power_of_10(γ),
235 full_primal_step_inf_norm, full_dual_step_inf_norm, primal_α, dual_α,
239#define print_iteration_diagnostics(...)
242#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
244inline void print_bottom_iteration_diagnostics() {
245 slp::println(
"└{:─^119}┘",
"");
248#define print_bottom_iteration_diagnostics(...)
257std::string histogram(
double value) {
258 value = std::clamp(value, 0.0, 1.0);
261 int fpart =
static_cast<int>(std::modf(value * Width, &ipart) * 8);
263 constexpr std::array strs{
" ",
"▏",
"▎",
"▍",
"▌",
"▋",
"▊",
"▉",
"█"};
267 while (index < ipart) {
275 while (index < Width) {
283#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
287inline void print_solver_diagnostics(
288 const gch::small_vector<SolveProfiler>& solve_profilers) {
289 auto solve_duration = to_ms(solve_profilers[0].total_duration());
291 slp::println(
"┏{:━^66}┓",
"");
292 slp::println(
"┃{:^21} {:^18} {:^10} {:^9} {:^4}┃",
"time trace",
"percentage",
293 "total",
"each",
"runs");
294 slp::println(
"┡{:━^66}┩",
"");
296 for (
auto& profiler : solve_profilers) {
297 double norm = solve_duration == 0.0
298 ? (&profiler == &solve_profilers[0] ? 1.0 : 0.0)
299 : to_ms(profiler.total_duration()) / solve_duration;
300 slp::println(
"│{:<21} {:>6.2f}%▕{}▏ {:>10.3f} {:>9.3f} {:>4}│",
301 profiler.name(), norm * 100.0, histogram<9>(norm),
302 to_ms(profiler.total_duration()),
303 to_ms(profiler.average_duration()), profiler.num_solves());
306 slp::println(
"└{:─^66}┘",
"");
309#define print_solver_diagnostics(...)
312#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
316inline void print_setup_diagnostics(
317 const gch::small_vector<SetupProfiler>& setup_profilers) {
318 auto setup_duration = to_ms(setup_profilers[0].duration());
322 "See https://sleipnirgroup.github.io/Sleipnir/md_usage.html#output for "
323 "diagnostic output description.\n");
326 slp::println(
"┏{:━^50}┓",
"");
327 slp::println(
"┃{:^21} {:^18} {:^9}┃",
"time trace",
"percentage",
"duration");
328 slp::println(
"┡{:━^50}┩",
"");
331 for (
auto& profiler : setup_profilers) {
332 double norm = setup_duration == 0.0
333 ? (&profiler == &setup_profilers[0] ? 1.0 : 0.0)
334 : to_ms(profiler.duration()) / setup_duration;
335 slp::println(
"│{:<21} {:>6.2f}%▕{}▏ {:>9.3f}│", profiler.name(),
336 norm * 100.0, histogram<9>(norm), to_ms(profiler.duration()));
339 slp::println(
"└{:─^50}┘",
"");
342#define print_setup_diagnostics(...)