EvolvingObjects
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 
00045 template<class EOT>
00046 class eoTruncatedSelectOne : public eoSelectOne<EOT>
00047 {
00048 public:
00050   eoTruncatedSelectOne(eoSelectOne<EOT>& _select,
00051                        double  _rateFertile,
00052                        bool _interpret_as_rateF = true)
00053     : select(_select),
00054       howManyFertile(_rateFertile, _interpret_as_rateF),
00055       tmpPop(), actualPop(tmpPop)
00056   {}
00057 
00059   eoTruncatedSelectOne(eoSelectOne<EOT>& _select,
00060                         eoHowMany _howManyFertile)
00061     : select(_select), howManyFertile(_howManyFertile),
00062     tmpPop(), actualPop(tmpPop)
00063   {}
00064 
00065 
00067   void setup(const eoPop<EOT>& _source)
00068   {
00069     unsigned fertile = howManyFertile(_source.size());
00070     if (fertile == _source.size())  // noting to do, all are fertile
00071       {
00072         actualPop = _source;
00073       }
00074     else
00075       {
00076         // copy only best ferile to actualPop
00077         tmpPop.resize(fertile);
00078         std::vector<const EOT *> result;
00079         _source.nth_element(fertile, result);
00080         for (unsigned i=0; i<fertile; i++)
00081           tmpPop[i] = *result[i];
00082 
00083         // and just in case
00084         actualPop = tmpPop;
00085       }
00086 
00087     // AND DON'T FORGET to call the embedded select setup method on actualPop
00088     select.setup(actualPop);
00089 
00090     return;
00091   }
00092 
00099   const EOT& operator()(const eoPop<EOT>& _pop)
00100   {
00101     return select(actualPop);
00102   }
00103 
00104 
00105 private :
00106   eoSelectOne<EOT>& select;        // selector for one guy
00107   eoHowMany howManyFertile;        // number of fertile guys
00108   eoPop<EOT> tmpPop;               // intermediate population - fertile guys
00109   eoPop<EOT> & actualPop;          // to avoid copies when 100% fertility
00110 };
00111 
00112 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends