yat  0.8.3pre
random.h
00001 #ifndef _theplu_yat_random_
00002 #define _theplu_yat_random_
00003 
00004 // $Id: random.h 2580 2011-10-05 17:35:22Z peter $
00005 
00006 /*
00007   Copyright (C) 2005, 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
00008   Copyright (C) 2009, 2010, 2011 Peter Johansson
00009 
00010   This file is part of the yat library, http://dev.thep.lu.se/yat
00011 
00012   The yat library is free software; you can redistribute it and/or
00013   modify it under the terms of the GNU General Public License as
00014   published by the Free Software Foundation; either version 3 of the
00015   License, or (at your option) any later version.
00016 
00017   The yat library is distributed in the hope that it will be useful,
00018   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00020   General Public License for more details.
00021 
00022   You should have received a copy of the GNU General Public License
00023   along with yat. If not, see <http://www.gnu.org/licenses/>.
00024 */
00025 
00026 #include "yat/statistics/Histogram.h"
00027 #include "yat/utility/deprecate.h"
00028 
00029 #include <boost/concept_check.hpp>
00030 
00031 #include <gsl/gsl_rng.h>
00032 #include <gsl/gsl_randist.h>
00033 
00034 #include <algorithm>
00035 #include <string>
00036 #include <vector>
00037 
00038 namespace theplu {
00039 namespace yat {
00040 namespace random {
00041 
00042   //forward declaration
00043   class RNG_state;
00044 
00080   class RNG
00081   {
00082   public:
00083 
00097     static RNG* instance(void);
00098 
00103     unsigned long max(void) const;
00104 
00109     unsigned long min(void) const;
00110 
00114     std::string name(void) const;
00115 
00119     const gsl_rng* rng(void) const;
00120 
00130     void seed(unsigned long s) const;
00131 
00137     unsigned long seed_from_devurandom(void);
00138 
00146     int set_state(const RNG_state&);
00147 
00148   private:
00149     RNG(void);
00150 
00158     RNG(const RNG&);
00159 
00164     RNG& operator=(const RNG&);
00165 
00166     virtual ~RNG(void);
00167 
00168     static RNG* instance_;
00169     gsl_rng* rng_;
00170   };
00171 
00172 
00176   class RNG_state
00177   {
00178   public:
00182     explicit RNG_state(const RNG*);
00183 
00189     RNG_state(const RNG_state&);
00190 
00194     ~RNG_state(void);
00195 
00199     const gsl_rng* rng(void) const;
00200 
00206     RNG_state& operator=(const RNG_state&);
00207 
00208   private:
00209     gsl_rng* rng_;
00210 
00211     void clone(const gsl_rng&);
00212   };
00213     
00214 
00215   // --------------------- Discrete distribtuions ---------------------
00216 
00225   class Discrete
00226   {
00227   public:
00231     Discrete(void);
00232 
00236     virtual ~Discrete(void);
00237 
00248     void seed(unsigned long s) const YAT_DEPRECATE;
00249 
00258     unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
00259 
00263     virtual unsigned long operator()(void) const = 0;
00264     
00265   protected:
00267     RNG* rng_;
00268   };
00269 
00273   class DiscreteGeneral : public Discrete 
00274   {
00275   public:
00281     explicit DiscreteGeneral(const statistics::Histogram& hist);
00282     
00288     DiscreteGeneral(const DiscreteGeneral&);
00289 
00293     ~DiscreteGeneral(void);
00294 
00303     unsigned long operator()(void) const;
00304 
00310     DiscreteGeneral& operator=(const DiscreteGeneral&);
00311 
00312   private:
00313     void free(void);
00314     void preproc(void);
00315 
00316     gsl_ran_discrete_t* gen_;
00317     std::vector<double> p_;
00318   };
00319 
00332   class DiscreteUniform 
00333     : public Discrete, 
00334       public std::unary_function<unsigned long, unsigned long>
00335   {
00336   public:
00350     explicit DiscreteUniform(unsigned long n=0);
00351 
00361     unsigned long operator()(void) const;
00362 
00373     unsigned long operator()(unsigned long n) const;
00374 
00375   private:
00376     unsigned long range_;
00377   };
00378 
00394   class Poisson : public Discrete
00395   {
00396   public:
00402     explicit Poisson(const double m=1);
00403 
00407     unsigned long operator()(void) const;
00408 
00415     unsigned long operator()(const double m) const;
00416 
00417   private:
00418     double m_;
00419   };
00420 
00421   // --------------------- Continuous distribtuions ---------------------
00422 
00428   class Continuous
00429   {
00430   public:
00431 
00435     Continuous(void);
00436 
00440     virtual ~Continuous(void);
00441 
00452     void seed(unsigned long s) const YAT_DEPRECATE;
00453 
00462     unsigned long seed_from_devurandom(void) YAT_DEPRECATE;
00463 
00467     virtual double operator()(void) const = 0;
00468 
00469   protected:
00471     RNG* rng_;
00472   };
00473 
00474   // ContinuousUniform is declared before ContinuousGeneral to avoid
00475   // forward declaration
00486   class ContinuousUniform : public Continuous
00487   {
00488   public:
00489     double operator()(void) const;
00490   };
00491 
00495   class ContinuousGeneral : public Continuous
00496   {
00497   public:
00503     explicit ContinuousGeneral(const statistics::Histogram& hist);
00504 
00513     double operator()(void) const;
00514 
00515   private:
00516     const DiscreteGeneral discrete_;
00517     const statistics::Histogram hist_;
00518     ContinuousUniform u_;
00519   };
00520 
00529   class Exponential : public Continuous
00530   {
00531   public:
00537     explicit Exponential(const double m=1);
00538 
00542     double operator()(void) const;
00543 
00550     double operator()(const double m) const;
00551 
00552   private:
00553     double m_;
00554   };
00555 
00569   class Gaussian : public Continuous
00570   {
00571   public:
00578     explicit Gaussian(const double s=1, const double m=0);
00579 
00583     double operator()(void) const;
00584 
00591     double operator()(const double s) const;
00592 
00599     double operator()(const double s, const double m) const;
00600 
00601   private:
00602     double m_;
00603     double s_;
00604   };
00605 
00615   template<typename RandomAccessIterator>
00616   void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
00617   {
00618     typedef RandomAccessIterator rai;
00619     BOOST_CONCEPT_ASSERT((boost::Mutable_RandomAccessIterator<rai>));
00620     DiscreteUniform rnd;
00621     std::random_shuffle(first, last, rnd);
00622   }
00623 
00624 }}} // of namespace random, yat, and theplu
00625 
00626 #endif

Generated on Thu Dec 20 2012 03:12:58 for yat by  doxygen 1.8.0-20120409