Stride Reference Manual  - generated for commit 9643b11
RnMan.cpp
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2018, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #include "RnMan.h"
22 #include "Rn.h"
23 #include "StringUtils.h"
24 
25 #include <trng/discrete_dist.hpp>
26 #include <trng/lcg64.hpp>
27 #include <trng/uniform01_dist.hpp>
28 #include <trng/uniform_int_dist.hpp>
29 #include <array>
30 #include <cctype>
31 #include <functional>
32 #include <pcg/pcg_random.hpp>
33 #include <randutils/randutils.hpp>
34 
35 using namespace std;
36 using namespace randutils;
37 
38 namespace stride {
39 namespace util {
40 
41 // The construction below lets you choose (at compile time)
42 // between the pcg64 and trng::lcg64 random engines. It's a hacky
43 // construct but required because the forward class definition
44 // of RnEgine in RnMan.h cannot be combined with a using
45 // statement here. A macro to generate the class definition
46 // would make it more scaleable, but since we have only two ...
47 
48 // If you want to use the pcg64 random engine, uncomment the
49 // class definition here and keep the one below commented out.
50 class RnEngine : public Rn<pcg64>
51 {
52  using Rn<pcg64>::Rn;
53 };
54 
55 // If you want to use the trng::lcg64 random engine, uncomment the
56 // class definition here and keep the one above commented out.
57 /*
58 class RnLcg64 : public Rn<trng::lcg64>
59 {
60  using Rn<trng::lcg64>::Rn;
61 };
62 */
63 
64 RnMan::RnMan() : m_rn(make_shared<RnEngine>()) {}
65 
66 RnMan::RnMan(const RnInfo& info) : m_rn(make_shared<RnEngine>(info)) {}
67 
68 bool RnMan::operator==(const RnMan& other) { return *m_rn == *(other.m_rn); }
69 
70 RnInfo RnMan::GetInfo() const { return m_rn->GetInfo(); }
71 
72 std::function<double()> RnMan::GetUniform01Generator(unsigned int i) { return m_rn->GetUniform01Generator(i); }
73 
74 std::function<int()> RnMan::GetUniformIntGenerator(int a, int b, unsigned int i)
75 {
76  return m_rn->GetUniformIntGenerator(a, b, i);
77 }
78 
79 std::function<int()> RnMan::GetDiscreteGenerator(const vector<double>& weights, unsigned int i)
80 {
81  return m_rn->GetDiscreteGenerator(weights.begin(), weights.end(), i);
82 }
83 
84 bool RnMan::MakeWeightedCoinFlip(double fraction, unsigned int i)
85 {
86  array<double, 2> weights{ 1.0 - fraction, fraction};
87  // -> 0, return is false -> not part of the fraction
88  // -> 1, return is true -> part of the fraction
89  auto dist = m_rn->GetDiscreteGenerator(weights.begin(), weights.end(), i);
90  return static_cast<bool>(dist());
91 }
92 
93 void RnMan::Initialize(const RnInfo& info) { m_rn->Initialize(info); }
94 
95 bool RnMan::IsEmpty() const { return m_rn->IsEmpty(); }
96 
97 void RnMan::Shuffle(vector<unsigned int>& indices, unsigned int i) { return m_rn->Shuffle(indices, i); }
98 
99 } // namespace util
100 } // namespace stride
bool IsEmpty() const
Is this een empty (i.e. non-initialized Rn)?
Definition: RnMan.cpp:95
Interface of RnPcg.
std::function< int()> GetDiscreteGenerator(const std::vector< double > &weights, unsigned int i=0U)
Return generator for ints [0, n-1[ with non-negative weights p_j (i=0,..,n-1) using i-th random strea...
Definition: RnMan.cpp:79
STL namespace.
Rn()
Default constructor build empty manager.
Definition: Rn.h:55
void Initialize(const RnInfo &info)
Initalize with data in Info.
Definition: RnMan.cpp:93
std::function< int()> GetUniformIntGenerator(int a, int b, unsigned int i=0U)
Return a generator for uniform ints in [a, b[ (a < b) using i-th random stream.
Definition: RnMan.cpp:74
Interface of RnMan.
void Shuffle(std::vector< unsigned int > &indices, unsigned int i)
Random shuffle of vector of int indices using i-th random stream.
Definition: RnMan.cpp:97
RnMan()
Default constructor builds empty (uninitialized) manager.
Definition: RnMan.cpp:64
bool operator==(const RnMan &other)
Equality of states.
Definition: RnMan.cpp:68
std::function< double()> GetUniform01Generator(unsigned int i=0U)
Return a generator for uniform doubles in [0, 1[ using i-th random stream.
Definition: RnMan.cpp:72
RnInfo GetInfo() const
Return the state of the random engines.
Definition: RnMan.cpp:70
bool MakeWeightedCoinFlip(double fraction, unsigned int i=0U)
Make weighted coin flip: <fraction> of the flips need to come up true.
Definition: RnMan.cpp:84
Miscellaneous string utilities.
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
Information on random number management state.
Definition: RnInfo.h:32
std::shared_ptr< RnEngine > m_rn
Definition: RnMan.h:75