Feat C++ API
A feature engineering automation tool
rnd.h
Go to the documentation of this file.
1 /* FEAT
2 copyright 2017 William La Cava
3 license: GNU/GPL v3
4 */
5 
6 #ifndef RND_H
7 #define RND_H
8 //external includes
9 #include <random>
10 #include <limits>
11 #include <vector>
12 
13 #include "../init.h"
14 #include "error.h"
15 
16 using namespace std;
17 using std::swap;
18 
19 namespace FT {
20 
21  namespace Util{
22 
24 
30  class Rnd
31  {
32  public:
33 
34  static Rnd* initRand();
35 
36  static void destroy();
37 
38  void set_seed(int new_seed);
39 
40  int get_seed(){return this->seed;};
41 
42  int rnd_int( int lowerLimit, int upperLimit );
43 
44  float rnd_flt(float min=0.0, float max=1.0);
45 
46  float rnd_dbl(float min=0.0, float max=1.0);
47 
48  float operator()(unsigned i);
49 
50  float operator()();
51 
52  template <class RandomAccessIterator>
53  void shuffle (RandomAccessIterator first,
54  RandomAccessIterator last)
55  {
56  for (auto i=(last-first)-1; i>0; --i)
57  {
58  std::uniform_int_distribution<decltype(i)> d(0,i);
59  swap (first[i], first[d(rg[omp_get_thread_num()])]);
60  }
61  }
62 
63  template<typename Iter>
64  Iter select_randomly(Iter start, Iter end)
65  {
66  std::uniform_int_distribution<> dis(0,
67  distance(start, end) - 1);
68  advance(start, dis(rg[omp_get_thread_num()]));
69  return start;
70  }
71 
72  template<typename T>
73  T random_choice(const vector<T>& v)
74  {
78  assert(v.size()>0 &&
79  " attemping to return random choice from empty vector");
80  return *select_randomly(v.begin(),v.end());
81  }
82 
83 
84  template<typename T, typename D>
85  T random_choice(const vector<T>& v, const vector<D>& w )
86  {
91  if(w.size() == 0)
92  {
93  if (v.size() == 0)
94  THROW_LENGTH_ERROR("random_choice() called with "
95  "w.size() = 0 and v.size() = 0");
96  else
97  {
98  THROW_LENGTH_ERROR("w.size() = 0, v.size() = "
99  +to_string(v.size())+
100  "; Calling random_choice(v)");
101 
102  return random_choice(v);
103  }
104  }
105  if(w.size() != v.size())
106  {
107  cout<<"WARN! random_choice() w.size() " << w.size() << "!= v.size() "
108  << v.size() << ", Calling random_choice(v)\n";
109  return random_choice(v);
110  }
111  else
112  {
113  assert(v.size() == w.size());
114  std::discrete_distribution<size_t> dis(w.begin(), w.end());
115  return v[dis(rg[omp_get_thread_num()])];
116  }
117  }
118 
119  float gasdev();
120 
121  private:
122 
123  Rnd();
124 
125  ~Rnd();
126 
127  vector<std::mt19937> rg;
128 
129  static Rnd* instance;
130 
131  int seed;
132 
133  };
134 
135  static Rnd &r = *Rnd::initRand();
136  }
137 }
138 #endif
Defines a multi-core random number generator and its operators.
Definition: rnd.h:31
int seed
Definition: rnd.h:131
int get_seed()
Definition: rnd.h:40
T random_choice(const vector< T > &v)
Definition: rnd.h:73
vector< std::mt19937 > rg
Definition: rnd.h:127
static Rnd * instance
Definition: rnd.h:129
T random_choice(const vector< T > &v, const vector< D > &w)
Definition: rnd.h:85
void shuffle(RandomAccessIterator first, RandomAccessIterator last)
Definition: rnd.h:53
Iter select_randomly(Iter start, Iter end)
Definition: rnd.h:64
#define THROW_LENGTH_ERROR(err)
Definition: error.h:32
#define omp_get_thread_num()
Definition: init.h:12
int last
Definition: population.cc:11
static Rnd & r
Definition: rnd.h:135
std::string to_string(const T &value)
template function to convert objects to string for logging
Definition: utils.h:422
main Feat namespace
Definition: data.cc:13
int i
Definition: params.cc:552