00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <iostream>
00022
00023 #include <math.h>
00024 #include <stdlib.h>
00025 #include <sys/time.h>
00026 #include <unistd.h>
00027 #include <iostream>
00028 #include <sys/types.h>
00029 #include <sys/stat.h>
00030 #include <fcntl.h>
00031 #include <sstream>
00032
00033
00034 #include "assert.h"
00035 #include "random-variable.h"
00036 #include "rng-stream.h"
00037 #include "fatal-error.h"
00038
00039 using namespace std;
00040
00041 namespace ns3{
00042
00043
00044
00045
00046
00047
00048 class RandomVariableBase
00049 {
00050 public:
00051 RandomVariableBase ();
00052 RandomVariableBase (const RandomVariableBase &o);
00053 virtual ~RandomVariableBase();
00054 virtual double GetValue() = 0;
00055 virtual uint32_t GetInteger();
00056 virtual RandomVariableBase* Copy(void) const = 0;
00057 virtual void GetSeed(uint32_t seed[6]);
00058
00059 static void UseDevRandom(bool udr = true);
00060 static void UseGlobalSeed(uint32_t s0, uint32_t s1, uint32_t s2,
00061 uint32_t s3, uint32_t s4, uint32_t s5);
00062 static void SetRunNumber(uint32_t n);
00063 private:
00064 static void GetRandomSeeds(uint32_t seeds[6]);
00065 private:
00066 static bool useDevRandom;
00067 static bool globalSeedSet;
00068 static int devRandom;
00069 static uint32_t globalSeed[6];
00070 friend class RandomVariableInitializer;
00071 protected:
00072 static unsigned long heuristic_sequence;
00073 static RngStream* m_static_generator;
00074 static uint32_t runNumber;
00075 static void Initialize();
00076 static bool initialized;
00077 RngStream* m_generator;
00078 };
00079
00080
00081
00082 bool RandomVariableBase::initialized = false;
00083 bool RandomVariableBase::useDevRandom = false;
00084 bool RandomVariableBase::globalSeedSet = false;
00085 int RandomVariableBase::devRandom = -1;
00086 uint32_t RandomVariableBase::globalSeed[6];
00087 unsigned long RandomVariableBase::heuristic_sequence;
00088 RngStream* RandomVariableBase::m_static_generator = 0;
00089 uint32_t RandomVariableBase::runNumber = 0;
00090
00091
00092
00093 static class RandomVariableInitializer
00094 {
00095 public:
00096 RandomVariableInitializer()
00097 {
00098
00099
00100
00101 }
00102 ~RandomVariableInitializer()
00103 {
00104 delete RandomVariableBase::m_static_generator;
00105 }
00106 } random_variable_initializer;
00107
00108 RandomVariableBase::RandomVariableBase()
00109 : m_generator(NULL)
00110 {
00111
00112
00113
00114 }
00115
00116 RandomVariableBase::RandomVariableBase(const RandomVariableBase& r)
00117 :m_generator(0)
00118 {
00119 if(r.m_generator)
00120 {
00121 m_generator = new RngStream(*r.m_generator);
00122 }
00123 }
00124
00125 RandomVariableBase::~RandomVariableBase()
00126 {
00127 delete m_generator;
00128 }
00129
00130 uint32_t RandomVariableBase::GetInteger()
00131 {
00132 return (uint32_t)GetValue();
00133 }
00134
00135 void RandomVariableBase::UseDevRandom(bool udr)
00136 {
00137 RandomVariableBase::useDevRandom = udr;
00138 }
00139
00140 void RandomVariableBase::GetSeed(uint32_t seed[6])
00141 {
00142 if(!m_generator)
00143 {
00144 m_generator = new RngStream();
00145 m_generator->InitializeStream();
00146 m_generator->ResetNthSubstream(RandomVariableBase::runNumber);
00147 }
00148 m_generator->GetState(seed);
00149 }
00150
00151
00152
00153
00154 void RandomVariableBase::UseGlobalSeed(uint32_t s0, uint32_t s1, uint32_t s2,
00155 uint32_t s3, uint32_t s4, uint32_t s5)
00156 {
00157 if (RandomVariableBase::globalSeedSet)
00158 {
00159 NS_FATAL_ERROR ("Random number generator already initialized! "
00160 "Call to RandomVariableBase::UseGlobalSeed() ignored");
00161 }
00162 RandomVariableBase::globalSeed[0] = s0;
00163 RandomVariableBase::globalSeed[1] = s1;
00164 RandomVariableBase::globalSeed[2] = s2;
00165 RandomVariableBase::globalSeed[3] = s3;
00166 RandomVariableBase::globalSeed[4] = s4;
00167 RandomVariableBase::globalSeed[5] = s5;
00168 if (!RngStream::CheckSeed(RandomVariableBase::globalSeed))
00169 NS_FATAL_ERROR("Invalid seed");
00170
00171 RandomVariableBase::globalSeedSet = true;
00172 }
00173
00174 void RandomVariableBase::Initialize()
00175 {
00176 if (RandomVariableBase::initialized) return;
00177 RandomVariableBase::initialized = true;
00178 if (!RandomVariableBase::globalSeedSet)
00179 {
00180 GetRandomSeeds(globalSeed);
00181 }
00182
00183 RngStream::SetPackageSeed(globalSeed);
00184 }
00185
00186 void RandomVariableBase::GetRandomSeeds(uint32_t seeds[6])
00187 {
00188
00189 if (RandomVariableBase::useDevRandom && RandomVariableBase::devRandom < 0)
00190 {
00191 RandomVariableBase::devRandom = open("/dev/random", O_RDONLY);
00192 }
00193 if (RandomVariableBase::devRandom > 0)
00194 {
00195 while(true)
00196 {
00197 for (int i = 0; i < 6; ++i)
00198 {
00199 ssize_t bytes_read = read (RandomVariableBase::devRandom,
00200 &seeds[i], sizeof (seeds[i]));
00201 if (bytes_read != sizeof (seeds[i]))
00202 {
00203 NS_FATAL_ERROR ("Read from /dev/random failed");
00204 }
00205 }
00206 if (RngStream::CheckSeed(seeds)) break;
00207 }
00208 }
00209 else
00210 {
00211
00212 while(true)
00213 {
00214 timeval tv;
00215 gettimeofday(&tv, 0);
00216 seeds[0] = (tv.tv_sec^tv.tv_usec^(++heuristic_sequence <<8))
00217 & 0x7fffffff;
00218 gettimeofday(&tv, 0);
00219 seeds[1] = (tv.tv_sec^tv.tv_usec^(++heuristic_sequence <<8))
00220 & 0x7fffffff;
00221 gettimeofday(&tv, 0);
00222 seeds[2] = (tv.tv_sec^tv.tv_usec^(++heuristic_sequence <<8))
00223 & 0x7fffffff;
00224 gettimeofday(&tv, 0);
00225 seeds[3] = (tv.tv_sec^tv.tv_usec^(++heuristic_sequence <<8))
00226 & 0x7fffffff;
00227 gettimeofday(&tv, 0);
00228 seeds[4] = (tv.tv_sec^tv.tv_usec^(++heuristic_sequence <<8))
00229 & 0x7fffffff;
00230 gettimeofday(&tv, 0);
00231 seeds[5] = (tv.tv_sec^tv.tv_usec^(++heuristic_sequence <<8))
00232 & 0x7fffffff;
00233 if (RngStream::CheckSeed(seeds)) break;
00234 }
00235 }
00236 }
00237
00238 void RandomVariableBase::SetRunNumber(uint32_t n)
00239 {
00240 runNumber = n;
00241 }
00242
00243
00244
00245 RandomVariable::RandomVariable()
00246 : m_variable (0)
00247 {}
00248 RandomVariable::RandomVariable(const RandomVariable&o)
00249 : m_variable (o.m_variable->Copy ())
00250 {}
00251 RandomVariable::RandomVariable (const RandomVariableBase &variable)
00252 : m_variable (variable.Copy ())
00253 {}
00254 RandomVariable &
00255 RandomVariable::operator = (const RandomVariable &o)
00256 {
00257 if (&o == this)
00258 {
00259 return *this;
00260 }
00261 delete m_variable;
00262 m_variable = o.m_variable->Copy ();
00263 return *this;
00264 }
00265 RandomVariable::~RandomVariable()
00266 {
00267 delete m_variable;
00268 }
00269 double
00270 RandomVariable::GetValue (void) const
00271 {
00272 return m_variable->GetValue ();
00273 }
00274
00275 uint32_t
00276 RandomVariable::GetInteger (void) const
00277 {
00278 return m_variable->GetInteger ();
00279 }
00280 void
00281 RandomVariable::GetSeed(uint32_t seed[6]) const
00282 {
00283 return m_variable->GetSeed (seed);
00284 }
00285 void
00286 RandomVariable::UseDevRandom(bool udr)
00287 {
00288 RandomVariableBase::UseDevRandom (udr);
00289 }
00290 void
00291 RandomVariable::UseGlobalSeed(uint32_t s0, uint32_t s1, uint32_t s2,
00292 uint32_t s3, uint32_t s4, uint32_t s5)
00293 {
00294 RandomVariableBase::UseGlobalSeed (s0, s1, s2, s3, s4, s5);
00295 }
00296 void
00297 RandomVariable::SetRunNumber(uint32_t n)
00298 {
00299 RandomVariableBase::SetRunNumber (n);
00300 }
00301 RandomVariableBase *
00302 RandomVariable::Peek (void) const
00303 {
00304 return m_variable;
00305 }
00306
00307 ATTRIBUTE_VALUE_IMPLEMENT (RandomVariable);
00308 ATTRIBUTE_CHECKER_IMPLEMENT (RandomVariable);
00309
00310
00311
00312
00313
00314 class UniformVariableImpl : public RandomVariableBase {
00315 public:
00316
00317
00318
00319
00320 UniformVariableImpl();
00321
00322
00323
00324
00325
00326
00327 UniformVariableImpl(double s, double l);
00328
00329 UniformVariableImpl(const UniformVariableImpl& c);
00330
00331 double GetMin (void) const;
00332 double GetMax (void) const;
00333
00334
00335
00336
00337 virtual double GetValue();
00338 virtual RandomVariableBase* Copy(void) const;
00339
00340 public:
00341
00342
00343
00344
00345
00346 static double GetSingleValue(double s, double l);
00347 private:
00348 double m_min;
00349 double m_max;
00350 };
00351
00352 UniformVariableImpl::UniformVariableImpl()
00353 : m_min(0), m_max(1.0) { }
00354
00355 UniformVariableImpl::UniformVariableImpl(double s, double l)
00356 : m_min(s), m_max(l) { }
00357
00358 UniformVariableImpl::UniformVariableImpl(const UniformVariableImpl& c)
00359 : RandomVariableBase(c), m_min(c.m_min), m_max(c.m_max) { }
00360
00361 double
00362 UniformVariableImpl::GetMin (void) const
00363 {
00364 return m_min;
00365 }
00366 double
00367 UniformVariableImpl::GetMax (void) const
00368 {
00369 return m_max;
00370 }
00371
00372
00373 double UniformVariableImpl::GetValue()
00374 {
00375 if(!RandomVariableBase::initialized)
00376 {
00377 RandomVariableBase::Initialize();
00378 }
00379 if(!m_generator)
00380 {
00381 m_generator = new RngStream();
00382 m_generator->InitializeStream();
00383 m_generator->ResetNthSubstream(RandomVariableBase::runNumber);
00384 }
00385 return m_min + m_generator->RandU01() * (m_max - m_min);
00386 }
00387
00388 RandomVariableBase* UniformVariableImpl::Copy() const
00389 {
00390 return new UniformVariableImpl(*this);
00391 }
00392
00393 double UniformVariableImpl::GetSingleValue(double s, double l)
00394 {
00395 if(!RandomVariableBase::m_static_generator)
00396 {
00397 RandomVariableBase::Initialize();
00398 RandomVariableBase::m_static_generator = new RngStream();
00399 RandomVariableBase::m_static_generator->InitializeStream();
00400 }
00401 return s + m_static_generator->RandU01() * (l - s);;
00402 }
00403
00404 UniformVariable::UniformVariable()
00405 : RandomVariable (UniformVariableImpl ())
00406 {}
00407 UniformVariable::UniformVariable(double s, double l)
00408 : RandomVariable (UniformVariableImpl (s, l))
00409 {}
00410 double
00411 UniformVariable::GetSingleValue(double s, double l)
00412 {
00413 return UniformVariableImpl::GetSingleValue (s, l);
00414 }
00415
00416
00417
00418
00419
00420
00421 class ConstantVariableImpl : public RandomVariableBase {
00422
00423 public:
00424
00425
00426
00427 ConstantVariableImpl();
00428
00429
00430
00431
00432
00433
00434 ConstantVariableImpl(double c);
00435
00436
00437 ConstantVariableImpl(const ConstantVariableImpl& c) ;
00438
00439
00440
00441
00442
00443 void NewConstant(double c);
00444
00445
00446
00447
00448 virtual double GetValue();
00449 virtual uint32_t GetInteger();
00450 virtual RandomVariableBase* Copy(void) const;
00451 private:
00452 double m_const;
00453 };
00454
00455 ConstantVariableImpl::ConstantVariableImpl()
00456 : m_const(0) { }
00457
00458 ConstantVariableImpl::ConstantVariableImpl(double c)
00459 : m_const(c) { };
00460
00461 ConstantVariableImpl::ConstantVariableImpl(const ConstantVariableImpl& c)
00462 : RandomVariableBase(c), m_const(c.m_const) { }
00463
00464 void ConstantVariableImpl::NewConstant(double c)
00465 { m_const = c;}
00466
00467 double ConstantVariableImpl::GetValue()
00468 {
00469 return m_const;
00470 }
00471
00472 uint32_t ConstantVariableImpl::GetInteger()
00473 {
00474 return (uint32_t)m_const;
00475 }
00476
00477 RandomVariableBase* ConstantVariableImpl::Copy() const
00478 {
00479 return new ConstantVariableImpl(*this);
00480 }
00481
00482 ConstantVariable::ConstantVariable()
00483 : RandomVariable (ConstantVariableImpl ())
00484 {}
00485 ConstantVariable::ConstantVariable(double c)
00486 : RandomVariable (ConstantVariableImpl (c))
00487 {}
00488 void
00489 ConstantVariable::SetConstant(double c)
00490 {
00491 *this = ConstantVariable (c);
00492 }
00493
00494
00495
00496
00497
00498
00499 class SequentialVariableImpl : public RandomVariableBase {
00500
00501 public:
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513 SequentialVariableImpl(double f, double l, double i = 1, uint32_t c = 1);
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525 SequentialVariableImpl(double f, double l, const RandomVariable& i, uint32_t c = 1);
00526
00527 SequentialVariableImpl(const SequentialVariableImpl& c);
00528
00529 ~SequentialVariableImpl();
00530
00531
00532
00533 virtual double GetValue();
00534 virtual RandomVariableBase* Copy(void) const;
00535 private:
00536 double m_min;
00537 double m_max;
00538 RandomVariable m_increment;
00539 uint32_t m_consecutive;
00540 double m_current;
00541 uint32_t m_currentConsecutive;
00542 };
00543
00544 SequentialVariableImpl::SequentialVariableImpl(double f, double l, double i, uint32_t c)
00545 : m_min(f), m_max(l), m_increment(ConstantVariable(i)), m_consecutive(c),
00546 m_current(f), m_currentConsecutive(0)
00547 {}
00548
00549 SequentialVariableImpl::SequentialVariableImpl(double f, double l, const RandomVariable& i, uint32_t c)
00550 : m_min(f), m_max(l), m_increment(i), m_consecutive(c),
00551 m_current(f), m_currentConsecutive(0)
00552 {}
00553
00554 SequentialVariableImpl::SequentialVariableImpl(const SequentialVariableImpl& c)
00555 : RandomVariableBase(c), m_min(c.m_min), m_max(c.m_max),
00556 m_increment(c.m_increment), m_consecutive(c.m_consecutive),
00557 m_current(c.m_current), m_currentConsecutive(c.m_currentConsecutive)
00558 {}
00559
00560 SequentialVariableImpl::~SequentialVariableImpl()
00561 {}
00562
00563 double SequentialVariableImpl::GetValue()
00564 {
00565 double r = m_current;
00566 if (++m_currentConsecutive == m_consecutive)
00567 {
00568 m_currentConsecutive = 0;
00569 m_current += m_increment.GetValue();
00570 if (m_current >= m_max)
00571 m_current = m_min + (m_current - m_max);
00572 }
00573 return r;
00574 }
00575
00576 RandomVariableBase* SequentialVariableImpl::Copy() const
00577 {
00578 return new SequentialVariableImpl(*this);
00579 }
00580
00581 SequentialVariable::SequentialVariable(double f, double l, double i, uint32_t c)
00582 : RandomVariable (SequentialVariableImpl (f, l, i, c))
00583 {}
00584 SequentialVariable::SequentialVariable(double f, double l, const RandomVariable& i, uint32_t c)
00585 : RandomVariable (SequentialVariableImpl (f, l, i, c))
00586 {}
00587
00588
00589
00590
00591
00592 class ExponentialVariableImpl : public RandomVariableBase {
00593 public:
00594
00595
00596
00597
00598 ExponentialVariableImpl();
00599
00600
00601
00602
00603
00604 explicit ExponentialVariableImpl(double m);
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617 ExponentialVariableImpl(double m, double b);
00618
00619 ExponentialVariableImpl(const ExponentialVariableImpl& c);
00620
00621
00622
00623
00624 virtual double GetValue();
00625 virtual RandomVariableBase* Copy(void) const;
00626 public:
00627
00628
00629
00630
00631
00632 static double GetSingleValue(double m, double b=0);
00633 private:
00634 double m_mean;
00635 double m_bound;
00636 };
00637
00638 ExponentialVariableImpl::ExponentialVariableImpl()
00639 : m_mean(1.0), m_bound(0) { }
00640
00641 ExponentialVariableImpl::ExponentialVariableImpl(double m)
00642 : m_mean(m), m_bound(0) { }
00643
00644 ExponentialVariableImpl::ExponentialVariableImpl(double m, double b)
00645 : m_mean(m), m_bound(b) { }
00646
00647 ExponentialVariableImpl::ExponentialVariableImpl(const ExponentialVariableImpl& c)
00648 : RandomVariableBase(c), m_mean(c.m_mean), m_bound(c.m_bound) { }
00649
00650 double ExponentialVariableImpl::GetValue()
00651 {
00652 if(!RandomVariableBase::initialized)
00653 {
00654 RandomVariableBase::Initialize();
00655 }
00656 if(!m_generator)
00657 {
00658 m_generator = new RngStream();
00659 m_generator->InitializeStream();
00660 m_generator->ResetNthSubstream(RandomVariableBase::runNumber);
00661 }
00662 double r = -m_mean*log(m_generator->RandU01());
00663 if (m_bound != 0 && r > m_bound) return m_bound;
00664 return r;
00665 }
00666
00667 RandomVariableBase* ExponentialVariableImpl::Copy() const
00668 {
00669 return new ExponentialVariableImpl(*this);
00670 }
00671 double ExponentialVariableImpl::GetSingleValue(double m, double b)
00672 {
00673 if(!RandomVariableBase::m_static_generator)
00674 {
00675 RandomVariableBase::Initialize();
00676 RandomVariableBase::m_static_generator = new RngStream();
00677 RandomVariableBase::m_static_generator->InitializeStream();
00678 }
00679 double r = -m*log(m_static_generator->RandU01());
00680 if (b != 0 && r > b) return b;
00681 return r;
00682 }
00683
00684 ExponentialVariable::ExponentialVariable()
00685 : RandomVariable (ExponentialVariableImpl ())
00686 {}
00687 ExponentialVariable::ExponentialVariable(double m)
00688 : RandomVariable (ExponentialVariableImpl (m))
00689 {}
00690 ExponentialVariable::ExponentialVariable(double m, double b)
00691 : RandomVariable (ExponentialVariableImpl (m, b))
00692 {}
00693 double
00694 ExponentialVariable::GetSingleValue(double m, double b)
00695 {
00696 return ExponentialVariableImpl::GetSingleValue (m, b);
00697 }
00698
00699
00700
00701
00702 class ParetoVariableImpl : public RandomVariableBase {
00703 public:
00704
00705
00706
00707
00708 ParetoVariableImpl();
00709
00710
00711
00712
00713
00714
00715 explicit ParetoVariableImpl(double m);
00716
00717
00718
00719
00720
00721
00722
00723 ParetoVariableImpl(double m, double s);
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737 ParetoVariableImpl(double m, double s, double b);
00738
00739 ParetoVariableImpl(const ParetoVariableImpl& c);
00740
00741
00742
00743
00744 virtual double GetValue();
00745 virtual RandomVariableBase* Copy() const;
00746 public:
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756 static double GetSingleValue(double m, double s, double b=0);
00757 private:
00758 double m_mean;
00759 double m_shape;
00760 double m_bound;
00761 };
00762
00763 ParetoVariableImpl::ParetoVariableImpl()
00764 : m_mean(1.0), m_shape(1.5), m_bound(0) { }
00765
00766 ParetoVariableImpl::ParetoVariableImpl(double m)
00767 : m_mean(m), m_shape(1.5), m_bound(0) { }
00768
00769 ParetoVariableImpl::ParetoVariableImpl(double m, double s)
00770 : m_mean(m), m_shape(s), m_bound(0) { }
00771
00772 ParetoVariableImpl::ParetoVariableImpl(double m, double s, double b)
00773 : m_mean(m), m_shape(s), m_bound(b) { }
00774
00775 ParetoVariableImpl::ParetoVariableImpl(const ParetoVariableImpl& c)
00776 : RandomVariableBase(c), m_mean(c.m_mean), m_shape(c.m_shape),
00777 m_bound(c.m_bound) { }
00778
00779 double ParetoVariableImpl::GetValue()
00780 {
00781 if(!RandomVariableBase::initialized)
00782 {
00783 RandomVariableBase::Initialize();
00784 }
00785 if(!m_generator)
00786 {
00787 m_generator = new RngStream();
00788 m_generator->InitializeStream();
00789 m_generator->ResetNthSubstream(RandomVariableBase::runNumber);
00790 }
00791 double scale = m_mean * ( m_shape - 1.0) / m_shape;
00792 double r = (scale * ( 1.0 / pow(m_generator->RandU01(), 1.0 / m_shape)));
00793 if (m_bound != 0 && r > m_bound) return m_bound;
00794 return r;
00795 }
00796
00797 RandomVariableBase* ParetoVariableImpl::Copy() const
00798 {
00799 return new ParetoVariableImpl(*this);
00800 }
00801
00802 double ParetoVariableImpl::GetSingleValue(double m, double s, double b)
00803 {
00804 if(!RandomVariableBase::m_static_generator)
00805 {
00806 RandomVariableBase::Initialize();
00807 RandomVariableBase::m_static_generator = new RngStream();
00808 RandomVariableBase::m_static_generator->InitializeStream();
00809 }
00810 double scale = m * ( s - 1.0) / s;
00811 double r = (scale * ( 1.0 / pow(m_static_generator->RandU01(), 1.0 / s)));
00812 if (b != 0 && r > b) return b;
00813 return r;
00814 }
00815
00816 ParetoVariable::ParetoVariable ()
00817 : RandomVariable (ParetoVariableImpl ())
00818 {}
00819 ParetoVariable::ParetoVariable(double m)
00820 : RandomVariable (ParetoVariableImpl (m))
00821 {}
00822 ParetoVariable::ParetoVariable(double m, double s)
00823 : RandomVariable (ParetoVariableImpl (m, s))
00824 {}
00825 ParetoVariable::ParetoVariable(double m, double s, double b)
00826 : RandomVariable (ParetoVariableImpl (m, s, b))
00827 {}
00828 double
00829 ParetoVariable::GetSingleValue(double m, double s, double b)
00830 {
00831 return ParetoVariableImpl::GetSingleValue (m, s, b);
00832 }
00833
00834
00835
00836
00837
00838 class WeibullVariableImpl : public RandomVariableBase {
00839 public:
00840
00841
00842
00843
00844 WeibullVariableImpl();
00845
00846
00847
00848
00849
00850
00851
00852 WeibullVariableImpl(double m) ;
00853
00854
00855
00856
00857
00858
00859
00860 WeibullVariableImpl(double m, double s);
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873 WeibullVariableImpl(double m, double s, double b);
00874
00875 WeibullVariableImpl(const WeibullVariableImpl& c);
00876
00877
00878
00879
00880 virtual double GetValue();
00881 virtual RandomVariableBase* Copy(void) const;
00882 public:
00883
00884
00885
00886
00887
00888
00889 static double GetSingleValue(double m, double s, double b=0);
00890 private:
00891 double m_mean;
00892 double m_alpha;
00893 double m_bound;
00894 };
00895
00896 WeibullVariableImpl::WeibullVariableImpl() : m_mean(1.0), m_alpha(1), m_bound(0) { }
00897 WeibullVariableImpl::WeibullVariableImpl(double m)
00898 : m_mean(m), m_alpha(1), m_bound(0) { }
00899 WeibullVariableImpl::WeibullVariableImpl(double m, double s)
00900 : m_mean(m), m_alpha(s), m_bound(0) { }
00901 WeibullVariableImpl::WeibullVariableImpl(double m, double s, double b)
00902 : m_mean(m), m_alpha(s), m_bound(b) { };
00903 WeibullVariableImpl::WeibullVariableImpl(const WeibullVariableImpl& c)
00904 : RandomVariableBase(c), m_mean(c.m_mean), m_alpha(c.m_alpha),
00905 m_bound(c.m_bound) { }
00906
00907 double WeibullVariableImpl::GetValue()
00908 {
00909 if(!RandomVariableBase::initialized)
00910 {
00911 RandomVariableBase::Initialize();
00912 }
00913 if(!m_generator)
00914 {
00915 m_generator = new RngStream();
00916 m_generator->InitializeStream();
00917 m_generator->ResetNthSubstream(RandomVariableBase::runNumber);
00918 }
00919 double exponent = 1.0 / m_alpha;
00920 double r = m_mean * pow( -log(m_generator->RandU01()), exponent);
00921 if (m_bound != 0 && r > m_bound) return m_bound;
00922 return r;
00923 }
00924
00925 RandomVariableBase* WeibullVariableImpl::Copy() const
00926 {
00927 return new WeibullVariableImpl(*this);
00928 }
00929
00930 double WeibullVariableImpl::GetSingleValue(double m, double s, double b)
00931 {
00932 if(!RandomVariableBase::m_static_generator)
00933 {
00934 RandomVariableBase::Initialize();
00935 RandomVariableBase::m_static_generator = new RngStream();
00936 RandomVariableBase::m_static_generator->InitializeStream();
00937 }
00938 double exponent = 1.0 / s;
00939 double r = m * pow( -log(m_static_generator->RandU01()), exponent);
00940 if (b != 0 && r > b) return b;
00941 return r;
00942 }
00943
00944 WeibullVariable::WeibullVariable()
00945 : RandomVariable (WeibullVariableImpl ())
00946 {}
00947 WeibullVariable::WeibullVariable(double m)
00948 : RandomVariable (WeibullVariableImpl (m))
00949 {}
00950 WeibullVariable::WeibullVariable(double m, double s)
00951 : RandomVariable (WeibullVariableImpl (m, s))
00952 {}
00953 WeibullVariable::WeibullVariable(double m, double s, double b)
00954 : RandomVariable (WeibullVariableImpl (m, s, b))
00955 {}
00956 double
00957 WeibullVariable::GetSingleValue(double m, double s, double b)
00958 {
00959 return WeibullVariableImpl::GetSingleValue (m, s, b);
00960 }
00961
00962
00963
00964
00965 class NormalVariableImpl : public RandomVariableBase {
00966
00967 public:
00968 static const double INFINITE_VALUE;
00969
00970
00971
00972
00973 NormalVariableImpl();
00974
00975
00976
00977
00978
00979
00980
00981 NormalVariableImpl(double m, double v, double b = INFINITE_VALUE);
00982
00983 NormalVariableImpl(const NormalVariableImpl& c);
00984
00985
00986
00987
00988 virtual double GetValue();
00989 virtual RandomVariableBase* Copy(void) const;
00990 public:
00991
00992
00993
00994
00995
00996
00997 static double GetSingleValue(double m, double v, double b = INFINITE_VALUE);
00998 private:
00999 double m_mean;
01000 double m_variance;
01001 double m_bound;
01002 bool m_nextValid;
01003 double m_next;
01004 static bool m_static_nextValid;
01005 static double m_static_next;
01006 };
01007
01008 bool NormalVariableImpl::m_static_nextValid = false;
01009 double NormalVariableImpl::m_static_next;
01010 const double NormalVariableImpl::INFINITE_VALUE = 1e307;
01011
01012 NormalVariableImpl::NormalVariableImpl()
01013 : m_mean(0.0), m_variance(1.0), m_bound(INFINITE_VALUE), m_nextValid(false){}
01014
01015 NormalVariableImpl::NormalVariableImpl(double m, double v, double b)
01016 : m_mean(m), m_variance(v), m_bound(b), m_nextValid(false) { }
01017
01018 NormalVariableImpl::NormalVariableImpl(const NormalVariableImpl& c)
01019 : RandomVariableBase(c), m_mean(c.m_mean), m_variance(c.m_variance),
01020 m_bound(c.m_bound), m_nextValid(false) { }
01021
01022 double NormalVariableImpl::GetValue()
01023 {
01024 if(!RandomVariableBase::initialized)
01025 {
01026 RandomVariableBase::Initialize();
01027 }
01028 if(!m_generator)
01029 {
01030 m_generator = new RngStream();
01031 m_generator->InitializeStream();
01032 m_generator->ResetNthSubstream(RandomVariableBase::runNumber);
01033 }
01034 if (m_nextValid)
01035 {
01036 m_nextValid = false;
01037 return m_next;
01038 }
01039 while(1)
01040 {
01041
01042
01043 double u1 = m_generator->RandU01();
01044 double u2 = m_generator->RandU01();;
01045 double v1 = 2 * u1 - 1;
01046 double v2 = 2 * u2 - 1;
01047 double w = v1 * v1 + v2 * v2;
01048 if (w <= 1.0)
01049 {
01050 double y = sqrt((-2 * log(w))/w);
01051 m_next = m_mean + v2 * y * sqrt(m_variance);
01052
01053 m_nextValid = fabs(m_next-m_mean) <= m_bound;
01054 double x1 = m_mean + v1 * y * sqrt(m_variance);
01055
01056 if (fabs(x1-m_mean) <= m_bound)
01057 {
01058 return x1;
01059 }
01060
01061 else if (m_nextValid)
01062 {
01063 m_nextValid = false;
01064 return m_next;
01065 }
01066
01067 }
01068 }
01069 }
01070
01071 RandomVariableBase* NormalVariableImpl::Copy() const
01072 {
01073 return new NormalVariableImpl(*this);
01074 }
01075
01076 double NormalVariableImpl::GetSingleValue(double m, double v, double b)
01077 {
01078 if(!RandomVariableBase::m_static_generator)
01079 {
01080 RandomVariableBase::Initialize();
01081 RandomVariableBase::m_static_generator = new RngStream();
01082 RandomVariableBase::m_static_generator->InitializeStream();
01083 }
01084 if (m_static_nextValid)
01085 {
01086 m_static_nextValid = false;
01087 return m_static_next;
01088 }
01089 while(1)
01090 {
01091
01092
01093 double u1 = m_static_generator->RandU01();
01094 double u2 = m_static_generator->RandU01();;
01095 double v1 = 2 * u1 - 1;
01096 double v2 = 2 * u2 - 1;
01097 double w = v1 * v1 + v2 * v2;
01098 if (w <= 1.0)
01099 {
01100 double y = sqrt((-2 * log(w))/w);
01101 m_static_next = m + v2 * y * sqrt(v);
01102
01103 m_static_nextValid = fabs(m_static_next-m) <= b;;
01104 double x1 = m + v1 * y * sqrt(v);
01105
01106 if (fabs(x1-m) <= b)
01107 {
01108 return x1;
01109 }
01110
01111 else if (m_static_nextValid)
01112 {
01113 m_static_nextValid = false;
01114 return m_static_next;
01115 }
01116
01117 }
01118 }
01119 }
01120
01121 NormalVariable::NormalVariable()
01122 : RandomVariable (NormalVariableImpl ())
01123 {}
01124 NormalVariable::NormalVariable(double m, double v)
01125 : RandomVariable (NormalVariableImpl (m, v))
01126 {}
01127 NormalVariable::NormalVariable(double m, double v, double b)
01128 : RandomVariable (NormalVariableImpl (m, v, b))
01129 {}
01130 double
01131 NormalVariable::GetSingleValue(double m, double v)
01132 {
01133 return NormalVariableImpl::GetSingleValue (m, v);
01134 }
01135 double
01136 NormalVariable::GetSingleValue(double m, double v, double b)
01137 {
01138 return NormalVariableImpl::GetSingleValue (m, v, b);
01139 }
01140
01141
01142
01143
01144 class EmpiricalVariableImpl : public RandomVariableBase {
01145 public:
01146
01147
01148
01149 explicit EmpiricalVariableImpl();
01150
01151 virtual ~EmpiricalVariableImpl();
01152 EmpiricalVariableImpl(const EmpiricalVariableImpl& c);
01153
01154
01155
01156 virtual double GetValue();
01157 virtual RandomVariableBase* Copy(void) const;
01158
01159
01160
01161
01162
01163 virtual void CDF(double v, double c);
01164
01165 private:
01166 class ValueCDF {
01167 public:
01168 ValueCDF();
01169 ValueCDF(double v, double c);
01170 ValueCDF(const ValueCDF& c);
01171 double value;
01172 double cdf;
01173 };
01174 virtual void Validate();
01175 virtual double Interpolate(double, double, double, double, double);
01176 bool validated;
01177 std::vector<ValueCDF> emp;
01178 };
01179
01180
01181
01182 EmpiricalVariableImpl::ValueCDF::ValueCDF()
01183 : value(0.0), cdf(0.0){ }
01184 EmpiricalVariableImpl::ValueCDF::ValueCDF(double v, double c)
01185 : value(v), cdf(c) { }
01186 EmpiricalVariableImpl::ValueCDF::ValueCDF(const ValueCDF& c)
01187 : value(c.value), cdf(c.cdf) { }
01188
01189
01190
01191
01192 EmpiricalVariableImpl::EmpiricalVariableImpl()
01193 : validated(false) { }
01194
01195 EmpiricalVariableImpl::EmpiricalVariableImpl(const EmpiricalVariableImpl& c)
01196 : RandomVariableBase(c), validated(c.validated), emp(c.emp) { }
01197
01198 EmpiricalVariableImpl::~EmpiricalVariableImpl() { }
01199
01200 double EmpiricalVariableImpl::GetValue()
01201 {
01202
01203 if(!RandomVariableBase::initialized)
01204 {
01205 RandomVariableBase::Initialize();
01206 }
01207 if(!m_generator)
01208 {
01209 m_generator = new RngStream();
01210 m_generator->InitializeStream();
01211 m_generator->ResetNthSubstream(RandomVariableBase::runNumber);
01212 }
01213 if (emp.size() == 0) return 0.0;
01214 if (!validated) Validate();
01215 double r = m_generator->RandU01();
01216 if (r <= emp.front().cdf)return emp.front().value;
01217 if (r >= emp.back().cdf) return emp.back().value;
01218
01219 std::vector<ValueCDF>::size_type bottom = 0;
01220 std::vector<ValueCDF>::size_type top = emp.size() - 1;
01221 while(1)
01222 {
01223 std::vector<ValueCDF>::size_type c = (top + bottom) / 2;
01224 if (r >= emp[c].cdf && r < emp[c+1].cdf)
01225 {
01226 return Interpolate(emp[c].cdf, emp[c+1].cdf,
01227 emp[c].value, emp[c+1].value,
01228 r);
01229 }
01230
01231 if (r < emp[c].cdf) top = c - 1;
01232 else bottom = c + 1;
01233 }
01234 }
01235
01236 RandomVariableBase* EmpiricalVariableImpl::Copy() const
01237 {
01238 return new EmpiricalVariableImpl(*this);
01239 }
01240
01241 void EmpiricalVariableImpl::CDF(double v, double c)
01242 {
01243
01244 emp.push_back(ValueCDF(v, c));
01245 }
01246
01247 void EmpiricalVariableImpl::Validate()
01248 {
01249 ValueCDF prior;
01250 for (std::vector<ValueCDF>::size_type i = 0; i < emp.size(); ++i)
01251 {
01252 ValueCDF& current = emp[i];
01253 if (current.value < prior.value || current.cdf < prior.cdf)
01254 {
01255 cerr << "Empirical Dist error,"
01256 << " current value " << current.value
01257 << " prior value " << prior.value
01258 << " current cdf " << current.cdf
01259 << " prior cdf " << prior.cdf << endl;
01260 NS_FATAL_ERROR("Empirical Dist error");
01261 }
01262 prior = current;
01263 }
01264 validated = true;
01265 }
01266
01267 double EmpiricalVariableImpl::Interpolate(double c1, double c2,
01268 double v1, double v2, double r)
01269 {
01270 return (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
01271 }
01272
01273 EmpiricalVariable::EmpiricalVariable()
01274 : RandomVariable (EmpiricalVariableImpl ())
01275 {}
01276 EmpiricalVariable::EmpiricalVariable (const RandomVariableBase &variable)
01277 : RandomVariable (variable)
01278 {}
01279 void
01280 EmpiricalVariable::CDF(double v, double c)
01281 {
01282 EmpiricalVariableImpl *impl = dynamic_cast<EmpiricalVariableImpl *> (Peek ());
01283 NS_ASSERT (impl);
01284 impl->CDF (v, c);
01285 }
01286
01287
01288
01289
01290
01291 class IntEmpiricalVariableImpl : public EmpiricalVariableImpl {
01292 public:
01293
01294 IntEmpiricalVariableImpl();
01295
01296 virtual RandomVariableBase* Copy(void) const;
01297
01298
01299
01300 virtual uint32_t GetInteger();
01301 private:
01302 virtual double Interpolate(double, double, double, double, double);
01303 };
01304
01305
01306 IntEmpiricalVariableImpl::IntEmpiricalVariableImpl() { }
01307
01308 uint32_t IntEmpiricalVariableImpl::GetInteger()
01309 {
01310 return (uint32_t)GetValue();
01311 }
01312
01313 RandomVariableBase* IntEmpiricalVariableImpl::Copy() const
01314 {
01315 return new IntEmpiricalVariableImpl(*this);
01316 }
01317
01318 double IntEmpiricalVariableImpl::Interpolate(double c1, double c2,
01319 double v1, double v2, double r)
01320 {
01321 return ceil(v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
01322 }
01323
01324 IntEmpiricalVariable::IntEmpiricalVariable()
01325 : EmpiricalVariable (IntEmpiricalVariableImpl ())
01326 {}
01327
01328
01329
01330
01331 class DeterministicVariableImpl : public RandomVariableBase
01332 {
01333
01334 public:
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344
01345
01346 explicit DeterministicVariableImpl(double* d, uint32_t c);
01347
01348 virtual ~DeterministicVariableImpl();
01349
01350
01351
01352 virtual double GetValue();
01353 virtual RandomVariableBase* Copy(void) const;
01354 private:
01355 uint32_t count;
01356 uint32_t next;
01357 double* data;
01358 };
01359
01360 DeterministicVariableImpl::DeterministicVariableImpl(double* d, uint32_t c)
01361 : count(c), next(c), data(d)
01362 {
01363 }
01364
01365 DeterministicVariableImpl::~DeterministicVariableImpl() { }
01366
01367 double DeterministicVariableImpl::GetValue()
01368 {
01369 if (next == count) next = 0;
01370 return data[next++];
01371 }
01372
01373 RandomVariableBase* DeterministicVariableImpl::Copy() const
01374 {
01375 return new DeterministicVariableImpl(*this);
01376 }
01377
01378 DeterministicVariable::DeterministicVariable(double* d, uint32_t c)
01379 : RandomVariable (DeterministicVariableImpl (d, c))
01380 {}
01381
01382
01383
01384
01385 class LogNormalVariableImpl : public RandomVariableBase {
01386 public:
01387
01388
01389
01390
01391 LogNormalVariableImpl (double mu, double sigma);
01392
01393
01394
01395
01396 virtual double GetValue ();
01397 virtual RandomVariableBase* Copy(void) const;
01398 public:
01399
01400
01401
01402
01403
01404 static double GetSingleValue(double mu, double sigma);
01405 private:
01406 double m_mu;
01407 double m_sigma;
01408 };
01409
01410
01411 RandomVariableBase* LogNormalVariableImpl::Copy () const
01412 {
01413 return new LogNormalVariableImpl (m_mu, m_sigma);
01414 }
01415
01416 LogNormalVariableImpl::LogNormalVariableImpl (double mu, double sigma)
01417 :m_mu(mu), m_sigma(sigma)
01418 {
01419 }
01420
01421
01422
01423
01424
01425
01426
01427
01428
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445
01446
01447 double
01448 LogNormalVariableImpl::GetValue ()
01449 {
01450 if(!RandomVariableBase::initialized)
01451 {
01452 RandomVariableBase::Initialize();
01453 }
01454 if(!m_generator)
01455 {
01456 m_generator = new RngStream();
01457 m_generator->InitializeStream();
01458 m_generator->ResetNthSubstream(RandomVariableBase::runNumber);
01459 }
01460 double u, v, r2, normal, z;
01461
01462 do
01463 {
01464
01465
01466 u = -1 + 2 * m_generator->RandU01 ();
01467 v = -1 + 2 * m_generator->RandU01 ();
01468
01469
01470 r2 = u * u + v * v;
01471 }
01472 while (r2 > 1.0 || r2 == 0);
01473
01474 normal = u * sqrt (-2.0 * log (r2) / r2);
01475
01476 z = exp (m_sigma * normal + m_mu);
01477
01478 return z;
01479 }
01480
01481 double LogNormalVariableImpl::GetSingleValue (double mu, double sigma)
01482 {
01483 if(!RandomVariableBase::m_static_generator)
01484 {
01485 RandomVariableBase::Initialize();
01486 RandomVariableBase::m_static_generator = new RngStream();
01487 RandomVariableBase::m_static_generator->InitializeStream();
01488 }
01489 double u, v, r2, normal, z;
01490 do
01491 {
01492
01493 u = -1 + 2 * m_static_generator->RandU01 ();
01494 v = -1 + 2 * m_static_generator->RandU01 ();
01495
01496
01497 r2 = u * u + v * v;
01498 }
01499 while (r2 > 1.0 || r2 == 0);
01500
01501 normal = u * sqrt (-2.0 * log (r2) / r2);
01502
01503 z = exp (sigma * normal + mu);
01504
01505 return z;
01506 }
01507
01508 LogNormalVariable::LogNormalVariable (double mu, double sigma)
01509 : RandomVariable (LogNormalVariableImpl (mu, sigma))
01510 {}
01511 double
01512 LogNormalVariable::GetSingleValue(double mu, double sigma)
01513 {
01514 return LogNormalVariableImpl::GetSingleValue (mu, sigma);
01515 }
01516
01517
01518
01519
01520 class GammaVariableImpl : public RandomVariableBase
01521 {
01522 public:
01523
01524
01525
01526
01527 GammaVariableImpl (double alpha, double beta);
01528
01529
01530
01531
01532 virtual double GetValue ();
01533 virtual RandomVariableBase* Copy(void) const;
01534 public:
01535
01536
01537
01538
01539
01540 static double GetSingleValue(double alpha, double beta);
01541 private:
01542 double m_alpha;
01543 double m_beta;
01544 NormalVariable m_normal;
01545 };
01546
01547
01548 RandomVariableBase* GammaVariableImpl::Copy () const
01549 {
01550 return new GammaVariableImpl (m_alpha, m_beta);
01551 }
01552
01553 GammaVariableImpl::GammaVariableImpl (double alpha, double beta)
01554 :m_alpha(alpha), m_beta(beta)
01555 {
01556 }
01557
01558
01559
01560
01561
01562
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574 double
01575 GammaVariableImpl::GetValue ()
01576 {
01577 if(!RandomVariableBase::initialized)
01578 {
01579 RandomVariableBase::Initialize();
01580 }
01581
01582 if(!m_generator)
01583 {
01584 m_generator = new RngStream();
01585 m_generator->InitializeStream();
01586 m_generator->ResetNthSubstream(RandomVariableBase::runNumber);
01587 }
01588
01589 if (m_alpha < 1) {
01590 double u = m_generator->RandU01 ();
01591 return GammaVariableImpl::GammaVariableImpl(1.0 + m_alpha, m_beta).GetValue() * pow (u, 1.0 / m_alpha);
01592 }
01593
01594 double x, v, u;
01595 double d = m_alpha - 1.0 / 3.0;
01596 double c = (1.0 / 3.0) / sqrt (d);
01597
01598 while (1)
01599 {
01600 do
01601 {
01602 x = m_normal.GetValue();
01603 v = 1.0 + c * x;
01604 } while (v <= 0);
01605
01606 v = v * v * v;
01607 u = m_generator->RandU01 ();
01608 if (u < 1 - 0.0331 * x * x * x * x)
01609 break;
01610 if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))
01611 break;
01612 }
01613
01614 return m_beta * d * v;
01615 }
01616
01617 double GammaVariableImpl::GetSingleValue (double alpha, double beta)
01618 {
01619 if(!RandomVariableBase::m_static_generator)
01620 {
01621 RandomVariableBase::Initialize();
01622 RandomVariableBase::m_static_generator = new RngStream();
01623 RandomVariableBase::m_static_generator->InitializeStream();
01624 }
01625
01626 if (alpha < 1) {
01627 double u = m_static_generator->RandU01 ();
01628 return GammaVariableImpl::GammaVariableImpl(1.0 + alpha, beta).GetValue() * pow (u, 1.0 / alpha);
01629 }
01630
01631 double x, v, u;
01632 double d = alpha - 1.0 / 3.0;
01633 double c = (1.0 / 3.0) / sqrt (d);
01634
01635 while (1)
01636 {
01637 do
01638 {
01639 x = NormalVariable::GetSingleValue(0.0, 1.0);
01640 v = 1.0 + c * x;
01641 } while (v <= 0);
01642
01643 v = v * v * v;
01644 u = m_static_generator->RandU01 ();
01645 if (u < 1 - 0.0331 * x * x * x * x)
01646 break;
01647 if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))
01648 break;
01649 }
01650
01651 return beta * d * v;
01652 }
01653
01654 GammaVariable::GammaVariable (double alpha, double beta)
01655 : RandomVariable (GammaVariableImpl (alpha, beta))
01656 {
01657 }
01658
01659 double
01660 GammaVariable::GetSingleValue(double alpha, double beta)
01661 {
01662 return GammaVariableImpl::GetSingleValue (alpha, beta);
01663 }
01664
01665
01666
01667
01668
01669 class ErlangVariableImpl : public RandomVariableBase
01670 {
01671 public:
01672
01673
01674
01675
01676 ErlangVariableImpl (double k, double lambda);
01677
01678
01679
01680
01681 virtual double GetValue ();
01682 virtual RandomVariableBase* Copy(void) const;
01683 public:
01684
01685
01686
01687
01688
01689 static double GetSingleValue(double k, double lambda);
01690 private:
01691 double m_k;
01692 double m_lambda;
01693 ExponentialVariable m_exponential;
01694 };
01695
01696
01697 RandomVariableBase* ErlangVariableImpl::Copy () const
01698 {
01699 return new ErlangVariableImpl (m_k, m_lambda);
01700 }
01701
01702 ErlangVariableImpl::ErlangVariableImpl (double k, double lambda)
01703 : m_k(k), m_lambda(lambda), m_exponential(lambda)
01704 {
01705 NS_ASSERT( floor(k) == k && k >= 0 );
01706 }
01707
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720 double
01721 ErlangVariableImpl::GetValue ()
01722 {
01723 if(!RandomVariableBase::initialized)
01724 {
01725 RandomVariableBase::Initialize();
01726 }
01727
01728 if(!m_generator)
01729 {
01730 m_generator = new RngStream();
01731 m_generator->InitializeStream();
01732 m_generator->ResetNthSubstream(RandomVariableBase::runNumber);
01733 }
01734
01735 double result = 0;
01736 for (int i = 0; i < m_k; ++i)
01737 {
01738 result += m_exponential.GetValue();
01739 }
01740
01741 return result;
01742 }
01743
01744 double ErlangVariableImpl::GetSingleValue (double k, double lambda)
01745 {
01746 if(!RandomVariableBase::m_static_generator)
01747 {
01748 RandomVariableBase::Initialize();
01749 RandomVariableBase::m_static_generator = new RngStream();
01750 RandomVariableBase::m_static_generator->InitializeStream();
01751 }
01752
01753 NS_ASSERT( floor(k) == k && k >= 0 );
01754
01755 ExponentialVariable exponential (lambda);
01756
01757 double result = 0;
01758 for (int i = 0; i < k; ++i)
01759 {
01760 result += exponential.GetValue();
01761 }
01762
01763 return result;
01764 }
01765
01766 ErlangVariable::ErlangVariable (double k, double lambda)
01767 : RandomVariable (ErlangVariableImpl (k, lambda))
01768 {
01769 }
01770
01771 double
01772 ErlangVariable::GetSingleValue(double k, double lambda)
01773 {
01774 return ErlangVariableImpl::GetSingleValue (k, lambda);
01775 }
01776
01777
01778
01779
01780 class TriangularVariableImpl : public RandomVariableBase {
01781 public:
01782
01783
01784
01785
01786 TriangularVariableImpl();
01787
01788
01789
01790
01791
01792
01793
01794
01795 TriangularVariableImpl(double s, double l, double mean);
01796
01797 TriangularVariableImpl(const TriangularVariableImpl& c);
01798
01799
01800
01801
01802 virtual double GetValue();
01803 virtual RandomVariableBase* Copy(void) const;
01804 public:
01805
01806
01807
01808
01809
01810
01811 static double GetSingleValue(double s, double l, double mean);
01812 private:
01813 double m_min;
01814 double m_max;
01815 double m_mode;
01816
01817 };
01818
01819 TriangularVariableImpl::TriangularVariableImpl()
01820 : m_min(0), m_max(1), m_mode(0.5) { }
01821
01822 TriangularVariableImpl::TriangularVariableImpl(double s, double l, double mean)
01823 : m_min(s), m_max(l), m_mode(3.0*mean-s-l) { }
01824
01825 TriangularVariableImpl::TriangularVariableImpl(const TriangularVariableImpl& c)
01826 : RandomVariableBase(c), m_min(c.m_min), m_max(c.m_max), m_mode(c.m_mode) { }
01827
01828 double TriangularVariableImpl::GetValue()
01829 {
01830 if(!RandomVariableBase::initialized)
01831 {
01832 RandomVariableBase::Initialize();
01833 }
01834 if(!m_generator)
01835 {
01836 m_generator = new RngStream();
01837 m_generator->InitializeStream();
01838 m_generator->ResetNthSubstream(RandomVariableBase::runNumber);
01839 }
01840 double u = m_generator->RandU01();
01841 if(u <= (m_mode - m_min) / (m_max - m_min) )
01842 return m_min + sqrt(u * (m_max - m_min) * (m_mode - m_min) );
01843 else
01844 return m_max - sqrt( (1-u) * (m_max - m_min) * (m_max - m_mode) );
01845 }
01846
01847 RandomVariableBase* TriangularVariableImpl::Copy() const
01848 {
01849 return new TriangularVariableImpl(*this);
01850 }
01851
01852 double TriangularVariableImpl::GetSingleValue(double s, double l, double mean)
01853 {
01854 if(!RandomVariableBase::m_static_generator)
01855 {
01856 RandomVariableBase::Initialize();
01857 RandomVariableBase::m_static_generator = new RngStream();
01858 RandomVariableBase::m_static_generator->InitializeStream();
01859 }
01860 double mode = 3.0*mean-s-l;
01861 double u = m_static_generator->RandU01();
01862 if(u <= (mode - s) / (l - s) )
01863 return s + sqrt(u * (l - s) * (mode - s) );
01864 else
01865 return l - sqrt( (1-u) * (l - s) * (l - mode) );
01866 }
01867
01868 TriangularVariable::TriangularVariable()
01869 : RandomVariable (TriangularVariableImpl ())
01870 {}
01871 TriangularVariable::TriangularVariable(double s, double l, double mean)
01872 : RandomVariable (TriangularVariableImpl (s,l,mean))
01873 {}
01874 double
01875 TriangularVariable::GetSingleValue(double s, double l, double mean)
01876 {
01877 return TriangularVariableImpl::GetSingleValue (s,l,mean);
01878 }
01879
01880
01881 std::ostream &operator << (std::ostream &os, const RandomVariable &var)
01882 {
01883 RandomVariableBase *base = var.Peek ();
01884 ConstantVariableImpl *constant = dynamic_cast<ConstantVariableImpl *> (base);
01885 if (constant != 0)
01886 {
01887 os << "Constant:" << constant->GetValue ();
01888 return os;
01889 }
01890 UniformVariableImpl *uniform = dynamic_cast<UniformVariableImpl *> (base);
01891 if (uniform != 0)
01892 {
01893 os << "Uniform:" << uniform->GetMin () << ":" << uniform->GetMax ();
01894 return os;
01895 }
01896
01897 os.setstate (std::ios_base::badbit);
01898 return os;
01899 }
01900 std::istream &operator >> (std::istream &is, RandomVariable &var)
01901 {
01902 std::string value;
01903 is >> value;
01904 std::string::size_type tmp;
01905 tmp = value.find (":");
01906 if (tmp == std::string::npos)
01907 {
01908 is.setstate (std::ios_base::badbit);
01909 return is;
01910 }
01911 std::string type = value.substr (0, tmp);
01912 value = value.substr (tmp + 1, value.npos);
01913 if (type == "Constant")
01914 {
01915 istringstream iss (value);
01916 double constant;
01917 iss >> constant;
01918 var = ConstantVariable (constant);
01919 }
01920 else if (type == "Uniform")
01921 {
01922 if (value.size () == 0)
01923 {
01924 var = UniformVariable ();
01925 }
01926 else
01927 {
01928 tmp = value.find (":");
01929 if (tmp == value.npos)
01930 {
01931 NS_FATAL_ERROR ("bad Uniform value: " << value);
01932 }
01933 istringstream issA (value.substr (0, tmp));
01934 istringstream issB (value.substr (tmp + 1, value.npos));
01935 double a, b;
01936 issA >> a;
01937 issB >> b;
01938 var = UniformVariable (a, b);
01939 }
01940 }
01941 else
01942 {
01943 NS_FATAL_ERROR ("RandomVariable deserialization not implemented for " << type);
01944
01945 }
01946 return is;
01947 }
01948
01949
01950
01951 }
01952
01953
01954 #ifdef RUN_SELF_TESTS
01955 #include "test.h"
01956 #include <vector>
01957
01958 namespace ns3 {
01959
01960
01961 class RandomVariableTest : public Test
01962 {
01963 public:
01964 RandomVariableTest () : Test ("RandomVariable") {}
01965 virtual bool RunTests (void)
01966 {
01967 bool result = true;
01968 const double desired_mean = 1.0;
01969 const double desired_stddev = 1.0;
01970 double tmp = log (1 + (desired_stddev/desired_mean)*(desired_stddev/desired_mean));
01971 double sigma = sqrt (tmp);
01972 double mu = log (desired_mean) - 0.5*tmp;
01973
01974
01975 {
01976 LogNormalVariable lognormal (mu, sigma);
01977 vector<double> samples;
01978 const int NSAMPLES = 10000;
01979 double sum = 0;
01980 for (int n = NSAMPLES; n; --n)
01981 {
01982 double value = lognormal.GetValue ();
01983 sum += value;
01984 samples.push_back (value);
01985 }
01986 double obtained_mean = sum / NSAMPLES;
01987 sum = 0;
01988 for (vector<double>::iterator iter = samples.begin (); iter != samples.end (); iter++)
01989 {
01990 double tmp = (*iter - obtained_mean);
01991 sum += tmp*tmp;
01992 }
01993 double obtained_stddev = sqrt (sum / (NSAMPLES - 1));
01994
01995 if (not (obtained_mean/desired_mean > 0.90 and obtained_mean/desired_mean < 1.10))
01996 {
01997 result = false;
01998 Failure () << "Obtained lognormal mean value " << obtained_mean << ", expected " << desired_mean << std::endl;
01999 }
02000
02001 if (not (obtained_stddev/desired_stddev > 0.90 and obtained_stddev/desired_stddev < 1.10))
02002 {
02003 result = false;
02004 Failure () << "Obtained lognormal stddev value " << obtained_stddev <<
02005 ", expected " << desired_stddev << std::endl;
02006 }
02007 }
02008
02009
02010 {
02011 vector<double> samples;
02012 const int NSAMPLES = 10000;
02013 double sum = 0;
02014 for (int n = NSAMPLES; n; --n)
02015 {
02016 double value = LogNormalVariable::GetSingleValue (mu, sigma);
02017 sum += value;
02018 samples.push_back (value);
02019 }
02020 double obtained_mean = sum / NSAMPLES;
02021 sum = 0;
02022 for (vector<double>::iterator iter = samples.begin (); iter != samples.end (); iter++)
02023 {
02024 double tmp = (*iter - obtained_mean);
02025 sum += tmp*tmp;
02026 }
02027 double obtained_stddev = sqrt (sum / (NSAMPLES - 1));
02028
02029 if (not (obtained_mean/desired_mean > 0.90 and obtained_mean/desired_mean < 1.10))
02030 {
02031 result = false;
02032 Failure () << "Obtained LogNormalVariable::GetSingleValue mean value " << obtained_mean
02033 << ", expected " << desired_mean << std::endl;
02034 }
02035
02036 if (not (obtained_stddev/desired_stddev > 0.90 and obtained_stddev/desired_stddev < 1.10))
02037 {
02038 result = false;
02039 Failure () << "Obtained LogNormalVariable::GetSingleValue stddev value " << obtained_stddev <<
02040 ", expected " << desired_stddev << std::endl;
02041 }
02042 }
02043
02044
02045 {
02046 RandomVariableValue val;
02047 val.DeserializeFromString ("Uniform:0.1:0.2", MakeRandomVariableChecker ());
02048 RandomVariable rng = val.Get ();
02049 NS_TEST_ASSERT_EQUAL (val.SerializeToString (MakeRandomVariableChecker ()), "Uniform:0.1:0.2");
02050 }
02051
02052 return result;
02053 }
02054 };
02055
02056
02057 static RandomVariableTest g_random_variable_tests;
02058
02059 }
02060
02061 #endif