-
Notifications
You must be signed in to change notification settings - Fork 2
/
LeaderSelection.m
275 lines (190 loc) · 8.95 KB
/
LeaderSelection.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
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
classdef LeaderSelection < Ranking
properties
AR = []; % rank vector
riskRank = []; % risk-rank vector
nonDomIdxAR = [];
nonDomDimension = [];
end
methods
function obj = LeaderSelection(type)
obj@Ranking(type);
%obj.logger.info('LeaderSelection', '############### start LeaderSelection #######################');
end
function retMatrix = normalizeDimensions(~, matrix)
% move cost is always in [0, 1];
for k = 1:1:size(matrix, 2)
if min(matrix(:, k)) == max(matrix(:, k))
retMatrix(:, k) = matrix(:, k);
continue;
end
retMatrix(:, k) = (matrix(:, k) - min(matrix(:, k))) / (max(matrix(:, k)) - min(matrix(:, k)));
end
end
function particle = run(obj)
if isempty(obj.dimensions)
obj.createDimensions();
end
%obj.calculateRanks();
if sum( range( obj.dimensions ) ) == 0
obj.bestIdx = round(getRandom(1, size(obj.dimensions, 1)));
%obj.logger.info('LeaderSelection/run', sprintf('pick (random): p%i', obj.bestIdx));
elseif strcmp(obj.type, obj.RT_STD)
%obj.logger.info('LeaderSelection/run', 'standard ranking');
obj.standardRanking();
elseif strcmp(obj.type, obj.RT_RS)
%obj.logger.info('LeaderSelection/run', ['risk ranking with risk = ' num2str(obj.riskval)]);
%obj.standardRanking();
obj.riskSelectionRanking();
else
error(['Ranking type not known: ' obj.type]);
end
if size(obj.particles, 2) > 0
particle = obj.particles(obj.bestIdx);
end
end
function calculateRanks(obj)
N = size(obj.dimensions, 1); % N = number of solutions
nbrDim = size(obj.dimensions, 2); % nbrDim = number of dimensions
obj.AR = ones(N, 1);
for i = 1:1:N
for j = 1:1:N
if i == j
continue;
end
% j is dominated by i
j_is_dominated = true;
for k = 1:1:nbrDim
if obj.dimensions(i, k) >= obj.dimensions(j, k)
j_is_dominated = false;
end
end
if j_is_dominated == true
%fprintf('%i is dominated by %i\n', j, i);
obj.AR(j) = obj.AR(j) + 1;
end
end
end
end
function calculateRiskRank(obj)
obj.riskRank = zeros(size(obj.dimensions, 1), 1);
normDimensions = obj.normalizeDimensions(obj.dimensions);
for i = 1:1:size(normDimensions, 1)
% rank = risk * profit + (1 - risk) * movecost
% => profit is a negativ value
obj.riskRank( i, 1 ) = obj.riskval * normDimensions(i, 2) + (1 - obj.riskval) * normDimensions(i, 1);
%obj.logger.info('LeaderSelection/calcualteRiskRank', sprintf('AR(%i)=%f, riskRank(%i)=%f', i, obj.AR(i), i, obj.riskRank(i)));
end
end
function standardRanking(obj)
obj.calculateRanks();
if size(obj.AR, 1) == 0
error('No AR array for standard ranking!');
end
[valMinAR, idxMinAR] = min(obj.AR);
if sum(obj.AR == valMinAR) == 1 % only one min-element
obj.bestIdx = idxMinAR;
else % more than one min-element
% idx´s of min-values from obj.AR
obj.nonDomIdxAR = obj.getMinIdx(obj.AR);
obj.bestIdx = obj.nonDomIdxAR( round( getRandom(1, size(obj.nonDomIdxAR, 1)) ), 1 );
end
%obj.logger.info('LeaderSelection/run', sprintf('pick p%i: dim=[%s], rank=%f', obj.bestIdx, sprintf('%f;', obj.dimensions(obj.bestIdx,:)), obj.AR(obj.bestIdx)));
end
function riskSelectionRanking(obj)
obj.calculateRiskRank();
[valMinRiskRank, idxMinRiskRank] = min(obj.riskRank);
if sum(obj.riskRank == valMinRiskRank) == 1 % only one min-element
obj.bestIdx = idxMinRiskRank;
else % more than one min-element
% idx´s of min-values from obj.riskRank
minIdxRiskRanks = obj.getMinIdx(obj.riskRank);
obj.bestIdx = minIdxRiskRanks( round( getRandom(1, size(minIdxRiskRanks, 1)) ), 1);
end
logtxt = sprintf('pick p%i: dim=[%s], riskRank=%f', obj.bestIdx, sprintf('%f;', obj.dimensions(obj.bestIdx,:)), obj.riskRank(obj.bestIdx));
obj.logger.info('LeaderSelection/run', logtxt);
end
function minIdx = getMinIdx(~, elements, minVal)
if nargin < 3
[minVal, ~] = min(elements);
end
minIdx = [];
nextIdx = 1;
for i = 1:1:size(elements, 1) % get all non-dominated solutions
if elements(i, 1) == minVal
minIdx(nextIdx, 1) = i;
nextIdx = nextIdx + 1;
end
end
end
function printResult(obj)
if isempty(obj.AR)
error('Distance Rank is not computed!');
end
for d = 1:1:size(obj.dimensions, 1)
fprintf('dimension = [%s] ', sprintf('%f;',obj.dimensions(d,:)));
fprintf('Rank = %f\n', obj.AR(d));
end
end
function plotDimensions2D(obj, d1, d2, newfigure)
if nargin < 4
newfigure = false;
end
if nargin < 3
d2 = 2;
end
if nargin < 2
d1 = 1;
end
if newfigure == true
figure;
end
%color = linspace(min(obj.dimensions(:,d1)),max(obj.dimensions(:,d1)),length(obj.dimensions(:,d1)));
scatter(obj.dimensions(:,d1), obj.dimensions(:,d2), 'filled', 'LineWidth', 0.5, 'markerfacecolor', 'b', 'MarkerEdgeColor', 'k');
hold on;
title(['risk=' num2str(obj.riskval)]);
xlabel(obj.dimensionDescription{d1});
ylabel(obj.dimensionDescription{d2});
if size(obj.nonDomIdxAR, 1) > 0
for i = obj.nonDomIdxAR
scatter(obj.dimensions(i, d1), obj.dimensions(i, d2), 'markerfacecolor', 'g', 'MarkerEdgeColor', 'k');
end
end
if obj.bestIdx > 0
scatter(obj.dimensions(obj.bestIdx, d1), obj.dimensions(obj.bestIdx, d2), 'markerfacecolor', 'r', 'MarkerEdgeColor', 'r');
end
hold off;
end
function plotDimensions3D(obj, d1, d2, d3, newfigure)
if nargin < 5
newfigure = false;
end
if nargin < 4
d3 = 3;
end
if nargin < 3
d2 = 2;
end
if nargin < 2
d1 = 1;
end
if newfigure == true
figure;
end
%color = linspace(min(obj.dimensions(:,d1)),max(obj.dimensions(:,d1)),length(obj.dimensions(:,d1)));
scatter3(obj.dimensions(:,d1), obj.dimensions(:,d2), obj.dimensions(:,d3), 'filled', 'LineWidth', 0.5, 'markerfacecolor', 'b', 'MarkerEdgeColor', 'k');
hold on;
title(['risk=' num2str(obj.riskval)]);
xlabel(obj.dimensionDescription{d1});
ylabel(obj.dimensionDescription{d2});
if size(obj.nonDomIdxAR, 1) > 0
for i = obj.nonDomIdxAR
scatter3(obj.dimensions(i, d1), obj.dimensions(i, d2), obj.dimensions(i, d3), 'markerfacecolor', 'g', 'MarkerEdgeColor', 'k');
end
end
if obj.bestIdx > 0
scatter3(obj.dimensions(obj.bestIdx, d1), obj.dimensions(obj.bestIdx, d2), obj.dimensions(obj.bestIdx, d3), 'markerfacecolor', 'r', 'MarkerEdgeColor', 'r');
end
hold off;
end
end
end