EvolvingObjects
eoUniformInit.h
00001 /* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003   -----------------------------------------------------------------------------
00004   eoUniformInit.h
00005 
00006  (c) Maarten Keijzer, GeNeura Team, Marc Schoenauer, 1999 - 2002
00007 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Lesser General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public
00019     License along with this library; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 
00022     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
00023              Marc.Schoenauer@inria.fr
00024  */
00025 
00026 //-----------------------------------------------------------------------------
00038 #ifndef eoUniformInit_h
00039 #define eoUniformInit_h
00040 
00041 #include "eoRNG.h"
00042 #include "eoInit.h"
00043 #include "eoRealBounds.h"
00044 #include <stdexcept>
00045 
00046 
00051 using eo::rng;
00052 
00067 template <class T = double> class eoUniformInit : public eoInit<T>
00068 {
00069   public :
00071   eoUniformInit(T _max = T(1.0), eoRng& _rng = rng) :
00072     minim(T(0.0)), range(_max), uniform(_rng)
00073   {}
00074 
00076   eoUniformInit(eoRealBounds & _bound, eoRng& _rng = rng) :
00077     minim(_bound.minimum()), range(_bound.range()), uniform(_rng)
00078   {}
00079 
00081   eoUniformInit(T _min, T _max, eoRng& _rng = rng) :
00082     minim(_min), range(_max-_min), uniform(_rng)
00083   {
00084     if (_min>_max)
00085       throw std::logic_error("Min is greater than Max in uniform_generator");
00086   }
00087 
00092   void operator()(T & _t)
00093   {
00094     _t = minim+static_cast<T>(uniform.uniform(range));
00095   }
00096 
00097 private :
00098   T minim;
00099   T range;
00100   eoRng& uniform;
00101 };
00102 
00103 
00105 template <>
00106 inline void eoUniformInit<bool>::operator()(bool & _b)
00107 {
00108     _b = uniform.flip(0.5);
00109 }
00110 
00115 class eoBooleanInit : public eoInit<bool>
00116 {
00117   public :
00118   eoBooleanInit(float _bias = 0.5, eoRng& _rng = rng) : bias(_bias), gen(_rng) {}
00119 
00120   void operator()(bool & _b) { _b = gen.flip(bias); }
00121   private :
00122   float bias;
00123   eoRng& gen;
00124 };
00125 
00131 template <class T = double> class eoNormalInit : public eoInit<T>
00132 {
00133   public :
00134     eoNormalInit(T _stdev = T(1.0), eoRng& _rng = rng) : stdev(_stdev), normal(_rng) {}
00135 
00136   void operator()(T & _t) { _t = (T) normal.normal(stdev); }
00137 
00138   private :
00139     T stdev;
00140   eoRng& normal;
00141 };
00142 
00148 template <class T = double> class eoNegExpInit : public eoInit<T>
00149 {
00150   public :
00151     eoNegExpInit(T _mean = 1.0, eoRng& _rng = rng) : mean(_mean), negexp(_rng) {}
00152 
00153   void operator()(T & _t) { _t = (T) negexp.negexp(mean); }
00154 
00155   private :
00156     T mean;
00157   eoRng& negexp;
00158 };
00159 
00161 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends