EvolvingObjects
eoFlOrBinOp.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoFlOrBinOp.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@inria.fr
00022              mkeijzer@cs.vu.nl
00023  */
00024 //-----------------------------------------------------------------------------
00025 
00026 #ifndef _eoFlOrBinOp_h
00027 #define _eoFlOrBinOp_h
00028 
00029 #include <eoFunctor.h>
00030 #include <eoOp.h>
00031 
00046 
00047 //                eoFlOrAllAtomBinOp
00049 
00052 template <class EOT>
00053 class eoFlOrAllAtomBinOp : public eoBinOp<EOT>
00054 {
00055 public :
00056 
00057   typedef typename EOT::AtomType AtomType;
00058 
00060   eoFlOrAllAtomBinOp( eoBinOp<AtomType>& _op, float _rate = 1.0):
00061     op(_op), rate( _rate ) {}
00062 
00064   bool operator()(EOT & _eo1, const EOT & _eo2)
00065   {
00066     if (_eo1.size() != _eo2.size())
00067       {
00068         string s = "Operand size don't match in " + className();
00069         throw runtime_error(s);
00070       }
00071     bool changed = false;
00072     for ( unsigned i = 0; i < _eo1.size(); i++ ) {
00073       if ( rng.flip( rate ) ) {
00074         bool changedHere = op( _eo1[i], _eo2[i] );
00075         changed |= changedHere;
00076       }
00077     }
00078     return changed;
00079   }
00080 
00082   virtual string className() const { return "eoFlOrAllAtomBinOp"; }
00083 
00084 private:
00085   double rate;
00086   eoBinOp<AtomType> & op;
00087 };
00088 
00090 //                 eoFlOrKAtomBinOp
00092 
00095 template <class EOT>
00096 class eoFlOrKAtomBinOp : public eoBinOp<EOT>
00097 {
00098 public :
00099 
00100   typedef typename EOT::AtomType AtomType;
00101 
00103   eoFlOrAtomBinOp( eoBinOp<AtomType>& _op, unsigned _k = 1):
00104     op(_op), k( _k ) {}
00105 
00107   bool operator()(EOT & _eo1, const EOT & _eo2)
00108   {
00109     if (_eo1.size() != _eo2.size())
00110       {
00111         string s = "Operand size don't match in " + className();
00112         throw runtime_error(s);
00113       }
00114 
00115     bool changed = false;
00116     for ( unsigned i = 0; i < k; i++ ) 
00117       {
00118         unsigned where = eo::rng.random(_eo1.size());
00119         bool changedHere = op( _eo1[where], _eo2[where] );
00120         changed |= changedHere;
00121       }
00122     return changed;
00123   }
00124 
00126   virtual string className() const { return "eoFlOrKAtomBinOp"; }
00127 
00128 private:
00129   unsigned k;
00130   eoBinOp<AtomType> & op;
00131 };
00132 
00133 
00135 //                        eoFlOrUniformBinOp
00137 
00139 template <class EOT>
00140 class eoFlOrUniformBinOp : public eoBinOp<EOT>
00141 {
00142 public :
00143 
00144   typedef typename EOT::AtomType AtomType;
00145 
00147   eoFlOrUniformBinOp(double _rate=0.5) : eoBinOp<EOT>(_size),
00148     rate(_rate) {}
00149 
00151   bool operator()(EOT & _eo1, const EOT & _eo2)
00152   {
00153     unsigned i;
00154     Atom tmp;
00155     if (_eo1.size() != _eo2.size())
00156       {
00157         string s = "Operand size don't match in " + className();
00158         throw runtime_error(s);
00159   }
00160     bool hasChanged = false;
00161     for (unsigned i=0; i<_eo1.size(); i++)
00162       {
00163         if ( (_eo1[i]!=_eo2[i]) && (eo::rng.filp(rate)) )
00164         {
00165           _eo1[i] = _eo2[i];
00166           hasChanged = true;
00167         }
00168       }
00169     return hasChanged;
00170   }
00171 
00173   virtual string className() const { return "eoFlOrUniformBinOp"; }
00174 
00175 private:
00176   double rate;
00177 };
00178 
00180 //                        eoFlOr1ptBinOp
00182 
00184 template <class EOT>
00185 class eoFlOr1ptBinOp : public eoBinOp<EOT>
00186 {
00187 public :
00188 
00189   typedef typename EOT::AtomType AtomType;
00190 
00192   eoVlUniformBinOp() {}
00193 
00195   bool operator()(EOT & _eo1, EOT & _eo2)
00196   {
00197     unsigned i;
00198     Atom tmp;
00199     if (_eo1.size() != _eo2.size())
00200       {
00201         string s = "Operand size don't match in " + className();
00202         throw runtime_error(s);
00203   }
00204     bool hasChanged = false;
00205     unsigned where = eo::rng.random(_eo1.size()-1);
00206     for (unsigned i=where+1; i<_eo1.size(); i++)
00207       {
00208         if ( (_eo1[i]!=_eo2[i]) )
00209         {
00210           _eo1[i] = _eo2[i];
00211           hasChanged = true;
00212         }
00213       }
00214     return hasChanged;
00215   }
00216 
00218   virtual string className() const { return "eoFlOr1ptBinOp"; }
00219 
00220 };
00221 
00224 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends