Fixed major memory bug and docs in RandomVariable
authorRaj Bhattacharjea <raj.b@gatech.edu>
Thu, 22 Mar 2007 12:16:10 -0400
changeset 360 7bffd987426c
parent 359 91b7ad7fa784
child 361 9b159ff6dbcb
Fixed major memory bug and docs in RandomVariable
src/core/random-variable.cc
src/core/random-variable.h
src/core/rng-stream.cc
src/core/rng-stream.h
--- a/src/core/random-variable.cc	Wed Mar 21 23:17:11 2007 -0700
+++ b/src/core/random-variable.cc	Thu Mar 22 12:16:10 2007 -0400
@@ -103,6 +103,12 @@
   m_generator->InitializeStream();
 }
 
+RandomVariable::RandomVariable(const RandomVariable& r)
+{
+  m_generator = new RngStream(*r.m_generator);
+  RandomVariable::Initialize();
+}
+
 RandomVariable::~RandomVariable()
 {
   delete m_generator;
@@ -237,7 +243,7 @@
   : m_min(s), m_max(l) { }
 
 UniformVariable::UniformVariable(const UniformVariable& c) 
-  : m_min(c.m_min), m_max(c.m_max) { }
+  : RandomVariable(c), m_min(c.m_min), m_max(c.m_max) { }
 
 double UniformVariable::GetValue()
 {
@@ -258,7 +264,7 @@
   : m_const(c) { };
   
 ConstantVariable::ConstantVariable(const ConstantVariable& c) 
-  : m_const(c.m_const) { }
+  : RandomVariable(c), m_const(c.m_const) { }
 
 void ConstantVariable::NewConstant(double c) 
   { m_const = c;}
@@ -293,12 +299,17 @@
 }
 
 SequentialVariable::SequentialVariable(const SequentialVariable& c)
-  : m_min(c.m_min), m_max(c.m_max),
+  : RandomVariable(c), m_min(c.m_min), m_max(c.m_max),
     m_increment(c.m_increment->Copy()), m_consecutive(c.m_consecutive),
     m_current(c.m_current), m_currentConsecutive(c.m_currentConsecutive)
 {
 }
 
+SequentialVariable::~SequentialVariable()
+{
+  delete m_increment;
+}
+
 double SequentialVariable::GetValue()
 { // Return a sequential series of values
   double r = m_current;
@@ -329,7 +340,7 @@
   : m_mean(m), m_bound(b) { }
   
 ExponentialVariable::ExponentialVariable(const ExponentialVariable& c) 
-  : m_mean(c.m_mean), m_bound(c.m_bound) { }
+  : RandomVariable(c), m_mean(c.m_mean), m_bound(c.m_bound) { }
 
 double ExponentialVariable::GetValue()
 {
@@ -358,7 +369,8 @@
   : m_mean(m), m_shape(s), m_bound(b) { }
 
 ParetoVariable::ParetoVariable(const ParetoVariable& c) 
-  : m_mean(c.m_mean), m_shape(c.m_shape), m_bound(c.m_bound) { }
+  : RandomVariable(c), m_mean(c.m_mean), m_shape(c.m_shape), 
+    m_bound(c.m_bound) { }
 
 double ParetoVariable::GetValue()
 {
@@ -383,7 +395,8 @@
 WeibullVariable::WeibullVariable(double m, double s, double b) 
   : m_mean(m), m_alpha(s), m_bound(b) { };
 WeibullVariable::WeibullVariable(const WeibullVariable& c) 
-  : m_mean(c.m_mean), m_alpha(c.m_alpha), m_bound(c.m_bound) { }
+  : RandomVariable(c), m_mean(c.m_mean), m_alpha(c.m_alpha),
+    m_bound(c.m_bound) { }
 
 double WeibullVariable::GetValue()
 {
@@ -407,7 +420,8 @@
   : m_mean(m), m_variance(v), m_bound(b), m_nextValid(false) { }
 
 NormalVariable::NormalVariable(const NormalVariable& c)
-  : m_mean(c.m_mean), m_variance(c.m_variance), m_bound(c.m_bound) { }
+  : RandomVariable(c), m_mean(c.m_mean), m_variance(c.m_variance),
+    m_bound(c.m_bound) { }
 
 double NormalVariable::GetValue()
 {
@@ -458,8 +472,8 @@
 EmpiricalVariable::EmpiricalVariable() 
   : validated(false) { }
 
-EmpiricalVariable::EmpiricalVariable(const EmpiricalVariable& c) 
-  : validated(c.validated), emp(c.emp) { }
+EmpiricalVariable::EmpiricalVariable(const EmpiricalVariable& c)
+  : RandomVariable(c), validated(c.validated), emp(c.emp) { }
 
 EmpiricalVariable::~EmpiricalVariable() { }
 
@@ -541,7 +555,6 @@
   return new IntEmpiricalVariable(*this);
 }
 
-
 double IntEmpiricalVariable::Interpolate(double c1, double c2,
                                    double v1, double v2, double r)
 { // Interpolate random value in range [v1..v2) based on [c1 .. r .. c2)
--- a/src/core/random-variable.h	Wed Mar 21 23:17:11 2007 -0700
+++ b/src/core/random-variable.h	Thu Mar 22 12:16:10 2007 -0400
@@ -88,6 +88,11 @@
   RandomVariable();
   
   /**
+   * \brief Copy constructor
+   */  
+  RandomVariable(const RandomVariable&);
+  
+  /**
    * \brief Destructor for a random number generator with a random seed.
    */
   virtual ~RandomVariable();
@@ -145,6 +150,7 @@
 
    /**
    * \brief Use the global seed to force precisely reproducible results.
+   *
    * It is often desirable to create a simulation that uses random
    * numbers, while at the same time is completely reproducible.
    * Specifying this set of six random seeds initializes the
@@ -210,6 +216,7 @@
 
 /**
  * \brief A random variable that returns a constant
+ *
  * Class ConstantVariable defines a random number generator that
  * returns the same value every sample.
  */
@@ -217,7 +224,7 @@
 
 public:
   /**
-   * \brief Construct a ConstantVariable RNG that returns zero every sample
+   * Construct a ConstantVariable RNG that returns zero every sample
    */
   ConstantVariable();
   
@@ -249,6 +256,7 @@
 
 /**
  * \brief Return a sequential list of values
+ *
  * Class SequentialVariable defines a random number generator that
  * returns a sequential sequence.  The sequence monotonically
  * increases for a period, then wraps around to the low value 
@@ -259,6 +267,7 @@
 public:
   /**
    * \brief Constructor for the SequentialVariable RNG.
+   *
    * The four parameters define the sequence.  For example
    * SequentialVariable(0,5,1,2) creates a RNG that has the sequence
    * 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 0, 0 ...
@@ -271,16 +280,19 @@
 
   /**
    * \brief Constructor for the SequentialVariable RNG.
+   *
    * Differs from the first only in that the increment parameter is a
    * random variable
    * \param f First value of the sequence.
    * \param l One more than the last value of the sequence.
-   * \param i Reference to a Random variable for the sequence increment
+   * \param i Reference to a RandomVariable for the sequence increment
    * \param c Number of times each member of the sequence is repeated
    */
   SequentialVariable(double f, double l, const RandomVariable& i, uint32_t c = 1);
 
   SequentialVariable(const SequentialVariable& c);
+  
+  ~SequentialVariable();
   /**
    * \return The next value in the Sequence
    */
@@ -297,6 +309,7 @@
 
 /**
  * \brief Exponentially Distributed random var
+ *
  * ExponentialVariable defines a random variable with an exponential distribution
  */
 class ExponentialVariable : public RandomVariable { 
@@ -316,6 +329,7 @@
   /**
    * \brief Constructs an exponential random variable with spefified
    * \brief mean and upper limit.
+   *
    * Since exponential distributions can theoretically return unbounded values,
    * it is sometimes useful to specify a fixed upper limit.  Note however when
    * the upper limit is specified, the true mean of the distribution is 
@@ -492,6 +506,7 @@
 
 /**
  * \brief EmpiricalVariable distribution random var
+ *
  * Defines a random variable  that has a specified, empirical 
  * distribution.  The distribution is specified by a
  * series of calls the the CDF member function, specifying a
@@ -531,14 +546,14 @@
 
 /**
  * Defines an empirical distribution where all values are integers.
- * Indentical to {\tt EmpiricalVariable}, but with slightly different
+ * Indentical to EmpiricalVariable, but with slightly different
  * interpolation between points.
  */
 class IntEmpiricalVariable : public EmpiricalVariable {
 public:
 
   IntEmpiricalVariable();
-
+  
   virtual RandomVariable* Copy() const;
   /**
    * \return An integer value from this empirical distribution
@@ -558,6 +573,7 @@
 public:
   /**
    * \brief Constructor
+   *
    * Creates a generator that returns successive elements of the d array
    * on successive calls to ::Value().  Note that the d pointer is copied
    * for use by the generator (shallow-copy), not its contents, so the 
--- a/src/core/rng-stream.cc	Wed Mar 21 23:17:11 2007 -0700
+++ b/src/core/rng-stream.cc	Thu Mar 22 12:16:10 2007 -0400
@@ -316,6 +316,18 @@
    // Stream initialization moved to separate method.
 }
 
+RngStream::RngStream(const RngStream& r)
+{
+   anti = r.anti;
+   incPrec = r.incPrec;
+   for (int i = 0; i < 6; ++i) {
+      Cg[i] = r.Cg[i];
+      Bg[i] = r.Bg[i];
+      Ig[i] = r.Ig[i];
+   }
+}
+      
+
 void RngStream::InitializeStream()
 { // Moved from the RngStream constructor above to allow seeding
   // AFTER the global package seed has been set in the Random
--- a/src/core/rng-stream.h	Wed Mar 21 23:17:11 2007 -0700
+++ b/src/core/rng-stream.h	Thu Mar 22 12:16:10 2007 -0400
@@ -23,13 +23,10 @@
 
 namespace ns3{
 
-/**
- * \brief RngStream by Pierre L'Ecuyer, University of Montreal
- * Adapted to NS3 by Rajib Bhattacharjea, Georgia Tech.
- */
 class RngStream {
 public:  //public api
   RngStream ();
+  RngStream (const RngStream&);
   void InitializeStream(); // Separate initialization
   void ResetStartStream ();
   void ResetStartSubstream ();