# eoVariableLengthMutation.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // eoVariableLengthMutation.h
    00005 // (c) Marc Schoenauer 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: Marc.Schoenauer@polytechnique.fr
    00022              mkeijzer@cs.vu.nl
    00023  */
    00024 //-----------------------------------------------------------------------------
    00025
    00026 #ifndef _eoVariableLengthMutation_h
    00027 #define _eoVariableLengthMutation_h
    00028
    00029 #include <eoFunctor.h>
    00030 #include <eoOp.h>
    00031 #include <eoInit.h>
    00032
    00047 template <class EOT>
    00048 class eoVlAddMutation : public eoMonOp<EOT>
    00049 {
    00050 public :
    00051
    00052   typedef typename EOT::AtomType AtomType;
    00053
    00059   eoVlAddMutation(unsigned _nMax, eoInit<AtomType> & _atomInit) :
    00060     nMax(_nMax), atomInit(_atomInit) {}
    00061
    00063   bool operator()(EOT & _eo)
    00064   {
    00065     if (_eo.size() >= nMax)
    00066       return false;                // unmodifed
    00067     AtomType atom;
    00068     atomInit(atom);
    00069     unsigned pos = rng.random(_eo.size()+1);
    00070     _eo.insert(_eo.begin()+pos, atom);
    00071     return true;
    00072   }
    00073
    00075   virtual std::string className() const { return "eoVlAddMutation"; }
    00076
    00077 private:
    00078   unsigned nMax;
    00079   eoInit<AtomType> & atomInit;
    00080 };
    00081
    00082
    00084 template <class EOT>
    00085 class eoGeneDelChooser : public eoUF<EOT &, unsigned int>
    00086 {
    00087 public:
    00088   virtual std::string className() const =0;
    00089
    00090 };
    00091
    00093 template <class EOT>
    00094 class eoUniformGeneChooser: public eoGeneDelChooser<EOT>
    00095 {
    00096 public:
    00097     eoUniformGeneChooser(){}
    00098     unsigned operator()(EOT & _eo)
    00099     {
    00100         return eo::rng.random(_eo.size());
    00101     }
    00102   virtual std::string className() const { return "eoUniformGeneChooser"; }
    00103 };
    00104
    00109 template <class EOT>
    00110 class eoVlDelMutation : public eoMonOp<EOT>
    00111 {
    00112 public :
    00113
    00114   typedef typename EOT::AtomType AtomType;
    00115
    00121   eoVlDelMutation(unsigned _nMin, eoGeneDelChooser<EOT> & _chooser) :
    00122     nMin(_nMin), uChooser(), chooser(_chooser) {}
    00123
    00128   eoVlDelMutation(unsigned _nMin) :
    00129     nMin(_nMin), uChooser(), chooser(uChooser) {}
    00130
    00134   bool operator()(EOT & _eo)
    00135   {
    00136     if (_eo.size() <= nMin)
    00137       return false;                // unmodifed
    00138     unsigned pos = chooser(_eo);
    00139     _eo.erase(_eo.begin()+pos);
    00140     return true;
    00141   }
    00142
    00143   virtual std::string className() const
    00144   {
    00145     std::ostringstream os;
    00146     os << "eoVlDelMutation("<<chooser.className() << ")";
    00147     return os.str();
    00148   }
    00149
    00150 private:
    00151     unsigned nMin;
    00152     eoUniformGeneChooser<EOT> uChooser;
    00153     eoGeneDelChooser<EOT> & chooser;
    00154 };
    00155
    00156
    00157
    00158 #endif