-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMerge.hpp
92 lines (71 loc) · 2.75 KB
/
Merge.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Merge.hpp
// NSGA-Parallel-Backend
//
// Created by a1091793 on 23/11/2015.
//
//
#ifndef Merge_h
#define Merge_h
#include "Population.hpp"
#include "DebsCrowdingDistance.hpp"
class DistanceComparator
{
public:
inline bool operator()(const std::pair<IndividualSPtr, double> & first, const std::pair<IndividualSPtr, double> & second)
{
return (first.second > second.second);
}
};
class DebsRankingAndCrowdingSelector
{
public:
PopulationSPtr
operator()(PopulationSPtr previous_parent_pop, PopulationSPtr previous_child_pop)
{
// Combine parent and offspring population
Population combined_set;
for (Population::size_type i = 0; i < previous_parent_pop->populationSize(); ++i)
{
combined_set.push_back((*previous_parent_pop)[i]);
// (*previous_parent_pop)[i]->parent = true;
}
for (Population::size_type i = 0; i < previous_child_pop->populationSize(); ++i)
{
combined_set.push_back((*previous_child_pop)[i]);
// (*previous_child_pop)[i]->child = true;
}
FrontsSPtr front_sets = combined_set.getFronts();
/***********************************************************************
* New Population *
**********************************************************************/
PopulationSPtr new_child_pop(new Population);
int i = 0;
// until the parent population is filled...
while (new_child_pop->populationSize() + (*front_sets)[i].size() <= previous_child_pop->populationSize())
{
// assign distances (needed for tournament selection)
calculateDebsCrowdingDistance((*front_sets)[i]);
for (IndividualSPtr ind : (*front_sets)[i])
{
new_child_pop->push_back(ind);
}
++i;
}
Population::size_type more_ind_need = previous_child_pop->populationSize() - new_child_pop->populationSize();
if (more_ind_need > 0)
{
std::vector<std::pair<IndividualSPtr, double> > distances = calculateDebsCrowdingDistance((*front_sets)[i]);
//sort by crowding distance in the next dominated set (sorts descendingly)
DistanceComparator dist_comparator;
std::sort(distances.begin(), distances.end(), dist_comparator);
//fill up the new population with solutions with the greatest distance
for (Population::size_type l = 0; l < more_ind_need; ++l)
{
new_child_pop->push_back(distances[l].first);
}
}
return (new_child_pop);
}
};
#endif /* Merge_h */