7 #ifndef JADE_OPTIMIZER_HPP__
8 #define JADE_OPTIMIZER_HPP__
10 #include "jade.improver.hpp"
11 #include "jade.settings.hpp"
12 #include "jade.stopwatch.hpp"
19 template <
typename TValue>
53 _clamp_f(settings, fa);
56 const auto fg = settings.
get_fg();
57 const auto fif = settings.
get_fif();
58 const auto & g = settings.
get_g();
59 const auto frb = opts.is_frb();
69 auto lle = g.compute_lle(q, fa, fb, qfa, qfb);
70 _emit_header(settings, sw1, lle);
72 for (
size_t iter = 1;; iter++)
74 if (opts.is_max_iterations_specified())
75 if (iter > opts.get_max_iterations())
78 if (opts.is_max_time_specified())
79 if (sw1 > opts.get_max_time())
84 if (!opts.is_fixed_q())
91 if (!opts.is_fixed_f())
94 _clamp_f(settings, fa);
100 const auto lle_prime = g.compute_lle(q, fa, fb, qfa, qfb);
101 const auto dlle = lle_prime - lle;
103 _emit_line(settings, sw2, iter, lle_prime, dlle);
107 if (opts.is_epsilon_specified())
108 if (dlle >=
value_type(0) && dlle <= opts.get_epsilon())
112 _emit_results(settings, q, fa);
119 const auto epsilon = settings.get_options().get_f_epsilon();
128 const auto fa_end = fa.get_data() + fa.get_length();
129 auto fa_ptr = fa.get_data();
130 auto fb_ptr = fb.get_data();
132 while (fa_ptr != fa_end)
137 static void _emit_header(
139 const stopwatch & sw,
142 const auto & opts = in.get_options();
147 std::ostringstream line;
148 line << 0 << std::fixed << std::setprecision(6)
154 <<
"seed: " << opts.get_seed() << std::endl
156 <<
"iter\tduration\tlog_likelihood\tdelta-lle" << std::endl
157 << line.str() << std::endl;
161 static void _emit_line(
163 const stopwatch & sw,
168 if (in.get_options().is_quiet())
171 std::ostringstream line;
174 << std::fixed << std::setprecision(6)
177 line <<
'\t' << lle <<
'\t' << dlle;
179 std::cout << line.str() << std::endl;
183 static void _emit_matrix(
185 const std::string & path,
187 const char *
const name)
192 std::cout <<
"[" << name <<
" Matrix]\n" << matrix;
196 if (!settings.get_options().is_quiet())
198 <<
"Writing " << name <<
" matrix to "
201 std::ofstream out (path);
203 throw error() <<
"failed to create matrix '" << path <<
"'";
210 static void _emit_results(
215 static const std::string no_path;
217 const auto & opts = in.get_options();
219 if (!opts.is_quiet())
220 std::cout << std::endl;
222 if (!opts.is_fixed_q())
224 const auto qout = opts.is_qout_specified()
225 ? opts.get_qout() : no_path;
226 _emit_matrix(q, qout, in,
"Q");
229 if (!opts.is_fixed_f())
231 if (!opts.is_fixed_q() && !opts.is_qout_specified())
232 std::cout << std::endl;
234 const auto fout = opts.is_fout_specified()
235 ? opts.get_fout() : no_path;
236 _emit_matrix(f, fout, in,
"F");
242 #endif // JADE_OPTIMIZER_HPP__