# eoEasyEA.h

    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 <eoPopAlgo.h>
    00033 #include <eoPopEvalFunc.h>
    00034 #include <eoContinue.h>
    00035 #include <eoSelect.h>
    00036 #include <eoTransform.h>
    00037 #include <eoBreed.h>
    00038 #include <eoMergeReduce.h>
    00039 #include <eoReplacement.h>
    00040
    00041
    00042
    00043 template <class EOT> class eoIslandsEasyEA ;
    00044
    00045 template <class EOT> class eoDistEvalEasyEA ;
    00046
    00063 template<class EOT> class eoEasyEA: public eoAlgo<EOT> {
    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          {}
    00081
    00082   /*
    00083   eoEasyEA(eoContinue <EOT> & _continuator,
    00084            eoPopEvalFunc <EOT> & _pop_eval,
    00085            eoBreed <EOT> & _breed,
    00086            eoReplacement <EOT> & _replace
    00087            ) :
    00088     continuator (_continuator),
    00089     eval (dummyEval),
    00090     loopEval(dummyEval),
    00091     popEval (_pop_eval),
    00092     selectTransform (dummySelect, dummyTransform),
    00093     breed (_breed),
    00094     mergeReduce (dummyMerge, dummyReduce),
    00095     replace (_replace) {
    00096
    00097   }
    00098   */
    00099
    00101      eoEasyEA(
    00102          eoContinue<EOT>& _continuator,
    00103          eoPopEvalFunc<EOT>& _eval,
    00104          eoBreed<EOT>& _breed,
    00105          eoReplacement<EOT>& _replace
    00106      ) : continuator(_continuator),
    00107          eval (dummyEval),
    00108          loopEval(dummyEval),
    00109          popEval(_eval),
    00110          selectTransform(dummySelect, dummyTransform),
    00111          breed(_breed),
    00112          mergeReduce(dummyMerge, dummyReduce),
    00113          replace(_replace)
    00114          {}
    00115
    00117     eoEasyEA(
    00118          eoContinue<EOT>& _continuator,
    00119          eoEvalFunc<EOT>& _eval,
    00120          eoBreed<EOT>& _breed,
    00121          eoMerge<EOT>& _merge,
    00122          eoReduce<EOT>& _reduce
    00123      ) : continuator(_continuator),
    00124          eval (_eval),
    00125          loopEval(_eval),
    00126          popEval(loopEval),
    00127          selectTransform(dummySelect, dummyTransform),
    00128          breed(_breed),
    00129          mergeReduce(_merge, _reduce),
    00130          replace(mergeReduce)
    00131          {}
    00132
    00134     eoEasyEA(
    00135          eoContinue<EOT>& _continuator,
    00136          eoEvalFunc<EOT>& _eval,
    00137          eoSelect<EOT>& _select,
    00138          eoTransform<EOT>& _transform,
    00139          eoReplacement<EOT>& _replace
    00140      ) : continuator(_continuator),
    00141          eval (_eval),
    00142          loopEval(_eval),
    00143          popEval(loopEval),
    00144          selectTransform(_select, _transform),
    00145          breed(selectTransform),
    00146          mergeReduce(dummyMerge, dummyReduce),
    00147          replace(_replace)
    00148          {}
    00149
    00151      eoEasyEA(
    00152          eoContinue<EOT>& _continuator,
    00153          eoEvalFunc<EOT>& _eval,
    00154          eoSelect<EOT>& _select,
    00155          eoTransform<EOT>& _transform,
    00156          eoMerge<EOT>&     _merge,
    00157          eoReduce<EOT>&    _reduce
    00158      ) : continuator(_continuator),
    00159          eval (_eval),
    00160          loopEval(_eval),
    00161          popEval(loopEval),
    00162          selectTransform(_select, _transform),
    00163          breed(selectTransform),
    00164          mergeReduce(_merge, _reduce),
    00165          replace(mergeReduce)
    00166          {}
    00167
    00168
    00169
    00170
    00172   virtual void operator()(eoPop<EOT>& _pop)
    00173   {
    00174     eoPop<EOT> offspring, empty_pop;
    00175     popEval(empty_pop, _pop); // A first eval of pop.
    00176     do
    00177     {
    00178       try
    00179       {
    00180          unsigned pSize = _pop.size();
    00181          offspring.clear(); // new offspring
    00182
    00183          breed(_pop, offspring);
    00184
    00185          popEval(_pop, offspring); // eval of parents + offspring if necessary
    00186
    00187          replace(_pop, offspring); // after replace, the new pop. is in _pop
    00188
    00189          if (pSize > _pop.size())
    00190              throw std::runtime_error("Population shrinking!");
    00191          else if (pSize < _pop.size())
    00192              throw std::runtime_error("Population growing!");
    00193
    00194       }
    00195       catch (std::exception& e)
    00196       {
    00197             std::string s = e.what();
    00198             s.append( " in eoEasyEA");
    00199             throw std::runtime_error( s );
    00200       }
    00201     } while ( continuator( _pop ) );
    00202   }
    00203
    00204 protected :
    00205
    00206   // If selectTransform needs not be used, dummySelect and dummyTransform are used
    00207   // to instantiate it.
    00208   class eoDummySelect : public eoSelect<EOT>
    00209      { public : void operator()(const eoPop<EOT>&, eoPop<EOT>&) {} } dummySelect;
    00210
    00211      class eoDummyTransform : public eoTransform<EOT>
    00212      { public : void operator()(eoPop<EOT>&) {} } dummyTransform;
    00213
    00214   class eoDummyEval : public eoEvalFunc<EOT>
    00215   {public: void operator()(EOT &) {} } dummyEval;
    00216
    00217   eoContinue<EOT>&          continuator;
    00218
    00219   eoEvalFunc <EOT> &        eval ;
    00220   eoPopLoopEval<EOT>        loopEval;
    00221
    00222   eoPopEvalFunc<EOT>&       popEval;
    00223
    00224   eoSelectTransform<EOT>    selectTransform;
    00225   eoBreed<EOT>&             breed;
    00226
    00227   // If mergeReduce needs not be used, dummyMerge and dummyReduce are used
    00228   // to instantiate it.
    00229   eoNoElitism<EOT>          dummyMerge;
    00230   eoTruncate<EOT>           dummyReduce;
    00231
    00232   eoMergeReduce<EOT>        mergeReduce;
    00233   eoReplacement<EOT>&       replace;
    00234
    00235   // Friend classes
    00236   friend class eoIslandsEasyEA <EOT> ;
    00237   friend class eoDistEvalEasyEA <EOT> ;
    00238 };
    00239
    00240 //-----------------------------------------------------------------------------
    00241
    00242 #endif
    00243