# eoSGA.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // eoSGA.h
    00005 // (c) GeNeura Team, 2000 - EEAAX 1999 - Maarten Keijzer 2000
    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              Marc.Schoenauer@polytechnique.fr
    00023              mak@dhi.dk
    00024  */
    00025 //-----------------------------------------------------------------------------
    00026
    00027 #ifndef _eoSGA_h
    00028 #define _eoSGA_h
    00029
    00030 #include <eoInvalidateOps.h>
    00031 #include <eoContinue.h>
    00032 #include <eoPop.h>
    00033 #include <eoSelectOne.h>
    00034 #include <eoSelectPerc.h>
    00035 #include <eoEvalFunc.h>
    00036 #include <eoAlgo.h>
    00037 #include <apply.h>
    00038
    00048 template <class EOT>
    00049 class eoSGA : public eoAlgo<EOT>
    00050 {
    00051 public :
    00052
    00053   // added this second ctor as I didn't like the ordering of the parameters
    00054   // in the one above. Any objection :-) MS
    00055   eoSGA(
    00056         eoSelectOne<EOT>& _select,
    00057         eoQuadOp<EOT>& _cross, float _crate,
    00058         eoMonOp<EOT>& _mutate, float _mrate,
    00059         eoEvalFunc<EOT>& _eval,
    00060         eoContinue<EOT>& _cont)
    00061     : cont(_cont),
    00062           mutate(_mutate),
    00063           mutationRate(_mrate),
    00064           cross(_cross),
    00065           crossoverRate(_crate),
    00066           select(_select),
    00067           eval(_eval) {}
    00068
    00069   void operator()(eoPop<EOT>& _pop)
    00070   {
    00071     eoPop<EOT> offspring;
    00072
    00073     do
    00074       {
    00075         select(_pop, offspring);
    00076
    00077         unsigned i;
    00078
    00079         for (i=0; i<_pop.size()/2; i++)
    00080           {
    00081             if ( rng.flip(crossoverRate) )
    00082             {
    00083                     // this crossover generates 2 offspring from two parents
    00084                     if (cross(offspring[2*i], offspring[2*i+1]))
    00085         {
    00086           offspring[2*i].invalidate();
    00087           offspring[2*i+1].invalidate();
    00088         }
    00089       }
    00090           }
    00091
    00092         for (i=0; i < _pop.size(); i++)
    00093           {
    00094             if (rng.flip(mutationRate) )
    00095             {
    00096                      if (mutate(offspring[i]))
    00097           offspring[i].invalidate();
    00098             }
    00099           }
    00100
    00101         _pop.swap(offspring);
    00102         apply<EOT>(eval, _pop);
    00103
    00104       } while (cont(_pop));
    00105   }
    00106
    00107 private :
    00108
    00109   eoContinue<EOT>& cont;
    00111   eoInvalidateMonOp<EOT> mutate;
    00112   float mutationRate;
    00113   // eoInvalidateQuadOp invalidates the embedded operator
    00114   eoInvalidateQuadOp<EOT> cross;
    00115   float crossoverRate;
    00116   eoSelectPerc<EOT> select;
    00117   eoEvalFunc<EOT>& eval;
    00118 };
    00119
    00120 #endif