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 00056 template <class EOT> 00057 class eoEsChromInit : public eoRealInitBounded<EOT> 00058 { 00059 public: 00060 00061 using eoRealInitBounded<EOT>::size; 00062 using eoRealInitBounded<EOT>::theBounds; 00063 00064 typedef typename EOT::Fitness FitT; 00065 00073 eoEsChromInit(eoRealVectorBounds& _bounds, double _sigma = 0.3, bool _to_scale=false) 00074 : eoRealInitBounded<EOT>(_bounds) 00075 { 00076 // a bit of pre-computations, to save time later (even if some are useless) 00077 // 00078 // first, in the case of one unique sigma 00079 // sigma is scaled by the average range (if that means anything!) 00080 if (_to_scale) 00081 { 00082 double scaleUnique = 0; 00083 for (unsigned i=0; i<size(); i++) 00084 scaleUnique += theBounds().range(i); 00085 scaleUnique /= size(); 00086 uniqueSigma = _sigma * scaleUnique; 00087 } 00088 else 00089 uniqueSigma = _sigma; 00090 // now the case of a vector of sigmas first allocate space according 00091 // to the size of the bounds (see eoRealInitBounded) 00092 vecSigma.resize(size()); 00093 // each sigma is scaled by the range of the corresponding variable 00094 for(unsigned i=0; i<size(); i++) 00095 if(_to_scale) 00096 vecSigma[i] = _sigma * theBounds().range(i); 00097 else 00098 vecSigma[i] = _sigma; 00099 } 00100 00101 00111 eoEsChromInit(eoRealVectorBounds& _bounds, const std::vector<double>& _vecSigma) 00112 : eoRealInitBounded<EOT>(_bounds), uniqueSigma(_vecSigma[0]), vecSigma(_vecSigma) 00113 { 00114 assert(_bounds.size() == size()); 00115 assert(_vecSigma.size() == size()); 00116 } 00117 00118 00119 void operator()(EOT& _eo) 00120 { 00121 eoRealInitBounded<EOT>::operator()(_eo); 00122 create_self_adapt(_eo); 00123 _eo.invalidate(); 00124 } 00125 00126 00127 private: 00128 00133 void create_self_adapt(eoReal<FitT>&) 00134 {} 00135 00136 00137 00144 void create_self_adapt(eoEsSimple<FitT>& result) 00145 { 00146 // pre-computed in the Ctor 00147 result.stdev = uniqueSigma; 00148 } 00149 00150 00151 00160 void create_self_adapt(eoEsStdev<FitT>& result) 00161 { 00162 // pre-computed in the constructor 00163 result.stdevs = vecSigma; 00164 } 00165 00166 00167 00174 void create_self_adapt(eoEsFull<FitT>& result) 00175 { 00176 // first the stdevs (pre-computed in the Ctor) 00177 result.stdevs = vecSigma; 00178 unsigned int theSize = size(); 00179 // nb of rotation angles: N*(N-1)/2 (in general!) 00180 result.correlations.resize(theSize*(theSize - 1) / 2); 00181 for (unsigned i=0; i<result.correlations.size(); ++i) 00182 { 00183 // uniform in [-PI, PI) 00184 result.correlations[i] = rng.uniform(2 * M_PI) - M_PI; 00185 } 00186 } 00187 00188 00189 00191 double uniqueSigma; 00192 00194 std::vector<double> vecSigma; 00195 }; 00196 00197 #endif 00198 00199 00200 00201 // Local Variables: 00202 // coding: iso-8859-1 00203 // mode:C++ 00204 // c-file-style: "Stroustrup" 00205 // comment-column: 35 00206 // fill-column: 80 00207 // End: