10 #include "crocoddyl/core/utils/callbacks.hpp"
14 CallbackVerbose::CallbackVerbose(VerboseLevel level,
int precision)
18 separator_short_(
" ") {
19 set_precision(precision);
22 CallbackVerbose::~CallbackVerbose() {}
24 VerboseLevel CallbackVerbose::get_level()
const {
return level_; }
26 void CallbackVerbose::set_level(VerboseLevel level) {
31 int CallbackVerbose::get_precision()
const {
return precision_; }
33 void CallbackVerbose::set_precision(
int precision) {
34 if (precision < 0) throw_pretty(
"The precision needs to be at least 0.");
35 precision_ = precision;
39 void CallbackVerbose::update_header() {
40 auto center_string = [](
const std::string& str,
int width,
41 bool right_padding =
true) {
42 const int padding_size = width -
static_cast<int>(str.length());
43 const int padding_left = padding_size > 0 ? padding_size / 2 : 0;
44 const int padding_right =
49 return std::string(padding_left,
' ') + str +
50 std::string(padding_right,
' ');
52 return std::string(padding_left,
' ') + str;
58 const int columnwidth = 6 + precision_;
59 header_ +=
"iter" + separator_;
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);
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);
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);
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);
112 header_ += center_string(
"cost", columnwidth) + separator_;
113 header_ += center_string(
"merit", columnwidth) + separator_;
114 header_ += center_string(
"stop", columnwidth) + separator_;
115 header_ += center_string(
"|grad|", columnwidth) + separator_;
116 header_ += center_string(
"preg", columnwidth) + separator_;
117 header_ += center_string(
"dreg", columnwidth) + separator_;
118 header_ += center_string(
"step", 6) + separator_;
119 header_ += center_string(
"||ffeas||", columnwidth) + separator_;
120 header_ += center_string(
"||gfeas||", columnwidth) + separator_;
121 header_ += center_string(
"||hfeas||", columnwidth) + separator_;
122 header_ += center_string(
"dV-exp", columnwidth) + separator_;
123 header_ += center_string(
"dV", columnwidth) + separator_;
124 header_ += center_string(
"dPhi-exp", columnwidth) + separator_;
125 header_ += center_string(
"dPhi", columnwidth,
false);
135 std::cout << header_ << std::endl << std::flush;
137 auto space_sign = [
this](
const double value) {
138 std::stringstream stream;
144 stream << std::scientific << std::setprecision(precision_) << abs(value);
148 std::cout << std::setw(4) << solver.
get_iter() << separator_;
151 std::cout << std::scientific << std::setprecision(precision_)
153 std::cout << solver.
get_stop() << separator_;
154 std::cout << abs(solver.
get_d()[0]) << separator_;
155 std::cout << solver.
get_preg() << separator_;
156 std::cout << std::fixed << std::setprecision(4) << solver.
get_steplength()
158 std::cout << space_sign(solver.
get_dVexp()) << separator_short_;
159 std::cout << space_sign(solver.
get_dV());
163 std::cout << std::scientific << std::setprecision(precision_)
164 << solver.
get_cost() << separator_short_;
165 std::cout << space_sign(solver.
get_merit()) << separator_;
166 std::cout << solver.
get_stop() << separator_;
167 std::cout << abs(solver.
get_d()[0]) << separator_;
168 std::cout << solver.
get_preg() << separator_;
169 std::cout << std::fixed << std::setprecision(4) << solver.
get_steplength()
171 std::cout << std::scientific << std::setprecision(precision_)
172 << solver.
get_ffeas() << separator_short_;
173 std::cout << space_sign(solver.
get_dVexp()) << separator_short_;
174 std::cout << space_sign(solver.
get_dV());
178 std::cout << std::scientific << std::setprecision(precision_)
179 << solver.
get_cost() << separator_short_;
180 std::cout << space_sign(solver.
get_merit()) << separator_;
181 std::cout << solver.
get_stop() << separator_;
182 std::cout << abs(solver.
get_d()[0]) << separator_;
183 std::cout << solver.
get_preg() << separator_;
184 std::cout << solver.
get_dreg() << separator_;
185 std::cout << std::fixed << std::setprecision(4) << solver.
get_steplength()
187 std::cout << std::scientific << std::setprecision(precision_)
188 << solver.
get_ffeas() << separator_short_;
189 std::cout << space_sign(solver.
get_dVexp()) << separator_short_;
190 std::cout << space_sign(solver.
get_dV());
194 std::cout << std::scientific << std::setprecision(precision_)
195 << solver.
get_cost() << separator_short_;
196 std::cout << space_sign(solver.
get_merit()) << separator_;
197 std::cout << solver.
get_stop() << separator_;
198 std::cout << abs(solver.
get_d()[0]) << separator_;
199 std::cout << solver.
get_preg() << separator_;
200 std::cout << solver.
get_dreg() << separator_;
201 std::cout << std::fixed << std::setprecision(4) << solver.
get_steplength()
203 std::cout << std::scientific << std::setprecision(precision_)
205 std::cout << solver.
get_gfeas() << separator_;
206 std::cout << solver.
get_hfeas() << separator_short_;
207 std::cout << space_sign(solver.
get_dVexp()) << separator_short_;
208 std::cout << space_sign(solver.
get_dV());
212 std::cout << std::scientific << std::setprecision(precision_)
213 << solver.
get_cost() << separator_short_;
214 std::cout << space_sign(solver.
get_merit()) << separator_;
215 std::cout << solver.
get_stop() << separator_;
216 std::cout << abs(solver.
get_d()[0]) << separator_;
217 std::cout << solver.
get_preg() << separator_;
218 std::cout << solver.
get_dreg() << separator_;
219 std::cout << std::fixed << std::setprecision(4) << solver.
get_steplength()
221 std::cout << std::scientific << std::setprecision(precision_)
223 std::cout << solver.
get_gfeas() << separator_;
224 std::cout << solver.
get_hfeas() << separator_short_;
225 std::cout << space_sign(solver.
get_dVexp()) << separator_short_;
226 std::cout << space_sign(solver.
get_dV()) << separator_short_;
229 std::cout << space_sign(solver.
get_dPhi());
235 std::cout << std::endl;
236 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.