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