VPTissue Reference Manual
RandomEngine.h
Go to the documentation of this file.
1 #ifndef SIMPT_MATH_RANDOM_ENGINE_H_INCLUDED
2 #define SIMPT_MATH_RANDOM_ENGINE_H_INCLUDED
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
21 #include "RandomEngineType.h"
22 
23 #include <trng/lcg64_shift.hpp>
24 #include <trng/lcg64.hpp>
25 #include <trng/mrg2.hpp>
26 #include <trng/mrg3.hpp>
27 #include <trng/yarn2.hpp>
28 #include <trng/yarn3.hpp>
29 #include <boost/property_tree/ptree.hpp>
30 #include <functional>
31 #include <string>
32 #include <vector>
33 
34 namespace SimPT_Sim {
35 
49 {
50 public:
54  RandomEngine();
55 
60 
67  template<typename D>
68  std::function<typename D::result_type()> GetGenerator(const D& d, int i = 0)
69  {
70  using namespace RandomEngineType;
71 
72  std::function<typename D::result_type()> gen;
73  switch (m_type_id) {
74  case TypeId::lcg64 :
75  gen = std::bind(d, std::ref(m_lcg64[i])); break;
76  case TypeId::lcg64_shift :
77  gen = std::bind(d, std::ref(m_lcg64_shift[i])); break;
78  case TypeId::mrg2 :
79  gen = std::bind(d, std::ref(m_mrg2[i])); break;
80  case TypeId::mrg3 :
81  gen = std::bind(d, std::ref(m_mrg3[i])); break;
82  case TypeId::yarn2 :
83  gen = std::bind(d, std::ref(m_yarn2[i])); break;
84  case TypeId::yarn3 :
85  gen = std::bind(d, std::ref(m_yarn3[i])); break;
86  }
87  return gen;
88  }
89 
93  bool Reinitialize();
94 
98  bool Reinitialize(const boost::property_tree::ptree& ptree);
99 
100 private:
102  void SetState(unsigned long);
103 
105  void SetState(const std::string& state);
106 
107  unsigned long m_seed;
108  RandomEngineType::TypeId m_type_id;
109 
110 private:
111  std::vector<trng::lcg64> m_lcg64;
112  std::vector<trng::lcg64_shift> m_lcg64_shift;
113  std::vector<trng::mrg2> m_mrg2;
114  std::vector<trng::mrg3> m_mrg3;
115  std::vector<trng::yarn2> m_yarn2;
116  std::vector<trng::yarn3> m_yarn3;
117 };
118 
119 } // namespace
120 
121 #endif // end-of-include-guard
RandomEngine()
Initializes to default engine( trng::mrg2 with seed 1).
Namespace for the core simulator.
bool Reinitialize()
Reset to the default engine (trng::mrg2 with seed 1).
std::function< typename D::result_type()> GetGenerator(const D &d, int i=0)
Produce a random generator out of a distribution bound to sim's random engine.
Definition: RandomEngine.h:68
TypeId
Enumerates type ids.
Interface of RandomEngineType.
Creates a random generator based on a seed.
Definition: RandomEngine.h:48
RandomEngineType::Info GetInfo() const
Return the state of the random engine.