-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDebsCrowdingDistance.hpp
74 lines (60 loc) · 2.16 KB
/
DebsCrowdingDistance.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// DebsCrowdingDistance.hpp
// NSGA-Parallel-Backend
//
// Created by a1091793 on 24/11/2015.
//
//
#ifndef DebsCrowdingDistance_h
#define DebsCrowdingDistance_h
#include <vector>
#include <boost/foreach.hpp>
#include "Population.hpp"
#include "Comparator.hpp"
//#include "DebsNondominatedSorting.hpp"
typedef std::vector<std::pair<IndividualSPtr, double > > IndividualsWithCrowdingDistanceVec;
inline
IndividualsWithCrowdingDistanceVec
calculateDebsCrowdingDistance(const Population & front_set)
{
typedef std::pair<IndividualSPtr, double> IndDistPair;
std::vector<IndDistPair> distances;
for(IndividualSPtr ind: front_set)
{
distances.push_back(std::make_pair(ind, 0));
}
//calculate Crowding distance for solutions in the next dominated set.
for (int j = 0; j < (distances[0]).first->numOfObjectives(); ++j)
{
ObjectiveValueCompator obj_comparator(j);
std::sort(distances.begin(), distances.end(), obj_comparator);
distances.front().second = std::numeric_limits<double>::max();
distances.front().first->setCrowdingScore(std::numeric_limits<double>::max());
double min_obj_val = distances.front().first->getObjective(j);
distances.back().second = std::numeric_limits<double>::max();
distances.back().first->setCrowdingScore(std::numeric_limits<double>::max());
double max_obj_val = distances.back().first->getObjective(j);
for (int k = 1; k < distances.size() -1; ++k)
{
distances[k].second += (distances[k+1].first->getObjective(j) - distances[k-1].first->getObjective(j)) / (max_obj_val - min_obj_val);
}
}
for (IndDistPair & ind: distances)
{
ind.first->setCrowdingScore(ind.second);
}
return (distances);
}
inline
std::vector< std::vector<std::pair<IndividualSPtr, double > > >
calculateDebsCrowdingDistance(const PopulationSPtr pop)
{
std::vector< std::vector<std::pair<IndividualSPtr, double > > > crowd_dist_by_front;
FrontsSPtr fronts = pop->getFronts();
for (Front front : *fronts)
{
crowd_dist_by_front.push_back(calculateDebsCrowdingDistance(front));
}
return (crowd_dist_by_front);
}
#endif /* DebsCrowdingDistance_h */