Advertisement
makispaiktis

Relay

May 9th, 2023 (edited)
1,246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 19.98 KB | None | 0 0
  1. clear all
  2. close all
  3. clc
  4.  
  5.  
  6. tic
  7.  
  8. global A_new B N_relay K xb yb v_max_mine delta_t h_b aggregators H F C1 C2 B_c x_cp y_cp init
  9.  
  10.  
  11. %% 1. kmeans clustering - Define aggregators positions
  12. % *************************************************************************
  13. % *************************************************************************
  14. % *************************************************************************
  15. % *************************************************************************
  16.  
  17. % Define the number of sensors and their positions
  18. LEN_X = 100;
  19. LEN_Y = LEN_X;
  20. K = 5;
  21. x_aggr = -LEN_X/2 + rand(1, K) * LEN_X;
  22. y_aggr = -LEN_Y/2 + rand(1, K) * LEN_Y;
  23. aggregators = [x_aggr; y_aggr];
  24.  
  25. points = [x_aggr; y_aggr];
  26. colors1 = ["red", "green", "blue", "magenta", "cyan", "yellow", "black"];
  27. colors2 = zeros(K, 3);
  28. for k = 1 : K
  29.     colors2(k, :) = [rand, rand, rand];
  30. end
  31.  
  32. colors = [];
  33. if K <= length(colors1)
  34.     colors = colors1;
  35. else
  36.     colors = colors2;
  37. end
  38.  
  39. % aggregators = [20 10 0 -20 5;15 5 10 -30 -30];
  40.  
  41.  
  42. %% 2. Fixed data about position and frequency
  43. % *************************************************************************
  44. % *************************************************************************
  45. % *************************************************************************
  46. % *************************************************************************
  47.  
  48. % BS === CP
  49. xb = 1 * LEN_X / 2;    yb = 0;    h_b = 15;    qb = [xb, yb];
  50. x_cp = xb;    y_cp = yb;    q_cp = [x_cp, y_cp];
  51.  
  52. % Data EM waves
  53. f = 2.4e9;    c = 3e8;    lambda = c / f;    B = 1e6;
  54. Gt = 1;    Gr = 1;    G = Gt*Gr;
  55. Gt_dB = natural_to_dB(Gt);    Gr_dB = natural_to_dB(Gr);
  56. P_tr = 0.001;                   % 0 dBm
  57. P_ant = 0.001;                  % 0 dBm
  58.  
  59. % Formulas for noise
  60. sigma_dBm = -174 + 10 * log10(B);
  61. sigma_dB = sigma_dBm - 30;
  62. P_noise = dB_to_natural(sigma_dB);
  63.  
  64. % Calculations
  65. Gt_dB = natural_to_dB(Gt);    Gr_dB = natural_to_dB(Gr);
  66. % FSPL
  67. d0 = 1;
  68. C0_1m_dB = - (20 * log10(d0 * f) + 20 * log10(4*pi/c) - Gt_dB - Gr_dB);
  69. C0_1m = dB_to_natural(C0_1m_dB);
  70.  
  71. % Data of UAV-RIS
  72. H = 30;
  73. d = lambda / 10;
  74. delta_t = 1;
  75. v_max_kmh = 62;
  76. v_max = v_max_kmh / 3.6;                          % 16.67 m/s
  77.  
  78. % SNR Threshold
  79. SNR_thr = 1;
  80. kappa_dB = 100;
  81. kappa = 10^(kappa_dB/10);
  82.  
  83.  
  84.  
  85.  
  86. %% 3. UAV Components - Find flight time
  87. % *************************************************************************
  88. % *************************************************************************
  89. % *************************************************************************
  90. % *************************************************************************
  91.  
  92. % UAV Frame = 50cm x 50cm
  93. desired_side = 0.5;                 % RIS Frame Side
  94. % BATTERY
  95. name = "Tattu 30.000 mAh";
  96. B_c = 180 * 3600;               % Wh  ---> W * s = Joule
  97. B_w = 1.35;                     % kgr
  98. % WEIGHTS
  99. UAV_w = 3.25;                   % kgr
  100. Ant_w = 8 / 1000;               % WiFi antenna = 8 gr weight
  101. T_max = 20;                     % kgr
  102. % Relay
  103. a_PA = 1.875;
  104. P_R = 1;
  105. P_RC = 1.5;
  106.  
  107.  
  108.  
  109. % b) M_opt
  110. max_logos_max = 1;
  111. logos_max = 0.65;                % (v / vmax)_max
  112. v_max_mine = v_max * logos_max;
  113.  
  114.  
  115. rho_air = 1.225;                % kgr/m^3
  116. v_a = 2.5;                      % 2.5 m/s
  117. C_d = 0.005;
  118. g = 9.81;                       % m/s^2
  119. A_UAV = desired_side^2;
  120. D_w = (rho_air * v_a^2 * C_d * A_UAV) / (2*g);
  121.  
  122.  
  123. % a) M_max_relay
  124. M_max_relay = find_M_max_relay(logos_max, T_max, B_w, D_w, UAV_w, Ant_w);
  125. M_max_relay = floor(M_max_relay);
  126.  
  127.  
  128.  
  129.  
  130. % Max antennas for the UAV to move with a standard logos_max (M_max)
  131. fprintf('0 <= a <= a_max\n<strong>0 <= a <= %.2f\n</strong>', max_logos_max);
  132. fprintf('<strong>Select: a = %.2f\n\n</strong>', logos_max);
  133.  
  134. display('**********************************************************');
  135. fprintf('<strong>Max relay antennas ---> T_max [kgr]</strong>\n');
  136. display(' ');
  137.  
  138.  
  139. [Ltmin_minutes_relay, P_tot_max_relay, W_relay] = find_Lt_minutes_relay(M_max_relay, logos_max, lambda, UAV_w, Ant_w, T_max, rho_air, v_a, C_d, g, v_max, B_w, B_c, desired_side, a_PA, P_R, P_RC);
  140. Ltmin_sec_relay = floor(Ltmin_minutes_relay * 60);
  141. N_min_relay = floor(Ltmin_sec_relay / delta_t);
  142. pretty_Lt(M_max_relay, logos_max, P_tot_max_relay, Ltmin_minutes_relay, N_min_relay, delta_t, W_relay, T_max);
  143. display('**********************************************************');
  144. display(' ');
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. % M selection
  153. [M_relay, sep] = select_M_relay(desired_side, lambda);
  154. M_rec = M_relay / 2;
  155. M_tr = M_relay / 2;
  156.  
  157. display('**********************************************************');
  158. fprintf('For a %dcm x %dcm frame, we need:\n', 100 * desired_side, 100 * desired_side);
  159. fprintf('<strong>M = %d antennas with separation = %.2fcm</strong>\n', M_relay, 100*sep);
  160. fprintf('located across the diagonal of frame\n');
  161. display('**********************************************************');
  162. fprintf('\n\n\n');
  163.  
  164. m_list = 1 : M_relay;
  165. D_w = (rho_air * v_a^2 * C_d * A_UAV) / (2*g);
  166.  
  167. [Lt_minutes_relay, P_tot_relay, W_relay] = find_Lt_minutes_relay(M_relay, logos_max, lambda, UAV_w, Ant_w, T_max, rho_air, v_a, C_d, g, v_max, B_w, B_c, desired_side, a_PA, P_R, P_RC);
  168. Lt_sec_relay = floor(Lt_minutes_relay * 60);
  169. N_relay = floor(Lt_sec_relay / delta_t);
  170.  
  171.  
  172.  
  173. % CONSTRAINTS FOR 'N'
  174. % 1. N must be even (for the spiral to be made)
  175. % 2. N must be in form: N = K * (sth), where (sth) will be
  176. % the time duration of benchmarks communication
  177.  
  178. display('**********************************************************');
  179. fprintf('For M=%d and a=%.2f, we have:\n\n', M_relay, logos_max);
  180. fprintf('N = %d, but:\n', N_relay);
  181. for N_new = N_relay : -1 : 0
  182.     if mod(N_new, 2) == 0 && mod(N_new, K) == 0
  183.         N_relay = N_new;
  184.         fprintf('1. N must be dividable by 2 (spiral)\n');
  185.         fprintf('2. N must be dividable by %d (benchmarks)\n', K);
  186.         fprintf('<strong>N = %d\n</strong>', N_relay);
  187.         break;
  188.     end
  189. end
  190. display('**********************************************************');
  191. fprintf('\n\n\n');
  192.  
  193. display('**********************************************************');
  194. fprintf('<strong>I select this value of M:</strong>\n\n');
  195. pretty_Lt(M_relay, logos_max, P_tot_relay, Lt_minutes_relay, N_relay, delta_t, W_relay, T_max);
  196. display('**********************************************************');
  197. display(' ');
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. %% 4. Technical RIS Stats - Weights and Dimensions
  207. fprintf('\n\n\n\n');
  208. display('**********************************************************');
  209. a = logos_max;
  210. W_total = REL_info(a, B_w, D_w, T_max, M_relay, Ant_w, UAV_w, f, B, lambda, N_relay, sep);
  211. display('**********************************************************');
  212. display(' ');
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221. %% 5. Initialize A, P, Q, Theta
  222. % *************************************************************************
  223. % *************************************************************************
  224. % *************************************************************************
  225. % *************************************************************************
  226.  
  227. n_list = 1 : N_relay;
  228.  
  229. % -------- VARIABLES ---------
  230. % TDMA  = 1 x N ----> A = K x N
  231. % POWER = 1 x N ----> P = K x N
  232. % Q = 2 x N
  233. % Theta = M x N, total_gain_optimized = 1 x N
  234. % SNR = 1 x N, SNR_av_optimized = scalar
  235. % ----------------------------
  236.  
  237. strofes = 3;
  238. alpha = 10;
  239. beta = (LEN_X/2 - alpha) / (strofes*2*pi);
  240.        
  241. iter = 0;
  242. Q = traj_ellipsis(q_cp, LEN_X, LEN_Y, N_relay);
  243. Q = traj_romvos(q_cp, LEN_X, LEN_Y, N_relay);
  244. Q = traj_speira2(alpha, beta, N_relay, strofes, LEN_X, q_cp);
  245. Q = traj_traveling_salesman(q_cp, N_relay, aggregators, K);
  246. Q = traj_romvos(q_cp, LEN_X, LEN_Y, N_relay);
  247. % Q = traj_straight_line(q_cp, LEN_X, LEN_Y, N);
  248. [TDMA, schedule, timestamps] = TDMA_fair_benchmark(Q, K, N_relay, aggregators);
  249. A = TDMA_to_A(TDMA, K, N_relay);
  250. POWER = P_tr * ones(1, N_relay);
  251. P = POWER_to_P(POWER, K, N_relay, TDMA);
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260. %% 6. Plots, velocity, acceleration
  261. min_sumrates_Mbps_list_relay = [];
  262. sum_sumrates_Mbps_list_relay = [];
  263. velocity = calculate_velocity(Q, delta_t, N_relay);
  264. plot_trajectory_TDMA(Q, LEN_X, LEN_Y, aggregators, ...
  265.     x_cp, y_cp, xb, yb, K, colors, iter, schedule, timestamps);
  266. plot(Q(1, :), Q(2, :));
  267.  
  268.  
  269.  
  270.  
  271. %% 7. Iteration 0 - Statistics
  272. % *************************************************************************
  273. % *************************************************************************
  274. % *************************************************************************
  275. % *************************************************************************
  276.  
  277. P_rel = M_relay*P_RC + (a_PA+1)*P_R;
  278.  
  279. display(' ');
  280. display(' ');
  281. display(' ');
  282. display(' ');
  283. display('**********************************************************');
  284. display('**********************************************************');
  285. display(' ');
  286. display('MAIN FUNCTION PARAMETERS');
  287. display(' ');
  288. fprintf('<strong>a = %.2f\nM = %d antennas\nN = %d timeslots\nK = %d sensors\n</strong>\n\n\n', logos_max, M_relay, N_relay, K);
  289.  
  290.  
  291. fprintf('<strong>-----------------------------</strong>\n');
  292. fprintf('<strong>-------- Iteration %d --------</strong>\n', iter);
  293. fprintf('<strong>-----------------------------</strong>\n\n');
  294.  
  295. sumrates_relay = find_sumrates_relay(Q, TDMA, K, N_relay, B, aggregators, qb, H, h_b, G, P_tr, P_noise, C0_1m, M_rec, M_tr, P_ant, kappa);
  296. [min_sumrate_relay, min_agg_relay] = min(sumrates_relay);
  297. min_sumrates_Mbps_list_relay = [min_sumrates_Mbps_list_relay, min_sumrate_relay/1e6];
  298. SUM_SUMRATES_MBPS_RELAY = sum(sumrates_relay) / 1e6;
  299. sum_sumrates_Mbps_list_relay = [sum_sumrates_Mbps_list_relay, SUM_SUMRATES_MBPS_RELAY];
  300.  
  301.  
  302. fprintf('Schedule of Fair TDMA: \n');
  303. SCHEDULE = "";
  304. for k = 1 : K
  305.     if k < K
  306.         SCHEDULE = SCHEDULE + num2str(schedule(k)) + " ---> ";
  307.     else
  308.         SCHEDULE = SCHEDULE + num2str(schedule(k));
  309.     end
  310. end
  311. fprintf('<strong>%s</strong>\n\n\n', SCHEDULE);
  312.  
  313.  
  314. display('******** QoS ********');
  315. sumrates_Mbps = sumrates_relay / 1e6;
  316. fprintf('Sumrates [Mbps] = \n[');
  317. fprintf('%g, ', sumrates_Mbps(1:end-1));
  318. fprintf('%g]\n\n', sumrates_Mbps(end));
  319. fprintf('<strong>Min (sumrate) = %.2f Mbps\nfor agg = %d\n</strong>', min_sumrate_relay/1e6, min_agg_relay);
  320. display(' ');
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329. %% 8. Alternate Algorithm - Iterations - Improve DF
  330. % *************************************************************************
  331. % *************************************************************************
  332. % *************************************************************************
  333. % *************************************************************************
  334.  
  335. max_iter = 1;   % In last iteration, I optimize only 'A', not 'Q'
  336. improve_perc = Inf;
  337. improve_perc_thr = 3/100;
  338.  
  339.  
  340.  
  341.  
  342. while iter < max_iter
  343.    
  344.    
  345.     %% 8a. Basics
  346.    
  347.     iter = iter + 1;
  348.     initial_A = A;
  349.     initial_Q = Q;
  350.     initial_Theta = Theta;
  351.     initial_total_gain_optimized = total_gain_optimized;
  352.     initial_SNR = SNR;
  353.     fprintf('<strong>-----------------------------</strong>\n');
  354.     fprintf('<strong>-------- Iteration %d --------</strong>\n', iter);
  355.     fprintf('<strong>-----------------------------</strong>\n\n');
  356.    
  357.     global F C1 C2 B_c;
  358.     F = (P_tr/P_noise) * Gt*Gr * C0_1m^2 * M_relay^2;
  359.     C1 = UAV_w + B_w + M_relay*Ant_w;
  360.     C2 = (T_max - C1) / v_max;
  361.    
  362.    
  363.    
  364.    
  365.     %% 8b. Optimization of 'A' - Find optimal TDMA
  366.     fprintf('<strong>Subproblem 1 - Optimize A\n</strong>');
  367.    
  368.     % Create the d1, d2 list using the OLD TRAJECTORY Q
  369.     d1_list = zeros(N_relay, K);
  370.     d2_list = zeros(N_relay, K);
  371.     for n = 1 : N_relay
  372.         UAV = initial_Q(:, n);
  373.         if iter>1
  374.             UAV = [qq(n),qq(N_relay+n)];
  375.         end
  376.         for k = 1 : K
  377.             agg = aggregators(:, k);
  378.             d1_list(n, k) = sqrt( (UAV(1)-agg(1))^2 + (UAV(2)-agg(2))^2 + H^2 );
  379.             d2_list(n, k) = sqrt( (UAV(1)-qb(1))^2 + (UAV(2)-qb(2))^2 + (H-h_b)^2 );
  380.         end
  381.     end
  382.    
  383.     % Solution VECTOR with: length = NK + 1
  384.     % z = [a_1[1], ..., a_K[1],
  385.     %      a_1[2], ..., a_K[2],
  386.     %      ...................,
  387.     %      a_1[N], ..., a_K[N],
  388.     %      t]
  389.    
  390.    
  391.    
  392.     % 1a. Objective function 'f'
  393.     f = zeros(N_relay*K+1, 1)';
  394.     f(N_relay*K+1) = -1;                  % min (-t)
  395.     % f = @(z) (-z(end));
  396.    
  397.     % 1b. Binary variables a_k[n] -------> (C3)
  398.     intcon = 1 : N_relay*K;
  399.     lb = zeros(N_relay*K+1, 1);
  400.     ub = ones(N_relay*K+1, 1);
  401.     lb(N_relay*K+1) = -Inf;
  402.     ub(N_relay*K+1) = Inf;
  403.    
  404.     % 1c. Equality Constraint -----------> (C4)
  405.     % For every n:   sum a_k[n] = 1
  406.     Aeq = zeros(N_relay, N_relay*K+1);
  407.     for n = 1 : N_relay
  408.         % indeces = (n-1)*K + 1:K;
  409.         Aeq(n, (n-1)*K+1:(n-1)*K+K) = 1;
  410.     end
  411.     beq = ones(N_relay, 1);
  412.    
  413.    
  414.    
  415.     % 1d. Inequality Constraints --------> (C7)
  416.     % For every k: -sum(....) + t <= 0
  417.     Aineq = zeros(K, N_relay*K+1);
  418.     for k = 1 : K
  419.         for n = 1 : N_relay
  420.             d1 = d1_list(n, k);
  421.             d2 = d2_list(n, k);
  422.             Aineq(k, k+(n-1)*K) = -log2(1 + F/d1^2/d2^2);
  423.         end
  424.     end
  425.     Aineq(:, N_relay*K+1) = ones(K, 1);           % for 't'
  426.     bineq = zeros(K, 1);
  427.    
  428.    
  429.    
  430.     fprintf('* (C4) has N=%d equality constraints:\n', N_relay);
  431.     fprintf('<strong> Aeq  = %dx%d</strong>, ', size(Aeq));
  432.     fprintf('<strong> beq  = %dx%d</strong>\n', size(beq));
  433.     fprintf('* (C7) has K=%d inequality constraints:\n', K');
  434.     fprintf('<strong>Aineq = %dx%d</strong>, ', size(Aineq));
  435.     fprintf('<strong>bineq = %dx%d</strong>\n\n', size(bineq));
  436.    
  437.    
  438.    
  439.    
  440.    
  441.     % 1e. Options (MaxTime = 60 seconds)
  442.     options = optimoptions(@intlinprog, 'MaxTime', 60);
  443.     [z, opt_val, exitflag, output] = ...
  444.     intlinprog(f, intcon, Aineq, bineq, Aeq, beq, lb, ub,[],options);
  445.     % This optimal value is in bps/Hz, so I have to multiply by
  446.     % B = 1 MHz to find the value in bps or Mbps
  447.     t_optimal_A = - opt_val * B;
  448.     t_optimal_A_Mbps = t_optimal_A / 1e6;
  449.    
  450.    
  451.    
  452.    
  453.     % 1f. Check if 'z' vector is alright
  454.     SUM_Z = sum(z) - z(end)
  455.    
  456.     % 1g. Transform the vector 'z' into the 'A' matrix or 'TDMA' vector
  457.     TDMA_new = zeros(1, N_relay);
  458.     for n = 1 : N_relay
  459.         subvector = z( (n-1)*K+1 : (n-1)*K+K );
  460.         agg_comm = find(abs(subvector-1) <= 1e-4);
  461.         TDMA_new(n) = agg_comm;
  462.     end
  463.     A_new = TDMA_to_A(TDMA_new, K, N_relay);
  464.     global A_new;
  465.    
  466.    
  467.     % 1h. Print statistics and improvement
  468.     [Theta_A, total_gain_A, SNR_A, SNR_av_dB_A] = ...
  469.     optimize_phase_shifts(N_relay, M_relay, TDMA_new, P_tr, initial_Q, aggregators, H, h_b, qb, ...
  470.     lambda, d, C0_1m, P_noise);
  471.  
  472.     sumrates_A = find_sumrates(initial_Q, TDMA_new, K, N_relay, B, aggregators, qb, H, h_b, G, P_tr, P_noise, C0_1m, M_relay);
  473.     [min_sumrate_A, min_agg_A] = min(sumrates_A);
  474.     sumrates_A_Mbps = sumrates_A / 1e6;
  475.     min_sumrates_Mbps_list_relay = [min_sumrates_Mbps_list_relay, min_sumrate_A/1e6];
  476.    
  477.     SUM_SUMRATES_MBPS_A = sum(sumrates_A_Mbps);
  478.     sum_sumrates_Mbps_list_relay = [sum_sumrates_Mbps_list_relay, SUM_SUMRATES_MBPS_A];
  479.    
  480.     display('******** QoS ********');
  481.     disp("SNR_av_dB = " + SNR_av_dB_A + " dB");
  482.     fprintf('Sumrates [Mbps] = \n[');
  483.     fprintf('%g, ', sumrates_A_Mbps(1:end-1));
  484.     fprintf('%g]\n\n', sumrates_A_Mbps(end));
  485.     fprintf('<strong>Optimization  = %.2f Mbps</strong>\n', t_optimal_A_Mbps);
  486.     fprintf('<strong>Min (sumrate) = %.2f Mbps\nfor agg = %d\n</strong>', min_sumrate_A/1e6, min_agg_A);
  487.     fprintf('\n\n');
  488.    
  489.     substep = " - Optimization TDMA - A";
  490.    
  491.     plot_trajectory_TDMA2(initial_Q, LEN_X, LEN_Y, aggregators, ...
  492.     x_cp, y_cp, xb, yb, K, colors, iter, substep, TDMA_new, N_relay);
  493.     plot(initial_Q(1, :), initial_Q(2, :));
  494.     OK = 1000;
  495.    
  496.    
  497.    
  498.    
  499.    
  500.    
  501.    
  502.    
  503.    
  504.     %% 8c. Optimization of 'Q' - Find optimal trajectory
  505.    
  506.     if iter <= max_iter-1
  507.        
  508.        
  509.         fprintf('<strong>\nSubproblem 2 - Optimize Q\n</strong>');
  510.  
  511.         % Solution vector in this form:
  512.         % qq = [.... xn .... | .... yn .... |
  513.         %       .... skn ... | .... tkn ... | t]
  514.         % with:
  515.         % skn = [s1(1), ..., sK(1),  s1(2), ..., sK(2), ..., s1(N), ..., sK(N)]
  516.         % tkn = [t1(1), ..., tK(1),  t1(2), ..., tK(2), ..., t1(N), ..., tK(N)]
  517.  
  518.  
  519.  
  520.         % Objective function and options
  521.  
  522.        exitflag =0;
  523.        if iter ==1
  524.            qq0 = [1*initial_Q(1, :) 1*initial_Q(2, :) 30*rand(1, 2*K*N_relay) t_optimal_A_Mbps];
  525.        else
  526.            qq0 = [1*initial_Q(1, :) 1*initial_Q(2, :) qq(2*N_relay+1:end)];
  527.        end
  528.        iterr = 0;
  529.        global init
  530.        obj = @(qq)(-1*qq(end));
  531.     %    exitflag==0 &&
  532.        while (iterr<2)
  533.         init = qq0/1.1;
  534.         iterr= iterr +1;
  535.         lb = -LEN_X/2 * ones(2*N_relay+2*K*N_relay+1, 1);
  536.         ub = LEN_X/2 * ones(2*N_relay+2*K*N_relay+1, 1);
  537.         lb(2*N_relay+1 : 2*N_relay+2*K*N_relay) = 0;
  538.         ub(2*N_relay+1 : 2*N_relay+2*K*N_relay) = 1000;
  539.         lb(2*N_relay+2*K*N_relay+1) = 0;
  540.         ub(2*N_relay+2*K*N_relay+1) = 1000;
  541.         options = optimoptions(@fmincon,'Algorithm','interior-point',...
  542.                       'MaxIter',1000,'MaxFunEvals',3000, 'Display', 'off', 'ConstraintTolerance', 0.01);
  543.         if iterr==1
  544.             [qq, fval, exitflag] = fmincon(@(qq)(0), qq0, [], [], [], [], lb, [], @nonlcon2_nikos, options);
  545.             qq0=qq;
  546.             [qq, fval, exitflag] = fmincon(obj, qq0, [], [], [], [], lb, [], @nonlcon2_nikos, options);
  547.             exitflag
  548.         end
  549.         if iterr>1
  550.            [qq, fval, exitflag] = fmincon(obj, qq0, [], [], [], [], lb, [], @nonlcon2_nikos, options);
  551.            exitflag
  552.         end
  553.          if exitflag ==0 || exitflag==1
  554.             qq0=qq;
  555.         end
  556.        end
  557.  
  558.         % Transform vector 'qq' into new trajectory Q_new
  559.         Q_new = [qq(1:N_relay); qq(N_relay+1:2*N_relay)];
  560.         Q=Q_new;
  561.         t_optimal_Q = fval;
  562.         t_optimal_Q_Mbps = t_optimal_Q / 1e6;
  563.  
  564.  
  565.         % Print statistics and improvement
  566.         [Theta_Q, total_gain_Q, SNR_Q, SNR_av_dB_Q] = ...
  567.         optimize_phase_shifts(N_relay, M_relay, TDMA_new, P_tr, Q_new, aggregators, H, h_b, qb, ...
  568.         lambda, d, C0_1m, P_noise);
  569.  
  570.         sumrates_Q = find_sumrates(Q_new, TDMA_new, K, N_relay, B, aggregators, qb, H, h_b, G, P_tr, P_noise, C0_1m, M_relay);
  571.         [min_sumrate_Q, min_agg_Q] = min(sumrates_Q);
  572.         sumrates_Q_Mbps = sumrates_Q / 1e6;
  573.         min_sumrates_Mbps_list_relay = [min_sumrates_Mbps_list_relay, min_sumrate_Q/1e6];
  574.  
  575.         SUM_SUMRATES_MBPS_Q = sum(sumrates_Q_Mbps);
  576.         sum_sumrates_Mbps_list_relay = [sum_sumrates_Mbps_list_relay, SUM_SUMRATES_MBPS_Q];
  577.  
  578.         display('******** QoS ********');
  579.         disp("SNR_av_dB = " + SNR_av_dB_Q + " dB");
  580.         fprintf('Sumrates [Mbps] = \n[');
  581.         fprintf('%g, ', sumrates_Q_Mbps(1:end-1));
  582.         fprintf('%g]\n\n', sumrates_Q_Mbps(end));
  583.         fprintf('<strong>Optimization  = %.2f Mbps</strong>\n', t_optimal_Q_Mbps);
  584.         fprintf('<strong>Min (sumrate) = %.2f Mbps\nfor agg = %d\n</strong>', min_sumrate_Q/1e6, min_agg_Q);
  585.         fprintf('\n\n');
  586.  
  587.         substep = " - Optimization TDMA - Q";
  588.  
  589.         plot_trajectory_TDMA2(Q_new, LEN_X, LEN_Y, aggregators, ...
  590.         x_cp, y_cp, xb, yb, K, colors, iter, substep, TDMA_new, N_relay);
  591.         plot(Q_new(1, :), Q_new(2, :));
  592.         OK = 1000;
  593.    
  594.     end
  595.    
  596. end
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605. %% 9. Plots of sumrates (min and sum)
  606. min_sumrates_Mbps_list_relay
  607. sum_sumrates_Mbps_list_relay
  608.  
  609. names = ["Initialization"];
  610. for iter = 1 : max_iter-1
  611.     str1 = "Iter." + num2str(iter) + " - A";
  612.     str2 = "Iter." + num2str(iter) + " - Q";
  613.     names = [names, str1, str2];
  614. end
  615. str3 = "Iter." + num2str(max_iter) + " - A";
  616. names = [names str3];
  617. X = categorical(names);
  618.  
  619. figure();
  620. bar(min_sumrates_Mbps_list_relay);
  621. xticklabels(X);
  622. ylabel('Min sumrate [Mbps]');
  623. title('Min sumrate [Mbps] vs Steps');
  624.  
  625. figure();
  626. bar(sum_sumrates_Mbps_list_relay);
  627. xticklabels(X);
  628. ylabel('Sum of sumrates [Mbps]');
  629. title('Sum of sumrates [Mbps] vs Steps');
  630.  
  631. toc
  632.  
  633.  
  634.  
  635.  
  636.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement