EvolvingObjects
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 
00042 /* @addtogroup Variators
00043  * @{
00044  */
00045 
00050 template <class EOT>
00051 class eoVlAddMutation : public eoMonOp<EOT>
00052 {
00053 public :
00054 
00055   typedef typename EOT::AtomType AtomType;
00056 
00062   eoVlAddMutation(unsigned _nMax, eoInit<AtomType> & _atomInit) :
00063     nMax(_nMax), atomInit(_atomInit) {}
00064 
00066   bool operator()(EOT & _eo)
00067   {
00068     if (_eo.size() >= nMax)
00069       return false;                // unmodifed
00070     AtomType atom;
00071     atomInit(atom);
00072     unsigned pos = rng.random(_eo.size()+1);
00073     _eo.insert(_eo.begin()+pos, atom);
00074     return true;
00075   }
00076 
00078   virtual std::string className() const { return "eoVlAddMutation"; }
00079 
00080 private:
00081   unsigned nMax;
00082   eoInit<AtomType> & atomInit;
00083 };
00084 
00085 
00087 template <class EOT>
00088 class eoGeneDelChooser : public eoUF<EOT &, unsigned int>
00089 {
00090 public:
00091   virtual std::string className() const =0;
00092 
00093 };
00094 
00096 template <class EOT>
00097 class eoUniformGeneChooser: public eoGeneDelChooser<EOT>
00098 {
00099 public:
00100     eoUniformGeneChooser(){}
00101     unsigned operator()(EOT & _eo)
00102     {
00103         return eo::rng.random(_eo.size());
00104     }
00105   virtual std::string className() const { return "eoUniformGeneChooser"; }
00106 };
00107 
00112 template <class EOT>
00113 class eoVlDelMutation : public eoMonOp<EOT>
00114 {
00115 public :
00116 
00117   typedef typename EOT::AtomType AtomType;
00118 
00124   eoVlDelMutation(unsigned _nMin, eoGeneDelChooser<EOT> & _chooser) :
00125     nMin(_nMin), uChooser(), chooser(_chooser) {}
00126 
00131   eoVlDelMutation(unsigned _nMin) :
00132     nMin(_nMin), uChooser(), chooser(uChooser) {}
00133 
00137   bool operator()(EOT & _eo)
00138   {
00139     if (_eo.size() <= nMin)
00140       return false;                // unmodifed
00141     unsigned pos = chooser(_eo);
00142     _eo.erase(_eo.begin()+pos);
00143     return true;
00144   }
00145 
00146   virtual std::string className() const
00147   {
00148     std::ostringstream os;
00149     os << "eoVlDelMutation("<<chooser.className() << ")";
00150     return os.str();
00151   }
00152 
00153 private:
00154     unsigned nMin;
00155     eoUniformGeneChooser<EOT> uChooser;
00156     eoGeneDelChooser<EOT> & chooser;
00157 };
00158 
00160 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends