source: pacpusframework/trunk/include/Pacpus/PacpusTools/math/rng.hpp

Last change on this file was 89, checked in by morasjul, 11 years ago

PACPUS 2.0 Beta deployed in new branch

Major changes:
-Add communication interface between components
-Add examples for communications interface (TestComponents)
-Move to Qt5 support

  • Property svn:executable set to *
File size: 7.4 KB
Line 
1// %pacpus:license{
2// This file is part of the PACPUS framework distributed under the
3// CECILL-C License, Version 1.0.
4// %pacpus:license}
5/// @file
6/// @author Firstname Surname <firstname.surname@utc.fr>
7/// @date Month, Year
8/// @version $Id: rng.hpp 76 2013-01-10 17:05:10Z kurdejma $
9/// @copyright Copyright (c) UTC/CNRS Heudiasyc 2006 - 2013. All rights reserved.
10/// @brief Random number generators.
11///
12/// Detailed description.
13
14#ifndef __BOOST_RNG__
15#define __BOOST_RNG__
16
17#include <boost/random.hpp>
18#include <boost/math/distributions.hpp>
19#include <ctime>
20
21namespace math {
22
23namespace rng {
24
25/// This class is basic normal random number generator
26///
27/// @tparam RealType describe number precision, e.g. @c float, @c double
28/// @tparam PRNG describe pseudo random number generator
29/// @see boost::random
30template <class RealType, class PRNG>
31class normal_generator
32{
33public:
34 /// Default construtor
35 normal_generator()
36 : engine(time(NULL))
37 , generator(engine,boost::normal_distribution<RealType>(0,1))
38 {}
39
40 /// Construtor
41 ///
42 /// @param dist a normal distribution
43 /// @see boost::distributions
44 normal_generator(const boost::math::normal_distribution<RealType> & dist)
45 : engine(time(NULL))
46 , generator(engine,boost::normal_distribution<RealType>(dist.mean(),dist.sigma()))
47 {}
48
49 /// Constructor
50 ///
51 /// @param mean mean of the normal distribution
52 /// @param sigma standard deviation of the normal distribution
53 normal_generator(const RealType & mean ,const RealType & sigma)
54 : engine(time(NULL))
55 , generator(engine,boost::normal_distribution<RealType>(mean(),sigma()))
56 {}
57
58 /// Get a random number
59 RealType operator()()
60 {
61 return generator();
62 }
63
64 /// Set normal distribution
65 ///
66 /// @param dist : a normal distribution
67 /// @see boost::distributions
68 void distribution( const boost::math::normal_distribution<RealType> & dist)
69 {
70 generator.dist= boost::normal_distribution<RealType>(dist.mean(),dist.standard_deviation());
71 }
72
73 /// Set normal distrubution
74 ///
75 /// @param mean : mean of the normal distribution
76 /// @param sigma : satndard deviation of the normal distribution
77 void distribution( const RealType & mean ,const RealType & sigma) {
78 generator.distribution()= boost::normal_distribution<RealType>(mean,sigma);
79 }
80
81private:
82 /// pseudorandom number generator
83 PRNG engine;
84
85 /// the normal random number generator
86 boost::variate_generator<PRNG, boost::normal_distribution<RealType> > generator;
87};
88
89/// Defines normal random number generator as a Mersenne Twister 19937 with double-precision numbers
90typedef class normal_generator<double, boost::mt19937> normal_rng;
91
92/// This class is basic uniform random number generator.
93///
94/// @tparam RealType describe number precision, e.g. @c float, @c double
95/// @tparam PRNG describe pseudo random number generator
96/// @see boost::random
97template <class RealType, class PRNG>
98class uniform_generator
99{
100public:
101 /// Default construtor
102 uniform_generator()
103 : engine(time(NULL))
104 , generator(engine, boost::uniform_real<RealType>(0,1))
105 {}
106
107 /// Construtor
108 /// @param dist a uniform distribution
109 /// @see boost::distributions
110 uniform_generator(const boost::math::uniform_distribution<RealType> & dist)
111 : engine(time(NULL))
112 , generator(engine, boost::uniform_real<RealType>(dist.lower(),dist.upper()))
113 {}
114
115 /// Constructor
116 ///
117 /// @param lower the lower value of the uniform distribution
118 /// @param upper the upper value of the uniform distribution
119 uniform_generator(const RealType & lower , const RealType & upper)
120 : engine(time(NULL))
121 , generator(engine, boost::uniform_real<RealType>(lower,upper))
122 {}
123
124 /// Get a random number
125 RealType operator()()
126 {
127 return generator();
128 }
129
130 /// Set uniform distribution
131 ///
132 /// @param dist a normal distribution
133 /// @see boost::distributions
134 void distribution( const boost::math::uniform_distribution<RealType> & dist)
135 {
136 generator.dist()= boost::uniform_real<RealType>(dist.lower(),dist.upper());
137 }
138
139 /// Set uniform distribution
140 ///
141 /// @param lower the lower value of the uniform distribution
142 /// @param upper the upper value of the uniform distribution
143 void distribution( const RealType & lower , const RealType & upper)
144 {
145 generator.dist()= boost::uniform_real<RealType>(lower,upper);
146 }
147
148private:
149 /// @brief pseudo random number generator
150 PRNG engine;
151 /// @brief the uniform random number generator
152 boost::variate_generator<PRNG, boost::uniform_real<RealType> > generator;
153};
154
155/// Defines uniform random number generator as a Mersenne Twister 19937 with double-precision numbers
156typedef class uniform_generator<double, boost::mt19937> uniform_rng;
157
158/// This class is basic triangular random number generator
159///
160/// @tparam RealType describe number precision, e.g. @c float, @c double
161/// @tparam PRNG describe pseudo random number generator ( see boost random)
162template <class RealType, class PRNG>
163class triangle_generator
164{
165public:
166 /// Default construtor
167 triangle_generator()
168 : engine(time(NULL))
169 , generator(engine, boost::triangle_distribution<RealType>(-1,0,1))
170 {}
171
172 /// Construtor
173 ///
174 /// @param dist : a traingle distribution
175 /// @see boost distributions
176 triangle_generator(const boost::math::triangular_distribution<RealType> & dist)
177 : engine(time(NULL))
178 ,generator(engine, boost::triangle_distribution<RealType>(dist.lower(),dist.upper()))
179 {}
180
181 /// Constructor
182 ///
183 /// @param lower the lower value of the uniform distribution
184 /// @param mode the mode value of the uniform distribution
185 /// @param upper the upper value of the uniform distribution
186 triangle_generator(const RealType & lower, const RealType & mode ,const RealType & upper)
187 : engine(time(NULL))
188 , generator(engine, boost::triangle_distribution<RealType>(lower,mode,upper))
189 {}
190
191 /// Get a random number
192 RealType operator()()
193 {
194 return generator();
195 }
196
197 /// Set triangle distribution
198 /// @param dist a normal distribution
199 /// @see boost::distributions
200 void distribution( const boost::math::triangular_distribution<RealType> & dist)
201 {
202 generator.dist()= boost::triangle_distribution<RealType>(dist.lower(),dist.mode(),dist.upper());
203 }
204
205 /// Set triangle distribution
206 /// @param lower the lower value of the uniform distribution
207 /// @param mode the mode value of the uniform distribution
208 /// @param upper the upper value of the uniform distribution
209 void distribution( const RealType & lower, const RealType & mode ,const RealType & upper)
210 {
211 generator.dist()= boost::triangle_distribution<RealType>(lower,mode,upper);
212 }
213
214private :
215 /// pseudorandom number generator
216 PRNG engine;
217 /// the triangle random number generator
218 boost::variate_generator<PRNG, boost::triangle_distribution<RealType> > generator;
219};
220
221typedef class triangle_generator<double, boost::mt19937> triangle_rng;
222
223} // namespace rng
224} // namespace math
225
226#endif // __BOOST_RNG__
Note: See TracBrowser for help on using the repository browser.