forked from Conedy/Conedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgslNoise.h
283 lines (201 loc) · 6.49 KB
/
gslNoise.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* last modifier: js */
#ifndef gslNoise_h
#define gslNoise_h gslNoise_h
#include <cmath>
#include<gsl/gsl_rng.h>
#include<gsl/gsl_randist.h>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#define STRINGIFY2( x) #x
#define STRINGIFY(x) STRINGIFY2(x)
#ifdef _WIN32
#include <time.h>
#else
#include <sys/time.h>
#include <time.h>
#endif
using namespace std;
using namespace boost;
//! Wrapper-klasse für die Zufallszahlen der GSL
class gslNoise {
private:
static gsl_rng *static_r;
static unsigned long theSeed;
public:
gslNoise() {
};
/* gslNoise(unsigned long seed)
{
gsl_rng_env_setup();
r=gsl_rng_alloc(gsl_rng_default);
theSeed = seed;
gsl_rng_set(r, seed);
}*/
static void initialise(unsigned long seed) {
gsl_rng_env_setup();
static_r=gsl_rng_alloc(gsl_rng_default);
gsl_rng_set(static_r, seed);
};
static void initialise() {
#ifdef _WIN32
//TODO better way to determine system time under windows and generate a random seed.
theSeed = rand();
#else
struct timeval tv;
gettimeofday(&tv, NULL);
srand((unsigned int)(tv.tv_sec + tv.tv_usec));
theSeed = rand();
#endif
gsl_rng_env_setup();
static_r=gsl_rng_alloc(gsl_rng_default);
cout << "#Using random Seed:" << theSeed << endl;
#ifdef SVN_REV
// printf( "Using svn Rev.: %s\n", STRINGIFY(SVN_REV)); // FIXME
cout << "#Version: " << STRINGIFY( SVN_REV) << endl; //FIXME
#endif
#ifdef ARCHITECTURE
cout << "#Architecture: " << STRINGIFY( ARCHITECTURE) << endl;
#endif
// gsl_rng_set(static_r, 3);
gsl_rng_set(static_r, theSeed);
};
static unsigned long getSeed() { return theSeed; };
// static void setSeed(unsigned long seed) { gsl_rng_set(static_r, seed); };
static void setSeed(long seed) { theSeed = seed; gsl_rng_set(static_r, seed); };
static double getUniform(double lower, double upper) {return gsl_rng_uniform(static_r) * (upper-lower) + lower; };
static double getGaussian(double mean, double sigma) { return gsl_ran_gaussian(static_r, 1.0)* sigma + mean; }
static double getBimodal(double lower, double upper, double prob) { return (gsl_rng_uniform(static_r) > prob) ? lower:upper; }
static double getPowerLaw(double exponent, double min) { double bound = pow(min, exponent+1)/fabs(exponent+1); return pow (fabs((exponent + 1))*(bound *getUniform(0.0,1.0)), 1/(exponent+1)); }
static double getConstant (double value) { return value; }
static int getUniformInt(int a, int b) { return (a+ (int)gsl_rng_uniform_int (static_r, b-a + 1) ); };
// Funktions
static function<double () > functionUniform (double lower, double upper) { return bind(&gslNoise::getUniform, lower, upper); }
static function<double () > functionGaussian (double mean, double sigma) { return bind(&gslNoise::getGaussian, mean, sigma); }
static function<double () > functionConstant (double mean) { return bind(&gslNoise::getConstant, mean); }
static function<double () > functionBimodal (double lower, double upper, double prob) { return bind (&gslNoise::getBimodal, lower, upper, prob); }
static function<double () > functionPowerLaw (double exponent, double bound) { return bind (&gslNoise::getPowerLaw, exponent, bound); }
static double getGaussian() { return gsl_ran_gaussian(static_r, 1.0); };
static double getUniform() { return gsl_rng_uniform(static_r); }
static double getExponential(double lambda) { return (gsl_ran_exponential (static_r, lambda)); }
static int getPoisson (double mu) {return gsl_ran_poisson(static_r,mu); }
static double getPoisson (double mu,double faktor) {
double re = ((double)gsl_ran_poisson(static_r,mu)) * faktor;
return re;
}
static void free()
{
gsl_rng_free(static_r);
}
};
//! Abstrakte Basisklasse für Zufallszahl-objekte. Der Klammeroperator gibt die nächste Zufallszahl zurück. Unterschiedliche Arten von Zufallszahlen (gauss-verteilt, poissonverteilt) werden in Erben von randomNumber implementiert.
template <typename T>
class randomNumber
{
protected:
static gslNoise noise;
public:
randomNumber() {};
virtual T operator() () { return (T)0;};
virtual ~randomNumber() {};
};
//! Funktionsobjekt, das gleichverteilte Zufallszahlen zurückgibt.
template <typename T>
class uniform : public randomNumber<T>
{
private:
T lower, upper;
public:
uniform (T a, T b) : randomNumber<T>(), lower(a), upper(b) {};
T operator()() { return ((T) (lower + (upper-lower)*randomNumber<T>::noise.getUniform())); }
virtual ~uniform() {};
};
template <typename T>
class bimodal : public randomNumber<T>
{
private:
T lower;
T upper;
double prob;
public:
bimodal (T l, T u, double p): randomNumber<T>(), lower(l), upper(u), prob(p) {};
T operator()()
{
if (randomNumber<T>::noise.getUniform() < prob)
return lower;
else
return upper;
}
};
template <typename T>
class binary : public randomNumber<T>
{
private:
T value;
double prob;
public:
binary (T v, double p) : randomNumber<T>(), value(v), prob(p) {};
T operator()()
{
if (randomNumber<T>::noise.getUniform() < prob)
return 0;
else return value;
}
};
//#
//#template <typename T>
//#class gaussian : public randomNumber<T>
//#{
//# private:
//# T mean, sigma;
//# public:
//#
//# static void * operator new (size_t size);
//# gaussian (T a, T b) : randomNumber<T>(), mean(a), sigma(b) {};
//# T operator()() { return ((T) (mean + sigma*randomNumber<T>::noise.getGaussian())); }
//# virtual ~gaussian() {};
//#
//#};
//#
//
class uniformInt
{
private:
gslNoise noise;
int lower, upper;
public:
uniformInt(int a, int b) : lower(a), upper(b) {};
int operator()() { return noise.getUniformInt(lower,upper);}
};
class uniformDouble
{
private:
gslNoise noise;
double lower, upper;
public:
uniformDouble(int a, int b) : lower(a), upper(b) {};
double operator()() { return (lower + (upper-lower)*noise.getUniform()) ; };
};
//! gibt mit Wahrscheinlichkeit a 1 zurück, andernfalls 0
class uniformBool
{
private:
gslNoise noise;
double prop;
public:
uniformBool(double a) : prop(a) {};
bool operator()() { return ((noise.getUniform() < prop) ? 1:0); }
};
class xToTheAlpha
{
private:
gslNoise noise;
double alpha;
double bound;
public:
xToTheAlpha(double a, int min) : alpha(a) { bound = pow(min, alpha+1)/fabs(alpha+1); }; // a: obere Grenze
double operator()() { return pow (fabs((alpha + 1))*(bound *noise.getUniform()), 1/(alpha+1)); }
};
template <class T>
gslNoise randomNumber<T>::noise;
#endif