- Main Page
- Classes
- Files

- Search for |

# moeoEntropyMetric.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // moeoEntropyMetric.h
    00005 // (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
    00006 /*
    00007     This library...
    00008
    00009     Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
    00010  */
    00011 //-----------------------------------------------------------------------------
    00012
    00013 #ifndef MOEOENTROPYMETRIC_H_
    00014 #define MOEOENTROPYMETRIC_H_
    00015
    00016 #include <metric/moeoMetric.h>
    00017
    00022 template < class ObjectiveVector >
    00023 class moeoEntropyMetric : public moeoVectorVsVectorBinaryMetric < ObjectiveVector, double >
    00024 {
    00025 public:
    00026
    00032     double operator()(const std::vector < ObjectiveVector > & _set1, const std::vector < ObjectiveVector > & _set2) {
    00033         // normalization
    00034         std::vector< ObjectiveVector > set1 = _set1;
    00035         std::vector< ObjectiveVector > set2= _set2;
    00036         removeDominated (set1);
    00037         removeDominated (set2);
    00038         prenormalize (set1);
    00039         normalize (set1);
    00040         normalize (set2);
    00041
    00042         // making of PO*
    00043         std::vector< ObjectiveVector > star; // rotf :-)
    00044         computeUnion (set1, set2, star);
    00045         removeDominated (star);
    00046
    00047         // making of PO1 U PO*
    00048         std::vector< ObjectiveVector > union_set1_star; // rotf again ...
    00049         computeUnion (set1, star, union_set1_star);
    00050
    00051         unsigned C = union_set1_star.size();
    00052         float omega=0;
    00053         float entropy=0;
    00054
    00055         for (unsigned i=0 ; i<C ; i++) {
    00056             unsigned N_i = howManyInNicheOf (union_set1_star, union_set1_star[i], star.size());
    00057             unsigned n_i = howManyInNicheOf (set1, union_set1_star[i], star.size());
    00058             if (n_i > 0) {
    00059                 omega += 1.0 / N_i;
    00060                 entropy += (float) n_i / (N_i * C) * log (((float) n_i / C) / log (2.0));
    00061             }
    00062         }
    00063         entropy /= - log (omega);
    00064         entropy *= log (2.0);
    00065         return entropy;
    00066     }
    00067
    00068
    00069 private:
    00070
    00072     std::vector<double> vect_min_val;
    00074     std::vector<double> vect_max_val;
    00075
    00076
    00081     void removeDominated(std::vector < ObjectiveVector > & _f) {
    00082         for (unsigned i=0 ; i<_f.size(); i++) {
    00083             bool dom = false;
    00084             for (unsigned j=0; j<_f.size(); j++)
    00085                 if (i != j && _f[j].dominates(_f[i])) {
    00086                     dom = true;
    00087                     break;
    00088                 }
    00089             if (dom) {
    00090                 _f[i] = _f.back();
    00091                 _f.pop_back();
    00092                 i--;
    00093             }
    00094         }
    00095     }
    00096
    00097
    00102     void prenormalize (const std::vector< ObjectiveVector > & _f) {
    00103         vect_min_val.clear();
    00104         vect_max_val.clear();
    00105
    00106         for (unsigned char i=0 ; i<ObjectiveVector::nObjectives(); i++) {
    00107             float min_val = _f.front()[i], max_val = min_val;
    00108             for (unsigned j=1 ; j<_f.size(); j++) {
    00109                 if (_f[j][i] < min_val)
    00110                     min_val = _f[j][i];
    00111                 if (_f[j][i]>max_val)
    00112                     max_val = _f[j][i];
    00113             }
    00114             vect_min_val.push_back(min_val);
    00115             vect_max_val.push_back (max_val);
    00116         }
    00117     }
    00118
    00119
    00124     void normalize (std::vector< ObjectiveVector > & _f) {
    00125         for (unsigned i=0 ; i<ObjectiveVector::nObjectives(); i++)
    00126             for (unsigned j=0; j<_f.size(); j++)
    00127                 _f[j][i] = (_f[j][i] - vect_min_val[i]) / (vect_max_val[i] - vect_min_val[i]);
    00128     }
    00129
    00130
    00137     void computeUnion(const std::vector< ObjectiveVector > & _f1, const std::vector< ObjectiveVector > & _f2, std::vector< ObjectiveVector > & _f) {
    00138         _f = _f1 ;
    00139         for (unsigned i=0; i<_f2.size(); i++) {
    00140             bool b = false;
    00141             for (unsigned j=0; j<_f1.size(); j ++)
    00142                 if (_f1[j] == _f2[i]) {
    00143                     b = true;
    00144                     break;
    00145                 }
    00146             if (! b)
    00147                 _f.push_back(_f2[i]);
    00148         }
    00149     }
    00150
    00151
    00155     unsigned howManyInNicheOf (const std::vector< ObjectiveVector > & _f, const ObjectiveVector & _s, unsigned _size) {
    00156         unsigned n=0;
    00157         for (unsigned i=0 ; i<_f.size(); i++) {
    00158             if (euclidianDistance(_f[i], _s) < (_s.size() / (double) _size))
    00159                 n++;
    00160         }
    00161         return n;
    00162     }
    00163
    00164
    00168     double euclidianDistance (const ObjectiveVector & _set1, const ObjectiveVector & _to, unsigned _deg = 2) {
    00169         double dist=0;
    00170         for (unsigned i=0; i<_set1.size(); i++)
    00171             dist += pow(fabs(_set1[i] - _to[i]), (int)_deg);
    00172         return pow(dist, 1.0 / _deg);
    00173     }
    00174
    00175 };
    00176
    00177 #endif /*MOEOENTROPYMETRIC_H_*/