clear; clc;
% ========== System Parameters Initialization ==========
I = 3; % Number of UAVs
J = 2; % Number of RSUs
M = 2; % Number of vehicles
T = 5; % Time steps
D = randi([4e6,18e6], T, I); % Data volume
S = randi([4e2,1e3], T, I); % Task size
sigma2 = 1e-9; % Noise power
B_mj = 1e6 * ones(J, M); % Bandwidth
Gamma_L = 0.5; % Utility parameter of completing low-priority task on time
Gamma_H = 1; % Utility parameter of completing low-priority task past the time
c = 0.1; % Attenuation coefficient
p_max = 0.8; % Maximum transmit power
eta_max = 2e10;% Resource allocation upper limit
max_iter = 20; % Maximum iterations
tol = 1e-3; % Convergence tolerance
delta = 1e9* (1 + 0.2 * rand(I, 1)); % UAV local computing resources (CPU Hz)
P = rand(T,I,J);% Line-of-sight probability
r_ij = 0.06e6 + (2.34e6 - 0.06e6) * rand(T,I,J);% Transmission power between UAV i and RSU j
r_im = 0.06e6 + (2.34e6 - 0.06e6) * rand(T,I,J,M);% Transmission power between UAV i and vehicle m
g_mj = 0.02 * ones(J,M);% Channel gain between vehicle m and RSU j
tau_i = zeros(T,I);
for t = 1:T
for i = 1: I
tau_i(t,i) = D(t,i) * S(t,i)/delta(i); % Task completion time threshold
end
end
v = randi([0,1], T,I); % Task priority (0: low, 1: high)
beta = max(tau_i(:)) * 2;%1e3; % Big-M constant
% ========== Proposed method ==========
% ========== Initialize Variables ==========
theta1_prev = 0.5 * ones(T,I); % Initial task offloading ratio
for t = 1:T
for i = 1:I
for j =1:J
for m =1 : M
z_mj1_prev(t,i,j,m) = (log(1 + (p_max * g_mj(j,m) / sigma2)) ) / log(2); % Initial auxiliary variable value
end
end
end
end
T_1_prev = tau_i;
eta1_prev = 0.5 * eta_max *ones(T,I,J);
for t = 1:T
for i = 1:I
for j = 1:J
lambda1(t,i,j) = (theta1_prev(t,i) * S(t,i) * D(t,i)) / eta1_prev(t,i,j);
end
end
end
% ========== SCA Iteration ==========
for k = 1:max_iter
% --- Build convex optimization problem for current iteration ---
cvx_begin quiet
variable theta1(T,I) % Optimization variable: task offloading ratio
variable z_mj1(T,I,J,M) % Auxiliary variable
variable eta1(T,I,J) % RSU resource allocation
variable p_mi1(T,I,M) % Vehicle transmit power
variable T_1(T,I);% Time required for task ki(t)
variable z1(T, I);% Big-M auxiliary variable
variable u1(T, I);
variable T_off1(T, I);
% --- Objective function definition ---
total_utility1 = 0;
for t = 1:T
for i = 1:I
term_local = (1 - theta1(t,i)) * S(t,i) * D(t,i) / delta(i);
T_1(t,i) >= term_local;
for j = 1:J
for m = 1:M
% Calculate Taylor-expanded C term
C = (theta1_prev(t,i)*D(t,i))/(B_mj(j, m)*z_mj1_prev(t,i,j,m)) + ...
(theta1(t,i) * D(t,i))/(B_mj(j, m)*z_mj1_prev(t,i,j,m)) - ...
(theta1_prev(t,i)*D(t,i))/(B_mj(j, m)*(z_mj1_prev(t,i,j,m))^2) * z_mj1(t,i,j,m);
G = P(t,i,j) * ((theta1(t,i) * D(t,i))*inv_pos(r_ij(t,i,j))) + (1 - P(t,i,j)) * (((theta1(t,i) * D(t,i))*inv_pos(r_im(t,i,j,m))) + C);
T_off1(t, i) <= G + theta1(t,i)*S(t,i)*D(t,i) - lambda1(t,i,j)*eta1(t,i,j);% Minimum offloading time
end
end
T_1(t,i) >= T_off1(t,i);
% Calculate task utility based on big-M method
% T_1(t,i)<= tau_i
f1 = v(t,i)*(log(1 + tau_i(t,i) - T_1(t,i))/log(2) + Gamma_L) + (1 - v(t,i))*Gamma_L;
% T_1(t,i)> tau_i
f2 = -v(t,i)*Gamma_H + (1-v(t,i))*Gamma_L * exp(-c*(T_1_prev(t,i) - tau_i(t,i))) ...
- c*(1-v(t,i))*Gamma_L * exp(-c*(T_1_prev(t,i) - tau_i(t,i))) * (T_1(t,i) - T_1_prev(t,i));
u1(t,i) <= f1 + beta * (1-z1(t,i))
u1(t,i) <= f2 + beta * z1(t,i)
total_utility1 = total_utility1 + u1(t,i);
end
end
maximize(total_utility1)
% --- Constraints ---
subject to
% Variable range constraints
0 <= theta1 <= 1;% Constraint C2
0 <= eta1 <= eta_max;% Constraint C3
% Constraint C4
for t = 1:T
for j = 1:J
sum(eta1(t,:,j)) <= eta_max;
end
end
0 <= p_mi1 <= p_max;% Constraint C5
% Constraint C6
for t = 1:T
for m = 1:M
sum(p_mi1(t,:,m)) <= p_max;
end
end
% Auxiliary variable constraints
for t = 1:T
for i = 1:I
for j =1:J
for m =1 : M
z_mj1(t,i,j,m) <= (log(1 + (p_mi1(t,i,m) * g_mj(j,m) / sigma2)) ) / log(2);
end
end
end
end
% Big-M method
for t = 1:T
for i = 1:I
T_1(t,i) <= tau_i(t,i) + beta * (1 - z1(t,i));
T_1(t,i) >= tau_i(t,i) - beta * z1(t,i);
% Relaxation
0<= z1(t,i) <= 1;
end
end
cvx_end
% --- Check convergence ---
if norm(theta1 - theta1_prev, 'fro') < tol
break;
end
% --- Save previous iteration variable values ---
theta1_prev = theta1;
z_mj1_prev = z_mj1;
T_1_prev = T_1;
eta1_prev = eta1;
% --- Update Dinkelbach parameters ---
for t = 1:T
for i = 1:I
for j = 1:J
lambda1(t,i,j) = (theta1(t,i) * S(t,i) * D(t,i)) / eta1(t,i,j);
end
end
end
end
% ========== Output Results ==========
fprintf('Proposed method total utility: %f\n', cvx_optval);
% ========== Traditional method ==========
% ========== Initialize Variables ==========
theta2_prev = 0.5 * ones(T,I); % Initial task offloading ratio
for t = 1:T
for i = 1:I
for j =1:J
for m =1 : M
z_mj2_prev(t,i,j,m) = (log(1 + (p_max * g_mj(j,m) / sigma2)) ) / log(2); % Initial auxiliary variable value
end
end
end
end
T_2_prev = tau_i;
eta2_prev = 0.5 * eta_max *ones(T,I,J);
for t = 1:T
for i = 1:I
for j = 1:J
lambda2(t,i,j) = (theta2_prev(t,i) * S(t,i) * D(t,i)) / eta2_prev(t,i,j);
end
end
end
% ========== SCA Iteration ==========
for k = 1:max_iter
% --- Build convex optimization problem for current iteration ---
cvx_begin quiet
variable theta2(T,I) % Optimization variable: task offloading ratio
variable z_mj2(T,I,J,M) % Auxiliary variable
variable eta2(T,I,J) % RSU resource allocation
variable p_mi2(T,I,M) % Vehicle transmit power
variable T_2(T,I);% Time required for task ki(t)
variable z2(T, I);% Big-M auxiliary variable
variable u2(T, I);
variable T_off2(T, I);
% --- Objective function definition ---
total_utility2 = 0;
for t = 1:T
for i = 1:I
term_local = (1 - theta2(t,i)) * S(t,i) * D(t,i) / delta(i);
T_2(t,i) >= term_local;
for j = 1:J
for m = 1:M
% Calculate Taylor-expanded C term
C = (theta2_prev(t,i)*D(t,i))/(B_mj(j, m)*z_mj2_prev(t,i,j,m)) + ...
(theta2(t,i) * D(t,i))/(B_mj(j, m)*z_mj2_prev(t,i,j,m)) - ...
(theta2_prev(t,i)*D(t,i))/(B_mj(j, m)*(z_mj2_prev(t,i,j,m))^2) * z_mj2(t,i,j,m);
G = P(t,i,j) * ((theta2(t,i) * D(t,i))*inv_pos(r_ij(t,i,j))) + (1 - P(t,i,j)) * (((theta2(t,i) * D(t,i))*inv_pos(r_im(t,i,j,m))) + C);
T_off2(t, i) <= G + theta2(t,i)*S(t,i)*D(t,i) - lambda2(t,i,j)*eta2(t,i,j);% Minimum offloading time
end
end
T_2(t,i) >= T_off2(t,i);
% Calculate task utility based on big-M method
% T_1(t,i)<= tau_i
f1 = v(t,i)*log(1 + tau_i(t,i) - T_2(t,i))/log(2) + (1 - v(t,i))*Gamma_L;
% T_1(t,i)> tau_i
f2 = -v(t,i)*Gamma_H + (1-v(t,i))*Gamma_L * exp(-c*(T_2_prev(t,i) - tau_i(t,i))) ...
- c*(1-v(t,i))*Gamma_L * exp(-c*(T_2_prev(t,i) - tau_i(t,i))) * (T_2(t,i) - T_2_prev(t,i));
u2(t,i) <= f1 + beta * (1-z2(t,i));
u2(t,i) <= f2 + beta * z2(t,i);
total_utility2 = total_utility2 + u2(t,i);
end
end
maximize(total_utility2)
% --- Constraints ---
subject to
% Constraint C2
0 <= theta2 <= 1;
% Constraint C3
0 <= eta2 <= eta_max;
% Constraint C4
for t = 1:T
for j = 1:J
sum(eta2(t,:,j)) <= eta_max;
end
end
% Constraint C5
0 <= p_mi2 <= p_max;
% Constraint C6
for t = 1:T
for m = 1:M
sum(p_mi2(t,:,m)) <= p_max;
end
end
% Auxiliary variable constraints
for t = 1:T
for i = 1:I
for j =1:J
for m =1 : M
z_mj2(t,i,j,m) <= (log(1 + (p_mi2(t,i,m) * g_mj(j,m) / sigma2)) ) / log(2);
end
end
end
end
% Big-M method
for t = 1:T
for i = 1:I
T_2(t,i) <= tau_i(t,i) + beta * (1 - z2(t,i));
T_2(t,i) >= tau_i(t,i) - beta * z2(t,i);
% Relaxation
0<= z2(t,i) <= 1;
end
end
cvx_end
% --- Check convergence ---
if norm(theta2 - theta2_prev, 'fro') < tol
break;
end
% --- Save previous iteration variable values ---
theta2_prev = theta2;
z_mj2_prev = z_mj2;
T_2_prev = T_2;
eta2_prev = eta2;
% --- Update Dinkelbach parameters ---
for t = 1:T
for i = 1:I
for j = 1:J
lambda2(t,i,j) = (theta2(t,i) * S(t,i) * D(t,i)) / eta2(t,i,j);
end
end
end
end
% ========== Output Results ==========
fprintf('Traditional method total utility: %f\n', cvx_optval);
% Generate bar chart
e1 = 0;
e2 = 0;
for t = 1:T
for i = 1:I
if v(t,i) == 1
e1 = e1 + eta1(t,i);
e2 = e2 + eta2(t,i);
end
end
end
pic = bar(1,[e1;e2]);
legend("Propose","Traditional",'Location','northwest');
title('Time comparasion')
xlabel('High priority task')
ylabel('Task execution time(s)')
xtips1 = pic(1).XEndPoints;
ytips1 = pic(1).YEndPoints;
labels1 = string(pic(1).YData);
a=text(xtips1,ytips1,labels1,'HorizontalAlignment','center',...
'VerticalAlignment','bottom','FontSize',10)
xtips2 = pic(2).XEndPoints;
ytips2 = pic(2).YEndPoints;
labels2 = string(pic(2).YData);
c=text(xtips2,ytips2,labels2,'HorizontalAlignment','center',...
'VerticalAlignment','bottom','FontSize',10)
ax = gca;
ax.FontSize = 10;
% Add axis labels
fontname("Times New Roman");
I am doing researches about task offloading and resource allocation and I wrote this code to compare resources allocated to high-priority tasks using two different ways to calculate the utility of completing tasks on time(see variable “f1”).
From “f1”, you can see that compared with traditional method,
log(1 + tau_i(t,i) - T_2(t,i))/log(2)
I add the utility of completing a low-priority task on time(Gamma_L, a constant) to the utility of completing a high-priority task on time in the proposed method to make sure the benefit of completing a high-priority task is always higher than completing a low-priority task (which cannot be guaranteed in the traditional method).
log(1 + tau_i(t,i) - T_1(t,i))/log(2) + Gamma_L
In my opinion, this could make sure the system allocate more resources to the high-priority tasks than low-priority tasks compared with the traditional method. However, the experimental results did not align with my expection. Is there anything wrong with my code or my design?