-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHyperVolume.cpp
More file actions
359 lines (299 loc) · 9.48 KB
/
HyperVolume.cpp
File metadata and controls
359 lines (299 loc) · 9.48 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* HyperVolume 21/03/2025
$$$$$$$$$$$$$$$$$$$$$$$
$ HyperVolume.cpp $
$$$$$$$$$$$$$$$$$$$$$$$
by W.B. Yates
Copyright (c) W.B. Yates. All rights reserved.
History:
Translated from python to c++. Tested against original.
"""
Hypervolume computation based on variant 3 of the algorithm in the paper:
C. M. Fonseca, L. Paquete, and M. Lopez-Ibanez. An improved dimension-sweep
algorithm for the hypervolume indicator. In IEEE Congress on Evolutionary
Computation, pages 1157-1163, Vancouver, Canada, July 2006.
Minimization is implicitly assumed here!
"""
Original code
# Copyright (C) 2010 Simon Wessing
# TU Dortmund University
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
__author__ = "Simon Wessing"
Test example point
HV = 7348493500.00000000
when
m_refPoint = {2000.0, 2000.0, 2000.0};
std::vector<Point> front = {
{ 495.0, -417.0, 0.0 },
{ 658.0, 366.0, 1.0 },
{ 471.0, 733.0, 0.5 },
{ 697.0, 258.0, 10.0 },
{ 1111.0, 214.0, 11.0 },
{ 876.0, 253.0, 12.0 },
{ 476.0, 713.0, 13.0 },
{ 908.0, 237.0, 10.0 },
{ 1133.0, 213.0, 10.0 },
{ 672.0, 306.0, 4.0 },
{ 467.0, 815.0, 3.0 },
{ 1321.0, 200.0, -1.0 },
{ 657.0, 374.0, -1.0 }
};
*/
#ifndef __HYPERVOLUME_H__
#include "HyperVolume.h"
#endif
#include <iostream>
#include <cassert>
#include <numeric>
HyperVolume::HyperVolume( void ) {}
HyperVolume::HyperVolume( const Point &refPoint )
{
m_refPoint = refPoint;
setSentinal();
}
void
HyperVolume::append(Node* node, int index)
// Appends a node to the end of the list at the given index.
{
Node *lastButOne = m_sentinel.prev[index];
node->next[index] = &m_sentinel;
node->prev[index] = lastButOne;
// set the last element as the new one
m_sentinel.prev[index] = node;
lastButOne->next[index] = node;
}
void
HyperVolume::remove(Node *node, int index, Point &bounds)
{
// Removes and returns 'node' from all lists in [0, 'index'[.
for (int i = 0; i < index; ++i)
{
Node* predecessor = node->prev[i];
Node* successor = node->next[i];
predecessor->next[i] = successor;
successor->prev[i] = predecessor;
if (bounds[i] > node->point[i])
bounds[i] = node->point[i];
}
}
void
HyperVolume::reinsert(Node *node, int index, Point &bounds)
/*
Inserts 'node' at the position it had in all lists in [0, 'index'[
before it was removed. This method assumes that the next and previous
nodes of the node that is reinserted are in the list.
*/
{
for (int i = 0; i < index; ++i)
{
node->prev[i]->next[i] = node;
node->next[i]->prev[i] = node;
if (bounds[i] > node->point[i])
bounds[i] = node->point[i];
}
}
bool
HyperVolume::weaklyDominates(const Point &point, const Point &other) const
{
for (int i = 0; i < point.size(); ++i)
{
if (point[i] > other[i])
return false;
}
return true;
}
double
HyperVolume::hvRecursive(int dimIndex, int length, Point &bounds)
/*
Recursive call to hypervolume calculation.
In contrast to the paper, the code assumes that the reference point
is [0, ..., 0]. This allows the avoidance of a few operations.
*/
{
double hvol = 0.0;
if (length == 0)
return hvol;
if (dimIndex == 0)
{
// special case: only one dimension - why using hypervolume at all?
return -m_sentinel.next[0]->point[0];
}
else if (dimIndex == 1)
{
// special case: two dimensions, end recursion
Node *q = m_sentinel.next[1];
double h = q->point[0];
Node *p = q->next[1];
while (p != &m_sentinel)
{
const Point &pCargo = p->point;
hvol += h * (q->point[1] - pCargo[1]);
if (pCargo[0] < h)
{
h = pCargo[0];
}
q = p;
p = q->next[1];
}
hvol += h * q->point[1];
return hvol;
}
else
{
Node *p = &m_sentinel;
Node *q = p->prev[dimIndex];
while (q != &m_sentinel)
{
if (q->ignore < dimIndex)
q->ignore = 0;
q = q->prev[dimIndex];
}
q = p->prev[dimIndex];
while (length > 1 && (q->point[dimIndex] > bounds[dimIndex] || q->prev[dimIndex]->point[dimIndex] >= bounds[dimIndex]))
{
p = q;
remove(p, dimIndex, bounds);
q = p->prev[dimIndex];
length -= 1;
}
std::vector<double> &qArea = q->area;
const Point &qCargo = q->point;
Node *qPrevDimIndex = q->prev[dimIndex];
if (length > 1)
{
hvol = qPrevDimIndex->volume[dimIndex] + qPrevDimIndex->area[dimIndex] * (qCargo[dimIndex] - qPrevDimIndex->point[dimIndex]);
}
else
{
qArea[0] = 1;
for (int j = 0; j < dimIndex; ++j)
qArea[j+1] = qArea[j] * -qCargo[j];
}
q->volume[dimIndex] = hvol;
if (q->ignore >= dimIndex)
{
qArea[dimIndex] = qPrevDimIndex->area[dimIndex];
}
else
{
qArea[dimIndex] = hvRecursive(dimIndex - 1, length, bounds);
if (qArea[dimIndex] <= qPrevDimIndex->area[dimIndex])
q->ignore = dimIndex;
}
while (p != &m_sentinel)
{
double pCargoDimIndex = p->point[dimIndex];
hvol += q->area[dimIndex] * (pCargoDimIndex - q->point[dimIndex]);
bounds[dimIndex] = pCargoDimIndex;
reinsert(p, dimIndex, bounds);
length++;
q = p;
p = p->next[dimIndex];
q->volume[dimIndex] = hvol;
if (q->ignore >= dimIndex)
q->area[dimIndex] = q->prev[dimIndex]->area[dimIndex];
else q->area[dimIndex] = hvRecursive(dimIndex - 1, length, bounds);
if (q->area[dimIndex] <= q->prev[dimIndex]->area[dimIndex])
q->ignore = dimIndex;
}
hvol -= q->area[dimIndex] * q->point[dimIndex];
return hvol;
}
}
void
HyperVolume::setRefPoint( const Point &refPoint )
{
m_refPoint = refPoint;
setSentinal();
}
void
HyperVolume::setSentinal( void )
{
int dims = (int) m_refPoint.size();
m_sentinel.point = Point(dims, 0.0);
m_sentinel.ignore = 0;
m_sentinel.next.clear();
m_sentinel.prev.clear();
m_sentinel.area.clear();
m_sentinel.volume.clear();
m_sentinel.next.resize(dims, nullptr);
m_sentinel.prev.resize(dims, nullptr);
m_sentinel.area.resize(dims, 0.0);
m_sentinel.volume.resize(dims, 0.0);
for (int i = 0; i < dims; ++i)
{
m_sentinel.next[i] = &m_sentinel;
m_sentinel.prev[i] = &m_sentinel;
}
}
void
HyperVolume::preProcess(const std::vector<Point> &front)
// Sets up the list data structure needed for calculation.
{
setSentinal();
int dims = (int) m_refPoint.size();
m_nodes.resize(front.size());
for (int i = 0; i < front.size(); ++i)
{
m_nodes[i] = Node(front[i]);
}
std::vector<int> order(m_nodes.size());
std::iota(order.begin(), order.end(), 0);
for (int i = 0; i < dims; ++i)
{
// order the nodes by dimention
auto op1 = [&](int idx1, int idx2) { return m_nodes[idx1].point[i] < m_nodes[idx2].point[i]; };
std::sort(order.begin(), order.end(), op1 );
for (int j : order)
{
Node *lastButOne = m_sentinel.prev[i];
m_nodes[j].next[i] = &m_sentinel;
m_nodes[j].prev[i] = lastButOne;
// set the last element as the new one
m_sentinel.prev[i] = &m_nodes[j];
lastButOne->next[i] = &m_nodes[j];
}
}
}
double
HyperVolume::compute(const std::vector<Point> &front)
{
if (front.empty())
return 0.0;
assert(m_refPoint.size() == front[0].size());
std::vector<Point> relevantPoints;
for (const Point &point : front)
{
// only consider points that dominate the reference point
if (weaklyDominates(point, m_refPoint))
relevantPoints.push_back(point);
}
int dims = (int) m_refPoint.size();
if (any(m_refPoint))
{
// shift points so that refPoint == [0, ..., 0]
// this way the reference point doesn't have to be explicitly used
// in the HV computation
for (int i = 0; i < relevantPoints.size(); ++i)
{
for (int j = 0; j < dims; ++j)
{
relevantPoints[i][j] -= m_refPoint[j];
}
}
}
preProcess(relevantPoints);
Point bounds(dims, -std::numeric_limits<double>::max());
return hvRecursive(dims - 1, (int) relevantPoints.size(), bounds);
}