moeoEntropyMetric.h

00001 /*
00002 * <moeoEntropyMetric.h>
00003 * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
00004 * (C) OPAC Team, LIFL, 2002-2007
00005 *
00006 * Arnaud Liefooghe
00007 *
00008 * This software is governed by the CeCILL license under French law and
00009 * abiding by the rules of distribution of free software.  You can  use,
00010 * modify and/ or redistribute the software under the terms of the CeCILL
00011 * license as circulated by CEA, CNRS and INRIA at the following URL
00012 * "http://www.cecill.info".
00013 *
00014 * As a counterpart to the access to the source code and  rights to copy,
00015 * modify and redistribute granted by the license, users are provided only
00016 * with a limited warranty  and the software's author,  the holder of the
00017 * economic rights,  and the successive licensors  have only  limited liability.
00018 *
00019 * In this respect, the user's attention is drawn to the risks associated
00020 * with loading,  using,  modifying and/or developing or reproducing the
00021 * software by the user in light of its specific status of free software,
00022 * that may mean  that it is complicated to manipulate,  and  that  also
00023 * therefore means  that it is reserved for developers  and  experienced
00024 * professionals having in-depth computer knowledge. Users are therefore
00025 * encouraged to load and test the software's suitability as regards their
00026 * requirements in conditions enabling the security of their systems and/or
00027 * data to be ensured and,  more generally, to use and operate it in the
00028 * same conditions as regards security.
00029 * The fact that you are presently reading this means that you have had
00030 * knowledge of the CeCILL license and that you accept its terms.
00031 *
00032 * ParadisEO WebSite : http://paradiseo.gforge.inria.fr
00033 * Contact: paradiseo-help@lists.gforge.inria.fr
00034 *
00035 */
00036 //-----------------------------------------------------------------------------
00037 
00038 #ifndef MOEOENTROPYMETRIC_H_
00039 #define MOEOENTROPYMETRIC_H_
00040 
00041 #include <vector>
00042 #include <comparator/moeoParetoObjectiveVectorComparator.h>
00043 #include <metric/moeoMetric.h>
00044 
00049 template < class ObjectiveVector >
00050 class moeoEntropyMetric : public moeoVectorVsVectorBinaryMetric < ObjectiveVector, double >
00051   {
00052   public:
00053 
00059     double operator()(const std::vector < ObjectiveVector > & _set1, const std::vector < ObjectiveVector > & _set2)
00060     {
00061       // normalization
00062       std::vector< ObjectiveVector > set1 = _set1;
00063       std::vector< ObjectiveVector > set2= _set2;
00064       removeDominated (set1);
00065       removeDominated (set2);
00066       prenormalize (set1);
00067       normalize (set1);
00068       normalize (set2);
00069 
00070       // making of PO*
00071       std::vector< ObjectiveVector > star; // rotf :-)
00072       computeUnion (set1, set2, star);
00073       removeDominated (star);
00074 
00075       // making of PO1 U PO*
00076       std::vector< ObjectiveVector > union_set1_star; // rotf again ...
00077       computeUnion (set1, star, union_set1_star);
00078 
00079       unsigned int C = union_set1_star.size();
00080       float omega=0;
00081       float entropy=0;
00082 
00083       for (unsigned int i=0 ; i<C ; i++)
00084         {
00085           unsigned int N_i = howManyInNicheOf (union_set1_star, union_set1_star[i], star.size());
00086           unsigned int n_i = howManyInNicheOf (set1, union_set1_star[i], star.size());
00087           if (n_i > 0)
00088             {
00089               omega += 1.0 / N_i;
00090               entropy += (float) n_i / (N_i * C) * log (((float) n_i / C) / log (2.0));
00091             }
00092         }
00093       entropy /= - log (omega);
00094       entropy *= log (2.0);
00095       return entropy;
00096     }
00097 
00098 
00099   private:
00100 
00102     std::vector<double> vect_min_val;
00104     std::vector<double> vect_max_val;
00106     moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator;
00107 
00108 
00113     void removeDominated(std::vector < ObjectiveVector > & _f)
00114     {
00115       for (unsigned int i=0 ; i<_f.size(); i++)
00116         {
00117           bool dom = false;
00118           for (unsigned int j=0; j<_f.size(); j++)
00119             if (i != j && paretoComparator(_f[i],_f[j]))
00120               {
00121                 dom = true;
00122                 break;
00123               }
00124           if (dom)
00125             {
00126               _f[i] = _f.back();
00127               _f.pop_back();
00128               i--;
00129             }
00130         }
00131     }
00132 
00133 
00138     void prenormalize (const std::vector< ObjectiveVector > & _f)
00139     {
00140       vect_min_val.clear();
00141       vect_max_val.clear();
00142 
00143       for (unsigned int i=0 ; i<ObjectiveVector::nObjectives(); i++)
00144         {
00145           float min_val = _f.front()[i], max_val = min_val;
00146           for (unsigned int j=1 ; j<_f.size(); j++)
00147             {
00148               if (_f[j][i] < min_val)
00149                 min_val = _f[j][i];
00150               if (_f[j][i]>max_val)
00151                 max_val = _f[j][i];
00152             }
00153           vect_min_val.push_back(min_val);
00154           vect_max_val.push_back (max_val);
00155         }
00156     }
00157 
00158 
00163     void normalize (std::vector< ObjectiveVector > & _f)
00164     {
00165       for (unsigned int i=0 ; i<ObjectiveVector::nObjectives(); i++)
00166         for (unsigned int j=0; j<_f.size(); j++)
00167           _f[j][i] = (_f[j][i] - vect_min_val[i]) / (vect_max_val[i] - vect_min_val[i]);
00168     }
00169 
00170 
00177     void computeUnion(const std::vector< ObjectiveVector > & _f1, const std::vector< ObjectiveVector > & _f2, std::vector< ObjectiveVector > & _f)
00178     {
00179       _f = _f1 ;
00180       for (unsigned int i=0; i<_f2.size(); i++)
00181         {
00182           bool b = false;
00183           for (unsigned int j=0; j<_f1.size(); j ++)
00184             if (_f1[j] == _f2[i])
00185               {
00186                 b = true;
00187                 break;
00188               }
00189           if (! b)
00190             _f.push_back(_f2[i]);
00191         }
00192     }
00193 
00194 
00198     unsigned int howManyInNicheOf (const std::vector< ObjectiveVector > & _f, const ObjectiveVector & _s, unsigned int _size)
00199     {
00200       unsigned int n=0;
00201       for (unsigned int i=0 ; i<_f.size(); i++)
00202         {
00203           if (euclidianDistance(_f[i], _s) < (_s.size() / (double) _size))
00204             n++;
00205         }
00206       return n;
00207     }
00208 
00209 
00213     double euclidianDistance (const ObjectiveVector & _set1, const ObjectiveVector & _to, unsigned int _deg = 2)
00214     {
00215       double dist=0;
00216       for (unsigned int i=0; i<_set1.size(); i++)
00217         dist += pow(fabs(_set1[i] - _to[i]), (int)_deg);
00218       return pow(dist, 1.0 / _deg);
00219     }
00220 
00221   };
00222 
00223 #endif /*MOEOENTROPYMETRIC_H_*/

Generated on Thu Mar 13 15:54:07 2008 for ParadisEO-MOEO-MultiObjectiveEvolvingObjects by  doxygen 1.4.7