EvolvingObjects
eoFlOrMonOp.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoFlOrMonOp.h
00005 // (c) Marc Schoenauer - Maarten Keijzer 2000-2003
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: Marc.Schoenauer@polytechnique.fr
00022              mkeijzer@cs.vu.nl
00023  */
00024 //-----------------------------------------------------------------------------
00025 
00026 #ifndef _eoFlOrMonOp_h
00027 #define _eoFlOrMonOp_h
00028 
00029 #include <eoFunctor.h>
00030 #include <eoOp.h>
00031 #include <eoInit.h>
00032 
00048 template <class EOT>
00049 class eoFlOrAllMutation : public eoMonOp<EOT>
00050 {
00051 public :
00052 
00053   typedef typename EOT::AtomType AtomType;
00054 
00056   eoFlOrAllMutation(eoMonOp<AtomType> & _atomMutation, double _rate=1.0) :
00057     atomMutation(_atomMutation), rate(_rate) {}
00058 
00060   bool operator()(EOT & _eo)
00061   {
00062     bool modified=false;
00063     for (unsigned i=0; i<_eo.size(); i++)
00064       if (eo::rng.flip(rate))
00065         if (atomMutation(_eo[i]))
00066           modified = true;
00067 
00068     return modified;
00069   }
00070 
00072   virtual std::string className() const
00073   {
00074     return "eoFlOrAllMutation(" + atomMutation.className() + ")";
00075   }
00076 
00077 private:
00078   eoMonOp<AtomType> & atomMutation; // the atom mutation
00079   double rate;                     // the mutation rate PER ATOM
00080 };
00081 
00085 template <class EOT>
00086 class eoFlOrKMutation : public eoMonOp<EOT>
00087 {
00088 public :
00089 
00090   typedef typename EOT::AtomType AtomType;
00091 
00093   eoFlOrKMutation(eoMonOp<AtomType> & _atomMutation, unsigned _nb=1) :
00094     nb(_nb), atomMutation(_atomMutation) {}
00095 
00096 
00098   bool operator()(EOT & _eo)
00099   {
00100     bool modified=false;
00101     for (unsigned k=0; k<nb; k++)
00102       {
00103         unsigned i = rng.random(_eo.size()); // we don't test for duplicates...
00104         if (atomMutation(_eo[i]))
00105           modified = true;
00106       }
00107     return modified;
00108   }
00109 
00111   virtual std::string className() const
00112   {
00113     return "eoFlOrKMutation(" + atomMutation.className() + ")";
00114   }
00115 
00116 private:
00117   unsigned nb;                     // the number of atoms to mutate
00118   eoMonOp<AtomType> & atomMutation; // the atom mutation
00119 };
00120 
00123 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends