EvolvingObjects
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 
00049 template <class EOT>
00050 class eoSGA : public eoAlgo<EOT>
00051 {
00052 public :
00053 
00054   // added this second ctor as I didn't like the ordering of the parameters
00055   // in the one above. Any objection :-) MS
00056   eoSGA(
00057         eoSelectOne<EOT>& _select,
00058         eoQuadOp<EOT>& _cross, float _crate,
00059         eoMonOp<EOT>& _mutate, float _mrate,
00060         eoEvalFunc<EOT>& _eval,
00061         eoContinue<EOT>& _cont)
00062     : cont(_cont),
00063           mutate(_mutate),
00064           mutationRate(_mrate),
00065           cross(_cross),
00066           crossoverRate(_crate),
00067           select(_select),
00068           eval(_eval) {}
00069 
00070   void operator()(eoPop<EOT>& _pop)
00071   {
00072     eoPop<EOT> offspring;
00073 
00074     do
00075       {
00076         select(_pop, offspring);
00077 
00078         unsigned i;
00079 
00080         for (i=0; i<_pop.size()/2; i++)
00081           {
00082             if ( rng.flip(crossoverRate) )
00083             {
00084                     // this crossover generates 2 offspring from two parents
00085                     if (cross(offspring[2*i], offspring[2*i+1]))
00086         {
00087           offspring[2*i].invalidate();
00088           offspring[2*i+1].invalidate();
00089         }
00090       }
00091           }
00092 
00093         for (i=0; i < offspring.size(); i++)
00094           {
00095             if (rng.flip(mutationRate) )
00096             {
00097                      if (mutate(offspring[i]))
00098           offspring[i].invalidate();
00099             }
00100           }
00101 
00102         _pop.swap(offspring);
00103         apply<EOT>(eval, _pop);
00104 
00105       } while (cont(_pop));
00106   }
00107 
00108 private :
00109 
00110   eoContinue<EOT>& cont;
00112   eoInvalidateMonOp<EOT> mutate;
00113   float mutationRate;
00114   // eoInvalidateQuadOp invalidates the embedded operator
00115   eoInvalidateQuadOp<EOT> cross;
00116   float crossoverRate;
00117   eoSelectPerc<EOT> select;
00118   eoEvalFunc<EOT>& eval;
00119 };
00120 
00121 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends