# eoTruncatedSelectOne.h

    00001
    00025 //-----------------------------------------------------------------------------
    00026
    00027 #ifndef _eoTruncatedSelectOne_h
    00028 #define _eoTruncatedSelectOne_h
    00029
    00030
    00031 //-----------------------------------------------------------------------------
    00032 #include <eoSelect.h>
    00033 #include <eoSelectOne.h>
    00034 #include <utils/eoHowMany.h>
    00035 #include <math.h>
    00036 //-----------------------------------------------------------------------------
    00037
    00044 template<class EOT>
    00045 class eoTruncatedSelectOne : public eoSelectOne<EOT>
    00046 {
    00047 public:
    00049   eoTruncatedSelectOne(eoSelectOne<EOT>& _select,
    00050                        double  _rateFertile,
    00051                        bool _interpret_as_rateF = true)
    00052     : select(_select),
    00053       howManyFertile(_rateFertile, _interpret_as_rateF),
    00054       tmpPop(), actualPop(tmpPop)
    00055   {}
    00056
    00058   eoTruncatedSelectOne(eoSelectOne<EOT>& _select,
    00059                         eoHowMany _howManyFertile)
    00060     : select(_select), howManyFertile(_howManyFertile),
    00061     tmpPop(), actualPop(tmpPop)
    00062   {}
    00063
    00064
    00066   void setup(const eoPop<EOT>& _source)
    00067   {
    00068     unsigned fertile = howManyFertile(_source.size());
    00069     if (fertile == _source.size())  // noting to do, all are fertile
    00070       {
    00071         actualPop = _source;
    00072       }
    00073     else
    00074       {
    00075         // copy only best ferile to actualPop
    00076         tmpPop.resize(fertile);
    00077         std::vector<const EOT *> result;
    00078         _source.nth_element(fertile, result);
    00079         for (unsigned i=0; i<fertile; i++)
    00080           tmpPop[i] = *result[i];
    00081
    00082         // and just in case
    00083         actualPop = tmpPop;
    00084       }
    00085
    00086     // AND DON'T FORGET to call the embedded select setup method on actualPop
    00087     select.setup(actualPop);
    00088
    00089     return;
    00090   }
    00091
    00098   const EOT& operator()(const eoPop<EOT>& _pop)
    00099   {
    00100     return select(actualPop);
    00101   }
    00102
    00103
    00104 private :
    00105   eoSelectOne<EOT>& select;        // selector for one guy
    00106   eoHowMany howManyFertile;        // number of fertile guys
    00107   eoPop<EOT> tmpPop;               // intermediate population - fertile guys
    00108   eoPop<EOT> & actualPop;          // to avoid copies when 100% fertility
    00109 };
    00110
    00111 #endif