00001
00002
00003
00004
00005
00006
00007
00008
00009
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 () (const eoPop < EOT > &_pop)
00041 {
00042
00043 unsigned i, j, pSize = _pop.size ();
00044
00045
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);
00051
00052 dMatrix distMatrix (pSize);
00053
00054
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
00062 distMatrix[i][j] = distMatrix[j][i] = dist (_pop[i], _pop[j]);
00063
00064 }
00065
00066 }
00067
00068
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
00078 }
00079
00080 eoDominanceMap < EOT > Dmap1;
00081 Dmap1.setup (_pop);
00082
00083 eoParetoRanking < EOT > rnk1 (Dmap1);
00084 rnk1.calculate_worths (_pop);
00085
00086 for (i = 0; i < pSize; ++i)
00087 {
00088 typename EOT::Fitness v;
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 eoPerf2Worth < EOT, worthT >::value ()[i] = rnk1.value ().operator[](i) / sim[i];
00101
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
00130
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 };