moeoParetoSharing.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // moeoParetoSharing.h
00005 // (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2006
00006 /*
00007     This library...
00008 
00009     Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
00010  */
00011 //-----------------------------------------------------------------------------
00012 
00013 #include <EO.h>
00014 #include <eoPerf2Worth.h>
00015 #include <old/moeoParetoPhenDist.h>
00016 #include <eoParetoRanking.h>
00017 
00018 template < class EOT, class worthT =
00019   double >class moeoParetoSharing:public eoPerf2Worth < EOT, worthT >
00020 {
00021 public:
00022 
00023   moeoParetoSharing (double _nicheSize):eoPerf2Worth < EOT,
00024     worthT > ("ParetoSharing"), nicheSize (_nicheSize), dist (euc_dist),
00025     Dmax (_nicheSize)
00026   {
00027   }
00028 
00029 
00030   moeoParetoSharing (double _nicheSize, moeoParetoPhenDist < EOT,
00031                      worthT > &_dist):eoPerf2Worth < EOT,
00032     worthT > ("ParetoSharing"), nicheSize (_nicheSize), dist (_dist),
00033     Dmax (_nicheSize)
00034   {
00035   }
00036 
00037 
00038 
00039 
00040   void operator  () /*calculate_worths */ (const eoPop < EOT > &_pop)
00041   {
00042 
00043     unsigned i, j, pSize = _pop.size ();
00044     //cout<<"**************************************************************************************\n";
00045     // std :: cout << "psize = " << pSize << std :: endl ;
00046     if (pSize <= 1)
00047       throw std::
00048         runtime_error ("Apptempt to do sharing with population of size 1");
00049     eoPerf2Worth < EOT, worthT >::value ().resize (pSize);
00050     std::vector < double >sim (pSize);  // to hold the similarities
00051 
00052     dMatrix distMatrix (pSize);
00053 
00054 // compute the distance
00055     distMatrix[0][0] = 0;
00056     for (i = 1; i < pSize; i++)
00057       {
00058         distMatrix[i][i] = 0;
00059         for (j = 0; j < i; j++)
00060           {
00061             //if
00062             distMatrix[i][j] = distMatrix[j][i] = dist (_pop[i], _pop[j]);
00063             //cout<<"   "<<distMatrix[j][i]<<"  "<<dist(_pop[i],_pop[j])<<"\n";
00064           }
00065 
00066       }
00067 
00068 //compute the similarities
00069     for (i = 0; i < pSize; i++)
00070       {
00071         double sum = 0.0;
00072         for (j = 0; j < pSize; j++)
00073 
00074           sum += sh (distMatrix[i][j], Dmax);
00075         sim[i] = sum;
00076 
00077 //cout<<"\n  i  ----->"<<sim[i]<<"\n";
00078       }
00079 
00080     eoDominanceMap < EOT > Dmap1;
00081     Dmap1.setup (_pop);
00082 
00083     eoParetoRanking < EOT > rnk1 (Dmap1);
00084     rnk1.calculate_worths (_pop);
00085 // now set the worthes values
00086     for (i = 0; i < pSize; ++i)
00087       {
00088         typename EOT::Fitness v;
00089 
00090 
00091 
00092 //cout<<"voila: "<<
00093 //rnk1.value().operator[](i);
00094 
00095 //vector<double> v;
00096 //v.resize(_pop[i].fitness().size());
00097 //for(unsigned k=0;k<_pop[i].fitness().size();++k)
00098 //v[k]=_pop[i].fitness().operator[](k)/sim[i];
00099 //_pop[i].fitness(v);//.operator[](k)=0;//_pop[i].fitness().operator[](k)/sim[i];
00100         eoPerf2Worth < EOT, worthT >::value ()[i] = rnk1.value ().operator[](i) / sim[i];       //*_pop[i].fitness().operator[](1)*_pop[i].fitness().operator[](1));
00101 //cout<<"\n__________"<<pSize<<"  "<<value()[i]<<"    "<<i;
00102       }
00103 
00104   }
00105 
00106 
00107 
00108 
00109   class dMatrix:public std::vector < std::vector < double > >
00110   {
00111   public:
00112     dMatrix (unsigned _s):rSize (_s)
00113     {
00114       this->resize (_s);
00115       for (unsigned i = 0; i < _s; ++i)
00116         this->operator[] (i).resize (_s);
00117     }
00118 
00119     void printOn (std::ostream & _os)
00120     {
00121       for (unsigned i = 0; i < rSize; i++)
00122         for (unsigned j = 0; j < rSize; ++j)
00123           {
00124             _os << this->operator[](i)[j] << " ";
00125             _os << endl;
00126           }
00127       _os << endl;
00128     }
00129 //public:
00130 //std::vector<double>v;
00131 
00132   private:
00133 
00134 
00135 
00136 
00137     unsigned rSize;
00138   };
00139 
00140 private:
00141 
00142   ;
00143 
00144   double sh (double dist, double Dmax)
00145   {
00146     if (dist < Dmax)
00147       return (1.0 - dist / Dmax);
00148     else
00149       return (0.0);
00150   }
00151 
00152   double nicheSize;
00153   moeoParetoPhenDist < EOT, worthT > &dist;
00154   moeoParetoEuclidDist < EOT > euc_dist;
00155   double Dmax;
00156 
00157 };

Generated on Tue Jan 16 15:49:53 2007 for ParadisEO-MOEO by  doxygen 1.5.1