# 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 //-----------------------------------------------------------------------------
    00039 #ifndef eoUniformInit_h
    00040 #define eoUniformInit_h
    00041
    00042 #include "eoRNG.h"
    00043 #include "eoInit.h"
    00044 #include "eoRealBounds.h"
    00045 #include <stdexcept>
    00046
    00047 using eo::rng;
    00048
    00062 template <class T = double> class eoUniformInit : public eoInit<T>
    00063 {
    00064   public :
    00066   eoUniformInit(T _max = T(1.0), eoRng& _rng = rng) :
    00067     minim(T(0.0)), range(_max), uniform(_rng)
    00068   {}
    00069
    00071   eoUniformInit(eoRealBounds & _bound, eoRng& _rng = rng) :
    00072     minim(_bound.minimum()), range(_bound.range()), uniform(_rng)
    00073   {}
    00074
    00076   eoUniformInit(T _min, T _max, eoRng& _rng = rng) :
    00077     minim(_min), range(_max-_min), uniform(_rng)
    00078   {
    00079     if (_min>_max)
    00080       throw std::logic_error("Min is greater than Max in uniform_generator");
    00081   }
    00082
    00087   void operator()(T & _t)
    00088   {
    00089     _t = minim+static_cast<T>(uniform.uniform(range));
    00090   }
    00091
    00092 private :
    00093   T minim;
    00094   T range;
    00095   eoRng& uniform;
    00096 };
    00097
    00098
    00100 template <>
    00101 inline void eoUniformInit<bool>::operator()(bool & _b)
    00102 {
    00103     _b = uniform.flip(0.5);
    00104 }
    00105
    00110 class eoBooleanInit : public eoInit<bool>
    00111 {
    00112   public :
    00113   eoBooleanInit(float _bias = 0.5, eoRng& _rng = rng) : bias(_bias), gen(_rng) {}
    00114
    00115   void operator()(bool & _b) { _b = gen.flip(bias); }
    00116   private :
    00117   float bias;
    00118   eoRng& gen;
    00119 };
    00120
    00126 template <class T = double> class eoNormalInit : public eoInit<T>
    00127 {
    00128   public :
    00129     eoNormalInit(T _stdev = T(1.0), eoRng& _rng = rng) : stdev(_stdev), normal(_rng) {}
    00130
    00131   void operator()(T & _t) { _t = (T) normal.normal(stdev); }
    00132
    00133   private :
    00134     T stdev;
    00135   eoRng& normal;
    00136 };
    00137
    00143 template <class T = double> class eoNegExpInit : public eoInit<T>
    00144 {
    00145   public :
    00146     eoNegExpInit(T _mean = 1.0, eoRng& _rng = rng) : mean(_mean), negexp(_rng) {}
    00147
    00148   void operator()(T & _t) { _t = (T) negexp.negexp(mean); }
    00149
    00150   private :
    00151     T mean;
    00152   eoRng& negexp;
    00153 };
    00154
    00155 #endif