eoRndGenerators.h

00001 /* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003   -----------------------------------------------------------------------------
00004   eoRndGenerators.h
00005 
00006   mimics the rnd_generator.h by putting the generators in the EO-hierarchy
00007 
00008  (c) Maarten Keijzer (mak@dhi.dk) and GeNeura Team, 1999, 2000
00009 
00010     This library is free software; you can redistribute it and/or
00011     modify it under the terms of the GNU Lesser General Public
00012     License as published by the Free Software Foundation; either
00013     version 2 of the License, or (at your option) any later version.
00014 
00015     This library is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018     Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public
00021     License along with this library; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 
00024     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
00025  */
00026 
00027 //-----------------------------------------------------------------------------
00028 
00029 #ifndef eoRndGenerators_h
00030 #define eoRndGenerators_h
00031 
00032 #include "eoRNG.h"
00033 #include <eoFunctor.h>
00034 #include <stdexcept>
00035 
00036 using eo::rng;
00037 
00046 template <class T>
00047 class eoRndGenerator : public eoF<T>
00048 {
00049   typedef T AtomType;
00050 };
00051 
00066 template <class T = double> class eoUniformGenerator : public eoRndGenerator<T>
00067 {
00068   // added new ctor with 2 params, and modified the data to minim and range
00069   // (was maxim only). MS 3/11/2000
00070   public :
00071     eoUniformGenerator(T _max = T(1.0), eoRng& _rng = rng) :
00072       minim(T(0.0)), range(_max), uniform(_rng) {}
00073     eoUniformGenerator(T _min, T _max, eoRng& _rng = rng) :
00074       minim(_min), range(_max-_min), uniform(_rng)
00075   {
00076     if (_min>_max)
00077       throw std::logic_error("Min is greater than Max in uniform_generator");
00078   }
00079 
00084   T operator()(void) { return minim+static_cast<T>(uniform.uniform(range)); }
00085 
00086 private :
00087   T minim;
00088   T range;
00089   eoRng& uniform;
00090 };
00091 
00092 
00094 template <>
00095 inline bool eoUniformGenerator<bool>::operator()(void)
00096 {
00097     return uniform.flip(0.5);
00098 }
00099 
00104 class eoBooleanGenerator : public eoRndGenerator<bool>
00105 {
00106   public :
00107   eoBooleanGenerator(float _bias = 0.5, eoRng& _rng = rng) : bias(_bias), gen(_rng) {}
00108 
00109   bool operator()(void) { return gen.flip(bias); }
00110   private :
00111   float bias;
00112   eoRng& gen;
00113 };
00114 
00120 template <class T = double> class eoNormalGenerator : public eoRndGenerator<T>
00121 {
00122   public :
00123     eoNormalGenerator(T _stdev = T(1.0), eoRng& _rng = rng) : stdev(_stdev), normal(_rng) {}
00124 
00125   T operator()(void) { return (T) normal.normal(stdev); }
00126 
00127   private :
00128     T stdev;
00129   eoRng& normal;
00130 };
00131 
00137 template <class T = double> class eoNegExpGenerator : public eoRndGenerator<T>
00138 {
00139   public :
00140     eoNegExpGenerator(T _mean = 1.0, eoRng& _rng = rng) : mean(_mean), negexp(_rng) {}
00141 
00142   T operator()(void) { return (T) negexp.negexp(mean); }
00143 
00144   private :
00145     T mean;
00146   eoRng& negexp;
00147 };
00148 
00149 #endif

Generated on Thu Oct 19 05:06:38 2006 for EO by  doxygen 1.3.9.1