10 #include "crocoddyl/core/utils/callbacks.hpp"
12 #include "crocoddyl/core/utils/exception.hpp"
16 CallbackVerbose::CallbackVerbose(VerboseLevel level,
int precision)
20 separator_short_(
" ") {
21 set_precision(precision);
24 CallbackVerbose::~CallbackVerbose() {}
26 VerboseLevel CallbackVerbose::get_level()
const {
return level_; }
28 void CallbackVerbose::set_level(VerboseLevel level) {
33 int CallbackVerbose::get_precision()
const {
return precision_; }
35 void CallbackVerbose::set_precision(
int precision) {
36 if (precision < 0) throw_pretty(
"The precision needs to be at least 0.");
37 precision_ = precision;
41 void CallbackVerbose::update_header() {
42 auto center_string = [](
const std::string& str,
int width,
43 bool right_padding =
true) {
44 const int padding_size = width -
static_cast<int>(str.length());
45 const int padding_left = padding_size > 0 ? padding_size / 2 : 0;
46 const int padding_right =
51 return std::string(padding_left,
' ') + str +
52 std::string(padding_right,
' ');
54 return std::string(padding_left,
' ') + str;
60 const int columnwidth = 6 + precision_;
61 header_ +=
"iter" + separator_;
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);
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);
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);
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);
114 header_ += center_string(
"cost", columnwidth) + separator_;
115 header_ += center_string(
"merit", columnwidth) + separator_;
116 header_ += center_string(
"stop", columnwidth) + separator_;
117 header_ += center_string(
"grad", columnwidth) + separator_;
118 header_ += center_string(
"preg", columnwidth) + separator_;
119 header_ += center_string(
"dreg", columnwidth) + separator_;
120 header_ += center_string(
"step", 6) + separator_;
121 header_ += center_string(
"||ffeas||", columnwidth) + separator_;
122 header_ += center_string(
"||gfeas||", columnwidth) + separator_;
123 header_ += center_string(
"||hfeas||", columnwidth) + separator_;
124 header_ += center_string(
"dV-exp", columnwidth) + separator_;
125 header_ += center_string(
"dV", columnwidth) + separator_;
126 header_ += center_string(
"dPhi-exp", columnwidth) + separator_;
127 header_ += center_string(
"dPhi", columnwidth,
false);
137 std::cout << header_ << std::endl;
139 auto space_sign = [
this](
const double value) {
140 std::stringstream stream;
146 stream << std::scientific << std::setprecision(precision_) << abs(value);
150 std::cout << std::setw(4) << solver.
get_iter() << separator_;
153 std::cout << std::scientific << std::setprecision(precision_)
155 std::cout << solver.
get_stop() << separator_;
156 std::cout << space_sign(-solver.
get_d()[1]) << separator_short_;
157 std::cout << solver.
get_preg() << separator_;
158 std::cout << std::fixed << std::setprecision(4) << solver.
get_steplength()
160 std::cout << space_sign(solver.
get_dVexp()) << separator_short_;
161 std::cout << space_sign(solver.
get_dV());
165 std::cout << std::scientific << std::setprecision(precision_)
166 << solver.
get_cost() << separator_short_;
167 std::cout << space_sign(solver.
get_merit()) << separator_;
168 std::cout << solver.
get_stop() << separator_short_;
169 std::cout << space_sign(-solver.
get_d()[1]) << separator_;
170 std::cout << solver.
get_preg() << separator_;
171 std::cout << std::fixed << std::setprecision(4) << solver.
get_steplength()
173 std::cout << std::scientific << std::setprecision(precision_)
174 << solver.
get_ffeas() << separator_short_;
175 std::cout << space_sign(solver.
get_dVexp()) << separator_short_;
176 std::cout << space_sign(solver.
get_dV());
180 std::cout << std::scientific << std::setprecision(precision_)
181 << solver.
get_cost() << separator_short_;
182 std::cout << space_sign(solver.
get_merit()) << separator_;
183 std::cout << solver.
get_stop() << separator_short_;
184 std::cout << space_sign(-solver.
get_d()[1]) << separator_;
185 std::cout << solver.
get_preg() << separator_;
186 std::cout << solver.
get_dreg() << separator_;
187 std::cout << std::fixed << std::setprecision(4) << solver.
get_steplength()
189 std::cout << std::scientific << std::setprecision(precision_)
190 << solver.
get_ffeas() << separator_short_;
191 std::cout << space_sign(solver.
get_dVexp()) << separator_short_;
192 std::cout << space_sign(solver.
get_dV());
196 std::cout << std::scientific << std::setprecision(precision_)
197 << solver.
get_cost() << separator_short_;
198 std::cout << space_sign(solver.
get_merit()) << separator_;
199 std::cout << solver.
get_stop() << separator_short_;
200 std::cout << space_sign(-solver.
get_d()[1]) << separator_;
201 std::cout << solver.
get_preg() << separator_;
202 std::cout << solver.
get_dreg() << separator_;
203 std::cout << std::fixed << std::setprecision(4) << solver.
get_steplength()
205 std::cout << std::scientific << std::setprecision(precision_)
207 std::cout << solver.
get_gfeas() << separator_;
208 std::cout << solver.
get_hfeas() << separator_short_;
209 std::cout << space_sign(solver.
get_dVexp()) << separator_short_;
210 std::cout << space_sign(solver.
get_dV());
214 std::cout << std::scientific << std::setprecision(precision_)
215 << solver.
get_cost() << separator_short_;
216 std::cout << space_sign(solver.
get_merit()) << separator_;
217 std::cout << solver.
get_stop() << separator_short_;
218 std::cout << space_sign(-solver.
get_d()[1]) << separator_;
219 std::cout << solver.
get_preg() << separator_;
220 std::cout << solver.
get_dreg() << separator_;
221 std::cout << std::fixed << std::setprecision(4) << solver.
get_steplength()
223 std::cout << std::scientific << std::setprecision(precision_)
225 std::cout << solver.
get_gfeas() << separator_;
226 std::cout << solver.
get_hfeas() << separator_short_;
227 std::cout << space_sign(solver.
get_dVexp()) << separator_short_;
228 std::cout << space_sign(solver.
get_dV()) << separator_short_;
231 std::cout << space_sign(solver.
get_dPhi());
237 std::cout << std::endl;
238 std::cout << std::flush;
Abstract class for optimal control solvers.
double get_cost() const
Return the cost for the current guess.
double get_dPhi() const
Return the reduction in the merit function .
std::size_t get_iter() const
Return the number of iterations performed by the solver.
double get_hfeas() const
Return the equality feasibility for the current guess.
double get_dVexp() const
Return the expected reduction in the cost function .
double get_dPhiexp() const
Return the expected reduction in the merit function .
double get_steplength() const
Return the step length .
double get_merit() const
Return the merit for the current guess.
double get_preg() const
Return the primal-variable regularization.
const Eigen::Vector2d & get_d() const
Return the linear and quadratic terms of the expected improvement.
double get_ffeas() const
Return the dynamic feasibility for the current guess.
double get_gfeas() const
Return the inequality feasibility for the current guess.
double get_stop() const
Return the stopping-criteria value computed by stoppingCriteria()
double get_dV() const
Return the reduction in the cost function .
double get_dreg() const
Return the dual-variable regularization.