18#include <gch/small_vector.hpp>
20#include "sleipnir/util/print.hpp"
21#include "sleipnir/util/profiler.hpp"
26enum class IterationType : uint8_t {
30 SECOND_ORDER_CORRECTION,
32 FEASIBILITY_RESTORATION
37template <
typename Rep,
typename Period = std::ratio<1>>
38constexpr double to_ms(
const std::chrono::duration<Rep, Period>& duration) {
39 using std::chrono::duration_cast;
40 using std::chrono::microseconds;
41 return duration_cast<microseconds>(duration).count() / 1e3;
48template <
typename Scalar>
49std::string power_of_10(Scalar value) {
50 if (value == Scalar(0)) {
54 int exponent =
static_cast<int>(log10(value));
58 }
else if (exponent == 1) {
62 int n = std::abs(exponent);
63 gch::small_vector<int> digits;
65 digits.emplace_back(n % 10);
69 std::string output =
"10";
75 constexpr std::array strs{
"⁰",
"¹",
"²",
"³",
"⁴",
76 "⁵",
"⁶",
"⁷",
"⁸",
"⁹"};
77 for (
const auto& digit : digits | std::views::reverse) {
78 output += strs[digit];
86#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
92template <
typename Scalar>
93void print_too_few_dofs_error(
94 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_e) {
95 slp::println(
"The problem has too few degrees of freedom.");
96 slp::println(
"Violated constraints (cₑ(x) = 0) in order of declaration:");
97 for (
int row = 0; row < c_e.rows(); ++row) {
98 if (c_e[row] < Scalar(0)) {
99 slp::println(
" {}/{}: {} = 0", row + 1, c_e.rows(), c_e[row]);
104#define print_too_few_dofs_error(...)
107#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
113template <
typename Scalar>
114void print_c_e_local_infeasibility_error(
115 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_e) {
117 "The problem is locally infeasible due to violated equality "
119 slp::println(
"Violated constraints (cₑ(x) = 0) in order of declaration:");
120 for (
int row = 0; row < c_e.rows(); ++row) {
121 if (c_e[row] < Scalar(0)) {
122 slp::println(
" {}/{}: {} = 0", row + 1, c_e.rows(), c_e[row]);
127#define print_c_e_local_infeasibility_error(...)
130#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
136template <
typename Scalar>
137void print_c_i_local_infeasibility_error(
138 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_i) {
140 "The problem is locally infeasible due to violated inequality "
142 slp::println(
"Violated constraints (cᵢ(x) ≥ 0) in order of declaration:");
143 for (
int row = 0; row < c_i.rows(); ++row) {
144 if (c_i[row] < Scalar(0)) {
145 slp::println(
" {}/{}: {} ≥ 0", row + 1, c_i.rows(), c_i[row]);
150#define print_c_i_local_infeasibility_error(...)
153#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
154inline void print_bound_constraint_global_infeasibility_error(
155 const std::span<
const std::pair<Eigen::Index, Eigen::Index>>
156 conflicting_lower_upper_bound_indices) {
158 "The problem is globally infeasible due to conflicting bound "
160 for (
const auto& [lower_bound_idx, upper_bound_idx] :
161 conflicting_lower_upper_bound_indices) {
163 " Inequality constraint {} gives a lower bound that is greater than "
164 "the upper bound given by inequality constraint {}",
165 lower_bound_idx, upper_bound_idx);
169#define print_bound_constraint_global_infeasibility_error(...)
172#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
193template <
typename Scalar,
typename Rep,
typename Period = std::ratio<1>>
194void print_iteration_diagnostics(
int iterations, IterationType type,
195 const std::chrono::duration<Rep, Period>& time,
196 Scalar error, Scalar cost,
197 Scalar infeasibility, Scalar complementarity,
198 Scalar μ, Scalar δ, Scalar γ,
199 Scalar full_primal_step_inf_norm,
200 Scalar full_dual_step_inf_norm,
201 Scalar primal_α, Scalar primal_α_max,
202 Scalar α_reduction_factor, Scalar dual_α) {
203 if (iterations % 20 == 0) {
204 if (iterations == 0) {
205 slp::println(
"┏{:━^119}┓",
"");
207 slp::println(
"┢{:━^119}┪",
"");
210 "┃{:^4} {:^9} {:^10} {:^11} {:^10} {:^8} {:^8} {:^5} {:^5} {:^8} "
211 "{:^8} {:^8} {:^8} {:^2}┃",
212 "iter",
"duration",
"error",
"cost",
"infeas.",
"complem.",
"μ",
"δ",
213 "γ",
"|p_pr|",
"|p_du|",
"α_pr",
"α_du",
"↩");
214 slp::println(
"┡{:━^119}┩",
"");
229 static_cast<int>(log(primal_α / primal_α_max) / log(α_reduction_factor));
231 constexpr std::array ITERATION_TYPES{
" ",
"s",
"r"};
233 "│{:4} {:1} {:9.3f} {:10.4e} {:11.4e} {:10.4e} {:8.2e} {:8.2e} {:<5} "
234 "{:<5} {:8.2e} {:8.2e} {:8.2e} {:8.2e} {:2d}│",
235 iterations, ITERATION_TYPES[std::to_underlying(type)], to_ms(time), error,
236 cost, infeasibility, complementarity, μ, power_of_10(δ), power_of_10(γ),
237 full_primal_step_inf_norm, full_dual_step_inf_norm, primal_α, dual_α,
241#define print_iteration_diagnostics(...)
244#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
246inline void print_bottom_iteration_diagnostics() {
247 slp::println(
"└{:─^119}┘",
"");
250#define print_bottom_iteration_diagnostics(...)
259std::string histogram(
double value) {
260 value = std::clamp(value, 0.0, 1.0);
263 int fpart =
static_cast<int>(std::modf(value * Width, &ipart) * 8);
265 constexpr std::array strs{
" ",
"▏",
"▎",
"▍",
"▌",
"▋",
"▊",
"▉",
"█"};
269 while (index < ipart) {
277 while (index < Width) {
285#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
289inline void print_solver_diagnostics(
290 const gch::small_vector<SolveProfiler>& solve_profilers) {
291 auto solve_duration = to_ms(solve_profilers[0].total_duration());
293 slp::println(
"┏{:━^66}┓",
"");
294 slp::println(
"┃{:^21} {:^18} {:^10} {:^9} {:^4}┃",
"time trace",
"percentage",
295 "total",
"each",
"runs");
296 slp::println(
"┡{:━^66}┩",
"");
298 for (
auto& profiler : solve_profilers) {
299 double norm = solve_duration == 0.0
300 ? (&profiler == &solve_profilers[0] ? 1.0 : 0.0)
301 : to_ms(profiler.total_duration()) / solve_duration;
302 slp::println(
"│{:<21} {:>6.2f}%▕{}▏ {:>10.3f} {:>9.3f} {:>4}│",
303 profiler.name(), norm * 100.0, histogram<9>(norm),
304 to_ms(profiler.total_duration()),
305 to_ms(profiler.average_duration()), profiler.num_solves());
308 slp::println(
"└{:─^66}┘",
"");
311#define print_solver_diagnostics(...)
314#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
318inline void print_setup_diagnostics(
319 const gch::small_vector<SetupProfiler>& setup_profilers) {
320 auto setup_duration = to_ms(setup_profilers[0].duration());
324 "See https://sleipnirgroup.github.io/Sleipnir/md_usage.html#output for "
325 "diagnostic output description.\n");
328 slp::println(
"┏{:━^50}┓",
"");
329 slp::println(
"┃{:^21} {:^18} {:^9}┃",
"time trace",
"percentage",
"duration");
330 slp::println(
"┡{:━^50}┩",
"");
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(
"│{:<21} {:>6.2f}%▕{}▏ {:>9.3f}│", profiler.name(),
338 norm * 100.0, histogram<9>(norm), to_ms(profiler.duration()));
341 slp::println(
"└{:─^50}┘",
"");
344#define print_setup_diagnostics(...)