OPTK  0.1.2
Toolkit for global optimisation algorithms
types.hpp
Go to the documentation of this file.
1 
28 #ifndef __TYPES_H_
29 #define __TYPES_H_
30 
31 #include <cstdlib>
32 #include <cmath>
33 #include <memory>
34 #include <random>
35 #include <stdexcept>
36 #include <string>
37 #include <tuple>
38 #include <vector>
39 #include <unordered_map>
40 
41 #include <sys/types.h>
42 
43 // Program context ------------------------------------------------------------
44 
45 namespace optk {
46 
47 typedef struct {
48  std::string outfile;
49  uint max_iters;
50  int threads;
51  bool error;
52 } ctx_t;
53 
54 } // namespace optk
55 
56 // Parameter values -----------------------------------------------------------
57 
60 namespace inst {
61 
63 enum class inst_t : char {
65  node,
67  int_val,
69  dbl_val,
71  str_val
72 };
73 
74 class param;
75 class node;
76 
83 typedef node *set;
84 
86 typedef std::unordered_map <std::string, param *> value_map;
87 
90 class param {
91  public:
92  param (const std::string &k, inst_t t);
93 
94  inst_t get_type () { return type; }
95  std::string get_key() { return key; }
96 
97  private:
98  const std::string key;
99  const inst_t type;
100 };
101 
102 // Could have used a template class for the following three classes, however
103 // this was not done because it ends up bloating the syntax. Also since only
104 // integers, doubles and strings are allowed, explicitly defining them gives a
105 // bit more 'type safety'.
106 
108 class int_val: public param {
109  public:
110  int_val (const std::string &k, int v);
111 
112  int get_val () { return val; }
113  void update_val (int v) { val = v; };
114  int *get_addr () { return &val; }
115 
116  private:
117  int val;
118 };
119 
121 class dbl_val: public param {
122  public:
123  dbl_val (const std::string &k, double v);
124 
125  double get_val () { return val; }
126  void update_val (double v) { val = v; };
127  double *get_addr () { return &val; }
128 
129  private:
130  double val;
131 };
132 
134 class str_val: public param {
135  public:
136  str_val (const std::string &k, const std::string &v);
137 
138  std::string get_val () { return val; }
139  void update_val (std::string v) { val = v; };
140  std::string *get_addr () { return &val; }
141 
142  private:
143  std::string val;
144 };
145 
150 class node : public param {
151  public:
154  node (const std::string &k);
155 
158  void add_item (param *p);
159 
162  void add_items (std::vector<param *> items);
163 
166  param *get_item(const std::string &k);
167 
170  void remove_item (const std::string &k);
171 
176  value_map *get_values ()
177  { return &values; }
178 
179  int getint(const std::string &key);
180  int getint(int i);
181  double getdbl(const std::string &key);
182  double getdbl(int i);
183  std::string getstr(const std::string &key);
184  std::string getstr(int i);
185 
186  private:
189  value_map values;
190 };
191 
197 template <class T>
198 T get (node *n, const std::string &k)
199 {
200  inst::param *tmp = n->get_item(k);
201  if (tmp != NULL)
202  return static_cast<T>(tmp);
203  return NULL;
204 }
205 
206 #define GETINT(dest, src, key) \
207  inst::int_val *dest = static_cast<inst::int_val *>((src)->get_item (key))
208 
209 #define GETDBL(dest, src, key) \
210  inst::dbl_val *dest = static_cast<inst::dbl_val *>((src)->get_item (key))
211 
212 #define GETSTR(dest, src, key) \
213  inst::str_val *dest = static_cast<inst::str_val *>((src)->get_item (key))
214 
215 #define GETNODE(dest, src, key) \
216  inst::node *dest = static_cast<inst::node *>((src)->get_item (key))
217 
224 void free_node (node *n);
225 
226 } // namespace inst end
227 
228 // Search space types ----------------------------------------------------------
229 
232 enum class pt: char {
244  choice,
247  randint,
250  uniform,
253  quniform,
256  loguniform,
259  qloguniform,
262  normal,
265  qnormal,
268  lognormal,
271  qlognormal
272 };
273 
274 namespace sspace {
275 
278 class param_t
279 {
280  public:
285  param_t (std::string n, pt t);
286 
289  virtual ~param_t () {};
290 
295  virtual pt
297  {
298  return m_type;
299  }
300 
305  virtual std::string
307  {
308  return m_name;
309  }
310 
311  private:
312  const std::string m_name;
313  const pt m_type;
314 };
315 
318 typedef std::vector<param_t *> sspace_t;
319 
329 template <typename T>
330 class categorical: public param_t
331 {
332  public:
338  categorical (std::string n, std::vector<int> *options):
340  {
341  if (options->size() == 0)
342  throw std::invalid_argument("Empty value list is invalid");
343  m_options = *options;
344  init_rand ();
345  }
346 
352  categorical (std::string n, std::vector<double> *options):
354  {
355  if (options->size() == 0)
356  throw std::invalid_argument("Empty value list is invalid");
357  m_options = *options;
358  init_rand ();
359  }
360 
366  categorical (std::string n, std::vector<std::string> *options):
368  {
369  if (options->size() == 0)
370  throw std::invalid_argument("Empty value list is invalid");
371  m_options = *options;
372  init_rand ();
373  }
374 
376  std::vector<T> *
378  {
379  return &m_options;
380  }
381 
383  long unsigned int
384  count ()
385  {
386  return m_options.size();
387  }
388 
396  T
397  get(long unsigned int i)
398  {
399  if (i < 0 || i > m_options.size())
400  throw "Out of range";
401  return m_options[i];
402  }
403 
407  T
409  {
410  return get(dist(generator));
411  }
412 
413  private:
414 
418  void
420  {
421  generator = std::mt19937 (rd ());
422  uint u = m_options.size() - 1;
423  if (u < 0) u = 0;
424  dist = std::uniform_int_distribution<int> (0, u);
425  }
426 
427  std::vector<T> m_options;
428 
429  // used to implement random sampling
430  std::random_device rd;
431  std::mt19937 generator;
432  std::uniform_int_distribution<int> dist;
433 };
434 
445 class choice: public param_t
446 {
447  public:
453  choice (std::string n, sspace_t *options);
454 
459  sspace_t *options();
460 
465  long unsigned int count ();
466 
473  param_t *get (long unsigned int i);
474 
475  private:
478  sspace_t *m_options;
479 };
480 
487 class randint: public param_t
488 {
489  public:
496  randint (std::string n, int lower, int upper);
497 
502  int sample ();
503 
509  int m_lower, m_upper;
510 
511  private:
512  std::random_device rd;
513  std::mt19937 generator;
514  std::uniform_int_distribution<int> dist;
515 };
516 
521 class uniform: public param_t
522 {
523  public:
524 
532  uniform (std::string n, double lower, double upper);
533 
541  uniform (std::string n, double lower, double upper, pt type);
542 
552  virtual double sample();
553 
555  double m_lower, m_upper;
556 
557  protected:
558  std::random_device rd;
559  std::mt19937 generator;
560  std::uniform_real_distribution<double> dist;
561 };
562 
573 class quniform: public uniform {
574  public:
582  quniform (std::string n, double lower, double upper, double q);
583 
596  double sample();
597 
598  double m_q;
599 };
600 
609 class loguniform: public uniform {
610  public:
619  loguniform (std::string n, double lower, double upper);
620 
625  loguniform (std::string n, double lower, double upper, pt t);
626 
635  double sample ();
636 };
637 
646 class qloguniform: public loguniform {
647 
648  public:
649 
658  qloguniform (std::string n, double lower, double upper, double q);
659 
671  double sample ();
672 
673  double m_q;
674 };
675 
680 class normal: public param_t {
681  public:
689  normal (std::string n, double mu, double sigma);
690 
695  normal (std::string n, double mu, double sigma, pt t);
696 
705  virtual double sample ();
706 
708  double m_mu, m_sigma;
709 
710  protected:
711  std::random_device rd;
712  std::mt19937 generator;
713  std::normal_distribution<double> dist;
714 };
715 
721 class qnormal: public normal {
722  public:
730  qnormal (std::string n, double mu, double sigma, double q);
731 
739  double sample ();
740 
741  double m_q;
742 };
743 
748 class lognormal: public normal {
749  public:
756  lognormal (std::string n, double mu, double sigma);
757 
761  lognormal (std::string n, double mu, double sigma, pt t);
762 
770  double sample ();
771 };
772 
780 class qlognormal: public lognormal {
781  public:
789  qlognormal (std::string n, double mu, double sigma, double q);
790 
798  double sample ();
799 
800  double m_q;
801 };
802 
814 
820 void free_ss (sspace_t *ss);
821 
822 } // namespace sspace
823 
824 #endif // __TYPES_H_
virtual pt get_type()
Definition: types.hpp:296
int threads
Max number of iterations to run per benchmark.
Definition: types.hpp:50
Definition: types.hpp:134
std::vector< param_t * > sspace_t
Definition: types.hpp:318
Definition: types.hpp:150
Definition: types.hpp:60
Definition: types.hpp:609
Definition: types.hpp:573
value_map * get_values()
Definition: types.hpp:176
Definition: types.hpp:278
T sample()
Definition: types.hpp:408
Definition: types.hpp:274
pt
Definition: types.hpp:232
Definition: types.hpp:330
Definition: types.hpp:521
virtual ~param_t()
Definition: types.hpp:289
categorical(std::string n, std::vector< std::string > *options)
Definition: types.hpp:366
inst_t
Definition: types.hpp:63
void init_rand()
Definition: types.hpp:419
uint max_iters
The name of the output file.
Definition: types.hpp:49
Definition: types.hpp:47
Definition: core.cpp:26
int m_lower
Definition: types.hpp:509
value_map values
Definition: types.hpp:189
Definition: types.hpp:445
void free_ss(sspace_t *ss)
Definition: types.cpp:654
double m_lower
Definition: types.hpp:555
bool error
The number of threads to use.
Definition: types.hpp:51
Definition: types.hpp:90
categorical(std::string n, std::vector< double > *options)
Definition: types.hpp:352
virtual std::string get_name()
Definition: types.hpp:306
void validate_param_values(inst::value_map *vals, sspace::sspace_t *sspace)
Definition: types.cpp:604
void free_node(node *n)
Definition: types.cpp:122
sspace_t * m_options
Definition: types.hpp:478
std::vector< T > * values()
Definition: types.hpp:377
Definition: types.hpp:121
Definition: types.hpp:646
double m_mu
Definition: types.hpp:708
Definition: types.hpp:487
Definition: types.hpp:748
param * get_item(const std::string &k)
Definition: types.cpp:91
Definition: types.hpp:721
Definition: types.hpp:780
Definition: types.hpp:108
Definition: types.hpp:680
std::unordered_map< std::string, param * > value_map
Definition: types.hpp:86
long unsigned int count()
Definition: types.hpp:384
categorical(std::string n, std::vector< int > *options)
Definition: types.hpp:338