-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility_scores.m
More file actions
347 lines (289 loc) · 14.7 KB
/
utility_scores.m
File metadata and controls
347 lines (289 loc) · 14.7 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
clear; clc; close all;
numLinks = 6;
Thr_max = 100e6;
simulationTimeSteps = 1;
ber_log10_mean = -5;
ber_log10_std = 1;
plr_mean = 0.01;
plr_std = 0.01;
thr_factor_mean = 0.7;
thr_factor_std = 0.3;
fprintf('--- Simulation Configuration ---\n');
fprintf('Number of Links: %d\n', numLinks);
fprintf('Maximum Throughput (Thr_max): %.2f Mbps\n', Thr_max/1e6);
fprintf('Link BER (log10) Distribution: N(%.1f, %.1f^2)\n', ber_log10_mean, ber_log10_std);
fprintf('Link PLR Distribution: N(%.4f, %.4f^2)\n', plr_mean, plr_std);
fprintf('Link Throughput Factor Distribution: N(%.2f, %.2f^2)\n', thr_factor_mean, thr_factor_std);
fprintf('-----------------------------\n\n');
scenarios = {};
dataTypes_s1 = [];
dataTypes_s1(1).Name = 'Alarm';
dataTypes_s1(1).P_req = 0.01; dataTypes_s1(1).B_req = 1e-4; dataTypes_s1(1).L_req = 0.5;
dataTypes_s1(1).Size = 1*128*8; dataTypes_s1(1).Weights = [0.5 0.4 0.1];
dataTypes_s1(2).Name = 'Picture';
dataTypes_s1(2).P_req = 0.04; dataTypes_s1(2).B_req = 2e-3; dataTypes_s1(2).L_req = 2.0;
dataTypes_s1(2).Size = 1024*768*3*8; dataTypes_s1(2).Weights = [0.2 0.25 0.55];
dataTypes_s1(3).Name = 'Sensor';
dataTypes_s1(3).P_req = 0.02; dataTypes_s1(3).B_req = 1e-3; dataTypes_s1(3).L_req = 5.0;
dataTypes_s1(3).Size = 512*8; dataTypes_s1(3).Weights = [0.4 0.4 0.2];
dataTypes_s1(4).Name = 'Text';
dataTypes_s1(4).P_req = 0.03; dataTypes_s1(4).B_req = 1e-3; dataTypes_s1(4).L_req = 1.0;
dataTypes_s1(4).Size = 5*64*8; dataTypes_s1(4).Weights= [0.35 0.35 0.3];
scenarios{end+1} = struct('Name', 'Baseline Mix Scenario', 'DataTypes', dataTypes_s1);
plot_colors = jet(numLinks);
BER_IDX = 1;
PLR_IDX = 2;
THR_IDX = 3;
UNAVAILABLE_FOR_SELECTION_UTILITY = -Inf;
for s_idx = 1:length(scenarios)
current_scenario = scenarios{s_idx};
scenario_name = current_scenario.Name;
dataTypes = current_scenario.DataTypes;
numDataTypes = numel(dataTypes);
fprintf('\n\n====================================================\n');
fprintf(' Running Scenario: %s (%d Data Types)\n', scenario_name, numDataTypes);
fprintf('====================================================\n\n');
for t = 1:simulationTimeSteps
fprintf('======= Time Step %d (for Scenario: %s) =======\n', t, scenario_name);
fprintf('Generating quality parameters for %d links:\n', numLinks);
rng('shuffle');
linkQuality = zeros(numLinks, 3);
for linkIdx = 1:numLinks
log10_ber = ber_log10_mean + ber_log10_std * randn();
currentBER = 10^log10_ber;
currentBER = max(1e-9, min(0.5, currentBER));
currentPLR = plr_mean + plr_std * randn();
currentPLR = max(1e-6, min(0.99, currentPLR));
thr_factor = thr_factor_mean + thr_factor_std * randn();
thr_factor_clamped = max(0.01, min(1.0, thr_factor));
currentThr = Thr_max * thr_factor_clamped;
currentThr = max(1, currentThr);
linkQuality(linkIdx, :) = [currentBER, currentPLR, currentThr];
fprintf(' Link %d: BER=%.3e, PLR=%.4f, Thr=%.2f Mbps\n', ...
linkIdx, currentBER, currentPLR, currentThr/1e6);
end
fprintf('Link parameter generation complete.\n\n');
utilities_all = nan(numLinks, numDataTypes);
chosenLinks = nan(1, numDataTypes);
availableLinks = true(1, numLinks);
fprintf('--- Starting Link Assignment by Priority for Scenario: %s ---\n', scenario_name);
for dt = 1:numDataTypes
info = dataTypes(dt);
fprintf('\n[Data Type: %s (Priority %d)]\n', info.Name, dt);
fprintf(' Requirements: PLR <= %.4f, BER <= %.3e, Latency <= %.2f s\n', info.P_req, info.B_req, info.L_req);
fprintf(' Weights: w_PLR=%.2f, w_BER=%.2f, w_Thr=%.2f\n', info.Weights(1), info.Weights(2), info.Weights(3));
fprintf(' Data Size: %.0f bits (%.2f KB)\n', info.Size, info.Size/(8*1024));
P_req = info.P_req; B_req = info.B_req; L_req = info.L_req;
Weights = info.Weights; DataSize = info.Size;
fprintf(' Evaluating utility of links:\n');
current_utilities_for_dt = zeros(numLinks, 1);
for linkIdx = 1:numLinks
currentBER = linkQuality(linkIdx, BER_IDX);
currentPLR = linkQuality(linkIdx, PLR_IDX);
currentThr = linkQuality(linkIdx, THR_IDX);
T_trans = inf;
if currentThr > 0
T_trans = DataSize / currentThr;
end
latencyOK = (T_trans <= L_req);
plrOK = (currentPLR <= P_req);
berOK = (currentBER <= B_req);
fprintf(' Link %d: BER=%.3e, PLR=%.4f, Thr=%.2f Mbps. ', linkIdx, currentBER, currentPLR, currentThr/1e6);
fprintf('T_trans = %.4f s. Req: PLR<=%.4f, BER<=%.3e, Latency<=%.2f s\n', ...
T_trans, P_req, B_req, L_req);
if ~latencyOK || ~plrOK || ~berOK
fprintf(' Requirement NOT met (LatencyOK: %d, PLR_OK: %d, BER_OK: %d). Link Unsuitable. Score = 0.\n', ...
latencyOK, plrOK, berOK);
current_utilities_for_dt(linkIdx) = 0;
else
fprintf(' All requirements met. Calculating utility [0, 1].\n');
if P_req > 0, fR = 1 - (currentPLR / P_req); else fR = 1.0; end
fR = max(0, min(1, fR));
if B_req > 0, fB = 1 - (currentBER / B_req); else fB = 1.0; end
fB = max(0, min(1, fB));
fT = currentThr / Thr_max;
fT = min(max(fT, 0), 1);
fprintf(' Component Scores (met reqs): f_PLR = %.3f, f_BER = %.3f, f_Thr = %.3f\n', fR, fB, fT);
util = Weights(1)*fR + Weights(2)*fB + Weights(3)*fT;
util = max(0, min(1, util));
fprintf(' Total Utility U = %.4f\n', util);
current_utilities_for_dt(linkIdx) = util;
end
end
utilities_all(:, dt) = current_utilities_for_dt;
fprintf(' Utility evaluation complete.\n');
utilities_available_for_selection = current_utilities_for_dt;
utilities_available_for_selection(~availableLinks) = UNAVAILABLE_FOR_SELECTION_UTILITY;
[maxU, bestLink] = max(utilities_available_for_selection);
if maxU > 0
chosenLinks(dt) = bestLink;
availableLinks(bestLink) = false;
fprintf(' Selected Link %d (Utility = %.4f) for %s.\n', bestLink, maxU, info.Name);
else
if maxU == 0
fprintf(' Warning: No available link found with utility > 0 for %s. Best available link(s) scored 0.\n', info.Name);
else
fprintf(' Warning: No available link found for %s.\n', info.Name);
end
chosenLinks(dt) = NaN;
fprintf(' No suitable link assigned for %s.\n', info.Name);
end
end
fprintf('\n--- Link Assignment Summary for Scenario: %s ---\n', scenario_name);
for dt_res = 1:numDataTypes
if ~isnan(chosenLinks(dt_res))
fprintf('Data Type "%s" assigned to Link %d\n', dataTypes(dt_res).Name, chosenLinks(dt_res));
else
fprintf('Data Type "%s" could not be assigned a link\n', dataTypes(dt_res).Name);
end
end
fprintf('------------------------------------------------------\n\n');
validUtilitySum_dyn = 0;
validUtilityCount_dyn = 0;
for dt_eval = 1:numDataTypes
selectedLink = chosenLinks(dt_eval);
if ~isnan(selectedLink)
u = utilities_all(selectedLink, dt_eval);
if ~isnan(u)
validUtilitySum_dyn = validUtilitySum_dyn + u;
validUtilityCount_dyn = validUtilityCount_dyn + 1;
end
end
end
if validUtilityCount_dyn > 0
AvgUtility_dyn = validUtilitySum_dyn / validUtilityCount_dyn;
else
AvgUtility_dyn = 0;
end
validUtilitySum_baseline = 0;
validUtilityCount_baseline = 0;
baselineChosenLinks = nan(1, numDataTypes);
for dt = 1:numDataTypes
info = dataTypes(dt);
linkIdx = dt;
if linkIdx > numLinks
continue;
end
ber = linkQuality(linkIdx, BER_IDX);
plr = linkQuality(linkIdx, PLR_IDX);
thr = linkQuality(linkIdx, THR_IDX);
T_trans = info.Size / thr;
if plr <= info.P_req && ber <= info.B_req && T_trans <= info.L_req
if info.P_req > 0, fR = 1 - (plr / info.P_req); else fR = 1.0; end
fR = max(0, min(1, fR));
if info.B_req > 0, fB = 1 - (ber / info.B_req); else fB = 1.0; end
fB = max(0, min(1, fB));
fT = thr / Thr_max;
fT = max(0, min(1, fT));
u = info.Weights(1)*fR + info.Weights(2)*fB + info.Weights(3)*fT;
u = max(0, min(1, u));
validUtilitySum_baseline = validUtilitySum_baseline + u;
validUtilityCount_baseline = validUtilityCount_baseline + 1;
baselineChosenLinks(dt) = linkIdx;
end
end
if validUtilityCount_baseline > 0
AvgUtility_baseline = validUtilitySum_baseline / validUtilityCount_baseline;
else
AvgUtility_baseline = 0;
end
fprintf('>>> [System Utility] Your Dynamic Selection Strategy: %.4f\n', AvgUtility_dyn);
fprintf('>>> [System Utility] Baseline (Sequential Assignment): %.4f\n', AvgUtility_baseline);
figure('Name', 'System-wide Utility Comparison', 'Position', [300 400 500 400]);
bar([AvgUtility_baseline, AvgUtility_dyn], 0.5);
set(gca, 'XTickLabel', {'Baseline', 'Dynamic'}, 'FontSize', 12);
ylabel('Average Utility Score', 'FontSize', 12);
title(sprintf('Scenario: %s - Strategy Comparison', scenario_name), 'FontSize', 14);
ylim([0 1]);
grid on;
fprintf('Generating new summary plots for Scenario: %s...\n', scenario_name);
dataTypeNames = cell(1, numDataTypes);
for i = 1:numDataTypes
dataTypeNames{i} = dataTypes(i).Name;
end
linkNames = cell(1, numLinks);
for i = 1:numLinks
linkNames{i} = sprintf('Link %d', i);
end
fig1_title = 'Utility Scores of Links per Data Type';
fig1 = figure('Name', fig1_title, 'Position', [50, 50, 900, 600]);
set(fig1, 'Color', 'w');
scores_to_plot = utilities_all';
b_scores = bar(scores_to_plot, 'grouped');
if numLinks <= size(plot_colors,1)
for k = 1:numLinks
if k <= length(b_scores)
b_scores(k).FaceColor = plot_colors(k,:);
end
end
end
set(gca, 'XTickLabel', dataTypeNames, 'FontSize', 11);
ylabel('Utility Score', 'FontSize', 12);
xlabel('Data Type', 'FontSize', 12);
title(fig1_title, 'FontSize', 14, 'Interpreter', 'none');
if numLinks > 0
legend(linkNames, 'Location', 'northwest', 'FontSize', 10);
end
grid on;
max_score_val = max(scores_to_plot(:), [], 'omitnan');
if isempty(max_score_val) || max_score_val == 0, max_score_val = 1; end
if isnan(max_score_val), max_score_val = 1; end
ylim_top = max(min(1.05 * max_score_val, 1.1), 0.1);
ylim([0, ylim_top]);
if numDataTypes > 1
current_ylim = ylim
for i = 1:(numDataTypes - 1)
separator_line_x_position = i + 0.5;
h_sep_line = line([separator_line_x_position separator_line_x_position], current_ylim, ...
'LineStyle', '--', 'Color', [0.5 0.5 0.5], 'LineWidth', 0.5);
h_sep_line.Annotation.LegendInformation.IconDisplayStyle = 'off';
end
end
fig2_title = 'Chosen Link per Data Type';
fig2 = figure('Name', fig2_title, 'Position', [1000, 50, 700, 500]);
set(fig2, 'Color', 'w');
chosenLinks_for_plot = chosenLinks;
chosenLinks_for_plot(isnan(chosenLinks_for_plot)) = 0;
if size(chosenLinks_for_plot,1) > 1 && size(chosenLinks_for_plot,2) > 1
chosenLinks_for_plot = chosenLinks_for_plot(1,:);
end
b_chosen = bar(1:numDataTypes, chosenLinks_for_plot, 0.5);
if ~isempty(b_chosen)
b_chosen.FaceColor = [0.25, 0.41, 0.88];
end
set(gca, 'XTick', 1:numDataTypes, 'XTickLabel', dataTypeNames, 'FontSize', 11);
ylabel('Chosen Link ID', 'FontSize', 12);
xlabel('Data Type', 'FontSize', 12);
title(fig2_title, 'FontSize', 14, 'Interpreter', 'none');
grid on;
if numLinks > 0
min_y_tick = 0;
if all(chosenLinks_for_plot > 0) && ~any(isnan(chosenLinks))
min_y_tick = 1;
end
y_ticks_vec = unique([min_y_tick, 1:numLinks]);
if isempty(y_ticks_vec) || numLinks == 0, y_ticks_vec = [0 1]; end
ylim([min_y_tick - 0.5, numLinks + 0.5]);
set(gca, 'YTick', y_ticks_vec);
else
ylim([0,1]);
end
if ~isempty(chosenLinks_for_plot)
for i = 1:length(chosenLinks_for_plot)
actual_chosen_link = chosenLinks(i);
plot_value = chosenLinks_for_plot(i);
if ~isnan(actual_chosen_link) && actual_chosen_link > 0
text(i, plot_value, num2str(actual_chosen_link), ...
'HorizontalAlignment','center', 'VerticalAlignment','bottom', ...
'FontSize',10, 'Color','k');
elseif isnan(actual_chosen_link)
text(i, plot_value, 'N/A', ...
'HorizontalAlignment','center', 'VerticalAlignment','bottom', ...
'FontSize',10, 'Color','k');
end
end
end
fprintf('New summary plots generation complete for Scenario: %s.\n', scenario_name);
end
end
fprintf('\nSimulation Finished.\n');