ʻOhana
Population structure, admixture history, and selection using learning methods.
nemeco/jade.optimizer.hpp
1 /* -------------------------------------------------------------------------
2  Ohana
3  Copyright (c) 2015-2020 Jade Cheng (\___/)
4  Jade Cheng <info@jade-cheng.com> (='.'=)
5  ------------------------------------------------------------------------- */
6 
7 #ifndef JADE_OPTIMIZER_HPP__
8 #define JADE_OPTIMIZER_HPP__
9 
10 #include "jade.controller_factory.hpp"
11 
12 namespace jade
13 {
14  ///
15  /// A template for a class that optimizes specified program settings.
16  ///
17  template <typename TValue>
18  class basic_optimizer
19  {
20  public:
21  /// The value type.
22  typedef TValue value_type;
23 
24  /// The controller type.
26 
27  /// The controller factory type.
29 
30  /// The settings type.
32 
33  /// The simplex type.
35 
36  /// The container type for the simplex.
38 
39  /// The log arguments type for the simplex.
41 
42  ///
43  /// Executes the optimizer based on the specified settings.
44  ///
45  static void execute(
46  const settings_type & settings) ///< The settings used to optimize.
47  {
48  std::cout << "iter\tduration\tdelta-lle\tlog-likelihood\n";
49 
50  const auto & opts = settings.get_options();
51 
52  //
53  // Depending upon whether or not the user specified a Newick tree,
54  // construct the appropriate controller for the Nelder-Mead
55  // algorithm, and use it to compute the objective function.
56  //
57  std::unique_ptr< controller_type > ctrl (
59  const auto objfunc = [&](const container_type & params)
60  -> value_type { return ctrl->compute_objfunc(params); };
61 
62  //
63  // Initialize the Nelder-Mead algorithm.
64  //
65  typedef typename simplex_type::options options_type;
66  options_type options (ctrl->init_parameters());
67  simplex_type simplex (objfunc, options);
68 
69  //
70  // Instruct the Nelder-Mead algorithm to use the settings based on
71  // supplied program options.
72  //
73  typedef typename simplex_type::execute_args execute_args_type;
74  execute_args_type execute_args;
75  execute_args.user = ctrl.get();
76  execute_args.logfunc = _logfunc;
77  if (opts.is_max_iterations_specified())
78  execute_args.max_iterations = opts.get_max_iterations();
79  if (opts.is_max_time_specified())
80  execute_args.max_seconds = opts.get_max_time();
81  if (opts.is_epsilon_specified())
82  execute_args.min_epsilon = opts.get_epsilon();
83 
84  //
85  // Perform the minimization.
86  //
87  const auto condition = simplex.execute(objfunc, execute_args);
88 
89  //
90  // Emit results.
91  //
92  ctrl->emit_results(opts, simplex, condition);
93  }
94 
95  private:
96  // --------------------------------------------------------------------
97  static void _logfunc(const log_args_type & log_args)
98  {
99  const auto ctrl = (controller_type *)(log_args.user);
100  assert(ctrl != nullptr);
101  ctrl->log_iteration(log_args);
102  }
103  };
104 }
105 
106 #endif // JADE_OPTIMIZER_HPP__
jade::basic_controller::init_parameters
virtual container_type init_parameters()=0
Creates and returns the initial set of parameters for the Nelder- Mead algorithm.
jade::basic_controller
A template for a class that performs the Nelder-Mead optimization. This class implements shared featu...
Definition: jade.controller.hpp:24
jade::basic_controller::compute_objfunc
value_type compute_objfunc(const container_type &params)
Computes the objective function by decoding the specified Nelder- Mead parameters into a covariance m...
Definition: jade.controller.hpp:68
jade::basic_options< value_type >
jade::basic_simplex::log_args
Arguments passed to the logging function.
Definition: jade.simplex.hpp:115
jade::basic_settings::get_options
const options_type & get_options() const
Definition: cpax/jade.settings.hpp:174
jade::basic_optimizer::log_args_type
simplex_type::log_args log_args_type
The log arguments type for the simplex.
Definition: nemeco/jade.optimizer.hpp:40
jade::basic_optimizer::controller_type
basic_controller< value_type > controller_type
The controller type.
Definition: nemeco/jade.optimizer.hpp:25
jade::basic_simplex::execute_args
Arguments to the execute method.
Definition: jade.simplex.hpp:157
jade::basic_controller_factory::create
static controller_type * create(const settings_type &settings)
Definition: jade.controller_factory.hpp:48
jade::basic_optimizer::container_type
simplex_type::container_type container_type
The container type for the simplex.
Definition: nemeco/jade.optimizer.hpp:37
jade::basic_simplex
A template for a class that implements the Nelder-Mead Simplex Method, which attempts to minimize an ...
Definition: jade.simplex.hpp:21
jade::basic_optimizer::execute
static void execute(const settings_type &settings)
Executes the optimizer based on the specified settings.
Definition: nemeco/jade.optimizer.hpp:45
jade::basic_simplex::container_type
std::vector< value_type > container_type
The container type.
Definition: jade.simplex.hpp:27
jade::basic_simplex::execute_args::user
void * user
User-supplied value.
Definition: jade.simplex.hpp:187
jade::basic_simplex::options
A data structure used to initialize the Nelder-Mead algorithm.
Definition: jade.simplex.hpp:303
jade::basic_controller::emit_results
virtual void emit_results(const options_type &opts, const simplex_type &simplex, const exit_condition_type &)
Writes results to standard output and files.
Definition: jade.controller.hpp:110
jade::basic_settings
A template for a class encapsulating the settings provided to the optimizer.
Definition: cpax/jade.settings.hpp:22
jade::basic_optimizer::simplex_type
controller_type::simplex_type simplex_type
The simplex type.
Definition: nemeco/jade.optimizer.hpp:34
jade::basic_optimizer::controller_factory_type
basic_controller_factory< value_type > controller_factory_type
The controller factory type.
Definition: nemeco/jade.optimizer.hpp:28
jade::basic_optimizer::value_type
TValue value_type
The value type.
Definition: nemeco/jade.optimizer.hpp:22
jade::basic_optimizer::options_type
basic_options< value_type > options_type
The options type.
Definition: cpax/jade.optimizer.hpp:30
jade::basic_simplex::execute
exit_condition_type execute(const TObjfunc objfunc, const execute_args &exe_args)
Calls the iterate method until an exit condition is reached.
Definition: jade.simplex.hpp:567
jade::basic_optimizer::settings_type
basic_settings< value_type > settings_type
The settings type.
Definition: nemeco/jade.optimizer.hpp:31
jade::basic_controller_factory
A template for a class that creates controllers that use or do not use Newick trees specified on the ...
Definition: jade.controller_factory.hpp:22