# eoRanking.h

    00001
    00025 //-----------------------------------------------------------------------------
    00026
    00027 #ifndef eoRanking_h
    00028 #define eoRanking_h
    00029
    00030 #include <eoPerf2Worth.h>
    00031
    00037 template <class EOT>
    00038 class eoRanking : public eoPerf2Worth<EOT> // false: do not cache fitness
    00039 {
    00040 public:
    00041
    00042     using eoPerf2Worth<EOT>::value;
    00043
    00044   /* Ctor:
    00045    @param _p selective pressure (in (1,2]
    00046    @param _e exponent (1 == linear)
    00047   */
    00048   eoRanking(double _p=2.0, double _e=1.0):
    00049     pressure(_p), exponent(_e) {}
    00050
    00051   /* helper function: finds index in _pop of _eo, an EOT * */
    00052   int lookfor(const EOT *_eo, const eoPop<EOT>& _pop)
    00053     {
    00054       typename eoPop<EOT>::const_iterator it;
    00055       for (it=_pop.begin(); it<_pop.end(); it++)
    00056         {
    00057           if (_eo == &(*it))
    00058             return it-_pop.begin();
    00059         }
    00060       throw std::runtime_error("Not found in eoLinearRanking");
    00061     }
    00062
    00063   /* COmputes the ranked fitness: fitnesses range in [m,M]
    00064      with m=2-pressure/popSize and M=pressure/popSize.
    00065      in between, the progression depstd::ends on exponent (linear if 1).
    00066    */
    00067   virtual void operator()(const eoPop<EOT>& _pop)
    00068     {
    00069       std::vector<const EOT *> rank;
    00070       _pop.sort(rank);
    00071       unsigned pSize =_pop.size();
    00072       unsigned int pSizeMinusOne = pSize-1;
    00073
    00074       if (pSize <= 1)
    00075         throw std::runtime_error("Cannot do ranking with population of size <= 1");
    00076
    00077       // value() refers to the std::vector of worthes (we're in an eoParamvalue)
    00078       value().resize(pSize);
    00079
    00080       double beta = (2-pressure)/pSize;
    00081       if (exponent == 1.0)         // no need for exponetial then
    00082         {
    00083           double alpha = (2*pressure-2)/(pSize*pSizeMinusOne);
    00084           for (unsigned i=0; i<pSize; i++)
    00085             {
    00086               int which = lookfor(rank[i], _pop);
    00087               value()[which] = alpha*(pSize-i)+beta; // worst -> 1/[P(P-1)/2]
    00088             }
    00089         }
    00090       else                                 // exponent != 1
    00091         {
    00092           double gamma = (2*pressure-2)/pSize;
    00093           for (unsigned i=0; i<pSize; i++)
    00094             {
    00095               int which = lookfor(rank[i], _pop);
    00096               // value in in [0,1]
    00097               double tmp = ((double)(pSize-i))/pSize;
    00098               // to the exponent, and back to [m,M]
    00099               value()[which] = gamma*pow(tmp, exponent)+beta;
    00100             }
    00101         }
    00102     }
    00103  private:
    00104   double pressure;      // selective pressure
    00105   double exponent;
    00106 };
    00107
    00108
    00109
    00110 #endif