-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhypeIndicatorExact8.m
128 lines (113 loc) · 3.25 KB
/
hypeIndicatorExact8.m
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
% hv(s1, s2) = hv(s1) + hv(s2) - hv(worse(s1,s2)), sort, using for
function [hv, nrW] = hypeIndicatorExact8(A, bounds, dim)
hv = 0; nrW = 0;
nrA = size(A, 1);
if nrA == 0
return;
elseif nrA == 1
hv = prod(bounds - A(1,:));
nrW = 0;
elseif nrA == 2
w = max(A(1,:), A(2,:));
hv = prod(bounds - A(1,:)) + prod(bounds - A(2,:)) - prod(bounds - w);
% nrW = 1;
elseif nrA == 3
w1 = max(A(1,:), A(2,:));
w2 = max(A(1,:), A(3,:));
w3 = max(A(2,:), A(3,:));
w4 = max(w1, A(3,:));
hv = prod(bounds - A(1,:)) + prod(bounds - A(2,:)) + prod(bounds - A(3,:)) - prod(bounds - w1) - prod(bounds - w2) - prod(bounds - w3) + prod(bounds - w4);
elseif dim == 2
%the points are slice to 2d, same values in other dimensions
hv = hypervolume2(A(:,1:2), bounds(1:2));
for i = 3:size(A,2)
hv = hv * (bounds(i) - A(1,i));
end
nrW = 0;
else
A = sortrows(A, dim);
while A(nrA, dim) == A(1, dim)
% fprintf('s');
dim = dim - 1;
A = sortrows(A, dim);
end
if dim == 2
%the points are slice to 2d, same values in other dimensions
hv = hypervolume2(A(:,1:2), bounds(1:2));
for i = 3:size(A,2)
hv = hv * (bounds(i) - A(1,i));
end
nrW = 0;
% nrW = nrA - 1;
else
hv = hv + prod(bounds - A(1,:));
for i = nrA:-1:2
v = prod(bounds - A(i,:));
if v > 0
W = worse(A(i,:), A(1:i-1,:));
[hv1, nrW1] = hypeIndicatorExact8(W, bounds, dim - 1);
hv = hv + v - hv1;
nrW = nrW + size(W,1) +nrW1;
end
end
end
end
end
function [W] = worse(A1, A2)
%how to quickly get the worse set
[nrA1, dim] = size(A1);
nrA2 = size(A2,1);
W = zeros(nrA1 * nrA2, dim);
nrW = 0;
for i = 1:nrA1
worse = max(repmat(A1(i,:), nrA2, 1), A2);
%find the non-dominated points in worse
for j = nrA2:-1:1
%the index (nrA2:-1:1) is important, because the last one is alwasys better (nondominated) than others
[W, nrW] = insertPoint(worse(j,:), W, nrW);
end
end
%find the non-equal, and non-dominated points in W
W = W(1:nrW,:);
end
function [W, nrW] = insertPoint(p, W, nrW)
if nrW == 0
W(1,:) = p;
nrW = 1;
else
dim = size(p,2);
i = 1;
while i <= nrW
diff = p - W(i,:);
if sum(diff >=0, 2) == dim
%same point, or p is dominated
% fprintf('%c', '1');
return;
end
if sum(diff <=0, 2) == dim
%some points in W are dominaed by p
W(i, :) = W(nrW, :);
% fprintf('%c', '2');
i = i - 1;
nrW = nrW - 1;
end
i = i + 1;
end
%non-dominated each other, add this point
nrW = nrW + 1;
W(nrW,:) = p;
end
end
function [hv] = hypervolume2(A, bounds)
%%get hypervolume for 2D
hv = 0;
[nrA, dim] = size(A);
if nrA == 0
return;
end
A = sortrows(A, dim);
for i=1:nrA-1
hv = hv + (A(i+1, 2) - A(i, 2)) * (bounds(1) - A(i,1));
end
hv = hv + (bounds(2) - A(nrA, 2)) * (bounds(1) - A(nrA,1));
end