ʻOhana
Population structure, admixture history, and selection using learning methods.
jade.agi_controller.hpp
1 /* -------------------------------------------------------------------------
2  Ohana
3  Copyright (c) 2015-2020 Jade Cheng (\___/)
4  Jade Cheng <info@jade-cheng.com> (='.'=)
5  ------------------------------------------------------------------------- */
6 
7 #ifndef JADE_AGI_CONTROLLER_HPP__
8 #define JADE_AGI_CONTROLLER_HPP__
9 
10 #include "jade.controller.hpp"
11 
12 namespace jade
13 {
14  ///
15  /// A template for a class that encodes and decodes parameters for the
16  /// Nelder-Mead algorithm. This class uses an admixture graph input file.
17  ///
18  template <typename TValue>
20  : public basic_controller<TValue>
21  {
22  public:
23  /// The value type.
24  typedef TValue value_type;
25 
26  /// The admixture graph input reader type.
28 
29  /// The shunting-yard algorithm type.
31 
32  /// The arguments passed to the shunting-yard algorithm.
34 
35  /// The matrix type.
37 
38  /// The options type.
40 
41  /// The settings type.
43 
44  /// The simplex type.
46 
47  /// The container type for the simplex.
49 
50  /// The exit condition type for the simplex.
52 
53  ///
54  /// Initializes a new instance of the class based on the specified
55  /// program settings.
56  ///
58  const settings_type & settings) ///< The program settings.
59  : basic_controller<TValue> (settings)
60  , _agi (settings.get_agi())
61  , _args (_agi.get_args())
62  {
63  }
64 
65  ///
66  /// Writes results to standard output and files.
67  ///
69  const options_type & opts, ///< The options.
70  const simplex_type & simplex, ///< The simplex.
71  const exit_condition_type & condition) ///< The context.
72  override
73  {
74  //
75  // The base class emits the C matrix; this also has the effect of
76  // updating the arguments used to evaluate the expressions.
77  //
78  basic_controller<TValue>::emit_results(opts, simplex, condition);
79 
80  //
81  // Loop over each argument and display its value.
82  //
83  std::cout << "\n[Admixture Graph Output]\n";
85  for (const auto & name : _agi.get_branch_names())
86  std::cout << name << "\t" << _args[name] << "\n";
87  for (const auto & name : _agi.get_proportion_names())
88  std::cout << name << "\t" << _args[name] << "\n";
89  std::cout.flush();
90  }
91 
92  ///
93  /// Creates and returns the initial set of parameters for the Nelder-
94  /// Mead algorithm.
95  ///
96  /// \return The initial parameters for the Nelder-Mead algorithm.
97  ///
99  {
100  //
101  // Return a container initially filled with 0.5 for both branch and
102  // proportion variables.
103  //
104  container_type out;
105  out.resize(_args.size(), value_type(0.5));
106  return out;
107  }
108 
109  protected:
110  ///
111  /// Decodes the specified Nelder-Mead container and stores the result
112  /// into the lower triangle, including the diagonal, of the covariance
113  /// matrix.
114  /// \return True if successful; otherwise, false.
115  ///
117  matrix_type & dst, ///< The covariance matrix.
118  const container_type & src) ///< The Nelder-Mead container.
119  override
120  {
121  //
122  // If any value in the container is not positive, then return false
123  // to indicate these are unacceptable values.
124  //
125  for (const auto item : src)
126  if (item <= value_type(0))
127  return false;
128 
129  //
130  // If any value corresponding to a proportion is not less than one,
131  // then return false to indicate these are unacceptable values.
132  // Note the proportion variables are stored first in the container
133  // (and are followed by the branch variables).
134  //
135  {
136  const auto n = _agi.get_proportion_names().size();
137  for (size_t i = 0; i < n; i++)
138  if (src[i] >= value_type(1))
139  return false;
140  }
141 
142  //
143  // Copy the values from the Nelder-Mead container into the table
144  // of arguments used to evaluate the expression. Since these values
145  // are the same for all expressions, the same container is used for
146  // each expression evaluated.
147  //
148  {
149  size_t i = 0;
150  for (const auto & name : _agi.get_proportion_names())
151  _args[name] = src[i++];
152  for (const auto & name : _agi.get_branch_names())
153  _args[name] = src[i++];
154  }
155 
156  //
157  // Copy the expression output to the lower-triangle of the
158  // covariance matrix:
159  //
160  // | 0 - - |
161  // | 1 2 - |
162  // | 3 4 5 |
163  //
164  // Each value in the matrix is determined by evaluating a
165  // corresponding expression.
166  //
167  {
168  const auto & entries = _agi.get_entries();
169  const auto rk = _agi.get_k() - 1;
170  size_t i = 0;
171  for (size_t row = 0; row < rk; row++)
172  for (size_t col = 0; col <= row; col++)
173  dst(row, col) = entries[i++].evaluate(_args);
174  }
175 
176  //
177  // Return true to indicate the values are acceptable.
178  //
179  return true;
180  }
181 
182  private:
183  const agi_reader_type & _agi; // The admixture graph input.
184  args_type _args; // Arguments used to evaluate entries.
185  };
186 }
187 
188 #endif // JADE_AGI_CONTROLLER_HPP__
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_options< value_type >
jade::basic_agi_controller::container_type
simplex_type::container_type container_type
The container type for the simplex.
Definition: jade.agi_controller.hpp:48
jade::basic_agi_controller::settings_type
basic_settings< value_type > settings_type
The settings type.
Definition: jade.agi_controller.hpp:42
jade::basic_shunting_yard::args_type
std::map< std::string, float_type > args_type
A map of strings to floating-point values. The string represent the variable names parsed from the ex...
Definition: jade.shunting_yard.hpp:33
jade::basic_agi_controller::exit_condition_type
simplex_type::exit_condition_type exit_condition_type
The exit condition type for the simplex.
Definition: jade.agi_controller.hpp:51
jade::basic_agi_controller::basic_agi_controller
basic_agi_controller(const settings_type &settings)
Initializes a new instance of the class based on the specified program settings.
Definition: jade.agi_controller.hpp:57
jade::basic_agi_controller::value_type
TValue value_type
The value type.
Definition: jade.agi_controller.hpp:24
jade::basic_agi_controller::matrix_type
basic_matrix< value_type > matrix_type
The matrix type.
Definition: jade.agi_controller.hpp:36
jade::basic_agi_controller::agi_reader_type
basic_agi_reader< value_type > agi_reader_type
The admixture graph input reader type.
Definition: jade.agi_controller.hpp:27
jade::basic_matrix< value_type >::set_high_precision
static void set_high_precision(std::ostream &out)
Sets the specified stream to scientific notation with a high precision.
Definition: jade.matrix.hpp:1154
jade::basic_agi_reader
A template for a class that reads an AGI (Admixture Graph Input) file. The file provides the followin...
Definition: jade.agi_reader.hpp:43
jade::basic_agi_reader::get_k
std::size_t get_k() const
Returns the number of components.
Definition: jade.agi_reader.hpp:198
jade::basic_agi_controller::args_type
shunting_yard_type::args_type args_type
The arguments passed to the shunting-yard algorithm.
Definition: jade.agi_controller.hpp:33
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_simplex::container_type
std::vector< value_type > container_type
The container type.
Definition: jade.simplex.hpp:27
jade::basic_agi_controller::shunting_yard_type
basic_shunting_yard< value_type > shunting_yard_type
The shunting-yard algorithm type.
Definition: jade.agi_controller.hpp:30
jade::basic_agi_reader::get_branch_names
const std::vector< std::string > & get_branch_names() const
Returns the vector of branch names.
Definition: jade.agi_reader.hpp:180
jade::basic_agi_controller::emit_results
void emit_results(const options_type &opts, const simplex_type &simplex, const exit_condition_type &condition) override
Writes results to standard output and files.
Definition: jade.agi_controller.hpp:68
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_agi_reader::get_entries
const std::vector< shunting_yard_type > & get_entries() const
Returns the vector of expressions for the C matrix entries.
Definition: jade.agi_reader.hpp:207
jade::basic_settings
A template for a class encapsulating the settings provided to the optimizer.
Definition: cpax/jade.settings.hpp:22
jade::basic_agi_controller
A template for a class that encodes and decodes parameters for the Nelder-Mead algorithm....
Definition: jade.agi_controller.hpp:21
jade::basic_shunting_yard
A template for a class that implements the shunting-yard algorithm. The implementation supports '+',...
Definition: jade.shunting_yard.hpp:23
jade::basic_simplex::exit_condition::type
type
The exit condition.
Definition: jade.simplex.hpp:38
jade::basic_agi_controller::simplex_type
basic_simplex< value_type > simplex_type
The simplex type.
Definition: jade.agi_controller.hpp:45
jade::basic_agi_reader::get_proportion_names
const std::vector< std::string > & get_proportion_names() const
Returns the vector of proportion names.
Definition: jade.agi_reader.hpp:189
jade::basic_agi_controller::options_type
basic_options< value_type > options_type
The options type.
Definition: jade.agi_controller.hpp:39
jade::basic_agi_controller::_decode_lower
bool _decode_lower(matrix_type &dst, const container_type &src) override
Decodes the specified Nelder-Mead container and stores the result into the lower triangle,...
Definition: jade.agi_controller.hpp:116
jade::basic_agi_controller::init_parameters
container_type init_parameters() override
Creates and returns the initial set of parameters for the Nelder- Mead algorithm.
Definition: jade.agi_controller.hpp:98
jade::basic_matrix< value_type >