EvolvingObjects
|
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 00047 template<class EOT> class eoSGATransform : public eoTransform<EOT> 00048 { 00049 public: 00050 00052 eoSGATransform(eoQuadOp<EOT>& _cross, double _cProba, 00053 eoMonOp<EOT>& _mutate, double _mProba) 00054 : cross(_cross), 00055 crossoverProba(_cProba), 00056 mutate(_mutate), 00057 mutationProba(_mProba) {} 00058 00059 00064 void operator()(eoPop<EOT>& _pop) 00065 { 00066 unsigned i; 00067 00068 for (i=0; i<_pop.size()/2; i++) 00069 { 00070 if ( rng.flip(crossoverProba) ) 00071 { 00072 // this crossover generates 2 offspring from two parents 00073 cross(_pop[2*i], _pop[2*i+1]); 00074 } 00075 } 00076 00077 for (i=0; i < _pop.size(); i++) 00078 { 00079 if (rng.flip(mutationProba) ) 00080 { 00081 mutate(_pop[i]); 00082 } 00083 00084 } 00085 }; 00086 00087 private: 00088 eoInvalidateQuadOp<EOT> cross; 00089 double crossoverProba; 00090 eoInvalidateMonOp<EOT> mutate; 00091 double mutationProba; 00092 }; 00093 00102 template<class EOT> class eoDynSGATransform : public eoTransform<EOT> 00103 { 00104 public: 00105 00107 eoDynSGATransform(eoQuadOp<EOT>& _cross, double _cProba, 00108 eoMonOp<EOT>& _mutate, double _mProba) 00109 : cross(_cross), 00110 crossoverProbaHolder(_cProba), crossoverProba(crossoverProbaHolder), 00111 mutate(_mutate), 00112 mutationProbaHolder(_mProba), mutationProba(mutationProbaHolder) {} 00113 00115 // these will usually be some eoValueParam<double>.value() 00116 // hence the ...Holder data will bever be used in this case 00117 eoDynSGATransform(eoQuadOp<EOT>& _cross, double* _cProbaRef, 00118 eoMonOp<EOT>& _mutate, double* _mProbaRef) 00119 : cross(_cross), 00120 crossoverProbaHolder(0), crossoverProba(*_cProbaRef), 00121 mutate(_mutate), 00122 mutationProbaHolder(0), mutationProba(*_mProbaRef) {} 00123 00124 00129 void operator()(eoPop<EOT>& _pop) 00130 { 00131 unsigned i; 00132 00133 for (i=0; i<_pop.size()/2; i++) 00134 { 00135 if ( rng.flip(crossoverProba) ) 00136 { 00137 // this crossover generates 2 offspring from two parents 00138 cross(_pop[2*i], _pop[2*i+1]); 00139 } 00140 } 00141 00142 for (i=0; i < _pop.size(); i++) 00143 { 00144 if (rng.flip(mutationProba) ) 00145 { 00146 mutate(_pop[i]); 00147 } 00148 00149 } 00150 }; 00151 // accessors - mainly for EASEA 00152 double & PCrossHandle() { return crossoverProba;} 00153 double & PMutHandle() { return mutationProba;} 00154 00155 private: 00156 // difference with eoSGATransform: the operator probabilities 00157 // they can be passed by reference or by value. 00158 // hence we need here to use a reference, and to eventually store a value 00159 eoInvalidateQuadOp<EOT> cross; 00160 double crossoverProbaHolder; // the value, used only if ctor gets a value 00161 double& crossoverProba; // the reference, to be used in operator() 00162 eoInvalidateMonOp<EOT> mutate; 00163 double mutationProbaHolder; // the value, used only if ctor gets a value 00164 double& mutationProba; // the reference, to be used in operator() 00165 }; 00166 00167 00168 #endif