EvolvingObjects
|
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 //----------------------------------------------------------------------------- 00004 // eoEasyEA.h 00005 // (c) GeNeura Team, 1998 00006 /* 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 Contact: todos@geneura.ugr.es, http://geneura.ugr.es 00022 */ 00023 //----------------------------------------------------------------------------- 00024 00025 #ifndef _eoEasyEA_h 00026 #define _eoEasyEA_h 00027 00028 //----------------------------------------------------------------------------- 00029 00030 #include <apply.h> 00031 #include <eoAlgo.h> 00032 #include <eoPopEvalFunc.h> 00033 #include <eoContinue.h> 00034 #include <eoSelect.h> 00035 #include <eoTransform.h> 00036 #include <eoBreed.h> 00037 #include <eoMergeReduce.h> 00038 #include <eoReplacement.h> 00039 00040 template <class EOT> class eoIslandsEasyEA ; 00041 00042 template <class EOT> class eoDistEvalEasyEA ; 00043 00062 template<class EOT> class eoEasyEA: public eoAlgo<EOT> 00063 { 00064 public: 00065 00067 eoEasyEA( 00068 eoContinue<EOT>& _continuator, 00069 eoEvalFunc<EOT>& _eval, 00070 eoBreed<EOT>& _breed, 00071 eoReplacement<EOT>& _replace 00072 ) : continuator(_continuator), 00073 eval (_eval), 00074 loopEval(_eval), 00075 popEval(loopEval), 00076 selectTransform(dummySelect, dummyTransform), 00077 breed(_breed), 00078 mergeReduce(dummyMerge, dummyReduce), 00079 replace(_replace), 00080 isFirstCall(true) 00081 {} 00082 00084 eoEasyEA( 00085 eoContinue<EOT>& _continuator, 00086 eoEvalFunc<EOT>& _eval, 00087 eoBreed<EOT>& _breed, 00088 eoReplacement<EOT>& _replace, 00089 unsigned _offspringSize 00090 ) : continuator(_continuator), 00091 eval (_eval), 00092 loopEval(_eval), 00093 popEval(loopEval), 00094 selectTransform(dummySelect, dummyTransform), 00095 breed(_breed), 00096 mergeReduce(dummyMerge, dummyReduce), 00097 replace(_replace), 00098 isFirstCall(true) 00099 { 00100 offspring.reserve(_offspringSize); // This line avoids an incremental resize of offsprings. 00101 } 00102 00110 eoEasyEA( 00111 eoContinue<EOT>& _continuator, 00112 eoEvalFunc<EOT>& _eval, 00113 eoPopEvalFunc<EOT>& _pop_eval, 00114 eoBreed<EOT>& _breed, 00115 eoReplacement<EOT>& _replace, 00116 unsigned _offspringSize 00117 ) : continuator(_continuator), 00118 eval (_eval), 00119 loopEval(_eval), 00120 popEval(_pop_eval), 00121 selectTransform(dummySelect, dummyTransform), 00122 breed(_breed), 00123 mergeReduce(dummyMerge, dummyReduce), 00124 replace(_replace), 00125 isFirstCall(true) 00126 { 00127 offspring.reserve(_offspringSize); // This line avoids an incremental resize of offsprings. 00128 } 00129 00130 /* 00131 eoEasyEA(eoContinue <EOT> & _continuator, 00132 eoPopEvalFunc <EOT> & _pop_eval, 00133 eoBreed <EOT> & _breed, 00134 eoReplacement <EOT> & _replace 00135 ) : 00136 continuator (_continuator), 00137 eval (dummyEval), 00138 loopEval(dummyEval), 00139 popEval (_pop_eval), 00140 selectTransform (dummySelect, dummyTransform), 00141 breed (_breed), 00142 mergeReduce (dummyMerge, dummyReduce), 00143 replace (_replace), 00144 isFirstCall(true) 00145 { 00146 00147 } 00148 */ 00149 00151 eoEasyEA( 00152 eoContinue<EOT>& _continuator, 00153 eoPopEvalFunc<EOT>& _eval, 00154 eoBreed<EOT>& _breed, 00155 eoReplacement<EOT>& _replace 00156 ) : continuator(_continuator), 00157 eval (dummyEval), 00158 loopEval(dummyEval), 00159 popEval(_eval), 00160 selectTransform(dummySelect, dummyTransform), 00161 breed(_breed), 00162 mergeReduce(dummyMerge, dummyReduce), 00163 replace(_replace), 00164 isFirstCall(true) 00165 {} 00166 00167 00169 eoEasyEA( 00170 eoContinue<EOT>& _continuator, 00171 eoPopEvalFunc<EOT>& _eval, 00172 eoSelect<EOT>& _select, 00173 eoTransform<EOT>& _transform, 00174 eoReplacement<EOT>& _replace 00175 ) : continuator(_continuator), 00176 eval (dummyEval), 00177 loopEval(dummyEval), 00178 popEval(_eval), 00179 selectTransform(_select, _transform), 00180 breed(selectTransform), 00181 mergeReduce(dummyMerge, dummyReduce), 00182 replace(_replace), 00183 isFirstCall(true) 00184 {} 00185 00187 eoEasyEA( 00188 eoContinue<EOT>& _continuator, 00189 eoEvalFunc<EOT>& _eval, 00190 eoBreed<EOT>& _breed, 00191 eoMerge<EOT>& _merge, 00192 eoReduce<EOT>& _reduce 00193 ) : continuator(_continuator), 00194 eval (_eval), 00195 loopEval(_eval), 00196 popEval(loopEval), 00197 selectTransform(dummySelect, dummyTransform), 00198 breed(_breed), 00199 mergeReduce(_merge, _reduce), 00200 replace(mergeReduce), 00201 isFirstCall(true) 00202 {} 00203 00205 eoEasyEA( 00206 eoContinue<EOT>& _continuator, 00207 eoEvalFunc<EOT>& _eval, 00208 eoSelect<EOT>& _select, 00209 eoTransform<EOT>& _transform, 00210 eoReplacement<EOT>& _replace 00211 ) : continuator(_continuator), 00212 eval (_eval), 00213 loopEval(_eval), 00214 popEval(loopEval), 00215 selectTransform(_select, _transform), 00216 breed(selectTransform), 00217 mergeReduce(dummyMerge, dummyReduce), 00218 replace(_replace), 00219 isFirstCall(true) 00220 {} 00221 00223 eoEasyEA( 00224 eoContinue<EOT>& _continuator, 00225 eoEvalFunc<EOT>& _eval, 00226 eoSelect<EOT>& _select, 00227 eoTransform<EOT>& _transform, 00228 eoMerge<EOT>& _merge, 00229 eoReduce<EOT>& _reduce 00230 ) : continuator(_continuator), 00231 eval (_eval), 00232 loopEval(_eval), 00233 popEval(loopEval), 00234 selectTransform(_select, _transform), 00235 breed(selectTransform), 00236 mergeReduce(_merge, _reduce), 00237 replace(mergeReduce), 00238 isFirstCall(true) 00239 {} 00240 00241 00242 00243 00245 virtual void operator()(eoPop<EOT>& _pop) 00246 { 00247 00248 if (isFirstCall) 00249 { 00250 size_t total_capacity = _pop.capacity() + offspring.capacity(); 00251 _pop.reserve(total_capacity); 00252 offspring.reserve(total_capacity); 00253 isFirstCall = false; 00254 } 00255 00256 eoPop<EOT> empty_pop; 00257 00258 do 00259 { 00260 try 00261 { 00262 unsigned pSize = _pop.size(); 00263 00264 offspring.clear(); // new offspring 00265 00266 breed(_pop, offspring); 00267 00268 popEval(_pop, offspring); // eval of parents + offspring if necessary 00269 00270 replace(_pop, offspring); // after replace, the new pop. is in _pop 00271 00272 if (pSize > _pop.size()) 00273 throw std::runtime_error("Population shrinking!"); 00274 else if (pSize < _pop.size()) 00275 throw std::runtime_error("Population growing!"); 00276 } 00277 catch (std::exception& e) 00278 { 00279 std::string s = e.what(); 00280 s.append( " in eoEasyEA"); 00281 throw std::runtime_error( s ); 00282 } 00283 } 00284 while ( continuator( _pop ) ); 00285 } 00286 00287 protected : 00288 00289 // If selectTransform needs not be used, dummySelect and dummyTransform are used 00290 // to instantiate it. 00291 class eoDummySelect : public eoSelect<EOT> 00292 { 00293 public : 00294 void operator()(const eoPop<EOT>&, eoPop<EOT>&) 00295 {} 00296 } 00297 dummySelect; 00298 00299 class eoDummyTransform : public eoTransform<EOT> 00300 { 00301 public : 00302 void operator()(eoPop<EOT>&) 00303 {} 00304 } 00305 dummyTransform; 00306 00307 class eoDummyEval : public eoEvalFunc<EOT> 00308 { 00309 public: 00310 void operator()(EOT &) 00311 {} 00312 } 00313 dummyEval; 00314 00315 eoContinue<EOT>& continuator; 00316 00317 eoEvalFunc <EOT> & eval ; 00318 eoPopLoopEval<EOT> loopEval; 00319 00320 eoPopEvalFunc<EOT>& popEval; 00321 00322 eoSelectTransform<EOT> selectTransform; 00323 eoBreed<EOT>& breed; 00324 00325 // If mergeReduce needs not be used, dummyMerge and dummyReduce are used 00326 // to instantiate it. 00327 eoNoElitism<EOT> dummyMerge; 00328 eoTruncate<EOT> dummyReduce; 00329 00330 eoMergeReduce<EOT> mergeReduce; 00331 eoReplacement<EOT>& replace; 00332 00333 eoPop<EOT> offspring; 00334 00335 bool isFirstCall; 00336 00337 // Friend classes 00338 friend class eoIslandsEasyEA <EOT> ; 00339 friend class eoDistEvalEasyEA <EOT> ; 00340 }; 00346 #endif