clear all; 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
D = 11e6 * ones(T, I); % Data volume
S = 7e2 * ones(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)
delta = 1.1e9 * ones(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
r_ij = 1.2e6 * ones(T,I,J);% Transmission power between UAV i and RSU j
r_im = 1.2e6 * ones(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
T_1_prev = tau_i;
eta1_prev = 0.5 * eta_max *ones(T,I,J);
p_mi1_prev = 0.5 * p_max * ones(T,I,M);
% ========== SCA Iteration ==========
for k = 1:max_iter
% --- Build convex optimization problem for current iteration ---
cvx_solver Mosek
cvx_begin
variable theta1(T,I) % Optimization variable: task offloading ratio
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) binary;% 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) * (log(1 + p_mi1_prev(t,i,m) * g_mj(j,m) / sigma2)/log(2)))+ ...
((theta1(t,i) - theta1_prev(t,i))*D(t,i))/(B_mj(j, m) * (log(1 + p_mi1_prev(t,i,m) * g_mj(j,m) / sigma2)/log(2)))- ...
((theta1_prev(t,i)*D(t,i)* g_mj(j,m))/(log(2) * B_mj(j, m) * sigma2 * (1 + p_mi1_prev(t,i,m) * g_mj(j,m) / sigma2) * (log(1 + p_mi1_prev(t,i,m) * g_mj(j,m) / sigma2)/log(2))^2) ) * (p_mi1(t,i,m) - p_mi1_prev(t,i,m));
G = P(t,i,j) * ((theta1(t,i) * D(t,i))/r_ij(t,i,j)) + (1 - P(t,i,j)) * (((theta1(t,i) * D(t,i))/r_im(t,i,j,m)) + C);
E = (theta1_prev(t,i)*S(t,i)*D(t,i))/eta1_prev(t,i,j) + ...
(theta1(t,i) - theta1_prev(t,i))*S(t,i)*D(t,i)/eta1_prev(t,i,j) - ...
(theta1_prev(t,i)*S(t,i)*D(t,i))/(eta1_prev(t,i,j)^2)*(eta1(t,i,j) - eta1_prev(t,i,j));
T_off1(t, i) <= G + E;
% T_1(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;
% f1 = v(t,i)*log(1 + tau_i(t,i) - T_1(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_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
% 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);
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 = max(z_mj1, 1e-10);
T_1_prev = T_1;
eta1_prev = eta1;
p_mi1_prev = p_mi1;
end
% ========== Output Results ==========
fprintf('Proposed method total utility: %f\n', cvx_optval);
After the first iteration, eta1, p_mi1 are all zero matrices and cause subsequent errors.
In my opinion, an increase in p_mi1 and eta1 reduces C and E, and reduce T_off1 indirectly. Additionally, a decrease in T_off1 reduces T_1, ultimately leading to an increase in u1 and total_utility1.
So why is this behavior happening? How could I fix it?