GCC Code Coverage Report


Directory: ./
File: src/core/utils/callbacks.cpp
Date: 2025-03-26 19:23:43
Exec Total Coverage
Lines: 82 190 43.2%
Branches: 148 892 16.6%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2025, LAAS-CNRS, University of Edinburgh,
5 // University of Oxford, Heriot-Watt University
6 // Copyright note valid unless otherwise stated in individual files.
7 // All rights reserved.
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "crocoddyl/core/utils/callbacks.hpp"
11
12 namespace crocoddyl {
13
14 26 CallbackVerbose::CallbackVerbose(VerboseLevel level, int precision)
15 : CallbackAbstract(),
16 26 level_(level),
17
1/2
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
26 separator_(" "),
18
1/2
✓ Branch 4 taken 26 times.
✗ Branch 5 not taken.
52 separator_short_(" ") {
19
1/2
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
26 set_precision(precision);
20 26 }
21
22 56 CallbackVerbose::~CallbackVerbose() {}
23
24 VerboseLevel CallbackVerbose::get_level() const { return level_; }
25
26 void CallbackVerbose::set_level(VerboseLevel level) {
27 level_ = level;
28 update_header();
29 }
30
31 int CallbackVerbose::get_precision() const { return precision_; }
32
33 26 void CallbackVerbose::set_precision(int precision) {
34
1/10
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
26 if (precision < 0) throw_pretty("The precision needs to be at least 0.");
35 26 precision_ = precision;
36 26 update_header();
37 26 }
38
39 26 void CallbackVerbose::update_header() {
40 364 auto center_string = [](const std::string& str, int width,
41 bool right_padding = true) {
42 364 const int padding_size = width - static_cast<int>(str.length());
43
2/2
✓ Branch 0 taken 286 times.
✓ Branch 1 taken 78 times.
364 const int padding_left = padding_size > 0 ? padding_size / 2 : 0;
44 364 const int padding_right =
45 364 padding_size % 2 != 0
46
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 130 times.
364 ? padding_left + 1
47 : padding_left; // If the padding is odd, add additional space
48
2/2
✓ Branch 0 taken 338 times.
✓ Branch 1 taken 26 times.
364 if (right_padding) {
49
2/4
✓ Branch 2 taken 338 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 338 times.
✗ Branch 6 not taken.
676 return std::string(padding_left, ' ') + str +
50
2/4
✓ Branch 2 taken 338 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 338 times.
✗ Branch 6 not taken.
1014 std::string(padding_right, ' ');
51 } else {
52
2/4
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
52 return std::string(padding_left, ' ') + str;
53 }
54 };
55
56 26 header_.clear();
57 // Scientific mode requires a column width of 6 + precision
58 26 const int columnwidth = 6 + precision_;
59
2/4
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 26 times.
✗ Branch 5 not taken.
26 header_ += "iter" + separator_;
60
1/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
✗ Branch 5 not taken.
26 switch (level_) {
61 case _0: {
62 header_ += center_string("cost", columnwidth) + separator_;
63 header_ += center_string("stop", columnwidth) + separator_;
64 header_ += center_string("|grad|", columnwidth) + separator_;
65 header_ += center_string("preg", columnwidth) + separator_;
66 header_ += center_string("step", 6) + separator_;
67 header_ += center_string("dV-exp", columnwidth) + separator_;
68 header_ += center_string("dV", columnwidth, false);
69 break;
70 }
71 case _1: {
72 header_ += center_string("cost", columnwidth) + separator_;
73 header_ += center_string("merit", columnwidth) + separator_;
74 header_ += center_string("stop", columnwidth) + separator_;
75 header_ += center_string("|grad|", columnwidth) + separator_;
76 header_ += center_string("preg", columnwidth) + separator_;
77 header_ += center_string("step", 6) + separator_;
78 header_ += center_string("||ffeas||", columnwidth) + separator_;
79 header_ += center_string("dV-exp", columnwidth) + separator_;
80 header_ += center_string("dV", columnwidth, false);
81 break;
82 }
83 case _2: {
84 header_ += center_string("cost", columnwidth) + separator_;
85 header_ += center_string("merit", columnwidth) + separator_;
86 header_ += center_string("stop", columnwidth) + separator_;
87 header_ += center_string("|grad|", columnwidth) + separator_;
88 header_ += center_string("preg", columnwidth) + separator_;
89 header_ += center_string("dreg", columnwidth) + separator_;
90 header_ += center_string("step", 6) + separator_;
91 header_ += center_string("||ffeas||", columnwidth) + separator_;
92 header_ += center_string("dV-exp", columnwidth) + separator_;
93 header_ += center_string("dV", columnwidth, false);
94 break;
95 }
96 case _3: {
97 header_ += center_string("cost", columnwidth) + separator_;
98 header_ += center_string("merit", columnwidth) + separator_;
99 header_ += center_string("stop", columnwidth) + separator_;
100 header_ += center_string("|grad|", columnwidth) + separator_;
101 header_ += center_string("preg", columnwidth) + separator_;
102 header_ += center_string("dreg", columnwidth) + separator_;
103 header_ += center_string("step", 6) + separator_;
104 header_ += center_string("||ffeas||", columnwidth) + separator_;
105 header_ += center_string("||gfeas||", columnwidth) + separator_;
106 header_ += center_string("||hfeas||", columnwidth) + separator_;
107 header_ += center_string("dV-exp", columnwidth) + separator_;
108 header_ += center_string("dV", columnwidth, false);
109 break;
110 }
111 26 case _4: {
112
4/8
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
26 header_ += center_string("cost", columnwidth) + separator_;
113
4/8
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
26 header_ += center_string("merit", columnwidth) + separator_;
114
4/8
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
26 header_ += center_string("stop", columnwidth) + separator_;
115
4/8
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
26 header_ += center_string("|grad|", columnwidth) + separator_;
116
4/8
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
26 header_ += center_string("preg", columnwidth) + separator_;
117
4/8
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
26 header_ += center_string("dreg", columnwidth) + separator_;
118
4/8
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
26 header_ += center_string("step", 6) + separator_;
119
4/8
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
26 header_ += center_string("||ffeas||", columnwidth) + separator_;
120
4/8
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
26 header_ += center_string("||gfeas||", columnwidth) + separator_;
121
4/8
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
26 header_ += center_string("||hfeas||", columnwidth) + separator_;
122
4/8
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
26 header_ += center_string("dV-exp", columnwidth) + separator_;
123
4/8
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
26 header_ += center_string("dV", columnwidth) + separator_;
124
4/8
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
26 header_ += center_string("dPhi-exp", columnwidth) + separator_;
125
3/6
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 26 times.
✗ Branch 9 not taken.
26 header_ += center_string("dPhi", columnwidth, false);
126 26 break;
127 }
128 26 default: {
129 }
130 }
131 26 }
132
133 106 void CallbackVerbose::operator()(SolverAbstract& solver) {
134
3/4
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 45 times.
✓ Branch 4 taken 61 times.
106 if (solver.get_iter() % 10 == 0) {
135
2/4
✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 45 times.
✗ Branch 5 not taken.
45 std::cout << header_ << std::endl;
136 }
137 1060 auto space_sign = [this](const double value) {
138
1/2
✓ Branch 1 taken 530 times.
✗ Branch 2 not taken.
530 std::stringstream stream;
139
2/2
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 67 times.
530 if (value >= 0.) {
140
1/2
✓ Branch 1 taken 463 times.
✗ Branch 2 not taken.
463 stream << " ";
141 } else {
142
1/2
✓ Branch 1 taken 67 times.
✗ Branch 2 not taken.
67 stream << "-";
143 }
144
3/6
✓ Branch 1 taken 530 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 530 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 530 times.
✗ Branch 10 not taken.
530 stream << std::scientific << std::setprecision(precision_) << abs(value);
145
1/2
✓ Branch 1 taken 530 times.
✗ Branch 2 not taken.
1060 return stream.str();
146 530 };
147
148
4/8
✓ Branch 2 taken 106 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 106 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 106 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 106 times.
✗ Branch 12 not taken.
106 std::cout << std::setw(4) << solver.get_iter() << separator_; // iter
149
1/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
106 switch (level_) {
150 case _0: {
151 std::cout << std::scientific << std::setprecision(precision_)
152 << solver.get_cost() << separator_; // cost
153 std::cout << solver.get_stop() << separator_; // stop
154 std::cout << abs(solver.get_d()[0]) << separator_; // grad
155 std::cout << solver.get_preg() << separator_; // preg
156 std::cout << std::fixed << std::setprecision(4) << solver.get_steplength()
157 << separator_short_; // step
158 std::cout << space_sign(solver.get_dVexp()) << separator_short_; // dVexp
159 std::cout << space_sign(solver.get_dV()); // dV
160 break;
161 }
162 case _1: {
163 std::cout << std::scientific << std::setprecision(precision_)
164 << solver.get_cost() << separator_short_; // cost
165 std::cout << space_sign(solver.get_merit()) << separator_; // merit
166 std::cout << solver.get_stop() << separator_; // stop
167 std::cout << abs(solver.get_d()[0]) << separator_; // grad
168 std::cout << solver.get_preg() << separator_; // preg
169 std::cout << std::fixed << std::setprecision(4) << solver.get_steplength()
170 << separator_; // step
171 std::cout << std::scientific << std::setprecision(precision_)
172 << solver.get_ffeas() << separator_short_; // ffeas
173 std::cout << space_sign(solver.get_dVexp()) << separator_short_; // dVexp
174 std::cout << space_sign(solver.get_dV()); // dV
175 break;
176 }
177 case _2: {
178 std::cout << std::scientific << std::setprecision(precision_)
179 << solver.get_cost() << separator_short_; // cost
180 std::cout << space_sign(solver.get_merit()) << separator_; // merit
181 std::cout << solver.get_stop() << separator_; // stop
182 std::cout << abs(solver.get_d()[0]) << separator_; // grad
183 std::cout << solver.get_preg() << separator_; // preg
184 std::cout << solver.get_dreg() << separator_; // dreg
185 std::cout << std::fixed << std::setprecision(4) << solver.get_steplength()
186 << separator_; // step
187 std::cout << std::scientific << std::setprecision(precision_)
188 << solver.get_ffeas() << separator_short_; // ffeas
189 std::cout << space_sign(solver.get_dVexp()) << separator_short_; // dVexp
190 std::cout << space_sign(solver.get_dV()); // dV
191 break;
192 }
193 case _3: {
194 std::cout << std::scientific << std::setprecision(precision_)
195 << solver.get_cost() << separator_short_; // cost
196 std::cout << space_sign(solver.get_merit()) << separator_; // merit
197 std::cout << solver.get_stop() << separator_; // stop
198 std::cout << abs(solver.get_d()[0]) << separator_; // grad
199 std::cout << solver.get_preg() << separator_; // preg
200 std::cout << solver.get_dreg() << separator_; // dreg
201 std::cout << std::fixed << std::setprecision(4) << solver.get_steplength()
202 << separator_; // step
203 std::cout << std::scientific << std::setprecision(precision_)
204 << solver.get_ffeas() << separator_; // ffeas
205 std::cout << solver.get_gfeas() << separator_; // gfeas
206 std::cout << solver.get_hfeas() << separator_short_; // hfeas
207 std::cout << space_sign(solver.get_dVexp()) << separator_short_; // dVexp
208 std::cout << space_sign(solver.get_dV()); // dV
209 break;
210 }
211 106 case _4: {
212
2/4
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 106 times.
✗ Branch 6 not taken.
106 std::cout << std::scientific << std::setprecision(precision_)
213
3/6
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 106 times.
✗ Branch 8 not taken.
106 << solver.get_cost() << separator_short_; // cost
214
4/8
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 106 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 106 times.
✗ Branch 11 not taken.
106 std::cout << space_sign(solver.get_merit()) << separator_; // merit
215
3/6
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 106 times.
✗ Branch 8 not taken.
106 std::cout << solver.get_stop() << separator_; // stop
216
4/8
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 106 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 106 times.
✗ Branch 12 not taken.
106 std::cout << abs(solver.get_d()[0]) << separator_; // grad
217
3/6
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 106 times.
✗ Branch 8 not taken.
106 std::cout << solver.get_preg() << separator_; // preg
218
3/6
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 106 times.
✗ Branch 8 not taken.
106 std::cout << solver.get_dreg() << separator_; // dreg
219
4/8
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 106 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 106 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 106 times.
✗ Branch 12 not taken.
106 std::cout << std::fixed << std::setprecision(4) << solver.get_steplength()
220
1/2
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
106 << separator_; // step
221
2/4
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 106 times.
✗ Branch 6 not taken.
106 std::cout << std::scientific << std::setprecision(precision_)
222
3/6
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 106 times.
✗ Branch 8 not taken.
106 << solver.get_ffeas() << separator_; // ffeas
223
3/6
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 106 times.
✗ Branch 8 not taken.
106 std::cout << solver.get_gfeas() << separator_; // gfeas
224
3/6
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 106 times.
✗ Branch 8 not taken.
106 std::cout << solver.get_hfeas() << separator_short_; // hfeas
225
4/8
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 106 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 106 times.
✗ Branch 11 not taken.
106 std::cout << space_sign(solver.get_dVexp()) << separator_short_; // dVexp
226
4/8
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 106 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 106 times.
✗ Branch 11 not taken.
106 std::cout << space_sign(solver.get_dV()) << separator_short_; // dV
227
2/4
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
212 std::cout << space_sign(solver.get_dPhiexp())
228
2/4
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
106 << separator_short_; // dPhiexp
229
3/6
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 106 times.
✗ Branch 8 not taken.
106 std::cout << space_sign(solver.get_dPhi()); // dPhi
230 106 break;
231 }
232 106 default: {
233 }
234 }
235
1/2
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
106 std::cout << std::endl;
236
1/2
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
106 std::cout << std::flush;
237 106 }
238
239 } // namespace crocoddyl
240