Aikido
RNG.hpp
Go to the documentation of this file.
1 #ifndef AIKIDO_COMMON_RNG_HPP_
2 #define AIKIDO_COMMON_RNG_HPP_
3 
4 #include <cassert>
5 #include <cmath>
6 #include <cstdint>
7 #include <memory>
8 #include <random>
9 
10 #include <Eigen/Geometry>
11 
13 
14 namespace aikido {
15 namespace common {
16 
18 
19 constexpr int NUM_DEFAULT_SEEDS{100};
21 
24 class RNG
25 {
26 public:
27  using result_type = std::uint32_t;
28 
32  static constexpr std::size_t NUM_BITS{32};
33 
34  virtual ~RNG() = default;
35 
39  static constexpr result_type min();
40 
44  static constexpr result_type max();
45 
49  virtual result_type operator()() = 0;
50 
54  virtual void discard(unsigned long long _z) = 0;
55 
59  virtual std::unique_ptr<RNG> clone() const = 0;
60 
65  virtual std::unique_ptr<RNG> clone(result_type _seed) const = 0;
66 };
67 
71 template <class T>
72 class RNGWrapper : virtual public RNG
73 {
74 public:
75  using RNG::result_type;
76  using engine_type
77  = std::independent_bits_engine<T, RNG::NUM_BITS, result_type>;
78 
80  RNGWrapper() = default;
81 
85  explicit RNGWrapper(const T& _rng);
86 
90  explicit RNGWrapper(result_type _seed);
91 
92  virtual ~RNGWrapper() = default;
93 
97  T& rng();
98 
102  const T& rng() const;
103 
104  // Documentation inherited.
105  result_type operator()() override;
106 
107  // Documentation inherited.
108  void discard(unsigned long long _z) override;
109 
110  // Documentation inherited.
111  std::unique_ptr<RNG> clone() const override;
112 
113  // Documentation inherited.
114  std::unique_ptr<RNG> clone(result_type _seed) const override;
115 
116 private:
118 };
119 
129 template <
130  class Engine,
131  class Scalar,
132  class Quaternion = Eigen::Quaternion<Scalar>>
133 Quaternion sampleQuaternion(
134  Engine& _engine, std::uniform_real_distribution<Scalar>& _distribution);
135 
145 std::vector<std::unique_ptr<common::RNG>> cloneRNGsFrom(
146  RNG& _engine,
147  std::size_t _numOutputs,
148  std::size_t _numSeeds = NUM_DEFAULT_SEEDS);
149 
158 std::vector<std::unique_ptr<common::RNG>> cloneRNGFrom(
159  RNG& _engine, std::size_t _numSeeds = NUM_DEFAULT_SEEDS);
160 
161 } // namespace common
162 } // namespace aikido
163 
164 #include "detail/RNG-impl.hpp"
165 
166 #endif // AIKIDO_COMMON_RNG_HPP_
aikido::common::RNGWrapper::clone
std::unique_ptr< RNG > clone() const override
Create a copy of this RNG, including its internal state.
Definition: RNG-impl.hpp:62
aikido::common::NUM_DEFAULT_SEEDS
constexpr int NUM_DEFAULT_SEEDS
Default number of seeds to by cloneRNGsFrom to seed new engines.
Definition: RNG.hpp:20
aikido::common::cloneRNGsFrom
std::vector< std::unique_ptr< common::RNG > > cloneRNGsFrom(RNG &_engine, std::size_t _numOutputs, std::size_t _numSeeds=NUM_DEFAULT_SEEDS)
Deterministically create different _numOutputs random number generators of the same type as the input...
aikido::common::RNG::operator()
virtual result_type operator()()=0
Advances the state of the engine and returns the generated value.
aikido::common::RNGWrapper::RNGWrapper
RNGWrapper()=default
Constructs a random engine with a default seed.
aikido::common::RNG::max
static constexpr result_type max()
Gets the largest possible value in the output range, 2^NUM_BITS - 1.
Definition: RNG-impl.hpp:13
aikido::common::RNG::~RNG
virtual ~RNG()=default
aikido
Format of serialized trajectory in YAML.
Definition: algorithm.hpp:4
aikido::common::RNGWrapper::engine_type
std::independent_bits_engine< T, RNG::NUM_BITS, result_type > engine_type
Definition: RNG.hpp:77
aikido::common::RNGWrapper
Concrete implementation of the RNG type erasure class.
Definition: RNG.hpp:72
aikido::common::RNG::min
static constexpr result_type min()
Gets the smallest possible value in the output range, always zero.
Definition: RNG-impl.hpp:7
aikido::common::RNGWrapper::operator()
result_type operator()() override
Advances the state of the engine and returns the generated value.
Definition: RNG-impl.hpp:48
aikido::common::RNG::NUM_BITS
static constexpr std::size_t NUM_BITS
Number of bits the generated numbers should have.
Definition: RNG.hpp:32
aikido::common::RNGWrapper::discard
void discard(unsigned long long _z) override
Advances the adaptor's state by a specified amount.
Definition: RNG-impl.hpp:55
aikido::common::RNG::discard
virtual void discard(unsigned long long _z)=0
Advances the adaptor's state by a specified amount.
aikido::common::RNGWrapper::rng
T & rng()
Gets the internal random engine.
Definition: RNG-impl.hpp:32
aikido::common::RNG
Implementation of the C++11 "random engine" concept that uses virtual function calls to erase the typ...
Definition: RNG.hpp:24
pointers.hpp
aikido::common::cloneRNGFrom
std::vector< std::unique_ptr< common::RNG > > cloneRNGFrom(RNG &_engine, std::size_t _numSeeds=NUM_DEFAULT_SEEDS)
Deterministically create a random number generator of the same type as the input _engine.
aikido::common::RNG::clone
virtual std::unique_ptr< RNG > clone() const =0
Create a copy of this RNG, including its internal state.
RNG-impl.hpp
aikido::common::sampleQuaternion
Quaternion sampleQuaternion(Engine &_engine, std::uniform_real_distribution< Scalar > &_distribution)
Sample a unit quaternion uniformly at random.
Definition: RNG-impl.hpp:76
aikido::common::RNGWrapper::mRng
engine_type mRng
Definition: RNG.hpp:117
aikido::common::RNGWrapper::~RNGWrapper
virtual ~RNGWrapper()=default
AIKIDO_DECLARE_POINTERS
#define AIKIDO_DECLARE_POINTERS(X)
Definition: pointers.hpp:21
aikido::common::RNG::result_type
std::uint32_t result_type
Definition: RNG.hpp:27