Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# eoProportionalCombinedOp.h

    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>
    00048
    00049
    00050
    00051
    00057 template <class EOT>
    00058 class eoPropCombinedMonOp: public eoMonOp<EOT>
    00059 {
    00060 public:
    00062   eoPropCombinedMonOp(eoMonOp<EOT> & _first, const double _rate)
    00063   {
    00064     ops.push_back(&_first);
    00065     rates.push_back(_rate);
    00066   }
    00067
    00068   virtual std::string className() const { return "eoPropCombinedMonOp"; }
    00069
    00070   virtual void add(eoMonOp<EOT> & _op, const double _rate, bool _verbose=false)
    00071   {
    00072     ops.push_back(&_op);
    00073     rates.push_back(_rate);
    00074     // compute the relative rates in percent - to warn the user!
    00075     if (_verbose)
    00076       printOn(std::cout);
    00077   }
    00078
    00079   // outputs the operators and percentages
    00080   virtual void printOn(std::ostream & _os)
    00081   {
    00082     double total = 0;
    00083     unsigned i;
    00084     for (i=0; i<ops.size(); i++)
    00085       total += rates[i];
    00086     _os << "In " << className() << "\n" ;
    00087     for (i=0; i<ops.size(); i++)
    00088       _os << ops[i]->className() << " with rate " << 100*rates[i]/total << " %\n";
    00089   }
    00090
    00091   virtual bool operator()(EOT & _indi)
    00092   {
    00093     unsigned what = rng.roulette_wheel(rates); // choose one op
    00094     return (*ops[what])(_indi);            // apply it
    00095   }
    00096 protected:
    00097 std::vector<eoMonOp<EOT>*> ops;
    00098 std::vector<double> rates;
    00099 };
    00100
    00104
    00108 template <class EOT>
    00109 class eoPropCombinedBinOp: public eoBinOp<EOT>
    00110 {
    00111 public:
    00113   eoPropCombinedBinOp(eoBinOp<EOT> & _first, const double _rate)
    00114   {
    00115     ops.push_back(&_first);
    00116     rates.push_back(_rate);
    00117   }
    00118
    00119 virtual std::string className() const { return "eoPropCombinedBinOp"; }
    00120
    00121 virtual void add(eoBinOp<EOT> & _op, const double _rate, bool _verbose=false)
    00122   {
    00123     ops.push_back(&_op);
    00124     rates.push_back(_rate);
    00125     // compute the relative rates in percent - to warn the user!
    00126     if (_verbose)
    00127       {
    00128         double total = 0;
    00129         unsigned i;
    00130         for (i=0; i<ops.size(); i++)
    00131           total += rates[i];
    00132         std::cout << "In " << className() << "\n" ;
    00133         for (i=0; i<ops.size(); i++)
    00134           std::cout << ops[i]->className() << " with rate " << 100*rates[i]/total << " %\n";
    00135       }
    00136   }
    00137
    00138   virtual void operator()(EOT & _indi1, const EOT & _indi2)
    00139   {
    00140     unsigned what = rng.roulette_wheel(rates); // choose one op index
    00141     return (*ops[what])(_indi1, _indi2);                   // apply it
    00142   }
    00143 private:
    00144 std::vector<eoBinOp<EOT>*> ops;
    00145 std::vector<double> rates;
    00146 };
    00147
    00148
    00152
    00162 template <class EOT>
    00163 class eoPropCombinedQuadOp: public eoQuadOp<EOT>
    00164 {
    00165 public:
    00167   eoPropCombinedQuadOp(eoQuadOp<EOT> & _first, const double _rate)
    00168   {
    00169     ops.push_back(&_first);
    00170     rates.push_back(_rate);
    00171   }
    00172
    00173 virtual std::string className() const { return "eoPropCombinedQuadOp"; }
    00174
    00175   // addition of a true operator
    00176 virtual void add(eoQuadOp<EOT> & _op, const double _rate, bool _verbose=false)
    00177   {
    00178     ops.push_back(&_op);
    00179     rates.push_back(_rate);
    00180     // compute the relative rates in percent - to warn the user!
    00181     if (_verbose)
    00182       printOn(std::cout);
    00183   }
    00184
    00185   // outputs the operators and percentages
    00186   virtual void printOn(std::ostream & _os)
    00187   {
    00188     double total = 0;
    00189     unsigned i;
    00190     for (i=0; i<ops.size(); i++)
    00191       total += rates[i];
    00192     _os << "In " << className() << "\n" ;
    00193     for (i=0; i<ops.size(); i++)
    00194       _os << ops[i]->className() << " with rate " << 100*rates[i]/total << " %\n";
    00195   }
    00196
    00197   virtual bool operator()(EOT & _indi1, EOT & _indi2)
    00198   {
    00199     unsigned what = rng.roulette_wheel(rates); // choose one op index
    00200     return (*ops[what])(_indi1, _indi2);                   // apply it
    00201   }
    00202 private:
    00203 std::vector<eoQuadOp<EOT>*> ops;
    00204 std::vector<double> rates;
    00205 };
    00206
    00207
    00208 // for General Ops, it's another story - see eoOpCOntainer
    00209 #endif
    00210