# eoSGATransform.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // eoSGA.h
    00005 // (c) Marc.Schoenauer 2000 - 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: todos@geneura.ugr.es, http://geneura.ugr.es
    00022              Marc.Schoenauer@polytechnique.fr
    00023              mak@dhi.dk
    00024  */
    00025 //-----------------------------------------------------------------------------
    00026
    00027 #ifndef _eoSGATransform_h
    00028 #define _eoSGATransform_h
    00029
    00030 #include <eoInvalidateOps.h>
    00031 #include <eoPop.h>
    00032
    00034 // class eoSGATransform
    00036 #include <vector>          // std::vector
    00037 #include <utils/eoRNG.h>
    00038 #include <eoTransform.h>
    00039
    00045 template<class EOT> class eoSGATransform : public eoTransform<EOT>
    00046 {
    00047  public:
    00048
    00050   eoSGATransform(eoQuadOp<EOT>& _cross, double _cProba,
    00051                  eoMonOp<EOT>& _mutate, double _mProba)
    00052     : cross(_cross),
    00053       crossoverProba(_cProba),
    00054       mutate(_mutate),
    00055       mutationProba(_mProba) {}
    00056
    00057
    00062   void operator()(eoPop<EOT>& _pop)
    00063   {
    00064     unsigned i;
    00065
    00066     for (i=0; i<_pop.size()/2; i++)
    00067       {
    00068         if ( rng.flip(crossoverProba) )
    00069           {
    00070             // this crossover generates 2 offspring from two parents
    00071             cross(_pop[2*i], _pop[2*i+1]);
    00072           }
    00073       }
    00074
    00075     for (i=0; i < _pop.size(); i++)
    00076       {
    00077         if (rng.flip(mutationProba) )
    00078           {
    00079             mutate(_pop[i]);
    00080           }
    00081
    00082       }
    00083   };
    00084
    00085  private:
    00086   eoInvalidateQuadOp<EOT> cross;
    00087   double crossoverProba;
    00088   eoInvalidateMonOp<EOT> mutate;
    00089   double mutationProba;
    00090 };
    00091
    00098 template<class EOT> class eoDynSGATransform : public eoTransform<EOT>
    00099 {
    00100  public:
    00101
    00103   eoDynSGATransform(eoQuadOp<EOT>& _cross, double _cProba,
    00104                  eoMonOp<EOT>& _mutate, double _mProba)
    00105     : cross(_cross),
    00106       crossoverProbaHolder(_cProba), crossoverProba(crossoverProbaHolder),
    00107       mutate(_mutate),
    00108       mutationProbaHolder(_mProba), mutationProba(mutationProbaHolder) {}
    00109
    00111   //  these will usually be some eoValueParam<double>.value()
    00112   //  hence the ...Holder data will bever be used in this case
    00113   eoDynSGATransform(eoQuadOp<EOT>& _cross, double* _cProbaRef,
    00114                  eoMonOp<EOT>& _mutate, double* _mProbaRef)
    00115     : cross(_cross),
    00116       crossoverProbaHolder(0), crossoverProba(*_cProbaRef),
    00117       mutate(_mutate),
    00118       mutationProbaHolder(0), mutationProba(*_mProbaRef) {}
    00119
    00120
    00125   void operator()(eoPop<EOT>& _pop)
    00126   {
    00127     unsigned i;
    00128
    00129     for (i=0; i<_pop.size()/2; i++)
    00130       {
    00131         if ( rng.flip(crossoverProba) )
    00132           {
    00133             // this crossover generates 2 offspring from two parents
    00134             cross(_pop[2*i], _pop[2*i+1]);
    00135           }
    00136       }
    00137
    00138     for (i=0; i < _pop.size(); i++)
    00139       {
    00140         if (rng.flip(mutationProba) )
    00141           {
    00142             mutate(_pop[i]);
    00143           }
    00144
    00145       }
    00146   };
    00147   // accessors - mainly for EASEA
    00148   double & PCrossHandle() { return crossoverProba;}
    00149   double & PMutHandle() { return mutationProba;}
    00150
    00151 private:
    00152   // difference with eoSGATransform: the operator probabilities
    00153   // they can be passed by reference or by value.
    00154   // hence we need here to use a reference, and to eventually store a value
    00155   eoInvalidateQuadOp<EOT> cross;
    00156   double crossoverProbaHolder;  // the value, used only if ctor gets a value
    00157   double& crossoverProba;       // the reference, to be used in operator()
    00158   eoInvalidateMonOp<EOT> mutate;
    00159   double mutationProbaHolder;   // the value, used only if ctor gets a value
    00160   double& mutationProba;        // the reference, to be used in operator()
    00161 };
    00162
    00163
    00164 #endif