diff --git a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoElitistReplacement2.h b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoElitistReplacement2.h index 4a4e6ae52..2f7544126 100644 --- a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoElitistReplacement2.h +++ b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoElitistReplacement2.h @@ -112,11 +112,9 @@ template < class MOEOT > class moeoElitistReplacement2:public moeoReplacement < diversityAssignment(_parents); // sorts the whole population - // the delindex contains the ordered index of the population according to the comparator - // again, we only sort the index of population instead of population + // the delindex contains the ordered index of the population according to the comparator again, we only sort the index of population instead of population std::vector delindex; vectorSortIndex( _parents, delindex, comparator); - //std::sort( delindex.begin(), delindex.end(), comparatorindex(_parents, delindex, comparator)); // now, in order to rezise of population we remove the populations whose index // are in the high of delindex std::sort(delindex.begin()+sz, delindex.end(), std::greater()); @@ -127,6 +125,15 @@ template < class MOEOT > class moeoElitistReplacement2:public moeoReplacement < _offspring.clear (); } + + /** + * Resizes the whole population obtained. + * @param _pop the whole population + * @param _index points to the population members which will be deleted + * @param _sz size of the resulting population + */ + + void reduce_population ( eoPop < MOEOT > &_pop, std::vector &index, unsigned int size) { for(int j=size; j< index.size(); j++) diff --git a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoFrontByFrontCrowdingDiversityAssignment2.h b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoFrontByFrontCrowdingDiversityAssignment2.h index d6c9b2d8b..c033978ad 100644 --- a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoFrontByFrontCrowdingDiversityAssignment2.h +++ b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoFrontByFrontCrowdingDiversityAssignment2.h @@ -91,25 +91,27 @@ class moeoFrontByFrontCrowdingDiversityAssignment2 : public moeoCrowdingDiversit } // sort the whole pop according to fitness values moeoFitnessThenDiversityComparator < MOEOT > fitnessComparator; - std::vector sortedpop; - sortedpop.resize(_pop.size()); - std::transform( _pop.begin(), _pop.end(), sortedpop.begin(), typename eoPop::Ref()); - //struct eoPop::Ref ref; - moeoPtrComparator cmp1( fitnessComparator); - std::sort(sortedpop.begin(), sortedpop.end(), cmp1); - //std::sort(_pop.begin(), _pop.end(), fitnessComparator); + std::vector sortedptrpop; + sortedptrpop.resize(_pop.size()); + // due to intensive sort operations for this diversity assignment, + // it is more efficient to perform sorts using only pointers to the + // population members in order to avoid copy of individuals + std::transform( _pop.begin(), _pop.end(), sortedptrpop.begin(), typename eoPop::Ref()); + //sort the pointers to population members + moeoPtrComparator cmp2( fitnessComparator); + std::sort(sortedptrpop.begin(), sortedptrpop.end(), cmp2); // compute the crowding distance values for every individual "front" by "front" (front : from a to b) a = 0; // the front starts at a while (a < _pop.size()) { - b = lastIndex(sortedpop,a); // the front ends at b + b = lastIndex(sortedptrpop,a); // the front ends at b //b = lastIndex(_pop,a); // the front ends at b // if there is less than 2 individuals in the front... if ((b-a) < 2) { for (unsigned int i=a; i<=b; i++) { - ((MOEOT *)sortedpop[i])->diversity(inf()); + ((MOEOT *)sortedptrpop[i])->diversity(inf()); //_pop[i].diversity(inf()); } } @@ -122,10 +124,10 @@ class moeoFrontByFrontCrowdingDiversityAssignment2 : public moeoCrowdingDiversit // sort in the descending order using the values of the objective 'obj' moeoOneObjectiveComparator < MOEOT > objComp(obj); moeoPtrComparator cmp2( objComp ); - std::sort(sortedpop.begin(), sortedpop.end(), cmp2); + std::sort(sortedptrpop.begin(), sortedptrpop.end(), cmp2); // min & max - min = (sortedpop[b])->objectiveVector()[obj]; - max = (sortedpop[a])->objectiveVector()[obj]; + min = (sortedptrpop[b])->objectiveVector()[obj]; + max = (sortedptrpop[a])->objectiveVector()[obj]; // avoid extreme case if (min == max) @@ -134,13 +136,13 @@ class moeoFrontByFrontCrowdingDiversityAssignment2 : public moeoCrowdingDiversit max += tiny(); } // set the diversity value to infiny for min and max - ((MOEOT *)sortedpop[a])->diversity(inf()); - ((MOEOT *)sortedpop[b])->diversity(inf()); + ((MOEOT *)sortedptrpop[a])->diversity(inf()); + ((MOEOT *)sortedptrpop[b])->diversity(inf()); // set the diversity values for the other individuals for (unsigned int i=a+1; iobjectiveVector()[obj] - (sortedpop[i+1])->objectiveVector()[obj]) / (max-min); - ((MOEOT *)sortedpop[i])->diversity(sortedpop[i]->diversity() + distance); + distance = ( (sortedptrpop[i-1])->objectiveVector()[obj] - (sortedptrpop[i+1])->objectiveVector()[obj]) / (max-min); + ((MOEOT *)sortedptrpop[i])->diversity(sortedptrpop[i]->diversity() + distance); } } } @@ -153,7 +155,7 @@ class moeoFrontByFrontCrowdingDiversityAssignment2 : public moeoCrowdingDiversit /** * Returns the index of the last individual having the same fitness value than _pop[_start] - * @param _pop the population + * @param _pop the vector of pointers to population individuals * @param _start the index to start from */ diff --git a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoObjVecStat.h b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoObjVecStat.h index 08f4d29d9..5794371da 100644 --- a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoObjVecStat.h +++ b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoObjVecStat.h @@ -23,6 +23,11 @@ #include +/** + This base class is a specialization of eoStat which is useful + to compute statistics for each objective function +*/ + #if defined(_MSC_VER) && (_MSC_VER < 1300) template class moeoObjVecStat : public eoStat @@ -46,6 +51,9 @@ class moeoObjVecStat : public eoStat virtual void doit(const eoPop &_pop) = 0; }; +/** Calculate the best solution for each objective (extreme points +*/ + template class moeoBestObjVecStat : public moeoObjVecStat { @@ -62,9 +70,19 @@ public: virtual std::string className(void) const { return "moeoBestObjVecStat"; } - const MOEOT & bestindividuals(unsigned int objective) { return *(best_individuals[objective]); } + /** + Return the best solutions for an given objective function + * @param which the objective function number + */ + const MOEOT & bestindividuals(unsigned int which) { + typedef typename moeoObjVecStat::Traits traits; + if(which > traits::nObjectives() ) throw std::logic_error("which is larger than the number of objectives"); + return *(best_individuals[which]); + } private : + + /** Vector of iterators pointing to best individuals for each objective function */ std::vector::const_iterator> best_individuals; @@ -78,7 +96,7 @@ private : return a.objectiveVector()[which] < b.objectiveVector()[which]; return a.objectiveVector()[which] > b.objectiveVector()[which]; - } + } unsigned which; bool maxim; @@ -101,6 +119,10 @@ private : // default }; +/** Calculates average scores for each objective +*/ + + template class moeoAverageObjVecStat : public moeoObjVecStat { public : diff --git a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoPtrComparator.h b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoPtrComparator.h index c8436e5fe..cec418139 100644 --- a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoPtrComparator.h +++ b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoPtrComparator.h @@ -29,11 +29,23 @@ template < class MOEOT > class moeoPtrComparator : public eoBF < const MOEOT *, const MOEOT *, const bool > { public: + + /** + * Ctor with a comparator + * @param _cmp comparator to be employed + + */ moeoPtrComparator( moeoComparator & _cmp) : cmp(_cmp) {} const bool operator() (const MOEOT *ptr1, const MOEOT *ptr2) { return cmp(*ptr1, *ptr2); } + + const bool operator() (MOEOT *ptr1, MOEOT *ptr2) + { + return cmp(*ptr1, *ptr2); + } + private: moeoComparator &cmp; };