GCC Code Coverage Report


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