forked from IITP-Load-Balancing/V2V-and-V2I-task-offloading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDGIST_getOffloadingDecision_with_cloud.m
366 lines (341 loc) · 18.4 KB
/
DGIST_getOffloadingDecision_with_cloud.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
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
360
361
362
363
364
365
366
function [DGIST_entities, DGIST_isOffloading] = DGIST_getOffloadingDecision_with_cloud(HBNU_prev_timeslot_capacity, t, simParams, DGIST_entities)
num_of_egos = length(DGIST_entities.trucks);
arrivals = [];
offloads = [];
if simParams.V2V_type == 0
%% Set RSU association
for i = 1:num_of_egos
associated_RSU_id = simParams.targetRsuID(i);
% DGIST_entities.trucks(i).truck.associatedRSU(t, 1) = find(simParams.idRSUs == associated_RSU_id);
% associated_RSU_id = 9; % Temporal...
DGIST_entities.trucks(i).truck.associatedRSU(t, 1) = find(simParams.idRSUs == associated_RSU_id);
end
for j = 1:simParams.numOfRSU
for i = 1:num_of_egos
associated_RSU_idx = DGIST_entities.trucks(i).truck.associatedRSU(t, 1);
DGIST_entities.rsus(j).rsu.associated_trucks(t, i) = associated_RSU_idx;
end
end
%% Make decisions on offloading
for i = 1:num_of_egos
arrival = normrnd(simParams.avg_arrival, simParams.std_arrival);
while arrival < 0
arrival = normrnd(simParams.avg_arrival, simParams.std_arrival);
end
arrivals = [arrivals, arrival];
if t == 1
offload = simParams.offloadBytesSize * 8;
DGIST_entities.trucks(i).truck.prev_possible_capacity(t, 1) = 1;
else
offload = simParams.offloadBytesSize * 8 * DGIST_entities.trucks(i).truck.prev_possible_capacity(t - 1, 1);
end
offloads = [offloads, offload];
DGIST_entities.trucks(i).truck.arrival(t, 1) = arrival;
DGIST_entities.trucks(i).truck.offload(t, 1) = offload;
idx_of_associated_rsu = DGIST_entities.trucks(i).truck.associatedRSU(t, 1);
i_truck_q = DGIST_entities.trucks(i).truck.processing_queue(t, 1);
j_rsu_q = DGIST_entities.rsus(idx_of_associated_rsu).rsu.processing_queue(t, 1);
cloud_q = DGIST_entities.cloud.processing_queue(t, 1);
%% Algorithm %%% with CLOUD
% Case 1 % OBU에서 처리하는 경우
min_fun = @(x) simParams.V * (simParams.alpha * x^3 + simParams.beta) - (x / simParams.gamma - arrivals(i)) * i_truck_q;
if i==1 || i==3
selected_frequency = fminbnd(min_fun, simParams.min_S_high, simParams.max_S_high);
solution_case1 = min_fun(selected_frequency);
elseif i==2 || i==4
selected_frequency = fminbnd(min_fun, simParams.min_S_low, simParams.max_S_low);
solution_case1 = min_fun(selected_frequency);
end
% Case 2 % rsu에서 처리하는 경우
solution_case2 = 0.7*simParams.V * simParams.network_power - (offloads(i) - arrivals(i)) * i_truck_q + offloads(i) * j_rsu_q;
% Case 3 % cloud에서 처리하는 경우
solution_case3 = 0.7*simParams.V * simParams.network_power - (offloads(i) - arrivals(i)) * i_truck_q + offloads(i) * cloud_q;
%% Find the best solution
solutions = [solution_case1, solution_case2, solution_case3];
idx_of_solution = find(solutions == min(solutions));
switch idx_of_solution(1)
case 1
% Local processing
DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) = 0;
DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) = 0;
DGIST_entities.trucks(i).truck.selected_frequency(t, 1) = selected_frequency;
case 2
% RSU processing
DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) = 1;
DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) = 0;
DGIST_entities.trucks(i).truck.selected_frequency(t, 1) = 0;
case 3
% Cloud processing
DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) = 0;
DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) = 1;
DGIST_entities.trucks(i).truck.selected_frequency(t, 1) = 0;
end
end
DGIST_isOffloading = zeros(1, num_of_egos);
for i = 1:num_of_egos
idx_of_associated_rsu = DGIST_entities.trucks(i).truck.associatedRSU(t, 1);
if DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) == 1 || DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) == 1
DGIST_isOffloading(i) = 1;
else
DGIST_isOffloading(i) = 0;
end
end
elseif simParams.V2V_type == 1
%% Set RSU association
for i = 1:num_of_egos
associated_RSU_id = simParams.targetRsuID(i);
% DGIST_entities.trucks(i).truck.associatedRSU(t, 1) = find(simParams.idRSUs == associated_RSU_id);
% associated_RSU_id = 9; % Temporal...
DGIST_entities.trucks(i).truck.associatedRSU(t, 1) = find(simParams.idRSUs == associated_RSU_id);
end
for j = 1:simParams.numOfRSU
for i = 1:num_of_egos
associated_RSU_idx = DGIST_entities.trucks(i).truck.associatedRSU(t, 1);
DGIST_entities.rsus(j).rsu.associated_trucks(t, i) = associated_RSU_idx;
end
end
%% Make decisions on offloading
for i = 1:num_of_egos
arrival = normrnd(simParams.avg_arrival, simParams.std_arrival);
while arrival < 0
arrival = normrnd(simParams.avg_arrival, simParams.std_arrival);
end
arrivals = [arrivals, arrival];
if t == 1
offload = simParams.offloadBytesSize * 8;
DGIST_entities.trucks(i).truck.prev_possible_capacity(t, 1) = 1;
else
offload = simParams.offloadBytesSize * 8 * DGIST_entities.trucks(i).truck.prev_possible_capacity(t - 1, 1);
end
offloads = [offloads, offload];
DGIST_entities.trucks(i).truck.arrival(t, 1) = arrival;
DGIST_entities.trucks(i).truck.offload(t, 1) = offload;
idx_of_associated_rsu = DGIST_entities.trucks(i).truck.associatedRSU(t, 1);
i_truck_q = DGIST_entities.trucks(i).truck.processing_queue(t, 1);
j_rsu_q = DGIST_entities.rsus(idx_of_associated_rsu).rsu.processing_queue(t, 1);
cloud_q = DGIST_entities.cloud.processing_queue(t, 1);
%% Algorithm %%% with CLOUD & other vehicles
% Case 1 % OBU에서 처리하는 경우
min_fun = @(x) simParams.V * (simParams.alpha * x^3 + simParams.beta) - (x / simParams.gamma - arrivals(i)) * i_truck_q;
if i==1 || i==3
selected_frequency = fminbnd(min_fun, simParams.min_S_high, simParams.max_S_high);
solution_case1 = min_fun(selected_frequency);
elseif i==2 || i==4
selected_frequency = fminbnd(min_fun, simParams.min_S_low, simParams.max_S_low);
solution_case1 = min_fun(selected_frequency);
end
% Case 2 % rsu에서 처리하는 경우
solution_case2 = 0.7*simParams.V * simParams.network_power - (offloads(i) - arrivals(i)) * i_truck_q + offloads(i) * j_rsu_q;
% Case 3 % cloud에서 처리하는 경우
solution_case3 = 0.7*simParams.V * simParams.network_power - (offloads(i) - arrivals(i)) * i_truck_q + offloads(i) * cloud_q;
% Case 4 % other vehicle에서 처리하는 경우
solution_case4_list = zeros(1,4); % 오프로딩 받을 vehicle 계산 위해서
for j = 1:num_of_egos
j_truck_q = DGIST_entities.trucks(j).truck.processing_queue(t, 1);
if i == j
solution_case4_list(j) = inf;
continue;
end
solution_case4_list(j) = 0.5*simParams.V*simParams.network_power - (offloads(i) - arrivals(i)) * i_truck_q + 0.5*offloads(i) * j_truck_q;
end
DGIST_entities.trucks(i).truck.offload_decision_to_vehicle(t,1) = find(solution_case4_list == min(solution_case4_list),1);
solution_case4 = min(solution_case4_list);
%% Find the best solution
solutions = [solution_case1, solution_case2, solution_case3, solution_case4];
idx_of_solution = find(solutions == min(solutions));
switch idx_of_solution(1)
case 1
% Local processing
DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) = 0;
DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) = 0;
DGIST_entities.trucks(i).truck.selected_frequency(t, 1) = selected_frequency;
DGIST_entities.trucks(i).truck.offload_decision_to_V2V(t,1) = 0;
case 2
% RSU processing
DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) = 1;
DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) = 0;
DGIST_entities.trucks(i).truck.selected_frequency(t, 1) = 0;
DGIST_entities.trucks(i).truck.offload_decision_to_V2V(t,1) = 0;
case 3
% Cloud processing
DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) = 0;
DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) = 1;
DGIST_entities.trucks(i).truck.selected_frequency(t, 1) = 0;
DGIST_entities.trucks(i).truck.offload_decision_to_V2V(t,1) = 0;
case 4
% V2V processing
DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) = 0;
DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) = 0;
DGIST_entities.trucks(i).truck.selected_frequency(t, 1) = 0;
DGIST_entities.trucks(i).truck.offload_decision_to_V2V(t,1) = 1;
end
end
DGIST_isOffloading = zeros(1, num_of_egos);
for i = 1:num_of_egos
idx_of_associated_rsu = DGIST_entities.trucks(i).truck.associatedRSU(t, 1);
if DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) == 1 || DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) == 1 || DGIST_entities.trucks(i).truck.offload_decision_to_V2V(t,1) == 1
DGIST_isOffloading(i) = 1;
else
DGIST_isOffloading(i) = 0;
end
end
end
% %% Set RSU association
% for i = 1:num_of_egos
% associated_RSU_id = simParams.targetRsuID(i);
% % DGIST_entities.trucks(i).truck.associatedRSU(t, 1) = find(simParams.idRSUs == associated_RSU_id);
% % associated_RSU_id = 9; % Temporal...
% DGIST_entities.trucks(i).truck.associatedRSU(t, 1) = find(simParams.idRSUs == associated_RSU_id);
% end
%
% for j = 1:simParams.numOfRSU
% for i = 1:num_of_egos
% associated_RSU_idx = DGIST_entities.trucks(i).truck.associatedRSU(t, 1);
% DGIST_entities.rsus(j).rsu.associated_trucks(t, i) = associated_RSU_idx;
% end
% end
%
% %% Make decisions on offloading
% for i = 1:num_of_egos
% arrival = normrnd(simParams.avg_arrival, simParams.std_arrival);
% while arrival < 0
% arrival = normrnd(simParams.avg_arrival, simParams.std_arrival);
% end
% arrivals = [arrivals, arrival];
%
% if t == 1
% offload = simParams.offloadBytesSize * 8;
% DGIST_entities.trucks(i).truck.prev_possible_capacity(t, 1) = 1;
% else
% offload = simParams.offloadBytesSize * 8 * DGIST_entities.trucks(i).truck.prev_possible_capacity(t - 1, 1);
% end
% offloads = [offloads, offload];
%
% DGIST_entities.trucks(i).truck.arrival(t, 1) = arrival;
% DGIST_entities.trucks(i).truck.offload(t, 1) = offload;
%
% idx_of_associated_rsu = DGIST_entities.trucks(i).truck.associatedRSU(t, 1);
% i_truck_q = DGIST_entities.trucks(i).truck.processing_queue(t, 1);
% j_rsu_q = DGIST_entities.rsus(idx_of_associated_rsu).rsu.processing_queue(t, 1);
% cloud_q = DGIST_entities.cloud.processing_queue(t, 1);
%
% %% Algorithm %%% with CLOUD
% % Case 1 % OBU에서 처리하는 경우
% min_fun = @(x) simParams.V * (simParams.alpha * x^3 + simParams.beta) - (x / simParams.gamma - arrivals(i)) * i_truck_q;
% if i==1 || i==3
% selected_frequency = fminbnd(min_fun, simParams.min_S_high, simParams.max_S_high);
% solution_case1 = min_fun(selected_frequency);
% elseif i==2 || i==4
% selected_frequency = fminbnd(min_fun, simParams.min_S_low, simParams.max_S_low);
% solution_case1 = min_fun(selected_frequency);
% end
%
% % Case 2 % rsu에서 처리하는 경우
% solution_case2 = simParams.V * simParams.network_power - (offloads(i) - arrivals(i)) * i_truck_q + offloads(i) * j_rsu_q;
%
% % Case 3 % cloud에서 처리하는 경우
% solution_case3 = simParams.V * simParams.network_power - (offloads(i) - arrivals(i)) * i_truck_q + offloads(i) * cloud_q;
%
% %% Find the best solution
% solutions = [solution_case1, solution_case2, solution_case3];
% idx_of_solution = find(solutions == min(solutions));
%
% switch idx_of_solution(1)
% case 1
% % Local processing
% DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) = 0;
% DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) = 0;
% DGIST_entities.trucks(i).truck.selected_frequency(t, 1) = selected_frequency;
% case 2
% % RSU processing
% DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) = 1;
% DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) = 0;
% DGIST_entities.trucks(i).truck.selected_frequency(t, 1) = 0;
% case 3
% % Cloud processing
% DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) = 0;
% DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) = 1;
% DGIST_entities.trucks(i).truck.selected_frequency(t, 1) = 0;
% end
% end
%
% DGIST_isOffloading = zeros(1, num_of_egos);
% for i = 1:num_of_egos
% idx_of_associated_rsu = DGIST_entities.trucks(i).truck.associatedRSU(t, 1);
% if DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) == 1 || DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) == 1
% DGIST_isOffloading(i) = 1;
% else
% DGIST_isOffloading(i) = 0;
% end
% end
% %% Algorithm %%% with CLOUD & other vehicles
% % Case 1 % OBU에서 처리하는 경우
% min_fun = @(x) simParams.V * (simParams.alpha * x^3 + simParams.beta) - (x / simParams.gamma - arrivals(i)) * i_truck_q;
% if i==1 || i==3
% selected_frequency = fminbnd(min_fun, simParams.min_S_high, simParams.max_S_high);
% solution_case1 = min_fun(selected_frequency);
% elseif i==2 || i==4
% selected_frequency = fminbnd(min_fun, simParams.min_S_low, simParams.max_S_low);
% solution_case1 = min_fun(selected_frequency);
% end
%
% % Case 2 % rsu에서 처리하는 경우
% solution_case2 = simParams.V * simParams.network_power - (offloads(i) - arrivals(i)) * i_truck_q + offloads(i) * j_rsu_q;
%
% % Case 3 % cloud에서 처리하는 경우
% solution_case3 = simParams.V * simParams.network_power - (offloads(i) - arrivals(i)) * i_truck_q + offloads(i) * cloud_q;
%
% % Case 4 % other vehicle에서 처리하는 경우
% solution_case4_list = zeros(1,4); % 오프로딩 받을 vehicle 계산 위해서
% for j = 1:num_of_egos
% j_truck_q = DGIST_entities.trucks(j).truck.processing_queue(t, 1);
% if i == j
% solution_case4_list(j) = inf;
% continue;
% end
% solution_case4_list(j) = 0.5*simParams.V*simParams.network_power - (offloads(i) - arrivals(i)) * i_truck_q + offloads(i) * j_truck_q;
% end
% DGIST_entities.trucks(i).truck.offload_decision_to_vehicle(t,1) = find(solution_case4_list == min(solution_case4_list),1);
% solution_case4 = min(solution_case4_list);
%
% %% Find the best solution
% solutions = [solution_case1, solution_case2, solution_case3, solution_case4];
% idx_of_solution = find(solutions == min(solutions));
%
% switch idx_of_solution(1)
% case 1
% % Local processing
% DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) = 0;
% DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) = 0;
% DGIST_entities.trucks(i).truck.selected_frequency(t, 1) = selected_frequency;
% DGIST_entities.trucks(i).truck.offload_decision_to_V2V(t,1) = 0;
% case 2
% % RSU processing
% DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) = 1;
% DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) = 0;
% DGIST_entities.trucks(i).truck.selected_frequency(t, 1) = 0;
% DGIST_entities.trucks(i).truck.offload_decision_to_V2V(t,1) = 0;
% case 3
% % Cloud processing
% DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) = 0;
% DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) = 1;
% DGIST_entities.trucks(i).truck.selected_frequency(t, 1) = 0;
% DGIST_entities.trucks(i).truck.offload_decision_to_V2V(t,1) = 0;
% case 4
% % V2V processing
% DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) = 0;
% DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) = 0;
% DGIST_entities.trucks(i).truck.selected_frequency(t, 1) = 0;
% DGIST_entities.trucks(i).truck.offload_decision_to_V2V(t,1) = 1;
% end
% end
%
% DGIST_isOffloading = zeros(1, num_of_egos);
% for i = 1:num_of_egos
% idx_of_associated_rsu = DGIST_entities.trucks(i).truck.associatedRSU(t, 1);
% if DGIST_entities.trucks(i).truck.offload_decision_to_RSU(t, idx_of_associated_rsu) == 1 || DGIST_entities.trucks(i).truck.offload_decision_to_cloud(t, 1) == 1 || DGIST_entities.trucks(i).truck.offload_decision_to_V2V(t,1) == 1
% DGIST_isOffloading(i) = 1;
% else
% DGIST_isOffloading(i) = 0;
% end
% end