special two-objective case of dominance depth ranking in O(n log n)

This commit is contained in:
liefooga 2013-05-31 16:13:45 +02:00
commit effaa56cfd

View file

@ -44,7 +44,7 @@
#include <comparator/moeoObjectiveVectorComparator.h> #include <comparator/moeoObjectiveVectorComparator.h>
#include <comparator/moeoParetoObjectiveVectorComparator.h> #include <comparator/moeoParetoObjectiveVectorComparator.h>
#include <fitness/moeoDominanceBasedFitnessAssignment.h> #include <fitness/moeoDominanceBasedFitnessAssignment.h>
#include <comparator/moeoPtrComparator.h>
/** /**
* Fitness assignment sheme based on Pareto-dominance count proposed in: * Fitness assignment sheme based on Pareto-dominance count proposed in:
@ -65,7 +65,7 @@ class moeoDominanceDepthFitnessAssignment : public moeoDominanceBasedFitnessAssi
/** /**
* Default ctor * Default ctor
*/ */
moeoDominanceDepthFitnessAssignment() : comparator(paretoComparator) moeoDominanceDepthFitnessAssignment(bool _rm_equiv_flag_in_2D = false) : comparator(paretoComparator), rm_equiv_flag_in_2D(_rm_equiv_flag_in_2D)
{} {}
@ -73,7 +73,7 @@ class moeoDominanceDepthFitnessAssignment : public moeoDominanceBasedFitnessAssi
* Ctor where you can choose your own way to compare objective vectors * Ctor where you can choose your own way to compare objective vectors
* @param _comparator the functor used to compare objective vectors * @param _comparator the functor used to compare objective vectors
*/ */
moeoDominanceDepthFitnessAssignment(moeoObjectiveVectorComparator < ObjectiveVector > & _comparator) : comparator(_comparator) moeoDominanceDepthFitnessAssignment(moeoObjectiveVectorComparator < ObjectiveVector > & _comparator, bool _rm_equiv_flag_in_2D = true) : comparator(_comparator), rm_equiv_flag_in_2D(_rm_equiv_flag_in_2D)
{} {}
@ -92,8 +92,8 @@ class moeoDominanceDepthFitnessAssignment : public moeoDominanceBasedFitnessAssi
} }
else if (nObjectives == 2) else if (nObjectives == 2)
{ {
// two objectives (the two objectives function is still to implement) // two objectives
mObjectives(_pop); twoObjectives(_pop);
} }
else if (nObjectives > 2) else if (nObjectives > 2)
{ {
@ -142,18 +142,20 @@ class moeoDominanceDepthFitnessAssignment : public moeoDominanceBasedFitnessAssi
moeoObjectiveVectorComparator < ObjectiveVector > & comparator; moeoObjectiveVectorComparator < ObjectiveVector > & comparator;
/** Functor to compare two objective vectors according to Pareto dominance relation */ /** Functor to compare two objective vectors according to Pareto dominance relation */
moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator; moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator;
/** flag to remove equivament solutions */
bool rm_equiv_flag_in_2D;
/** Functor allowing to compare two solutions according to their first objective value, then their second, and so on. */ /** Functor allowing to compare two solutions according to their first objective value, then their second, and so on. */
class ObjectiveComparator : public moeoComparator < MOEOT > class ObjectiveComparator : public moeoComparator < MOEOT >
{ {
public: public:
/** /**
* Returns true if _moeo1 < _moeo2 on the first objective, then on the second, and so on * Returns true if _moeo1 > _moeo2 on the first objective, then on the second, and so on
* @param _moeo1 the first solution * @param _moeo1 the first solution
* @param _moeo2 the second solution * @param _moeo2 the second solution
*/ */
bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2) bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2)
{ {
return cmp(_moeo1.objectiveVector(), _moeo2.objectiveVector()); return cmp(_moeo2.objectiveVector(), _moeo1.objectiveVector());
} }
private: private:
/** the corresponding comparator for objective vectors */ /** the corresponding comparator for objective vectors */
@ -172,10 +174,10 @@ class moeoDominanceDepthFitnessAssignment : public moeoDominanceBasedFitnessAssi
std::sort(_pop.begin(), _pop.end(), objComparator); std::sort(_pop.begin(), _pop.end(), objComparator);
// assign fitness values // assign fitness values
unsigned int rank = 1; unsigned int rank = 1;
_pop[_pop.size()-1].fitness(rank); _pop[0].fitness(rank);
for (int i=_pop.size()-2; i>=0; i--) for (unsigned int i=1; i<_pop.size(); i++)
{ {
if (_pop[i].objectiveVector() != _pop[i+1].objectiveVector()) if (_pop[i].objectiveVector() != _pop[i-1].objectiveVector())
{ {
rank++; rank++;
} }
@ -190,7 +192,73 @@ class moeoDominanceDepthFitnessAssignment : public moeoDominanceBasedFitnessAssi
*/ */
void twoObjectives (eoPop < MOEOT > & _pop) void twoObjectives (eoPop < MOEOT > & _pop)
{ {
//... TO DO ! double value_obj1;
unsigned int front;
unsigned int last_front = 0;
bool equiv_flag;
// sort pointers to pop's individuals with respect to the first objective (0) in the reverse order
std::vector<MOEOT *> sortedptrpop;
sortedptrpop.resize(_pop.size());
for(unsigned int i=0; i<_pop.size(); i++)
{
sortedptrpop[i] = & (_pop[i]);
}
moeoPtrComparator<MOEOT> cmp(objComparator);
std::sort(sortedptrpop.begin(), sortedptrpop.end(), cmp);
// compute an upper bound on the second objective (1)
double max_obj1 = std::numeric_limits<double>::min();
for(unsigned int i=0; i<_pop.size(); i++)
{
max_obj1 = std::max(max_obj1, _pop[i].objectiveVector()[1]);
}
max_obj1 += 1.0;
// initialize a vector with the max_obj1 value everywhere
std::vector<double> d(_pop.size(), max_obj1);
// initialize fronts
std::vector<std::vector<unsigned int> > fronts(_pop.size());
// compute rank for each individual
for(unsigned int i=0; i<sortedptrpop.size(); i++)
{
equiv_flag = false;
// check for equivalent solutions and assign them to the worst front
if (i>0)
{
if ( (rm_equiv_flag_in_2D) && (sortedptrpop[i]->objectiveVector() == sortedptrpop[i-1]->objectiveVector()) )
{
equiv_flag = true;
fronts.back().push_back(i);
}
}
if (!equiv_flag)
{
// the value of the second objective for the current solutions
value_obj1 = sortedptrpop[i]->objectiveVector()[1];
// if we maximize, take the opposite value
if (MOEOT::ObjectiveVector::maximizing(1))
value_obj1 = max_obj1 - value_obj1;
// perform binary search (log n)
std::vector<double>::iterator it = std::upper_bound(d.begin(), d.begin() + last_front, value_obj1);
// retrieve the corresponding front
front = (unsigned int)(it - d.begin());
if (front == last_front)
last_front++;
// update
*it = value_obj1;
// add the solution to the corresponding front
fronts[front].push_back(i);
}
}
// assign the fitness value (rank) to each individual
for (unsigned int i=0; i<fronts.size(); i++)
{
for (unsigned int j=0; j<fronts[i].size(); j++)
{
sortedptrpop[fronts[i][j]]->fitness(i+1);
}
}
} }