// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- //----------------------------------------------------------------------------- // eoCombinedOp.h // (c) GeNeura Team, 1998, Marc Schoenauer, 2000 /* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact: todos@geneura.ugr.es, http://geneura.ugr.es */ //----------------------------------------------------------------------------- #ifndef _eoCombinedOp_H #define _eoCombinedOp_H #include #include #include #include #include #include /** \defgroup Utilities Utilities Classes that may help you write elegant code. */ /** @defgroup Combination Operators combination Classes that permits to combine several operators in a single one. @ingroup Utilities @{ */ /** @name PropCombined Genetic operators Combination of same-type Genetic Operators. This files contains the classes eoPropCombinedXXXOp (XXX in {Mon, Bin, Quad}) that allow to use more than a single operator of a specific class into an algorithm, chosing by a roulette wheel based on user-defined rates @author Marc Schoenauer @version 0.1 */ ////////////////////////////////////////////////////// //// combined MonOp ////////////////////////////////////////////////////// /** eoMonOp is the monary operator: genetic operator that takes only one EO * now accepts generic operators */ template class eoPropCombinedMonOp: public eoMonOp { public: /// Ctor from a "true" operator eoPropCombinedMonOp(eoMonOp & _first, const double _rate) { ops.push_back(&_first); rates.push_back(_rate); } virtual std::string className() const { return "eoPropCombinedMonOp"; } virtual void add(eoMonOp & _op, const double _rate, bool _verbose=false) { ops.push_back(&_op); rates.push_back(_rate); // compute the relative rates in percent - to warn the user! if (_verbose) printOn( eo::log << eo::logging ); } // outputs the operators and percentages virtual void printOn(std::ostream & _os) { double total = 0; unsigned i; for (i=0; iclassName() << " with rate " << 100*rates[i]/total << " %\n"; } virtual bool operator()(EOT & _indi) { unsigned what = rng.roulette_wheel(rates); // choose one op return (*ops[what])(_indi); // apply it } protected: std::vector*> ops; std::vector rates; }; ////////////////////////////////////////////////////// //// combined BinOp ////////////////////////////////////////////////////// /** COmbined Binary genetic operator: * operator() has two operands, only the first one can be modified */ template class eoPropCombinedBinOp: public eoBinOp { public: /// Ctor eoPropCombinedBinOp(eoBinOp & _first, const double _rate) { ops.push_back(&_first); rates.push_back(_rate); } virtual std::string className() const { return "eoPropCombinedBinOp"; } virtual void add(eoBinOp & _op, const double _rate, bool _verbose=false) { ops.push_back(&_op); rates.push_back(_rate); // compute the relative rates in percent - to warn the user! if (_verbose) { double total = 0; unsigned i; for (i=0; iclassName() << " with rate " << 100*rates[i]/total << " %" << std::endl; } } virtual void operator()(EOT & _indi1, const EOT & _indi2) { unsigned what = rng.roulette_wheel(rates); // choose one op index return (*ops[what])(_indi1, _indi2); // apply it } private: std::vector*> ops; std::vector rates; }; ////////////////////////////////////////////////////// //// combined QuadOp ////////////////////////////////////////////////////// /** Quad genetic operator: subclasses eoOp, and defines basically the operator() with two operands, both can be modified. */ /** Combined quad genetic operator: * operator() has two operands, both can be modified * generic operators are now allowed: there are imbedded into * the corresponding "true" operator */ template class eoPropCombinedQuadOp: public eoQuadOp { public: /// Ctor from a true operator eoPropCombinedQuadOp(eoQuadOp & _first, const double _rate) { ops.push_back(&_first); rates.push_back(_rate); } virtual std::string className() const { return "eoPropCombinedQuadOp"; } virtual void add(eoQuadOp & _op, const double _rate, bool _verbose) { #ifndef DEPRECATED_MESSAGES #pragma message "The use of the verbose parameter in eoPropCombinedQuadOp::add is deprecated and will be removed in the next release." 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; #endif // !DEPRECATED_MESSAGES add(_op,_rate); } // addition of a true operator virtual void add(eoQuadOp & _op, const double _rate) { ops.push_back(&_op); rates.push_back(_rate); // compute the relative rates in percent - to warn the user! printOn( eo::log << eo::logging ); } // outputs the operators and percentages virtual void printOn(std::ostream & _os) { double total = 0; unsigned i; for (i=0; iclassName() << " with rate " << 100*rates[i]/total << " %\n"; } virtual bool operator()(EOT & _indi1, EOT & _indi2) { unsigned what = rng.roulette_wheel(rates); // choose one op index return (*ops[what])(_indi1, _indi2); // apply it } private: std::vector*> ops; std::vector rates; }; // for General Ops, it's another story - see eoOpContainer #endif /** @} */