EvolvingObjects
eoFlOrQuadOp.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoFlOrQuadOp.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 _eoFlOrQuadOp_h
00027 #define _eoFlOrQuadOp_h
00028 
00029 #include <eoFunctor.h>
00030 #include <eoOp.h>
00031 
00043 
00044 //                        eoFlOrAllAtomQuadOp
00046 
00049 template <class EOT>
00050 class eoFlOrAllAtomQuadOp : public eoQuadOp<EOT>
00051 {
00052 public :
00053 
00054   typedef typename EOT::AtomType AtomType;
00055 
00057   eoFlOrAllAtomQuadOp( eoQuadOp<AtomType>& _op, double _rate = 1):
00058     op(_op), rate( _rate ) {}
00059 
00061   bool operator()(EOT & _eo1, EOT & _eo2)
00062   {
00063     bool changed = false;
00064     for ( unsigned i = 0; i < _eo1.size(); i++ ) {
00065       if ( rng.flip( rate ) ) {
00066         bool changedHere = op( _eo1[i], _eo2[i] );
00067         changed |= changedHere;
00068       }
00069     }
00070     return changed;
00071   }
00072 
00074   virtual string className() const { return "eoFlOrAllAtomQuadOp"; }
00075 
00076 private:
00077   double rate;
00078   eoQuadOp<AtomType> & op;
00079 };
00080 
00082 //                        eoFlOrKAtomQuadOp
00084 
00087 template <class EOT>
00088 class eoFlOrKAtomQuadOp : public eoQuadOp<EOT>
00089 {
00090 public :
00091 
00092   typedef typename EOT::AtomType AtomType;
00093 
00095   eoFlOrAtomQuadOp( eoQuadOp<AtomType>& _op, unsigned _k = 1):
00096     op(_op), k( _k ) {}
00097 
00099   bool operator()(EOT & _eo1, const EOT & _eo2)
00100   {
00101     if (_eo1.size() != _eo2.size())
00102       {
00103         string s = "Operand size don't match in " + className();
00104         throw runtime_error(s);
00105       }
00106 
00107     bool changed = false;
00108     for ( unsigned i = 0; i < k; i++ ) 
00109       {
00110         unsigned where = eo::rng.random(_eo1.size());
00111         bool changedHere = op( _eo1[where], _eo2[where] );
00112         changed |= changedHere;
00113       }
00114     return changed;
00115   }
00116 
00118   virtual string className() const { return "eoFlOrKAtomQuadOp"; }
00119 
00120 private:
00121   unsigned k;
00122   eoQuadOp<AtomType> & op;
00123 };
00124 
00125 
00127 //                        eoFlOrUniformQuadOp
00129 
00130 template <class EOT>
00131 class eoFlOrUniformQuadOp : public eoQuadOp<EOT>
00132 {
00133 public :
00134 
00135   typedef typename EOT::AtomType AtomType;
00136 
00138   eoVlUniformQuadOp(double _rate=0.5) : eoQuadOp<EOT>(_size),
00139     rate(_rate) {}
00140 
00142   bool operator()(EOT & _eo1, EOT & _eo2)
00143   {
00144     unsigned i;
00145     Atom tmp;
00146     if (_eo1.size() != _eo2.size())
00147       {
00148         string s = "Operand size don't match in " + className();
00149         throw runtime_error(s);
00150   }
00151     bool hasChanged = false;
00152     for (unsigned i=0; i<_eo1.size(); i++)
00153       {
00154         if ( (_eo1[i]!=_eo2[i]) && (eo::rng.filp(rate)) )
00155         {
00156           tmp = _eo1[i];
00157           _eo1[i] = _eo2[i];
00158           _eo2[i] = tmp;
00159           hasChanged = true;
00160         }
00161       }
00162     return hasChanged;
00163   }
00164 
00166   virtual string className() const { return "eoFlOrUniformQuadOp"; }
00167 
00168 private:
00169   double rate;
00170 };
00171 
00173 //                        eoFlOr1ptQuadOp
00175 
00176 template <class EOT>
00177 class eoFlOr1ptQuadOp : public eoQuadOp<EOT>
00178 {
00179 public :
00180 
00181   typedef typename EOT::AtomType AtomType;
00182 
00184   eoVlUniformQuadOp() {}
00185 
00187   bool operator()(EOT & _eo1, EOT & _eo2)
00188   {
00189     unsigned i;
00190     Atom tmp;
00191     if (_eo1.size() != _eo2.size())
00192       {
00193         string s = "Operand size don't match in " + className();
00194         throw runtime_error(s);
00195   }
00196     bool hasChanged = false;
00197     unsigned where = eo::rng.random(_eo1.size()-1);
00198     for (unsigned i=where+1; i<_eo1.size(); i++)
00199       {
00200         if ( (_eo1[i]!=_eo2[i]) )
00201         {
00202           tmp = _eo1[i];
00203           _eo1[i] = _eo2[i];
00204           _eo2[i] = tmp;
00205           hasChanged = true;
00206         }
00207       }
00208     return hasChanged;
00209   }
00210 
00212   virtual string className() const { return "eoFlOr1ptQuadOp"; }
00213 
00214 };
00215 
00218 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends