Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# eoSharing.h

    00001
    00024 //-----------------------------------------------------------------------------
    00025
    00026 #ifndef eoSharing_h
    00027 #define eoSharing_h
    00028
    00029 #include <eoPerf2Worth.h>
    00030 #include <utils/eoDistance.h>
    00031
    00037   class dMatrix : public std::vector<double>
    00038   {
    00039   public:
    00040     // Ctor : sets size
    00041     dMatrix(unsigned _s) : std::vector<double>(_s*_s), rSize(_s) {}
    00042
    00044     double operator()(unsigned _i, unsigned _j) const
    00045     {
    00046       return this->operator[](_i*rSize + _j);
    00047     }
    00048
    00050     double & operator()(unsigned _i, unsigned _j)
    00051     {
    00052       return this->operator[](_i*rSize + _j);
    00053     }
    00054
    00056     void printOn(std::ostream & _os)
    00057     {
    00058       unsigned index=0;
    00059       for (unsigned i=0; i<rSize; i++)
    00060         {
    00061           for (unsigned j=0; j<rSize; j++)
    00062             _os << this->operator[](index++) << " " ;
    00063           _os << std::endl;
    00064         }
    00065       _os << std::endl;
    00066     }
    00067
    00068     private:
    00069       unsigned rSize;              // row size (== number of columns!)
    00070   };
    00071
    00072
    00078 template <class EOT>
    00079 class eoSharing : public eoPerf2Worth<EOT>
    00080 {
    00081 public:
    00082
    00083     using eoPerf2Worth<EOT>::value;
    00084
    00085
    00086   /* Ctor requires a distance - cannot have a default distance! */
    00087   eoSharing(double _nicheSize, eoDistance<EOT> & _dist) : eoPerf2Worth<EOT>("Sharing"),
    00088                                              nicheSize(_nicheSize),
    00089                                              dist(_dist)
    00090   {}
    00091
    00094     void operator()(const eoPop<EOT>& _pop)
    00095     {
    00096       unsigned i, j,
    00097         pSize=_pop.size();
    00098       if (pSize <= 1)
    00099         throw std::runtime_error("Apptempt to do sharing with population of size 1");
    00100       value().resize(pSize);
    00101       std::vector<double> sim(pSize);      // to hold the similarities
    00102       dMatrix distMatrix(pSize*(pSize-1)/2); // to hold the distances
    00103
    00104       // compute the similarities (wrong name for distMatrix, I know)
    00105       distMatrix(0,0)=1;
    00106       for (i=1; i<pSize; i++)
    00107         {
    00108           distMatrix(i,i)=1;
    00109           for (j=0; j<i; j++)
    00110             {
    00111               double d =  dist(_pop[i], _pop[j]);
    00112               distMatrix(i,j) =
    00113                 distMatrix(j,i) = ( d>nicheSize ? 0 : 1-(d/nicheSize) );
    00114             }
    00115         }
    00116
    00117       for (i=0; i<pSize; i++)
    00118         {
    00119           double sum=0.0;
    00120           for (j=0; j<pSize; j++)
    00121             sum += distMatrix(i,j);
    00122           sim[i] = sum;
    00123         }
    00124
    00125       // now set the worthes values
    00126       for (i = 0; i < _pop.size(); ++i)
    00127         value()[i]=_pop[i].fitness()/sim[i];
    00128     }
    00129     // private data of class eoSharing
    00130 private:
    00131   double nicheSize;
    00132   eoDistance<EOT> & dist;            // specific distance
    00133 };
    00134
    00135
    00136
    00137 #endif