clear; clc;
I = 3;
J = 2;
M = 2;
T = 5;
D = randi([4e6,18e6], T, I);
S = randi([4e2,1e3], T, I);
sigma2 = 1e-9;
B_mj = 1e6 * ones(J, M);
Gamma_L = 0.5; Gamma_H = 1;
c = 0.1;
p_max = 0.8;
eta_max = 2e10;
max_iter = 20;
tol = 1e-3;
delta = 1e9* (1 + 0.2 * rand(I, 1));
P = rand(T,I,J);
r_ij = 0.06e6 + (2.34e6 - 0.06e6) * rand(T,I,J);
r_im = 0.06e6 + (2.34e6 - 0.06e6) * rand(T,I,J,M);
g_mj = 0.02 * ones(J,M);
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);
end
end
v = randi([0,1], T,I);
beta = max(tau_i(:)) * 2;
% ========== 初始化变量 ==========
theta_prev = 0.5 * ones(T,I);
for t = 1:T
for i = 1:I
for j =1:J
for m =1 : M
z_mj_prev(t,i,j,m) = (log(1 + (p_max * g_mj(j,m) / sigma2)) ) / log(2);
end
end
end
end
T_i_prev = tau_i;
eta_prev = 0.5 * eta_max *ones(T,I,J);
for t = 1:T
for i = 1:I
for j = 1:J
lambda(t,i,j) = (theta_prev(t,i) * S(t,i) * D(t,i)) / eta_prev(t,i,j); end
end
end
for k = 1:max_iter
cvx_begin quiet
variable theta(T,I)
variable z_mj(T,I,J,M)
variable eta(T,I,J)
variable p_mi(T,I,M)
variable T_i(T,I);
variable z(T, I);
variable u(T, I);
total_utility = 0;
for t = 1:T
for i = 1:I
term_local = (1 - theta(t,i)) * S(t,i) * D(t,i) / delta(i);
T_i(t,i) >= term_local;
for j = 1:J
for m = 1:M
C = (theta_prev(t,i)*D(t,i))/(B_mj(j, m)*z_mj_prev(t,i,j,m)) + ...
(theta(t,i) * D(t,i))/(B_mj(j, m)*z_mj_prev(t,i,j,m)) - ...
(theta_prev(t,i)*D(t,i))/(B_mj(j, m)*(z_mj_prev(t,i,j,m))^2) * z_mj(t,i,j,m);
G = P(t,i,j) * ((theta(t,i) * D(t,i))*inv_pos(r_ij(t,i,j))) + (1 - P(t,i,j)) * (((theta(t,i) * D(t,i))*inv_pos(r_im(t,i,j,m))) + C);
term_offload = G + theta(t,i)*S(t,i)*D(t,i) - lambda(t,i,j)*eta(t,i,j);
T_i(t,i) >= term_offload;
% T_i(t,i)= max(term_local,term_offload);
end
end
f1 = v(t,i)*log(1 + tau_i(t,i) - T_i(t,i))/log(2) + Gamma_L;
f2 = -v(t,i)*Gamma_H + (1-v(t,i))*Gamma_L * exp(-c*(T_i_prev(t,i) - tau_i(t,i))) ...
- c*(1-v(t,i))*Gamma_L * exp(-c*(T_i_prev(t,i) - tau_i(t,i))) * (T_i(t,i) - T_i_prev(t,i));
u(t,i) <= f1 + beta * (1-z(t,i));
u(t,i) <= f2 + beta * z(t,i);
total_utility = total_utility + u(t,i);
end
end
maximize(total_utility)
subject to
0 <= theta <= 1;
0 <= eta <= eta_max;
for t = 1:T
for j = 1:J
sum(eta(t,:,j)) <= eta_max;
end
end
0 <= p_mi <= p_max;
for t = 1:T
for m = 1:M
sum(p_mi(t,:,m)) <= p_max;
end
end
for t = 1:T
for i = 1:I
for j =1:J
for m =1 : M
z_mj(t,i,j,m) <= (log(1 + (p_mi(t,i,m) * g_mj(j,m) / sigma2)) ) / log(2);
end
end
end
end
for t = 1:T
for i = 1:I
T_i(t,i) <= tau_i(t,i) + beta * (1 - z(t,i));
T_i(t,i) >= tau_i(t,i) - beta * z(t,i);
0<= z(t,i) <= 1;
end
end
cvx_end
if norm(theta - theta_prev, 'fro') < tol
break;
end
theta_prev = theta;
z_mj_prev = z_mj;
T_i_prev = T_i;
eta_prev = eta;
for t = 1:T
for i = 1:I
for j = 1:J
lambda(t,i,j) = (theta(t,i) * S(t,i) * D(t,i)) / eta(t,i,j);
end
end
end
end
fprintf('最优总效用: %f\n', cvx_optval);
I generated the code with the help of AI and it could work. However, I’m puzzled about why these codes:
- T_i(t,i) >= term_local;
- T_i(t,i) >= term_offload;
- u(t,i) <= f1 + beta * (1-z(t,i));
- u(t,i) <= f2 + beta * z(t,i);
are placed before the “subject to”? Are they constraints or something else? If not, what functions do they have?