EvolvingObjects
|
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 00042 using eo::rng; 00043 00052 template <class T> 00053 class eoRndGenerator : public eoF<T> 00054 { 00055 typedef T AtomType; 00056 }; 00057 00072 template <class T = double> class eoUniformGenerator : public eoRndGenerator<T> 00073 { 00074 // added new ctor with 2 params, and modified the data to minim and range 00075 // (was maxim only). MS 3/11/2000 00076 public : 00077 eoUniformGenerator(T _max = T(1.0), eoRng& _rng = rng) : 00078 minim(T(0.0)), range(_max), uniform(_rng) {} 00079 eoUniformGenerator(T _min, T _max, eoRng& _rng = rng) : 00080 minim(_min), range(_max-_min), uniform(_rng) 00081 { 00082 if (_min>_max) 00083 throw std::logic_error("Min is greater than Max in uniform_generator"); 00084 } 00085 00090 T operator()(void) { return minim+static_cast<T>(uniform.uniform(range)); } 00094 private : 00095 T minim; 00096 T range; 00097 eoRng& uniform; 00098 }; 00103 00104 template <> 00105 inline bool eoUniformGenerator<bool>::operator()(void) 00106 { 00107 return uniform.flip(0.5); 00108 } 00109 00115 class eoBooleanGenerator : public eoRndGenerator<bool> 00116 { 00117 public : 00118 eoBooleanGenerator(float _bias = 0.5, eoRng& _rng = rng) : bias(_bias), gen(_rng) {} 00119 00120 bool operator()(void) { return gen.flip(bias); } 00121 private : 00122 float bias; 00123 eoRng& gen; 00124 }; 00125 00131 template <class T = double> class eoNormalGenerator : public eoRndGenerator<T> 00132 { 00133 public : 00134 eoNormalGenerator(T _stdev = T(1.0), eoRng& _rng = rng) : stdev(_stdev), normal(_rng) {} 00135 00136 T operator()(void) { return (T) normal.normal(stdev); } 00137 00138 private : 00139 T stdev; 00140 eoRng& normal; 00141 }; 00142 00148 template <class T = double> class eoNegExpGenerator : public eoRndGenerator<T> 00149 { 00150 public : 00151 eoNegExpGenerator(T _mean = 1.0, eoRng& _rng = rng) : mean(_mean), negexp(_rng) {} 00152 00153 T operator()(void) { return (T) negexp.negexp(mean); } 00154 00155 private : 00156 T mean; 00157 eoRng& negexp; 00158 }; 00159 00161 #endif