// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- //----------------------------------------------------------------------------- // eoEasyEA.h // (c) GeNeura Team, 1998 /* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact: todos@geneura.ugr.es, http://geneura.ugr.es */ //----------------------------------------------------------------------------- #ifndef _eoEasyEA_h #define _eoEasyEA_h //----------------------------------------------------------------------------- #include #include #include #include #include #include #include /** eoEasyEA: An easy-to-use evolutionary algorithm; you can use any chromosome, and any selection transformation, merging and evaluation algorithms; you can even change in runtime parameters of those sub-algorithms */ template class eoEasyEA: public eoAlgo { public: /// Ctor taking a breed and merge. eoEasyEA( eoContinue& _continuator, eoEvalFunc& _eval, eoBreed& _breed, eoReplacement& _replace ) : continuator(_continuator), eval(_eval), selectTransform(dummySelect, dummyTransform), breed(_breed), mergeReduce(dummyMerge, dummyReduce), replace(_replace) {} /// Ctor eoBreed, eoMerge and eoReduce. eoEasyEA( eoContinue& _continuator, eoEvalFunc& _eval, eoBreed& _breed, eoMerge& _merge, eoReduce& _reduce ) : continuator(_continuator), eval(_eval), selectTransform(dummySelect, dummyTransform), breed(_breed), mergeReduce(_merge, _reduce), replace(mergeReduce) {} /// Ctor eoSelect, eoTransform, and eoReplacement eoEasyEA( eoContinue& _continuator, eoEvalFunc& _eval, eoSelect& _select, eoTransform& _transform, eoReplacement& _replace ) : continuator(_continuator), eval(_eval), selectTransform(_select, _transform), breed(selectTransform), mergeReduce(dummyMerge, dummyReduce), replace(_replace) {} /// Ctor eoSelect, eoTransform, eoMerge and eoReduce. eoEasyEA( eoContinue& _continuator, eoEvalFunc& _eval, eoSelect& _select, eoTransform& _transform, eoMerge& _merge, eoReduce& _reduce ) : continuator(_continuator), eval(_eval), selectTransform(_select, _transform), breed(selectTransform), mergeReduce(_merge, _reduce), replace(mergeReduce) {} /// Apply a few generation of evolution to the population. virtual void operator()(eoPop& _pop) { eoPop offspring; while ( continuator( _pop ) ) { try { unsigned pSize = _pop.size(); breed(_pop, offspring); apply(eval, offspring); replace(_pop, offspring); // after replace, the new pop. is in _pop if (pSize > _pop.size()) throw runtime_error("Population shrinking!"); else if (pSize < _pop.size()) throw runtime_error("Population growing!"); } catch (exception& e) { string s = e.what(); s.append( " in eoEasyEA"); throw runtime_error( s ); } } // while } private: // If selectTransform needs not be used, dummySelect and dummyTransform are used // to instantiate it. class eoDummySelect : public eoSelect { public : void operator()(const eoPop&, eoPop&) {} } dummySelect; class eoDummyTransform : public eoTransform { public : void operator()(eoPop&) {} } dummyTransform; eoContinue& continuator; eoEvalFunc& eval; eoSelectTransform selectTransform; eoBreed& breed; // If mergeReduce needs not be used, dummyMerge and dummyReduce are used // to instantiate it. eoNoElitism dummyMerge; eoTruncate dummyReduce; eoMergeReduce mergeReduce; eoReplacement& replace; }; //----------------------------------------------------------------------------- #endif eoSelectTransformReduce_h