38template <
typename Rep,
typename Period = std::ratio<1>>
39constexpr double to_ms(
const std::chrono::duration<Rep, Period>& duration) {
40 using std::chrono::duration_cast;
41 using std::chrono::microseconds;
42 return duration_cast<microseconds>(duration).count() / 1e3;
49template <
typename Scalar>
51 if (value == Scalar(0)) {
55 int exponent =
static_cast<int>(
log10(value));
59 }
else if (exponent == 1) {
63 int n = std::abs(exponent);
66 digits.emplace_back(n % 10);
70 std::string output =
"10";
76 constexpr std::array strs = {
"⁰",
"¹",
"²",
"³",
"⁴",
77 "⁵",
"⁶",
"⁷",
"⁸",
"⁹"};
78 for (
const auto& digit : digits | std::views::reverse) {
79 output += strs[digit];
87#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
93template <
typename Scalar>
95 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_e) {
96 slp::println(
"The problem has too few degrees of freedom.");
97 slp::println(
"Violated constraints (cₑ(x) = 0) in order of declaration:");
98 for (
int row = 0; row < c_e.rows(); ++row) {
99 if (c_e[row] < Scalar(0)) {
100 slp::println(
" {}/{}: {} = 0", row + 1, c_e.rows(), c_e[row]);
105#define print_too_few_dofs_error(...)
108#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
114template <
typename Scalar>
116 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_e) {
118 "The problem is locally infeasible due to violated equality "
120 slp::println(
"Violated constraints (cₑ(x) = 0) in order of declaration:");
121 for (
int row = 0; row < c_e.rows(); ++row) {
122 if (c_e[row] < Scalar(0)) {
123 slp::println(
" {}/{}: {} = 0", row + 1, c_e.rows(), c_e[row]);
128#define print_c_e_local_infeasibility_error(...)
131#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
137template <
typename Scalar>
139 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_i) {
141 "The problem is locally infeasible due to violated inequality "
143 slp::println(
"Violated constraints (cᵢ(x) ≥ 0) in order of declaration:");
144 for (
int row = 0; row < c_i.rows(); ++row) {
145 if (c_i[row] < Scalar(0)) {
146 slp::println(
" {}/{}: {} ≥ 0", row + 1, c_i.rows(), c_i[row]);
151#define print_c_i_local_infeasibility_error(...)
154#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
156 const std::span<
const std::pair<Eigen::Index, Eigen::Index>>
157 conflicting_lower_upper_bound_indices) {
159 "The problem is globally infeasible due to conflicting bound "
161 for (
const auto& [lower_bound_idx, upper_bound_idx] :
162 conflicting_lower_upper_bound_indices) {
164 " Inequality constraint {} gives a lower bound that is greater than "
165 "the upper bound given by inequality constraint {}",
166 lower_bound_idx, upper_bound_idx);
170#define print_bound_constraint_global_infeasibility_error(...)
173#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
191template <
typename Scalar,
typename Rep,
typename Period = std::ratio<1>>
193 const std::chrono::duration<Rep, Period>& time,
194 Scalar error, Scalar cost,
195 Scalar infeasibility, Scalar complementarity,
196 Scalar μ, Scalar δ, Scalar primal_α,
197 Scalar primal_α_max, Scalar α_reduction_factor,
199 if (iterations % 20 == 0) {
200 if (iterations == 0) {
206 "{:━^4}┯{:━^4}┯{:━^9}┯{:━^12}┯{:━^13}┯{:━^12}┯{:━^12}┯{:━^8}┯{:━^5}┯"
207 "{:━^8}┯{:━^8}┯{:━^2}",
208 "",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"");
209 if (iterations == 0) {
215 "┃{:^4}│{:^4}│{:^9}│{:^12}│{:^13}│{:^12}│{:^12}│{:^8}│{:^5}│{:^8}│{:^8}"
217 "iter",
"type",
"time (ms)",
"error",
"cost",
"infeas.",
"complement.",
218 "μ",
"reg",
"primal α",
"dual α",
"↩");
220 "┡{:━^4}┷{:━^4}┷{:━^9}┷{:━^12}┷{:━^13}┷{:━^12}┷{:━^12}┷{:━^8}┷{:━^5}┷"
221 "{:━^8}┷{:━^8}┷{:━^2}┩",
222 "",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"");
237 static_cast<int>(
log(primal_α / primal_α_max) /
log(α_reduction_factor));
239 constexpr std::array ITERATION_TYPES = {
"norm",
"✓SOC",
"XSOC",
"rest"};
241 "│{:4} {:4} {:9.3f} {:12e} {:13e} {:12e} {:12e} {:.2e} {:<5} {:.2e} "
244 cost, infeasibility, complementarity, μ,
power_of_10(δ), primal_α, dual_α,
248#define print_iteration_diagnostics(...)
251#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
257#define print_bottom_iteration_diagnostics(...)
267 value = std::clamp(value, 0.0, 1.0);
270 int fpart =
static_cast<int>(std::modf(value * Width, &ipart) * 8);
272 constexpr std::array strs = {
" ",
"▏",
"▎",
"▍",
"▌",
"▋",
"▊",
"▉",
"█"};
276 while (
index < ipart) {
284 while (
index < Width) {
292#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
298 auto solve_duration =
to_ms(solve_profilers[0].total_duration());
300 slp::println(
"┏{:━^21}┯{:━^18}┯{:━^10}┯{:━^9}┯{:━^4}┓",
"",
"",
"",
"",
"");
301 slp::println(
"┃{:^21}│{:^18}│{:^10}│{:^9}│{:^4}┃",
"solver trace",
"percent",
302 "total (ms)",
"each (ms)",
"runs");
303 slp::println(
"┡{:━^21}┷{:━^18}┷{:━^10}┷{:━^9}┷{:━^4}┩",
"",
"",
"",
"",
"");
305 for (
auto& profiler : solve_profilers) {
306 double norm = solve_duration == 0.0
307 ? (&profiler == &solve_profilers[0] ? 1.0 : 0.0)
308 :
to_ms(profiler.total_duration()) / solve_duration;
309 slp::println(
"│{:<21} {:>6.2f}%▕{}▏ {:>10.3f} {:>9.3f} {:>4}│",
311 to_ms(profiler.total_duration()),
312 to_ms(profiler.average_duration()), profiler.num_solves());
318#define print_solver_diagnostics(...)
321#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
327 auto setup_duration =
to_ms(setup_profilers[0].duration());
330 slp::println(
"┏{:━^21}┯{:━^18}┯{:━^10}┯{:━^9}┯{:━^4}┓",
"",
"",
"",
"",
"");
331 slp::println(
"┃{:^21}│{:^18}│{:^10}│{:^9}│{:^4}┃",
"setup trace",
"percent",
332 "total (ms)",
"each (ms)",
"runs");
333 slp::println(
"┡{:━^21}┷{:━^18}┷{:━^10}┷{:━^9}┷{:━^4}┩",
"",
"",
"",
"",
"");
336 for (
auto& profiler : setup_profilers) {
337 double norm = setup_duration == 0.0
338 ? (&profiler == &setup_profilers[0] ? 1.0 : 0.0)
339 :
to_ms(profiler.duration()) / setup_duration;
340 slp::println(
"│{:<21} {:>6.2f}%▕{}▏ {:>10.3f} {:>9.3f} {:>4}│",
342 to_ms(profiler.duration()),
to_ms(profiler.duration()),
"1");
348#define print_setup_diagnostics(...)
@ index
Definition base.h:690
wpi::util::SmallVector< T > small_vector
Definition small_vector.hpp:10
Definition to_underlying.hpp:7
void print_iteration_diagnostics(int iterations, IterationType type, const std::chrono::duration< Rep, Period > &time, Scalar error, Scalar cost, Scalar infeasibility, Scalar complementarity, Scalar μ, Scalar δ, Scalar primal_α, Scalar primal_α_max, Scalar α_reduction_factor, Scalar dual_α)
Prints diagnostics for the current iteration.
Definition print_diagnostics.hpp:192
constexpr std::underlying_type_t< Enum > to_underlying(Enum e) noexcept
Definition to_underlying.hpp:10
Variable< Scalar > log10(const Variable< Scalar > &x)
log10() for Variables.
Definition variable.hpp:530
void print_c_i_local_infeasibility_error(const Eigen::Vector< Scalar, Eigen::Dynamic > &c_i)
Prints inequality constraint local infeasibility error.
Definition print_diagnostics.hpp:138
constexpr double to_ms(const std::chrono::duration< Rep, Period > &duration)
Converts std::chrono::duration to a number of milliseconds rounded to three decimals.
Definition print_diagnostics.hpp:39
void print_setup_diagnostics(const gch::small_vector< SetupProfiler > &setup_profilers)
Prints setup diagnostics.
Definition print_diagnostics.hpp:325
std::string histogram(double value)
Renders histogram of the given normalized value.
Definition print_diagnostics.hpp:266
IterationType
Iteration type.
Definition print_diagnostics.hpp:25
@ NORMAL
Normal iteration.
Definition print_diagnostics.hpp:27
@ ACCEPTED_SOC
Accepted second-order correction iteration.
Definition print_diagnostics.hpp:29
@ FEASIBILITY_RESTORATION
Feasibility restoration iteration.
Definition print_diagnostics.hpp:33
@ REJECTED_SOC
Rejected second-order correction iteration.
Definition print_diagnostics.hpp:31
void print_bound_constraint_global_infeasibility_error(const std::span< const std::pair< Eigen::Index, Eigen::Index > > conflicting_lower_upper_bound_indices)
Definition print_diagnostics.hpp:155
Variable< Scalar > log(const Variable< Scalar > &x)
log() for Variables.
Definition variable.hpp:521
std::string power_of_10(Scalar value)
Renders value as power of 10.
Definition print_diagnostics.hpp:50
void print_bottom_iteration_diagnostics()
Prints bottom of iteration diagnostics table.
Definition print_diagnostics.hpp:253
void println(fmt::format_string< T... > fmt, T &&... args)
Wrapper around fmt::println() that squelches write failure exceptions.
Definition print.hpp:37
void print_solver_diagnostics(const gch::small_vector< SolveProfiler > &solve_profilers)
Prints solver diagnostics.
Definition print_diagnostics.hpp:296
void print_too_few_dofs_error(const Eigen::Vector< Scalar, Eigen::Dynamic > &c_e)
Prints error for too few degrees of freedom.
Definition print_diagnostics.hpp:94
void print(fmt::format_string< T... > fmt, T &&... args)
Wrapper around fmt::print() that squelches write failure exceptions.
Definition print.hpp:19
void print_c_e_local_infeasibility_error(const Eigen::Vector< Scalar, Eigen::Dynamic > &c_e)
Prints equality constraint local infeasibility error.
Definition print_diagnostics.hpp:115