-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDebsNondominatedSorting.hpp
209 lines (169 loc) · 6.12 KB
/
DebsNondominatedSorting.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// DebsNondominatedSorting.hpp
// NSGA-Parallel-Backend
//
// Created by a1091793 on 24/11/2015.
//
//
#ifndef DebsNondominatedSorting_h
#define DebsNondominatedSorting_h
//class DebsNonDominatesSorting
//{
//public:
// static
// std::vector<std::vector<IndividualPtr> >
// sort(PopulationSPtr pop);
// static
// std::vector<Population>
// sort(Population population_ptrs);
//};
#include <iostream>
#include <vector>
#include "Comparator.hpp"
#include "Population.hpp"
class DominationInfo
{
public:
IndividualSPtr ind;
std::vector<DominationInfo *> dominates;
int dominated;
DominationInfo()
:
dominated(0)
{
}
};
inline FrontsSPtr
debNonDominatedSort(const Population & population_ref)
{
/***********************************************************************
* NON-DOMINATED-SORTING *
**********************************************************************/
if (population_ref.size() == 0)
{
FrontsSPtr fronts(new std::vector<Front>(0));
return fronts;
}
std::vector<DominationInfo> population_set(population_ref.size());
for (std::vector<DominationInfo>::size_type i = 0; i < population_ref.size(); ++i)
{
population_set[i].ind = population_ref[i];
}
FrontsSPtr fronts(new std::vector<Front>(1));
std::vector<std::vector<DominationInfo * > > front_sets(1);
// std::map<IndivudalPtr, std::vector<IndivudalPtr> > dominates_sets;
// std::map<IndivudalPtr, int> number_dominated_by;
const int P_DOMINATES = 1;
const int Q_DOMINATES = 2;
for (std::vector<DominationInfo>::size_type j = 0; j < population_set.size(); ++j)
{
DominationInfo & p = population_set[j];
IndividualSPtr p_ptr = p.ind;
int & p_dominated_by = p.dominated;
std::vector<DominationInfo *> & p_dominates = p.dominates;
for (std::vector<DominationInfo>::size_type k = j+1; k < population_set.size(); ++k)
{
DominationInfo & q = population_set[k];
IndividualSPtr q_ptr = q.ind;
int & q_dominated_by = q.dominated;
std::vector<DominationInfo *> & q_dominates = q.dominates;
if (p_ptr != q_ptr)
{
//If p dominates q
int compare = Comparator::whichDominates(p_ptr,q_ptr);
if (compare == P_DOMINATES)
{
p_dominates.push_back(&q);
++q_dominated_by;
}
else if (compare == Q_DOMINATES)
{
q_dominates.push_back(&p);
++p_dominated_by;
}
}
}
}
int front_num = 0;
for(DominationInfo & p: population_set)
{
IndividualSPtr p_ptr = p.ind;
const int & p_dominated_by = p.dominated;
if (p_dominated_by == 0)
{
front_sets[front_num].push_back(&p);
(*fronts)[front_num].push_back(p_ptr);
p_ptr->setRank(front_num+1);
}
}
std::vector<DominationInfo * > dummy_front1;
Population dummy_front2;
//Could truncate this loop once the top pop_size are sorted.
while (front_sets[front_num].size() != 0)
{
front_sets.push_back(dummy_front1);
fronts->push_back(dummy_front2);
for (DominationInfo * p: front_sets[front_num])
{
for (DominationInfo * q: p->dominates)
{
// std::cout << "from " << q->dominated;
q->dominated = --(q->dominated);
// std::cout << "to " << q->dominated << std::endl;
if (q->dominated == 0)
{
front_sets[front_num+1].push_back(q);
(*fronts)[front_num+1].push_back(q->ind);
q->ind->setRank(front_num+2);
}
}
}
++front_num;
}
// BOOST_FOREACH(DominationInfo & p, population_set)
// {
// IndividualSPtr p_ptr = p.ind;
// int & p_dominated_by = p.dominated;
// p_dominated_by = 0;
// std::vector<DominationInfo *> & p_dominates = p.dominates;
//
// BOOST_FOREACH(DominationInfo & q, population_set)
// {
// IndividualSPtr q_ptr = q.ind;
// if (p_ptr != q_ptr)
// {
// //If p dominates q
// int compare = Comparator::whichDominates(p_ptr,q_ptr);
// if (compare == P_DOMINATES)
// {
// p_dominates.push_back(&q);
// }
// else if (compare == Q_DOMINATES)
// {
// ++p_dominated_by;
// }
// }
// }
// if (p_dominated_by == 0)
// {
// front_sets[0].push_back(&p);
// (*fronts)[0].push_back(p_ptr);
// p_ptr->setRank(1);
//
// }
//
// }
//
// int i = 0;
if (fronts->back().size() == 0)
{
fronts->pop_back();
}
return (fronts);
}
//inline FrontsSPtr
//debNonDominatedSort(PopulationSPtr pop)
//{
// return(debNonDominatedSort(*pop));
//}
#endif /* DebsNondominatedSorting_h */