EvolvingObjects
|
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 //----------------------------------------------------------------------------- 00003 // eoCombinedOp.h 00004 // (c) GeNeura Team, 1998, Marc Schoenauer, 2000 00005 /* 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 00020 Contact: todos@geneura.ugr.es, http://geneura.ugr.es 00021 */ 00022 //----------------------------------------------------------------------------- 00023 00024 #ifndef _eoCombinedOp_H 00025 #define _eoCombinedOp_H 00026 00027 #include <eoObject.h> 00028 #include <eoPrintable.h> 00029 #include <eoFunctor.h> 00030 #include <eoOp.h> 00031 #include <utils/eoRNG.h> 00032 #include <utils/eoLogger.h> 00033 00062 00063 00064 00065 00071 template <class EOT> 00072 class eoPropCombinedMonOp: public eoMonOp<EOT> 00073 { 00074 public: 00076 eoPropCombinedMonOp(eoMonOp<EOT> & _first, const double _rate) 00077 { 00078 ops.push_back(&_first); 00079 rates.push_back(_rate); 00080 } 00081 00082 virtual std::string className() const { return "eoPropCombinedMonOp"; } 00083 00084 virtual void add(eoMonOp<EOT> & _op, const double _rate, bool _verbose=false) 00085 { 00086 ops.push_back(&_op); 00087 rates.push_back(_rate); 00088 // compute the relative rates in percent - to warn the user! 00089 if (_verbose) 00090 printOn( eo::log << eo::logging ); 00091 } 00092 00093 // outputs the operators and percentages 00094 virtual void printOn(std::ostream & _os) 00095 { 00096 double total = 0; 00097 unsigned i; 00098 for (i=0; i<ops.size(); i++) 00099 total += rates[i]; 00100 _os << "In " << className() << "\n" ; 00101 for (i=0; i<ops.size(); i++) 00102 _os << ops[i]->className() << " with rate " << 100*rates[i]/total << " %\n"; 00103 } 00104 00105 virtual bool operator()(EOT & _indi) 00106 { 00107 unsigned what = rng.roulette_wheel(rates); // choose one op 00108 return (*ops[what])(_indi); // apply it 00109 } 00110 protected: 00111 std::vector<eoMonOp<EOT>*> ops; 00112 std::vector<double> rates; 00113 }; 00114 00118 00122 template <class EOT> 00123 class eoPropCombinedBinOp: public eoBinOp<EOT> 00124 { 00125 public: 00127 eoPropCombinedBinOp(eoBinOp<EOT> & _first, const double _rate) 00128 { 00129 ops.push_back(&_first); 00130 rates.push_back(_rate); 00131 } 00132 00133 virtual std::string className() const { return "eoPropCombinedBinOp"; } 00134 00135 virtual void add(eoBinOp<EOT> & _op, const double _rate, bool _verbose=false) 00136 { 00137 ops.push_back(&_op); 00138 rates.push_back(_rate); 00139 // compute the relative rates in percent - to warn the user! 00140 if (_verbose) 00141 { 00142 double total = 0; 00143 unsigned i; 00144 for (i=0; i<ops.size(); i++) 00145 total += rates[i]; 00146 eo::log << eo::logging << "In " << className() << std::endl ; 00147 for (i=0; i<ops.size(); i++) 00148 eo::log << eo::logging << ops[i]->className() << " with rate " << 100*rates[i]/total << " %" << std::endl; 00149 } 00150 } 00151 00152 virtual void operator()(EOT & _indi1, const EOT & _indi2) 00153 { 00154 unsigned what = rng.roulette_wheel(rates); // choose one op index 00155 return (*ops[what])(_indi1, _indi2); // apply it 00156 } 00157 private: 00158 std::vector<eoBinOp<EOT>*> ops; 00159 std::vector<double> rates; 00160 }; 00161 00162 00166 00176 template <class EOT> 00177 class eoPropCombinedQuadOp: public eoQuadOp<EOT> 00178 { 00179 public: 00181 eoPropCombinedQuadOp(eoQuadOp<EOT> & _first, const double _rate) 00182 { 00183 ops.push_back(&_first); 00184 rates.push_back(_rate); 00185 } 00186 00187 virtual std::string className() const { return "eoPropCombinedQuadOp"; } 00188 00189 /* FIXME remove in next release 00190 virtual void add(eoQuadOp<EOT> & _op, const double _rate, bool _verbose) 00191 { 00192 #ifndef DEPRECATED_MESSAGES 00193 #pragma message "The use of the verbose parameter in eoPropCombinedQuadOp::add is deprecated and will be removed in the next release." 00194 eo::log << eo::warnings << "WARNING: the use of the verbose parameter in eoPropCombinedQuadOp::add is deprecated and will be removed in the next release." << std::endl; 00195 #endif // !DEPRECATED_MESSAGES 00196 00197 add(_op,_rate); 00198 } 00199 */ 00200 00201 // addition of a true operator 00202 virtual void add(eoQuadOp<EOT> & _op, const double _rate) 00203 { 00204 ops.push_back(&_op); 00205 rates.push_back(_rate); 00206 // compute the relative rates in percent - to warn the user! 00207 printOn( eo::log << eo::logging ); 00208 } 00209 00210 // outputs the operators and percentages 00211 virtual void printOn(std::ostream & _os) 00212 { 00213 double total = 0; 00214 unsigned i; 00215 for (i=0; i<ops.size(); i++) 00216 total += rates[i]; 00217 _os << "In " << className() << "\n" ; 00218 for (i=0; i<ops.size(); i++) 00219 _os << ops[i]->className() << " with rate " << 100*rates[i]/total << " %\n"; 00220 } 00221 00222 virtual bool operator()(EOT & _indi1, EOT & _indi2) 00223 { 00224 unsigned what = rng.roulette_wheel(rates); // choose one op index 00225 return (*ops[what])(_indi1, _indi2); // apply it 00226 } 00227 private: 00228 std::vector<eoQuadOp<EOT>*> ops; 00229 std::vector<double> rates; 00230 }; 00231 00232 00233 // for General Ops, it's another story - see eoOpContainer 00234 #endif 00235