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 * Sebastien Cahon, 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         // normalization
00061         std::vector< ObjectiveVector > set1 = _set1;
00062         std::vector< ObjectiveVector > set2= _set2;
00063         removeDominated (set1);
00064         removeDominated (set2);
00065         prenormalize (set1);
00066         normalize (set1);
00067         normalize (set2);
00068 
00069         // making of PO*
00070         std::vector< ObjectiveVector > star; // rotf :-)
00071         computeUnion (set1, set2, star);
00072         removeDominated (star);
00073 
00074         // making of PO1 U PO*
00075         std::vector< ObjectiveVector > union_set1_star; // rotf again ...
00076         computeUnion (set1, star, union_set1_star);
00077 
00078         unsigned int C = union_set1_star.size();
00079         float omega=0;
00080         float entropy=0;
00081 
00082         for (unsigned int i=0 ; i<C ; i++) {
00083             unsigned int N_i = howManyInNicheOf (union_set1_star, union_set1_star[i], star.size());
00084             unsigned int n_i = howManyInNicheOf (set1, union_set1_star[i], star.size());
00085             if (n_i > 0) {
00086                 omega += 1.0 / N_i;
00087                 entropy += (float) n_i / (N_i * C) * log (((float) n_i / C) / log (2.0));
00088             }
00089         }
00090         entropy /= - log (omega);
00091         entropy *= log (2.0);
00092         return entropy;
00093     }
00094 
00095 
00096 private:
00097 
00099     std::vector<double> vect_min_val;
00101     std::vector<double> vect_max_val;
00103     moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator;
00104 
00105 
00110     void removeDominated(std::vector < ObjectiveVector > & _f) {
00111         for (unsigned int i=0 ; i<_f.size(); i++) {
00112             bool dom = false;
00113             for (unsigned int j=0; j<_f.size(); j++)
00114                 if (i != j && paretoComparator(_f[i],_f[j]))
00115                 {
00116                     dom = true;
00117                     break;
00118                 }
00119             if (dom) {
00120                 _f[i] = _f.back();
00121                 _f.pop_back();
00122                 i--;
00123             }
00124         }
00125     }
00126 
00127 
00132     void prenormalize (const std::vector< ObjectiveVector > & _f) {
00133         vect_min_val.clear();
00134         vect_max_val.clear();
00135 
00136         for (unsigned int i=0 ; i<ObjectiveVector::nObjectives(); i++) {
00137             float min_val = _f.front()[i], max_val = min_val;
00138             for (unsigned int j=1 ; j<_f.size(); j++) {
00139                 if (_f[j][i] < min_val)
00140                     min_val = _f[j][i];
00141                 if (_f[j][i]>max_val)
00142                     max_val = _f[j][i];
00143             }
00144             vect_min_val.push_back(min_val);
00145             vect_max_val.push_back (max_val);
00146         }
00147     }
00148 
00149 
00154     void normalize (std::vector< ObjectiveVector > & _f) {
00155         for (unsigned int i=0 ; i<ObjectiveVector::nObjectives(); i++)
00156             for (unsigned int j=0; j<_f.size(); j++)
00157                 _f[j][i] = (_f[j][i] - vect_min_val[i]) / (vect_max_val[i] - vect_min_val[i]);
00158     }
00159 
00160 
00167     void computeUnion(const std::vector< ObjectiveVector > & _f1, const std::vector< ObjectiveVector > & _f2, std::vector< ObjectiveVector > & _f) {
00168         _f = _f1 ;
00169         for (unsigned int i=0; i<_f2.size(); i++) {
00170             bool b = false;
00171             for (unsigned int j=0; j<_f1.size(); j ++)
00172                 if (_f1[j] == _f2[i]) {
00173                     b = true;
00174                     break;
00175                 }
00176             if (! b)
00177                 _f.push_back(_f2[i]);
00178         }
00179     }
00180 
00181 
00185     unsigned int howManyInNicheOf (const std::vector< ObjectiveVector > & _f, const ObjectiveVector & _s, unsigned int _size) {
00186         unsigned int n=0;
00187         for (unsigned int i=0 ; i<_f.size(); i++) {
00188             if (euclidianDistance(_f[i], _s) < (_s.size() / (double) _size))
00189                 n++;
00190         }
00191         return n;
00192     }
00193 
00194 
00198     double euclidianDistance (const ObjectiveVector & _set1, const ObjectiveVector & _to, unsigned int _deg = 2) {
00199         double dist=0;
00200         for (unsigned int i=0; i<_set1.size(); i++)
00201             dist += pow(fabs(_set1[i] - _to[i]), (int)_deg);
00202         return pow(dist, 1.0 / _deg);
00203     }
00204 
00205 };
00206 
00207 #endif /*MOEOENTROPYMETRIC_H_*/

Generated on Fri Oct 12 15:16:04 2007 for ParadisEO-MOEO:MultiObjectiveEvolvingObjects by  doxygen 1.4.7