# 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
    00044 template <class EOT>
    00045 class eoFlOrAllMutation : public eoMonOp<EOT>
    00046 {
    00047 public :
    00048
    00049   typedef typename EOT::AtomType AtomType;
    00050
    00052   eoFlOrAllMutation(eoMonOp<AtomType> & _atomMutation, double _rate=1.0) :
    00053     atomMutation(_atomMutation), rate(_rate) {}
    00054
    00056   bool operator()(EOT & _eo)
    00057   {
    00058     bool modified=false;
    00059     for (unsigned i=0; i<_eo.size(); i++)
    00060       if (eo::rng.flip(rate))
    00061         if (atomMutation(_eo[i]))
    00062           modified = true;
    00063
    00064     return modified;
    00065   }
    00066
    00068   virtual std::string className() const
    00069   {
    00070     return "eoFlOrAllMutation(" + atomMutation.className() + ")";
    00071   }
    00072
    00073 private:
    00074   eoMonOp<AtomType> & atomMutation; // the atom mutation
    00075   double rate;                     // the mutation rate PER ATOM
    00076 };
    00077
    00081 template <class EOT>
    00082 class eoFlOrKMutation : public eoMonOp<EOT>
    00083 {
    00084 public :
    00085
    00086   typedef typename EOT::AtomType AtomType;
    00087
    00089   eoFlOrKMutation(eoMonOp<AtomType> & _atomMutation, unsigned _nb=1) :
    00090     nb(_nb), atomMutation(_atomMutation) {}
    00091
    00092
    00094   bool operator()(EOT & _eo)
    00095   {
    00096     bool modified=false;
    00097     for (unsigned k=0; k<nb; k++)
    00098       {
    00099         unsigned i = rng.random(_eo.size()); // we don't test for duplicates...
    00100         if (atomMutation(_eo[i]))
    00101           modified = true;
    00102       }
    00103     return modified;
    00104   }
    00105
    00107   virtual std::string className() const
    00108   {
    00109     return "eoFlOrKMutation(" + atomMutation.className() + ")";
    00110   }
    00111
    00112 private:
    00113   unsigned nb;                     // the number of atoms to mutate
    00114   eoMonOp<AtomType> & atomMutation; // the atom mutation
    00115 };
    00116
    00117
    00118 #endif