-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathComparator.hpp
222 lines (193 loc) · 6.26 KB
/
Comparator.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
210
211
212
213
214
215
216
217
218
219
220
221
/*
//
// Comparator.hpp
// NSGA-Parallel-Backend
//
// Created by a1091793 on 23/11/2015.
//
//
*/
#ifndef Comparator_h
#define Comparator_h
#include "Individual.hpp"
#include <tuple>
class Comparator
{
public:
static
bool
compareObjective(const Individual& ind1, const Individual& ind2, ProblemDefinitions::ObjectivesT::size_type index)
{
if (ind1.isMinimiseOrMaximise(index) == ProblemDefinitions::MINIMISATION)
{
return (ind1.getObjective(index) < ind2.getObjective(index));
}
return (ind1.getObjective(index) > ind2.getObjective(index));
}
static
bool
compareObjective(const IndividualSPtr ind1, const IndividualSPtr ind2, ProblemDefinitions::ObjectivesT::size_type index)
{
return (compareObjective(*ind1, *ind2, index));
}
static
int
compareObjective2(const Individual& ind1, const Individual& ind2, ProblemDefinitions::ObjectivesT::size_type index)
{
if (ind1.isMinimiseOrMaximise(index) == ProblemDefinitions::MINIMISATION)
{
if (ind1.getObjective(index) < ind2.getObjective(index)) return 1;
if (ind1.getObjective(index) == ind2.getObjective(index)) return 0;
return 2;
}
else
{
if (ind1.getObjective(index) < ind2.getObjective(index)) return 2;
if (ind1.getObjective(index) == ind2.getObjective(index)) return 0;
return 1;
}
return (3);
}
static
int
compareObjective2(const IndividualSPtr ind1, const IndividualSPtr ind2, ProblemDefinitions::ObjectivesT::size_type index)
{
return (compareObjective2(*ind1, *ind2, index));
}
static int
whichDominates(const IndividualSPtr ind1, const IndividualSPtr ind2)
{
return whichDominates(*ind1, *ind2);
}
static int
whichDominates(const Individual & ind1, const Individual & ind2)
{
// Calculate the number of constraints violated, and the relative amount of violation.
int ind1_num_constr_violatn = 0;
int ind2_num_constr_violatn = 0;
double rel_violation_ind1 = 0;
double rel_violation_ind2 = 0;
for (ProblemDefinitions::ConstraintsT::size_type i = 0; i < ind1.numOfConstraints(); ++i)
{
const double & constraint1 = ind1.getConstraint(i);
const double & constraint2 = ind2.getConstraint(i);
if (constraint1 > 0)
{
++ind1_num_constr_violatn;
if (constraint2 > 0)
{
++ind2_num_constr_violatn;
if (constraint1 > constraint2)
{
rel_violation_ind1 += 1;
rel_violation_ind2 += constraint2/constraint1;
}
else
{
rel_violation_ind2 += 1;
rel_violation_ind1 += constraint1/constraint2;
}
}
else
{
rel_violation_ind1 += 1;
}
}
else if (constraint2 > 0)
{
++ind2_num_constr_violatn;
rel_violation_ind2 += 1;
}
}
// If both solutions infeasible, pick one which is has the smaller overall constraint violation
if (ind1_num_constr_violatn != 0 && ind2_num_constr_violatn != 0)
{
if (ind1_num_constr_violatn < ind2_num_constr_violatn)
{
return (1);
}
else if (ind1_num_constr_violatn > ind2_num_constr_violatn)
{
return (2);
}
else
{
if (rel_violation_ind1 < rel_violation_ind2)
{
return (1);
}
else if (rel_violation_ind1 > rel_violation_ind2)
{
return (2);
}
}
}
// if one solution is feasible.....
// else if (ind1_num_constr_violatn > 0 && ind2_num_constr_violatn == 0)
else if (ind1_num_constr_violatn > 0)
{
return (2);
}
// else if (ind1_num_constr_violatn == 0 && ind2_num_constr_violatn > 0)
else if (ind2_num_constr_violatn > 0)
{
return (1);
}
// if both solutions are feasible (or they have the same number and relative constraint values,) choose
// the one which dominates via objective values.
int flag1 = 0;
int flag2 = 0;
for (ProblemDefinitions::ObjectivesT::size_type j=0; j < ind1.numOfObjectives(); ++j)
{
int which_is_better = compareObjective2(ind1, ind2, j);
if (which_is_better == 1) flag1 = 1;
if (which_is_better == 2) flag2 = 1;
}
if (flag1==1 && flag2==0)
{
return (1);
}
else if (flag1==0 && flag2==1)
{
return (2);
}
else
{
return (0);
}
return (3);
}
bool
operator()(const IndividualSPtr ind1, const IndividualSPtr ind2)
{
return this->operator ()(*ind1, *ind2);
}
bool
operator()(const Individual & ind1, const Individual & ind2)
{
if (whichDominates(ind1, ind2) == 1) return true;
return false;
}
};
class ObjectiveValueCompator
{
int objective_index;
public:
ObjectiveValueCompator(int _objective_index)
: objective_index(_objective_index)
{
}
inline bool operator()(const std::pair<IndividualSPtr, double> & first, const std::pair<IndividualSPtr, double> & second)
{
return (Comparator::compareObjective((first.first), (second.first), objective_index));
}
inline bool operator()(const IndividualSPtr first, const IndividualSPtr second)
{
return (Comparator::compareObjective(first, second, objective_index));
}
inline bool operator()(const Individual & first, const Individual & second)
{
return (Comparator::compareObjective(first, second, objective_index));
}
};
#endif /* Comparator_h */