EvolvingObjects
|
00001 // 00002 /* (c) Maarten Keijzer 2000, GeNeura Team, 1998 - EEAAX 1999 00003 00004 This library is free software; you can redistribute it and/or modify it under 00005 the terms of the GNU Lesser General Public License as published by the Free 00006 Software Foundation; either version 2 of the License, or (at your option) any 00007 later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT ANY 00010 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 00011 PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 00012 00013 You should have received a copy of the GNU Lesser General Public License along 00014 with this library; if not, write to the Free Software Foundation, Inc., 59 00015 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00016 00017 Contact: http://eodev.sourceforge.net 00018 todos@geneura.ugr.es, http://geneura.ugr.es 00019 Marc.Schoenauer@polytechnique.fr 00020 mak@dhi.dk 00021 */ 00022 00023 00024 #ifndef _eoEsChromInit_H 00025 #define _eoEsChromInit_H 00026 00027 #include <algorithm> 00028 #include <cassert> 00029 #include <cmath> 00030 #include <vector> 00031 00032 #include <es/eoRealInitBounded.h> 00033 #include <es/eoEsSimple.h> 00034 #include <es/eoEsStdev.h> 00035 #include <es/eoEsFull.h> 00036 00037 #ifndef M_PI 00038 #define M_PI 3.1415926535897932384626433832795 00039 #endif 00040 00057 template <class EOT> 00058 class eoEsChromInit : public eoRealInitBounded<EOT> 00059 { 00060 public: 00061 00062 using eoRealInitBounded<EOT>::size; 00063 using eoRealInitBounded<EOT>::theBounds; 00064 00065 typedef typename EOT::Fitness FitT; 00066 00074 eoEsChromInit(eoRealVectorBounds& _bounds, double _sigma = 0.3, bool _to_scale=false) 00075 : eoRealInitBounded<EOT>(_bounds) 00076 { 00077 // a bit of pre-computations, to save time later (even if some are useless) 00078 // 00079 // first, in the case of one unique sigma 00080 // sigma is scaled by the average range (if that means anything!) 00081 if (_to_scale) 00082 { 00083 double scaleUnique = 0; 00084 for (unsigned i=0; i<size(); i++) 00085 scaleUnique += theBounds().range(i); 00086 scaleUnique /= size(); 00087 uniqueSigma = _sigma * scaleUnique; 00088 } 00089 else 00090 uniqueSigma = _sigma; 00091 // now the case of a vector of sigmas first allocate space according 00092 // to the size of the bounds (see eoRealInitBounded) 00093 vecSigma.resize(size()); 00094 // each sigma is scaled by the range of the corresponding variable 00095 for(unsigned i=0; i<size(); i++) 00096 if(_to_scale) 00097 vecSigma[i] = _sigma * theBounds().range(i); 00098 else 00099 vecSigma[i] = _sigma; 00100 } 00101 00102 00112 eoEsChromInit(eoRealVectorBounds& _bounds, const std::vector<double>& _vecSigma) 00113 : eoRealInitBounded<EOT>(_bounds), uniqueSigma(_vecSigma[0]), vecSigma(_vecSigma) 00114 { 00115 assert(_bounds.size() == size()); 00116 assert(_vecSigma.size() == size()); 00117 } 00118 00119 00120 void operator()(EOT& _eo) 00121 { 00122 eoRealInitBounded<EOT>::operator()(_eo); 00123 create_self_adapt(_eo); 00124 _eo.invalidate(); 00125 } 00126 00127 00128 private: 00129 00134 void create_self_adapt(eoReal<FitT>&) 00135 {} 00136 00137 00138 00145 void create_self_adapt(eoEsSimple<FitT>& result) 00146 { 00147 // pre-computed in the Ctor 00148 result.stdev = uniqueSigma; 00149 } 00150 00151 00152 00161 void create_self_adapt(eoEsStdev<FitT>& result) 00162 { 00163 // pre-computed in the constructor 00164 result.stdevs = vecSigma; 00165 } 00166 00167 00168 00175 void create_self_adapt(eoEsFull<FitT>& result) 00176 { 00177 // first the stdevs (pre-computed in the Ctor) 00178 result.stdevs = vecSigma; 00179 unsigned int theSize = size(); 00180 // nb of rotation angles: N*(N-1)/2 (in general!) 00181 result.correlations.resize(theSize*(theSize - 1) / 2); 00182 for (unsigned i=0; i<result.correlations.size(); ++i) 00183 { 00184 // uniform in [-PI, PI) 00185 result.correlations[i] = rng.uniform(2 * M_PI) - M_PI; 00186 } 00187 } 00188 00189 00190 00192 double uniqueSigma; 00193 00195 std::vector<double> vecSigma; 00196 }; 00197 00198 #endif 00199 00200 00201 00202 // Local Variables: 00203 // coding: iso-8859-1 00204 // mode:C++ 00205 // c-file-style: "Stroustrup" 00206 // comment-column: 35 00207 // fill-column: 80 00208 // End: