eoTruncatedSelectMany.h

00001 
00025 //-----------------------------------------------------------------------------
00026 
00027 #ifndef _eoTruncatedSelectMany_h
00028 #define _eoTruncatedSelectMany_h
00029 
00030 
00031 //-----------------------------------------------------------------------------
00032 #include <eoSelect.h>
00033 #include <eoSelectOne.h>
00034 #include <utils/eoHowMany.h>
00035 #include <math.h>
00036 //-----------------------------------------------------------------------------
00037 
00052 template<class EOT>
00053 class eoTruncatedSelectMany : public eoSelect<EOT>
00054 {
00055  public:
00057      eoTruncatedSelectMany(eoSelectOne<EOT>& _select, 
00058                   double  _rateGenitors, double  _rateFertile, 
00059                   bool _interpret_as_rateG = true, 
00060                   bool _interpret_as_rateF = true)
00061          : select(_select), 
00062            howManyGenitors(_rateGenitors, _interpret_as_rateG),
00063            howManyFertile(_rateFertile, _interpret_as_rateF) {}
00064 
00065      // Ctor with eoHowManys
00066      eoTruncatedSelectMany(eoSelectOne<EOT>& _select, 
00067                   eoHowMany _howManyGenitors, eoHowMany _howManyFertile) 
00068          : select(_select), howManyGenitors(_howManyGenitors),
00069            howManyFertile(_howManyFertile) {}
00070 
00077   virtual void operator()(const eoPop<EOT>& _source, eoPop<EOT>& _dest)
00078   {
00079     unsigned target = howManyGenitors(_source.size());
00080 
00081     _dest.resize(target);
00082 
00083     unsigned nbFertile = howManyFertile(_source.size());
00084 
00085     //revert to standard selection (see eoSelectMany) if no truncation
00086     if (nbFertile == _source.size())
00087       {
00088         select.setup(_source);
00089         
00090         for (size_t i = 0; i < _dest.size(); ++i)
00091           _dest[i] = select(_source);
00092       }
00093     else
00094       {
00095     // at the moment, brute force (rush rush, no good)
00096     // what we would need otherwise is a std::vector<EOT &> class
00097     // and selectors that act on such a thing
00098         eoPop<EOT> tmpPop = _source; // hum hum, could be a pain in the ass
00099 
00100         tmpPop.sort();             // maybe we could only do partial sort?
00101         tmpPop.resize(nbFertile);  // only the best guys here now
00102         tmpPop.shuffle();          // as some selectors are order-sensitive
00103 
00104         select.setup(tmpPop);
00105         
00106         for (size_t i = 0; i < _dest.size(); ++i)
00107           _dest[i] = select(tmpPop);
00108       }
00109   }
00110   
00111 private :
00112   eoSelectOne<EOT>& select;        // selector for one guy
00113   eoHowMany howManyGenitors;       // number of guys to select
00114   eoHowMany howManyFertile;        // number of fertile guys
00115 };
00116 
00117 #endif

Generated on Thu Oct 19 05:06:39 2006 for EO by  doxygen 1.3.9.1