diff -r aa1bff27656c src/core/random-variable.cc --- a/src/core/random-variable.cc Tue Mar 03 18:52:00 2009 +0000 +++ b/src/core/random-variable.cc Tue Mar 03 15:30:46 2009 -0500 @@ -95,6 +95,7 @@ virtual double GetValue() = 0; virtual uint32_t GetInteger(); virtual RandomVariableBase* Copy(void) const = 0; + virtual void SerializeTo (std::ostream &os) const = 0; protected: RngStream* m_generator; //underlying generator being wrapped @@ -207,6 +208,7 @@ virtual double GetValue(double s, double l); virtual RandomVariableBase* Copy(void) const; + virtual void SerializeTo (std::ostream &os) const; private: double m_min; @@ -257,6 +259,11 @@ return new UniformVariableImpl(*this); } +void UniformVariableImpl::SerializeTo (std::ostream& os) const +{ + os << "Uniform:" << GetMin () << ":" << GetMax (); +} + UniformVariable::UniformVariable() : RandomVariable (UniformVariableImpl ()) {} @@ -309,6 +316,7 @@ virtual double GetValue(); virtual uint32_t GetInteger(); virtual RandomVariableBase* Copy(void) const; + virtual void SerializeTo (std::ostream& os) const; private: double m_const; }; @@ -338,6 +346,11 @@ RandomVariableBase* ConstantVariableImpl::Copy() const { return new ConstantVariableImpl(*this); +} + +void ConstantVariableImpl::SerializeTo (std::ostream& os) const +{ + os << "Constant:" << m_const; } ConstantVariable::ConstantVariable() @@ -393,6 +406,7 @@ */ virtual double GetValue(); virtual RandomVariableBase* Copy(void) const; + virtual void SerializeTo (std::ostream& os) const; private: double m_min; double m_max; @@ -439,6 +453,12 @@ return new SequentialVariableImpl(*this); } +void SequentialVariableImpl::SerializeTo (std::ostream& os) const +{ + // XXX: support other distributions + os.setstate (std::ios_base::badbit); +} + SequentialVariable::SequentialVariable(double f, double l, double i, uint32_t c) : RandomVariable (SequentialVariableImpl (f, l, i, c)) {} @@ -484,6 +504,7 @@ */ virtual double GetValue(); virtual RandomVariableBase* Copy(void) const; + virtual void SerializeTo (std::ostream& os) const; private: double m_mean; // Mean value of RV @@ -519,6 +540,12 @@ RandomVariableBase* ExponentialVariableImpl::Copy() const { return new ExponentialVariableImpl(*this); +} + +void ExponentialVariableImpl::SerializeTo (std::ostream& os) const +{ + // XXX: support other distributions + os.setstate (std::ios_base::badbit); } ExponentialVariable::ExponentialVariable() @@ -578,6 +605,7 @@ */ virtual double GetValue(); virtual RandomVariableBase* Copy() const; + virtual void SerializeTo (std::ostream& os) const; private: double m_mean; // Mean value of RV @@ -621,6 +649,12 @@ return new ParetoVariableImpl(*this); } +void ParetoVariableImpl::SerializeTo (std::ostream& os) const +{ + // XXX: support other distributions + os.setstate (std::ios_base::badbit); +} + ParetoVariable::ParetoVariable () : RandomVariable (ParetoVariableImpl ()) {} @@ -682,6 +716,7 @@ */ virtual double GetValue(); virtual RandomVariableBase* Copy(void) const; + virtual void SerializeTo (std::ostream& os) const; private: double m_mean; // Mean value of RV @@ -720,6 +755,12 @@ return new WeibullVariableImpl(*this); } +void WeibullVariableImpl::SerializeTo (std::ostream& os) const +{ + // XXX: support other distributions + os.setstate (std::ios_base::badbit); +} + WeibullVariable::WeibullVariable() : RandomVariable (WeibullVariableImpl ()) {} @@ -762,6 +803,7 @@ */ virtual double GetValue(); virtual RandomVariableBase* Copy(void) const; + virtual void SerializeTo (std::ostream& os) const; double GetMean (void) const; double GetVariance (void) const; @@ -839,6 +881,16 @@ return new NormalVariableImpl(*this); } +void NormalVariableImpl::SerializeTo (std::ostream& os) const +{ + os << "Normal:" << GetMean () << ":" << GetVariance (); + double bound = GetBound (); + if (bound != NormalVariableImpl::INFINITE_VALUE) + { + os << ":" << bound; + } +} + double NormalVariableImpl::GetMean (void) const { @@ -883,6 +935,7 @@ */ virtual double GetValue(); virtual RandomVariableBase* Copy(void) const; + virtual void SerializeTo (std::ostream& os) const; /** * \brief Specifies a point in the empirical distribution * \param v The function value for this point @@ -978,6 +1031,12 @@ return new EmpiricalVariableImpl(*this); } +void EmpiricalVariableImpl::SerializeTo (std::ostream& os) const +{ + // XXX: support other distributions + os.setstate (std::ios_base::badbit); +} + void EmpiricalVariableImpl::CDF(double v, double c) { // Add a new empirical datapoint to the empirical cdf // NOTE. These MUST be inserted in non-decreasing order @@ -1034,6 +1093,7 @@ IntEmpiricalVariableImpl(); virtual RandomVariableBase* Copy(void) const; + virtual void SerializeTo (std::ostream& os) const; /** * \return An integer value from this empirical distribution */ @@ -1053,6 +1113,12 @@ RandomVariableBase* IntEmpiricalVariableImpl::Copy() const { return new IntEmpiricalVariableImpl(*this); +} + +void IntEmpiricalVariableImpl::SerializeTo (std::ostream& os) const +{ + // XXX: support other distributions + os.setstate (std::ios_base::badbit); } double IntEmpiricalVariableImpl::Interpolate(double c1, double c2, @@ -1091,6 +1157,7 @@ */ virtual double GetValue(); virtual RandomVariableBase* Copy(void) const; + virtual void SerializeTo (std::ostream& os) const; private: uint32_t count; uint32_t next; @@ -1118,6 +1185,12 @@ return new DeterministicVariableImpl(*this); } +void DeterministicVariableImpl::SerializeTo (std::ostream& os) const +{ + // XXX: support other distributions + os.setstate (std::ios_base::badbit); +} + DeterministicVariable::DeterministicVariable(double* d, uint32_t c) : RandomVariable (DeterministicVariableImpl (d, c)) {} @@ -1138,6 +1211,7 @@ */ virtual double GetValue (); virtual RandomVariableBase* Copy(void) const; + virtual void SerializeTo (std::ostream& os) const; private: double m_mu; @@ -1148,6 +1222,12 @@ RandomVariableBase* LogNormalVariableImpl::Copy () const { return new LogNormalVariableImpl (m_mu, m_sigma); +} + +void LogNormalVariableImpl::SerializeTo (std::ostream& os) const +{ + // XXX: support other distributions + os.setstate (std::ios_base::badbit); } LogNormalVariableImpl::LogNormalVariableImpl (double mu, double sigma) @@ -1240,6 +1320,7 @@ */ virtual double GetValue(); virtual RandomVariableBase* Copy(void) const; + virtual void SerializeTo (std::ostream& os) const; private: double m_min; @@ -1279,6 +1360,12 @@ return new TriangularVariableImpl(*this); } +void TriangularVariableImpl::SerializeTo (std::ostream& os) const +{ + // XXX: support other distributions + os.setstate (std::ios_base::badbit); +} + TriangularVariable::TriangularVariable() : RandomVariable (TriangularVariableImpl ()) {} @@ -1290,31 +1377,7 @@ std::ostream &operator << (std::ostream &os, const RandomVariable &var) { RandomVariableBase *base = var.Peek (); - ConstantVariableImpl *constant = dynamic_cast (base); - if (constant != 0) - { - os << "Constant:" << constant->GetValue (); - return os; - } - UniformVariableImpl *uniform = dynamic_cast (base); - if (uniform != 0) - { - os << "Uniform:" << uniform->GetMin () << ":" << uniform->GetMax (); - return os; - } - NormalVariableImpl *normal = dynamic_cast (base); - if (normal != 0) - { - os << "Normal:" << normal->GetMean () << ":" << normal->GetVariance (); - double bound = normal->GetBound (); - if (bound != NormalVariableImpl::INFINITE_VALUE) - { - os << ":" << bound; - } - return os; - } - // XXX: support other distributions - os.setstate (std::ios_base::badbit); + base->SerializeTo (os); return os; } std::istream &operator >> (std::istream &is, RandomVariable &var)