├── README.md ├── configure.m ├── new_T_compare_area_MonteCarlo.m ├── new_T_compare_number_MonteCarlo.m ├── new_UAV.m ├── new_X_Y_R_compare.m ├── new_X_Y_R_compare1.m ├── new_X_Y_R_compare2.m ├── new_X_Y_R_compare3.m ├── new_single_area_multi_users.m ├── new_single_area_single_user.m ├── new_traceroute.m └── new_traceroute_2.m /README.md: -------------------------------------------------------------------------------- 1 | # Coarse-Closed-loop-Trajectory-Design-of-Multiple-UAVs-for-Parallel-Data-Collection 2 | UAV-DETECT CODE 3 | ===== 4 | # configure project parameters 5 | run config 6 | ## get fig 9 7 | run new_single_area_single_user 8 | ## get fig 10 9 | run new_single_area_multi_users 10 | ## get fig 11 11 | run new_X_Y_R compare_3 12 | ## get fig 12 13 | run new_UAV then run new_traceroute 14 | ## get fig 13 15 | run new_UAV then run new_traceroute_2 16 | ## get fig 14 17 | run new_UAV then run new_T_compare_area_MonteCarlo 18 | ## get fig 15 19 | run new_UAV then run new_T_compare_number_MonteCarlo 20 | ### additional 21 | run new_X_Y_R compare/new_X_Y_R compare_1/new_X_Y_R compare_2 get three-dimensional time compare 22 | -------------------------------------------------------------------------------- /configure.m: -------------------------------------------------------------------------------- 1 | w = 4000; % 总宽度 2 | l = 4000; % 总长度 3 | B = 20*(10^6); 4 | Alpha = 2.01; 5 | density = 0.012; 6 | % h = 100; 7 | D = 2*(10^6); 8 | noise = -146; % dbm/HZ 9 | noise = 10^(noise/10)*B; 10 | P = 30; % dbm 11 | P = 10^(P/10); 12 | d0 = 1; 13 | K = 10^(-4.64); 14 | threshold = 15; 15 | distance_max = (P*K/(threshold*noise))^(1/Alpha)*d0; 16 | h_max = 110; 17 | h_min = 50; -------------------------------------------------------------------------------- /new_T_compare_area_MonteCarlo.m: -------------------------------------------------------------------------------- 1 | w_min = 400; 2 | w_max = 4000; 3 | w_gap = 400; 4 | l_min = 400; 5 | l_max = 4000; 6 | l_gap = 400; 7 | row = (w_max - w_min)/w_gap + 1; 8 | column = (l_max - l_min)/l_gap + 1; 9 | T_array = []; 10 | X_array = []; 11 | Y_array = []; 12 | R_array = []; 13 | User_number_array = []; 14 | for w = w_min:w_gap:w_max 15 | User_number_array(end+1) = w^2*density; 16 | for l = l_min:l_gap:l_max 17 | R_max = sqrt(distance_max^2 - h_min^2); 18 | T_value = inf; 19 | for X= ceil(2^(-3/2)*w/sqrt(distance_max^2 - h_min^2)):1:ceil(2^(-3/2)*w/sqrt(distance_max^2 - h_max^2)) 20 | for Y = ceil(2^(-3/2)*l/sqrt(distance_max^2 - h_min^2)):1:ceil(2^(-3/2)*l/sqrt(distance_max^2 - h_max^2)) 21 | R_min = max([sqrt(distance_max^2 - h_max^2),w/(2*sqrt(2)*X),l/(2*sqrt(2)*Y)]); 22 | R_number = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,10^-5); 23 | value = The_integral(B,distance_max,Alpha,D,noise,P,d0,K,R_number,X,Y,density); 24 | if value < T_value 25 | T_value = value; 26 | X_value = X; 27 | Y_value = Y; 28 | R_value = R_number; 29 | end 30 | end 31 | end 32 | T_array(end+1) = T_value; 33 | X_array(end+1) = X_value; 34 | Y_array(end+1) = Y_value; 35 | R_array(end+1) = R_value; 36 | end 37 | end 38 | T_array = reshape(T_array,row,column); 39 | X_array = reshape(X_array,row,column); 40 | Y_array = reshape(Y_array,row,column); 41 | R_array = reshape(R_array,row,column); 42 | R_max = sqrt(distance_max^2 - h_min^2); 43 | Un_max = 8; 44 | Un_min = 4; 45 | Un_gap = Un_min; 46 | UAV_compare_t = zeros(2,length(X_array)); 47 | UAV_compare_t_lower = zeros(2,length(X_array)); 48 | UAV_compare_t_MonteCarlo = zeros(2,length(X_array)); 49 | for Un = Un_min:Un_gap:8 % UAV_number 50 | f = zeros(1,Un+1); 51 | f(1) = 1; 52 | intcon = 2:Un; 53 | Aeq = ones(1,Un+1); 54 | Aeq(1) = 0 ; 55 | s = ones(1,Un); 56 | for i = 1:length(s) 57 | % if i <= 2 58 | % s(i) = 5; 59 | % elseif i <= 4 60 | % s(i) = 10; 61 | % else 62 | % s(i) = 15; 63 | % end 64 | s(i) = 10; 65 | end 66 | A = zeros(Un,Un+1); 67 | b = zeros(Un,1); 68 | %length(X_array) 69 | for select_row = 1:length(X_array) 70 | select_colmun = select_row;%暂且设定区域为正方形,X_use与Y_use均取矩阵对角线数据 71 | X_use = X_array(select_row,select_colmun); 72 | Y_use = Y_array(select_row,select_colmun); 73 | R_use = R_array(select_row,select_colmun); 74 | T_use = T_array(select_row,select_colmun); 75 | beq = X_use*Y_use; 76 | for i= 1:size(A) 77 | A(i,1) = -1; 78 | A(i,i+1) = 4*sqrt(2)*R_use/s(i) + T_use/(X_use*Y_use); 79 | end 80 | %x = intlinprog(f,intcon,A,b,Aeq,beq); 81 | [t,fval] = intlinprog(f,intcon,A,b,Aeq,beq); 82 | UAV_compare_t(Un/Un_gap,select_row) = fval; 83 | UAV_compare_t_lower(Un/Un_gap,select_row) = calculate(Un,X_use,Y_use,R_use,s(i),T_use); 84 | grid_number = t(2:end); 85 | index_matrix = zeros(2*length(grid_number),round(max(grid_number))); 86 | grid_select = ones(X_use,Y_use); 87 | %color_select = ['r','g','b','y','m','c','k','w']; 88 | for i = 1:X_use 89 | for j = 1:Y_use 90 | grid_select(i,j) = sqrt(i^2 + j^2); 91 | end 92 | end 93 | %grid_select 94 | index = 1; 95 | while (index <= length(grid_number)) 96 | this_number = 1; 97 | max_value = max(max(grid_select)); 98 | [i,j] = find(grid_select==max_value); 99 | %select_i,select_j为本次选定的格子 100 | select_i = i(1); 101 | select_j = j(1); 102 | %记录每次选的格子索引,奇数行为行索引,偶数行记录列索引,用于划线 103 | index_matrix(2*index-1,this_number) = select_i; 104 | index_matrix(2*index,this_number) = select_j; 105 | x_axis = [0 1 1 0] + select_i - 1; 106 | y_axis = [0 0 1 1] + select_j - 1; 107 | %patch('xData', x_axis, 'yData', y_axis, 'FaceColor', color_select(index)); 108 | grid_select(select_i,select_j) = 0; 109 | this_number = this_number + 1; 110 | %this_number 111 | grid_this_select = grid_select; 112 | for i =1:X_use 113 | for j = 1:Y_use 114 | if grid_select(i,j) ~= 0 115 | grid_this_select(i,j) = -1; 116 | end 117 | end 118 | end 119 | while this_number <= int8(grid_number(index)) 120 | if select_i + 1 <= Y_use && grid_this_select(select_i+1,select_j) == -1 121 | grid_this_select(select_i+1,select_j) = sqrt((select_i+1)^2 + select_j^2); 122 | end 123 | if select_i - 1 >= 1 && grid_this_select(select_i-1,select_j) == -1 124 | grid_this_select(select_i-1,select_j) = sqrt((select_i-1)^2 + select_j^2); 125 | end 126 | if select_j + 1 <= X_use && grid_this_select(select_i,select_j+1) == -1 127 | grid_this_select(select_i,select_j+1) = sqrt((select_j+1)^2 + select_i^2); 128 | end 129 | if select_j - 1 >= 1 && grid_this_select(select_i,select_j-1) == -1 130 | grid_this_select(select_i,select_j-1) = sqrt((select_j-1)^2 + select_i^2); 131 | end 132 | max_value = max(max(grid_this_select)); 133 | [i,j] = find(grid_this_select==max_value); 134 | select_i = i(1); 135 | select_j = j(1); 136 | index_matrix(2*index-1,this_number) = select_i; 137 | index_matrix(2*index,this_number) = select_j; 138 | x_axis = [0 1 1 0] + select_i - 1; 139 | y_axis = [0 0 1 1] + select_j - 1; 140 | %patch('xData', x_axis, 'yData', y_axis, 'FaceColor', color_select(index)); 141 | grid_this_select(select_i,select_j) = 0; 142 | grid_select(select_i,select_j) = 0; 143 | this_number = this_number + 1; 144 | end 145 | index = index + 1; 146 | end 147 | %随着w,l的不断改变,后续w = w_min+w_gap*(select_row-1),l = l_min+l_gap*(select_row-1) 148 | User_number = User_number_array(select_row); 149 | grid_x_gap = (w_min+w_gap*(select_row-1))/X_use; 150 | grid_y_gap = (l_min+l_gap*(select_row-1))/Y_use; 151 | h = sqrt(distance_max^2 - R_use^2); 152 | % user_number_test = 0; 153 | Monte_number = 1; %撒点模拟1000次 154 | for Monte = 1:Monte_number 155 | %蒙特卡洛撒点 156 | User_x_position = randi([0,w_min+w_gap*(select_row-1)],1,User_number); 157 | User_y_position = randi([0,l_min+l_gap*(select_row-1)],1,User_number); 158 | UAV_time = zeros(1,Un); 159 | for select_UAV_index = 1:size(index_matrix,1)/2 160 | uav_time = 0; 161 | for select_grid_index = 1:size(index_matrix,2) 162 | select_x = index_matrix(2*select_UAV_index-1,select_grid_index); 163 | select_y = index_matrix(2*select_UAV_index,select_grid_index); 164 | for user_index = 1:User_number 165 | x0 = ceil(User_x_position(user_index)/grid_x_gap); 166 | y0 = ceil(User_y_position(user_index)/grid_y_gap); 167 | if x0 == 0 168 | x0 = 1; 169 | end 170 | if y0 == 0 171 | y0 = 1; 172 | end 173 | if x0 == select_x && y0 == select_y 174 | %uav_time 175 | uav_time = uav_time + MonteCarlo_calculate(x0,y0,P,K,Alpha,noise,B,D,d0,grid_x_gap,grid_y_gap,h); 176 | % if select_UAV_index == 1 177 | % user_number_test = user_number_test + 1; 178 | % end 179 | end 180 | end 181 | end 182 | UAV_time(select_UAV_index) = uav_time + grid_number(select_UAV_index)*4*(grid_x_gap/2)/s(select_UAV_index); %默认长宽间隔相等(正方形区域) 183 | 184 | end 185 | UAV_compare_t_MonteCarlo(Un/Un_gap,select_row) = UAV_compare_t_MonteCarlo(Un/Un_gap,select_row) + max(UAV_time); 186 | % UAV_compare_t_MonteCarlo 187 | end 188 | UAV_compare_t_MonteCarlo(Un/Un_gap,select_row) = UAV_compare_t_MonteCarlo(Un/Un_gap,select_row)/Monte_number; 189 | end 190 | end 191 | make_config(w_min,w_gap,w_max,UAV_compare_t,UAV_compare_t_lower,UAV_compare_t_MonteCarlo); 192 | 193 | function[time] = calculate(Un,X_use,Y_use,r,s_i,T_use) 194 | z = X_use*Y_use/Un; 195 | time = 4*sqrt(2)*r*z/s_i + T_use*z/(X_use*Y_use); 196 | end 197 | 198 | function[time] = MonteCarlo_calculate(x0,y0,P,K,Alpha,noise,B,D,d0,grid_x_gap,grid_y_gap,h) 199 | x_accurate = mod(x0,grid_x_gap); 200 | y_accurate = mod(y0,grid_y_gap); 201 | if x_accurate <= grid_x_gap/2 202 | if y_accurate <= grid_y_gap/2 203 | distance = sqrt((x0-grid_x_gap/4)^2 + (y0-grid_y_gap/4)^2+h^2); 204 | else 205 | distance = sqrt((x0-grid_x_gap/4)^2 + (y0-grid_y_gap*3/4)^2+h^2); 206 | end 207 | else 208 | if y_accurate <= grid_y_gap/2 209 | distance = sqrt((x0-grid_x_gap*3/4)^2 + (y0-grid_y_gap/4)^2+h^2); 210 | else 211 | distance = sqrt((x0-grid_x_gap*3/4)^2 + (y0-grid_y_gap*3/4)^2+h^2); 212 | end 213 | end 214 | Pr = P*K*(d0/distance)^Alpha; 215 | SNR = Pr/noise; 216 | rate = B*log2(1+SNR); 217 | time = D/rate; 218 | %rate,time 219 | end 220 | 221 | function[] = make_config(w_min,w_gap,w_max,UAV_compare_t,UAV_compare_t_lower,UAV_compare_t_MonteCarlo) 222 | x = w_min:w_gap:w_max; 223 | plot(x,UAV_compare_t(1,:),'-*b',x,UAV_compare_t_lower(1,:),'-*r',x,UAV_compare_t_MonteCarlo(1,:),'-*g',x,UAV_compare_t(2,:),'--*b',x,UAV_compare_t_lower(2,:),'--*r',x,UAV_compare_t_MonteCarlo(2,:),'--*g'); 224 | % scatter(x,flip(t_max_h(1:6)),'*','r',x,flip(t_min_h(1:6)),'*','r',x,flip(t_max_h(7:12)),'*','r',x,flip(t_min_h(7:12)),'or',x,flip(t_max_h(13:18)),'xr',x,flip(t_min_h(13:18)),'xr'); 225 | legend('UAV=4,T','UAV=4,T.lowerbound','UAV=4,T.MonentCarlo','UAV=8,T','UAV=8,T.lowerbound','UAV=8,T.MonentCarlo'); 226 | xlabel('区域边长') 227 | ylabel('完成时间/s') 228 | end 229 | 230 | function[value] = The_integral(B,distance_max,Alpha,D,noise,P,d0,K,R_number,X,Y,density) 231 | prefix = 32*D*density*X*Y*log(2)/(Alpha*B); 232 | p = 2/Alpha*log(P*K*d0^Alpha/noise); 233 | func = @(theta,x) 1./(p-log(x)); 234 | x_max = @(theta) distance_max.^2 - (2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_number.^2; 235 | x_min = distance_max.^2 - R_number.^2; 236 | value = prefix * integral2(func,0,pi/4,x_min,x_max); 237 | end 238 | 239 | function[value] = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold) 240 | prefix = 32*D*X*Y*log(2)/(Alpha*B); 241 | p = 2/Alpha*log(P*K*d0^Alpha/noise); 242 | %lambda = (2.*cos(theta).^2-1)./(2.*cos(theta).^2); 243 | func_R_max = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_max./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_max.^2))+2.*R_max/(p-log(distance_max.^2-R_max.^2)); 244 | func_R_min = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_min./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_min.^2))+2.*R_min/(p-log(distance_max.^2-R_min.^2)); 245 | slope_R_max = prefix*integral(func_R_max,0,pi/4); 246 | slope_R_min = prefix*integral(func_R_min,0,pi/4); 247 | if slope_R_max == 0 && slope_R_min < 0 248 | value = R_min; 249 | elseif slope_R_max == 0 && slope_R_min > 0 250 | value = R_max; 251 | elseif slope_R_min == 0 && slope_R_max < 0 252 | value = R_max; 253 | elseif slope_R_min == 0 && slope_R_max > 0 254 | value = R_min; 255 | elseif slope_R_min > 0 && slope_R_max > 0 256 | value = R_min; 257 | elseif slope_R_min < 0 && slope_R_max < 0 258 | value = R_max; 259 | else 260 | R_number = (R_max+R_min)/2; 261 | func_R_number = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_number./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2)*R_number.^2))+2.*R_number/(p-log(distance_max.^2-R_number.^2)); 262 | slope_R_number = prefix*integral(func_R_number,0,pi/4); 263 | if slope_R_min < 0 && slope_R_number < 0 264 | if slope_R_max - slope_R_number <= threshold 265 | value = R_number; 266 | else 267 | R_min = R_number; 268 | value = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold); 269 | end 270 | elseif slope_R_min < 0 && slope_R_number > 0 271 | if slope_R_number - slope_R_min <= threshold 272 | value = R_number; 273 | else 274 | R_max = R_number; 275 | value = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold); 276 | end 277 | end 278 | end 279 | end -------------------------------------------------------------------------------- /new_T_compare_number_MonteCarlo.m: -------------------------------------------------------------------------------- 1 | User_number = w*l*density; 2 | R_max = sqrt(distance_max^2 - h_min^2); 3 | Un_max = 8; 4 | UAV_compare_t = zeros(1,Un_max); 5 | UAV_compare_t_lower = zeros(1,Un_max); 6 | UAV_compare_t_MonteCarlo = zeros(1,Un_max); 7 | % plot(User_x_position,User_y_position,'.'); 8 | T_value = inf; 9 | for X= ceil(2^(-3/2)*w/sqrt(distance_max^2 - h_min^2)):1:ceil(2^(-3/2)*w/sqrt(distance_max^2 - h_max^2)) 10 | for Y = ceil(2^(-3/2)*l/sqrt(distance_max^2 - h_min^2)):1:ceil(2^(-3/2)*l/sqrt(distance_max^2 - h_max^2)) 11 | R_min = max([sqrt(distance_max^2 - h_max^2),w/(2*sqrt(2)*X),l/(2*sqrt(2)*Y)]); 12 | R_number = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,10^-5); 13 | value = The_integral(B,distance_max,Alpha,D,noise,P,d0,K,R_number,X,Y,density); 14 | if value < T_value 15 | T_value = value; 16 | X_value = X; 17 | Y_value = Y; 18 | R_value = R_number; 19 | end 20 | end 21 | end 22 | grid_x_gap = w/X_value; 23 | grid_y_gap = l/Y_value; 24 | h = sqrt(distance_max^2 - R_value^2); 25 | X_use = X_value; 26 | Y_use = Y_value; 27 | r = R_value; 28 | %T_use = T_value; 29 | T_use = T_value; 30 | for Un = 1:Un_max % UAV_number 31 | f = zeros(1,Un+1); 32 | f(1) = 1; 33 | intcon = 2:Un; 34 | Aeq = ones(1,Un+1); 35 | Aeq(1) = 0 ; 36 | s = ones(1,Un); 37 | for i = 1:length(s) 38 | % if i <= 2 39 | % s(i) = 5; 40 | % elseif i <= 4 41 | % s(i) = 10; 42 | % else 43 | % s(i) = 15; 44 | % end 45 | s(i) = 10; 46 | end 47 | A = zeros(Un,Un+1); 48 | b = zeros(Un,1); 49 | beq = X_use*Y_use; 50 | for i= 1:size(A) 51 | A(i,1) = -1; 52 | A(i,i+1) = 4*sqrt(2)*r/s(i) + T_use/(X_use*Y_use); 53 | end 54 | %x = intlinprog(f,intcon,A,b,Aeq,beq); 55 | [t,fval] = intlinprog(f,intcon,A,b,Aeq,beq); 56 | UAV_compare_t(Un) = fval; 57 | UAV_compare_t_lower(Un) = calculate(Un,X_use,Y_use,r,s(i),T_use); 58 | grid_number = t(2:end); 59 | index_matrix = zeros(2*length(grid_number),round(max(grid_number))); 60 | grid_select = ones(X_use,Y_use); 61 | %color_select = ['r','g','b','y','m','c','k','w']; 62 | for i = 1:X_use 63 | for j = 1:Y_use 64 | grid_select(i,j) = sqrt(i^2 + j^2); 65 | end 66 | end 67 | %grid_select 68 | index = 1; 69 | while (index <= length(grid_number)) 70 | this_number = 1; 71 | max_value = max(max(grid_select)); 72 | [i,j] = find(grid_select==max_value); 73 | %select_i,select_j为本次选定的格子 74 | select_i = i(1); 75 | select_j = j(1); 76 | %记录每次选的格子索引,奇数行为行索引,偶数行记录列索引,用于划线 77 | index_matrix(2*index-1,this_number) = select_i; 78 | index_matrix(2*index,this_number) = select_j; 79 | x_axis = [0 1 1 0] + select_i - 1; 80 | y_axis = [0 0 1 1] + select_j - 1; 81 | %patch('xData', x_axis, 'yData', y_axis, 'FaceColor', color_select(index)); 82 | grid_select(select_i,select_j) = 0; 83 | this_number = this_number + 1; 84 | %this_number 85 | grid_this_select = grid_select; 86 | for i =1:X_use 87 | for j = 1:Y_use 88 | if grid_select(i,j) ~= 0 89 | grid_this_select(i,j) = -1; 90 | end 91 | end 92 | end 93 | while this_number <= int8(grid_number(index)) 94 | if select_i + 1 <= Y_use && grid_this_select(select_i+1,select_j) == -1 95 | grid_this_select(select_i+1,select_j) = sqrt((select_i+1)^2 + select_j^2); 96 | end 97 | if select_i - 1 >= 1 && grid_this_select(select_i-1,select_j) == -1 98 | grid_this_select(select_i-1,select_j) = sqrt((select_i-1)^2 + select_j^2); 99 | end 100 | if select_j + 1 <= X_use && grid_this_select(select_i,select_j+1) == -1 101 | grid_this_select(select_i,select_j+1) = sqrt((select_j+1)^2 + select_i^2); 102 | end 103 | if select_j - 1 >= 1 && grid_this_select(select_i,select_j-1) == -1 104 | grid_this_select(select_i,select_j-1) = sqrt((select_j-1)^2 + select_i^2); 105 | end 106 | max_value = max(max(grid_this_select)); 107 | [i,j] = find(grid_this_select==max_value); 108 | select_i = i(1); 109 | select_j = j(1); 110 | index_matrix(2*index-1,this_number) = select_i; 111 | index_matrix(2*index,this_number) = select_j; 112 | x_axis = [0 1 1 0] + select_i - 1; 113 | y_axis = [0 0 1 1] + select_j - 1; 114 | %patch('xData', x_axis, 'yData', y_axis, 'FaceColor', color_select(index)); 115 | grid_this_select(select_i,select_j) = 0; 116 | grid_select(select_i,select_j) = 0; 117 | this_number = this_number + 1; 118 | end 119 | index = index + 1; 120 | end 121 | Monte_number = 1; %撒点模拟1000次 122 | for Monte = 1:Monte_number 123 | %蒙特卡洛撒点 124 | User_x_position = randi([0,w],1,User_number); 125 | User_y_position = randi([0,l],1,User_number); 126 | UAV_time = zeros(1,Un); 127 | for select_UAV_index = 1:size(index_matrix,1)/2 128 | uav_time = 0; 129 | for select_grid_index = 1:size(index_matrix,2) 130 | select_x = index_matrix(2*select_UAV_index-1,select_grid_index); 131 | select_y = index_matrix(2*select_UAV_index,select_grid_index); 132 | for user_index = 1:User_number 133 | x0 = ceil(User_x_position(user_index)/grid_x_gap); 134 | y0 = ceil(User_y_position(user_index)/grid_y_gap); 135 | if x0 == 0 136 | x0 = 1; 137 | end 138 | if y0 == 0 139 | y0 = 1; 140 | end 141 | if x0 == select_x && y0 == select_y 142 | uav_time = uav_time + MonteCarlo_calculate(x0,y0,P,K,Alpha,noise,B,D,d0,grid_x_gap,grid_y_gap,h); 143 | end 144 | end 145 | end 146 | UAV_time(select_UAV_index) = uav_time + grid_number(select_UAV_index)*4*(grid_x_gap/2)/s(select_UAV_index); %默认长宽间隔相等(正方形区域) 147 | end 148 | UAV_compare_t_MonteCarlo(Un) = UAV_compare_t_MonteCarlo(Un) + max(UAV_time); 149 | end 150 | UAV_compare_t_MonteCarlo(Un) = UAV_compare_t_MonteCarlo(Un)/Monte_number; 151 | end 152 | make_config(Un,UAV_compare_t,UAV_compare_t_lower,UAV_compare_t_MonteCarlo); 153 | 154 | function[time] = calculate(Un,X_use,Y_use,r,s_i,T_use) 155 | z = X_use*Y_use/Un; 156 | time = 4*sqrt(2)*r*z/s_i + T_use*z/(X_use*Y_use); 157 | end 158 | 159 | function[] = make_config(Un,UAV_compare_t,UAV_compare_t_lower,UAV_compare_t_MonteCarlo) 160 | x = 1:1:Un; 161 | plot(x,UAV_compare_t,'-*b',x,UAV_compare_t_lower,'-*r',x,UAV_compare_t_MonteCarlo,'-*g'); 162 | % scatter(x,flip(t_max_h(1:6)),'*','r',x,flip(t_min_h(1:6)),'*','r',x,flip(t_max_h(7:12)),'*','r',x,flip(t_min_h(7:12)),'or',x,flip(t_max_h(13:18)),'xr',x,flip(t_min_h(13:18)),'xr'); 163 | legend('T','T.lowerbound','T.MonentCarlo'); 164 | xlabel('无人机数量') 165 | ylabel('完成时间/s') 166 | end 167 | 168 | function[time] = MonteCarlo_calculate(x0,y0,P,K,Alpha,noise,B,D,d0,grid_x_gap,grid_y_gap,h) 169 | x_accurate = mod(x0,grid_x_gap); 170 | y_accurate = mod(y0,grid_y_gap); 171 | if x_accurate <= grid_x_gap/2 172 | if y_accurate <= grid_y_gap/2 173 | distance = sqrt((x0-grid_x_gap/4)^2 + (y0-grid_y_gap/4)^2+h^2); 174 | else 175 | distance = sqrt((x0-grid_x_gap/4)^2 + (y0-grid_y_gap*3/4)^2+h^2); 176 | end 177 | else 178 | if y_accurate <= grid_y_gap/2 179 | distance = sqrt((x0-grid_x_gap*3/4)^2 + (y0-grid_y_gap/4)^2+h^2); 180 | else 181 | distance = sqrt((x0-grid_x_gap*3/4)^2 + (y0-grid_y_gap*3/4)^2+h^2); 182 | end 183 | end 184 | Pr = P*K*(d0/distance)^Alpha; 185 | SNR = Pr/noise; 186 | rate = B*log2(1+SNR); 187 | time = D/rate; 188 | %rate,time 189 | end 190 | 191 | function[value] = The_integral(B,distance_max,Alpha,D,noise,P,d0,K,R_number,X,Y,density) 192 | prefix = 32*D*density*X*Y*log(2)/(Alpha*B); 193 | p = 2/Alpha*log(P*K*d0^Alpha/noise); 194 | func = @(theta,x) 1./(p-log(x)); 195 | x_max = @(theta) distance_max.^2 - (2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_number.^2; 196 | x_min = distance_max.^2 - R_number.^2; 197 | value = prefix * integral2(func,0,pi/4,x_min,x_max); 198 | end 199 | 200 | function[value] = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold) 201 | prefix = 32*D*X*Y*log(2)/(Alpha*B); 202 | p = 2/Alpha*log(P*K*d0^Alpha/noise); 203 | %lambda = (2.*cos(theta).^2-1)./(2.*cos(theta).^2); 204 | func_R_max = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_max./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_max.^2))+2.*R_max/(p-log(distance_max.^2-R_max.^2)); 205 | func_R_min = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_min./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_min.^2))+2.*R_min/(p-log(distance_max.^2-R_min.^2)); 206 | slope_R_max = prefix*integral(func_R_max,0,pi/4); 207 | slope_R_min = prefix*integral(func_R_min,0,pi/4); 208 | if slope_R_max == 0 && slope_R_min < 0 209 | value = R_min; 210 | elseif slope_R_max == 0 && slope_R_min > 0 211 | value = R_max; 212 | elseif slope_R_min == 0 && slope_R_max < 0 213 | value = R_max; 214 | elseif slope_R_min == 0 && slope_R_max > 0 215 | value = R_min; 216 | elseif slope_R_min > 0 && slope_R_max > 0 217 | value = R_min; 218 | elseif slope_R_min < 0 && slope_R_max < 0 219 | value = R_max; 220 | else 221 | R_number = (R_max+R_min)/2; 222 | func_R_number = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_number./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2)*R_number.^2))+2.*R_number/(p-log(distance_max.^2-R_number.^2)); 223 | slope_R_number = prefix*integral(func_R_number,0,pi/4); 224 | if slope_R_min < 0 && slope_R_number < 0 225 | if slope_R_max - slope_R_number <= threshold 226 | value = R_number; 227 | else 228 | R_min = R_number; 229 | value = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold); 230 | end 231 | elseif slope_R_min < 0 && slope_R_number > 0 232 | if slope_R_number - slope_R_min <= threshold 233 | value = R_number; 234 | else 235 | R_max = R_number; 236 | value = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold); 237 | end 238 | end 239 | end 240 | end -------------------------------------------------------------------------------- /new_UAV.m: -------------------------------------------------------------------------------- 1 | R_max = sqrt(distance_max^2 - h_min^2); 2 | T_value = inf; 3 | for X= ceil(2^(-3/2)*w/sqrt(distance_max^2 - h_min^2)):1:ceil(2^(-3/2)*w/sqrt(distance_max^2 - h_max^2)) 4 | for Y = ceil(2^(-3/2)*l/sqrt(distance_max^2 - h_min^2)):1:ceil(2^(-3/2)*l/sqrt(distance_max^2 - h_max^2)) 5 | R_min = max([sqrt(distance_max^2 - h_max^2),w/(2*sqrt(2)*X),l/(2*sqrt(2)*Y)]); 6 | R_number = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,10^-5); 7 | value = The_integral(B,distance_max,Alpha,D,noise,P,d0,K,R_number,X,Y,density); 8 | if value < T_value 9 | T_value = value; 10 | X_value = X; 11 | Y_value = Y; 12 | R_value = R_number; 13 | end 14 | end 15 | end 16 | Un = 6; % UAV_number 17 | r = R_value; 18 | T_use = T_value; %T_use为P5问题中的T* 19 | X_use = X_value;%X_use为P5问题中的X* 20 | Y_use = Y_value;%Y_use为P5问题中的Y* 21 | f = zeros(1,Un+1); 22 | f(1) = 1; 23 | intcon = 2:Un; 24 | Aeq = ones(1,Un+1); 25 | Aeq(1) = 0 ; 26 | s = ones(1,Un); 27 | for i = 1:length(s) 28 | if i <= 2 29 | s(i) = 8; 30 | elseif i <= 4 31 | s(i) = 10; 32 | else 33 | s(i) = 12; 34 | end 35 | end 36 | A = zeros(Un,Un+1); 37 | b = zeros(Un,1); 38 | beq = X_use*Y_use; 39 | for i= 1:size(A) 40 | A(i,1) = -1; 41 | A(i,i+1) = 4*sqrt(2)*r/s(i) + T_use/(X_use*Y_use); 42 | end 43 | %x = intlinprog(f,intcon,A,b,Aeq,beq); 44 | [t,fval] = intlinprog(f,intcon,A,b,Aeq,beq); 45 | 46 | function[value] = The_integral(B,distance_max,Alpha,D,noise,P,d0,K,R_number,X,Y,density) 47 | prefix = 32*D*density*X*Y*log(2)/(Alpha*B); 48 | p = 2/Alpha*log(P*K*d0^Alpha/noise); 49 | func = @(theta,x) 1./(p-log(x)); 50 | x_max = @(theta) distance_max.^2 - (2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_number.^2; 51 | x_min = distance_max.^2 - R_number.^2; 52 | value = prefix * integral2(func,0,pi/4,x_min,x_max); 53 | end 54 | 55 | function[value] = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold) 56 | prefix = 32*D*X*Y*log(2)/(Alpha*B); 57 | p = 2/Alpha*log(P*K*d0^Alpha/noise); 58 | %lambda = (2.*cos(theta).^2-1)./(2.*cos(theta).^2); 59 | func_R_max = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_max./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_max.^2))+2.*R_max/(p-log(distance_max.^2-R_max.^2)); 60 | func_R_min = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_min./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_min.^2))+2.*R_min/(p-log(distance_max.^2-R_min.^2)); 61 | slope_R_max = prefix*integral(func_R_max,0,pi/4); 62 | slope_R_min = prefix*integral(func_R_min,0,pi/4); 63 | if slope_R_max == 0 && slope_R_min < 0 64 | value = R_min; 65 | elseif slope_R_max == 0 && slope_R_min > 0 66 | value = R_max; 67 | elseif slope_R_min == 0 && slope_R_max < 0 68 | value = R_max; 69 | elseif slope_R_min == 0 && slope_R_max > 0 70 | value = R_min; 71 | elseif slope_R_min > 0 && slope_R_max > 0 72 | value = R_min; 73 | elseif slope_R_min < 0 && slope_R_max < 0 74 | value = R_max; 75 | else 76 | R_number = (R_max+R_min)/2; 77 | func_R_number = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_number./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2)*R_number.^2))+2.*R_number/(p-log(distance_max.^2-R_number.^2)); 78 | slope_R_number = prefix*integral(func_R_number,0,pi/4); 79 | if slope_R_min < 0 && slope_R_number < 0 80 | if slope_R_max - slope_R_number <= threshold 81 | value = R_number; 82 | else 83 | R_min = R_number; 84 | value = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold); 85 | end 86 | elseif slope_R_min < 0 && slope_R_number > 0 87 | if slope_R_number - slope_R_min <= threshold 88 | value = R_number; 89 | else 90 | R_max = R_number; 91 | value = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold); 92 | end 93 | end 94 | end 95 | end -------------------------------------------------------------------------------- /new_X_Y_R_compare.m: -------------------------------------------------------------------------------- 1 | w_min = 100; 2 | w_max = 4000; 3 | w_gap = 50; 4 | l_min = 100; 5 | l_max = 4000; 6 | l_gap = 50; 7 | row = (w_max - w_min)/w_gap + 1; 8 | column = (l_max - l_min)/l_gap + 1; 9 | T_array = []; 10 | X_array = [];%X数组存储w,l对应的X值用于compare2 11 | Y_array = [];%Y数组存储w,l对应的Y值用于compare2 12 | R_array = [];%R数组存储w,l对应的R值用于compare2 13 | w_MonteCarlo = []; 14 | l_MonteCarlo = []; 15 | for w = w_min:w_gap:w_max 16 | for l = l_min:l_gap:l_max 17 | w_MonteCarlo(end+1) = w; 18 | l_MonteCarlo(end+1) = l; 19 | R_max = sqrt(distance_max^2 - h_min^2); 20 | T_value = inf; 21 | for X= ceil(2^(-3/2)*w/sqrt(distance_max^2 - h_min^2)):1:ceil(2^(-3/2)*w/sqrt(distance_max^2 - h_max^2)) 22 | for Y = ceil(2^(-3/2)*l/sqrt(distance_max^2 - h_min^2)):1:ceil(2^(-3/2)*l/sqrt(distance_max^2 - h_max^2)) 23 | R_min = max([sqrt(distance_max^2 - h_max^2),w/(2*sqrt(2)*X),l/(2*sqrt(2)*Y)]); 24 | R_number = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,10^-5); 25 | value = The_integral(B,distance_max,Alpha,D,noise,P,d0,K,R_number,X,Y,density); 26 | if value < T_value 27 | T_value = value; 28 | X_value = X; 29 | Y_value = Y; 30 | R_value = R_number; 31 | end 32 | end 33 | end 34 | T_array(end+1) = T_value; 35 | X_array(end+1) = X_value; 36 | Y_array(end+1) = Y_value; 37 | R_array(end+1) = R_value; 38 | end 39 | end 40 | T_array = reshape(T_array,row,column); 41 | [W,L] = meshgrid(w_min:w_gap:w_max,l_min:l_gap:l_max); 42 | C = zeros(size(W)); 43 | surf(W,L,T_array,C); 44 | colormap = ([0,0,1]); 45 | xlabel('w'); 46 | ylabel('l'); 47 | zlabel('T'); 48 | % text(X_value,Y_value,R_value,' \leftarrow R最大值'); 49 | title('w-l-T'); 50 | 51 | function[value] = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold) 52 | prefix = 32*D*X*Y*log(2)/(Alpha*B); 53 | p = 2/Alpha*log(P*K*d0^Alpha/noise); 54 | %lambda = (2.*cos(theta).^2-1)./(2.*cos(theta).^2); 55 | func_R_max = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_max./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_max.^2))+2.*R_max/(p-log(distance_max.^2-R_max.^2)); 56 | func_R_min = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_min./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_min.^2))+2.*R_min/(p-log(distance_max.^2-R_min.^2)); 57 | slope_R_max = prefix*integral(func_R_max,0,pi/4); 58 | slope_R_min = prefix*integral(func_R_min,0,pi/4); 59 | if slope_R_max == 0 && slope_R_min < 0 60 | value = R_min; 61 | elseif slope_R_max == 0 && slope_R_min > 0 62 | value = R_max; 63 | elseif slope_R_min == 0 && slope_R_max < 0 64 | value = R_max; 65 | elseif slope_R_min == 0 && slope_R_max > 0 66 | value = R_min; 67 | elseif slope_R_min > 0 && slope_R_max > 0 68 | value = R_min; 69 | elseif slope_R_min < 0 && slope_R_max < 0 70 | value = R_max; 71 | else 72 | R_number = (R_max+R_min)/2; 73 | func_R_number = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_number./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2)*R_number.^2))+2.*R_number/(p-log(distance_max.^2-R_number.^2)); 74 | slope_R_number = prefix*integral(func_R_number,0,pi/4); 75 | if slope_R_min < 0 && slope_R_number < 0 76 | if slope_R_max - slope_R_number <= threshold 77 | value = R_number; 78 | else 79 | R_min = R_number; 80 | value = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold); 81 | end 82 | elseif slope_R_min < 0 && slope_R_number > 0 83 | if slope_R_number - slope_R_min <= threshold 84 | value = R_number; 85 | else 86 | R_max = R_number; 87 | value = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold); 88 | end 89 | end 90 | end 91 | end 92 | 93 | function[value] = The_integral(B,distance_max,Alpha,D,noise,P,d0,K,R_number,X,Y,density) 94 | prefix = 32*D*density*X*Y*log(2)/(Alpha*B); 95 | p = 2/Alpha*log(P*K*d0^Alpha/noise); 96 | func = @(theta,x) 1./(p-log(x)); 97 | x_max = @(theta) distance_max.^2 - (2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_number.^2; 98 | x_min = distance_max.^2 - R_number.^2; 99 | value = prefix * integral2(func,0,pi/4,x_min,x_max); 100 | end -------------------------------------------------------------------------------- /new_X_Y_R_compare1.m: -------------------------------------------------------------------------------- 1 | w_min = 100; 2 | w_max = 4000; 3 | w_gap = 50; 4 | l_min = 100; 5 | l_max = 4000; 6 | l_gap = 50; 7 | row = (w_max - w_min)/w_gap + 1; 8 | colmun = (l_max - l_min)/l_gap + 1; 9 | T = []; 10 | for w = w_min:w_gap:w_max 11 | for l = l_min:l_gap:l_max 12 | R_max = sqrt(distance_max^2 - h_min^2); 13 | X = ceil(2^(-3/2)*w/sqrt(distance_max^2 - h_min^2)); 14 | Y = ceil(2^(-3/2)*l/sqrt(distance_max^2 - h_min^2)); 15 | % R_value = max([sqrt(distance_max^2 - h_max^2),w/(2*sqrt(2)*X),l/(2*sqrt(2)*Y)]); 16 | prefix = 32*D*density*X*Y*log(2)/(Alpha*B); 17 | p = 2/Alpha*log(P*K*d0^Alpha/noise); 18 | func = @(theta,x) 1./(p-log(x)); 19 | x_max = @(theta) distance_max.^2 - (2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_max.^2; 20 | x_min = distance_max.^2 - R_max.^2; 21 | T_value = prefix * integral2(func,0,pi/4,x_min,x_max); 22 | T(end+1) = T_value; 23 | end 24 | end 25 | T = reshape(T,row,colmun); 26 | [W,L] = meshgrid(w_min:w_gap:w_max,l_min:l_gap:l_max); 27 | C = zeros(size(W)); 28 | surf(W,L,T,C); 29 | colormap = ([1,0,0]); 30 | alpha(0.5); 31 | xlabel('w'); 32 | ylabel('l'); 33 | zlabel('T'); 34 | % text(X_value,Y_value,R_value,' \leftarrow R最大值'); 35 | title('w-l-T'); -------------------------------------------------------------------------------- /new_X_Y_R_compare2.m: -------------------------------------------------------------------------------- 1 | w_min = 100; 2 | w_max = 4000; 3 | w_gap = 50; 4 | l_min = 100; 5 | l_max = 4000; 6 | l_gap = 50; 7 | row = (w_max - w_min)/w_gap + 1; 8 | column = (l_max - l_min)/l_gap + 1; 9 | T_average = []; 10 | for i = 1:length(X_array(:)) 11 | r = R_array(i); 12 | h = sqrt(distance_max^2 - r^2); 13 | X = X_array(i); 14 | Y = Y_array(i); 15 | %t_average = Time_average(w_MonteCarlo,l_MonteCarlo,i,B,Alpha,D,noise,P,d0,K,h,r,X,Y,density); 16 | t_average = Time_average(B,Alpha,D,noise,P,d0,K,h,r,X,Y,density); 17 | T_average(end+1) = t_average; 18 | end 19 | T_average = reshape(T_average,row,column); 20 | [W,L] = meshgrid(w_min:w_gap:w_max,l_min:l_gap:l_max); 21 | C = zeros(size(W)); 22 | surf(W,L,T_average,C); 23 | colormap = ([1,0,0]); 24 | %alpha(0.5); 25 | xlabel('w'); 26 | ylabel('l'); 27 | zlabel('T'); 28 | % text(X_value,Y_value,R_value,' \leftarrow R最大值'); 29 | title('w-l-T'); 30 | 31 | function[T_average] = Time_average(B,Alpha,D,noise,P,d0,K,h,r,X,Y,density) 32 | time = []; 33 | loop_number = 100; 34 | UE_real_number = ceil(2*r^2*density); 35 | for i = 1:loop_number 36 | this_time = 0; 37 | for j = 1:UE_real_number 38 | %theta = rand(1,1)*2*pi; 39 | %R = unifrnd(0,r^2,1); 40 | %x = cos(theta)*sqrt(R); 41 | %y = sin(theta)*sqrt(R); 42 | x = unifrnd(sqrt(2)/(-2)*r,sqrt(2)/2*r); 43 | y = unifrnd(sqrt(2)/(-2)*r,sqrt(2)/2*r); 44 | distance = sqrt(x^2 + y^2+h^2); 45 | Pr = P*K*(d0/distance)^Alpha; 46 | SNR = Pr/noise; 47 | rate = B*log2(1+SNR); 48 | t = D/rate; 49 | this_time = this_time + t; 50 | end 51 | time(end+1) = this_time; 52 | end 53 | %w = w_MonteCarlo(number); 54 | %l = l_MonteCarlo(number); 55 | T_average = mean(time(:))*4*X*Y; 56 | end -------------------------------------------------------------------------------- /new_X_Y_R_compare3.m: -------------------------------------------------------------------------------- 1 | w_min = 100; 2 | w_max = 4000; 3 | w_gap = 100; 4 | l_min = 100; 5 | l_max = 4000; 6 | l_gap = 100; 7 | row = (w_max - w_min)/w_gap + 1; 8 | column = (l_max - l_min)/l_gap + 1; 9 | T_square = []; 10 | T_square_compare_min = []; 11 | T_square_compare_max = []; 12 | T_square_average = []; 13 | theta_all = []; 14 | for w = w_min:w_gap:w_max 15 | l = w; 16 | R_max = sqrt(distance_max^2 - h_min^2); 17 | T_value = inf; 18 | for X= ceil(2^(-3/2)*w/sqrt(distance_max^2 - h_min^2)):1:ceil(2^(-3/2)*w/sqrt(distance_max^2 - h_max^2)) 19 | for Y = ceil(2^(-3/2)*l/sqrt(distance_max^2 - h_min^2)):1:ceil(2^(-3/2)*l/sqrt(distance_max^2 - h_max^2)) 20 | R_min = max([sqrt(distance_max^2 - h_max^2),w/(2*sqrt(2)*X),l/(2*sqrt(2)*Y)]); 21 | R_number = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,10^-5); 22 | value = The_integral(B,distance_max,Alpha,D,noise,P,d0,K,R_number,X,Y,density); 23 | if value < T_value 24 | T_value = value; 25 | X_value = X; 26 | Y_value = Y; 27 | R_value = R_number; 28 | end 29 | end 30 | end 31 | theta = acos(R_value/distance_max)*180/pi; 32 | theta_all(end+1) = theta; 33 | %compare组的X,Y 34 | X1 = ceil(2^(-3/2)*w/sqrt(distance_max^2 - h_max^2)); 35 | Y1 = ceil(2^(-3/2)*l/sqrt(distance_max^2 - h_max^2)); 36 | X2 = ceil(2^(-3/2)*w/sqrt(distance_max^2 - h_min^2)); 37 | Y2 = ceil(2^(-3/2)*w/sqrt(distance_max^2 - h_min^2)); 38 | T_square(end+1) = T_value; 39 | %T_square_compare(end+1) = The_integral(B,distance_max,Alpha,D,noise,P,d0,K,R_max,X1,Y1,density); 40 | T_square_compare_min(end+1) = The_integral(B,distance_max,Alpha,D,noise,P,d0,K,R_min,X1,Y1,density); 41 | T_square_compare_max(end+1) = The_integral(B,distance_max,Alpha,D,noise,P,d0,K,R_max,X2,Y2,density); 42 | T_square_average(end+1) = Time_average(B,Alpha,D,noise,P,d0,K,sqrt(distance_max^2-R_value^2),R_value,X_value,Y_value,density); 43 | end 44 | 45 | make_config(T_square,T_square_compare_min,T_square_compare_max,T_square_average,w_min,w_gap,w_max); 46 | 47 | function[T_average] = Time_average(B,Alpha,D,noise,P,d0,K,h,r,X,Y,density) 48 | time = []; 49 | loop_number = 100; 50 | UE_real_number = ceil(2*r^2*density); 51 | for i = 1:loop_number 52 | this_time = 0; 53 | for j = 1:UE_real_number 54 | %theta = rand(1,1)*2*pi; 55 | %R = unifrnd(0,r^2,1); 56 | %x = cos(theta)*sqrt(R); 57 | %y = sin(theta)*sqrt(R); 58 | x = unifrnd(sqrt(2)/(-2)*r,sqrt(2)/2*r); 59 | y = unifrnd(sqrt(2)/(-2)*r,sqrt(2)/2*r); 60 | distance = sqrt(x^2 + y^2+h^2); 61 | Pr = P*K*(d0/distance)^Alpha; 62 | SNR = Pr/noise; 63 | rate = B*log2(1+SNR); 64 | t = D/rate; 65 | this_time = this_time + t; 66 | end 67 | time(end+1) = this_time; 68 | end 69 | %T_average = mean(time(:))*(8*r^2*(X-1)*(Y-1)+(X-1)*2*sqrt(2)*r*(l-(Y-1)*2*sqrt(2)*r)+(Y-1)*2*sqrt(2)*r*(w-(X-1)*2*sqrt(2)*r)+(l-(Y-1)*2*sqrt(2)*r)*(w-(X-1)*2*sqrt(2)*r))*density; 70 | T_average = mean(time(:))*4*X*Y; 71 | end 72 | 73 | function[value] = The_integral(B,distance_max,Alpha,D,noise,P,d0,K,R_number,X,Y,density) 74 | prefix = 32*D*density*X*Y*log(2)/(Alpha*B); 75 | p = 2/Alpha*log(P*K*d0^Alpha/noise); 76 | func = @(theta,x) 1./(p-log(x)); 77 | x_max = @(theta) distance_max.^2 - (2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_number.^2; 78 | x_min = distance_max.^2 - R_number.^2; 79 | value = prefix * integral2(func,0,pi/4,x_min,x_max); 80 | end 81 | 82 | function[value] = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold) 83 | prefix = 32*D*X*Y*log(2)/(Alpha*B); 84 | p = 2/Alpha*log(P*K*d0^Alpha/noise); 85 | %lambda = (2.*cos(theta).^2-1)./(2.*cos(theta).^2); 86 | func_R_max = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_max./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_max.^2))+2.*R_max/(p-log(distance_max.^2-R_max.^2)); 87 | func_R_min = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_min./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_min.^2))+2.*R_min/(p-log(distance_max.^2-R_min.^2)); 88 | slope_R_max = prefix*integral(func_R_max,0,pi/4); 89 | slope_R_min = prefix*integral(func_R_min,0,pi/4); 90 | if slope_R_max == 0 && slope_R_min < 0 91 | value = R_min; 92 | elseif slope_R_max == 0 && slope_R_min > 0 93 | value = R_max; 94 | elseif slope_R_min == 0 && slope_R_max < 0 95 | value = R_max; 96 | elseif slope_R_min == 0 && slope_R_max > 0 97 | value = R_min; 98 | elseif slope_R_min > 0 && slope_R_max > 0 99 | value = R_min; 100 | elseif slope_R_min < 0 && slope_R_max < 0 101 | value = R_max; 102 | else 103 | R_number = (R_max+R_min)/2; 104 | func_R_number = @(theta) -2.*(2.*cos(theta).^2-1)./(2.*cos(theta).^2).*R_number./(p-log(distance_max.^2-(2.*cos(theta).^2-1)./(2.*cos(theta).^2)*R_number.^2))+2.*R_number/(p-log(distance_max.^2-R_number.^2)); 105 | slope_R_number = prefix*integral(func_R_number,0,pi/4); 106 | if slope_R_min < 0 && slope_R_number < 0 107 | if slope_R_max - slope_R_number <= threshold 108 | value = R_number; 109 | else 110 | R_min = R_number; 111 | value = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold); 112 | end 113 | elseif slope_R_min < 0 && slope_R_number > 0 114 | if slope_R_number - slope_R_min <= threshold 115 | value = R_number; 116 | else 117 | R_max = R_number; 118 | value = select_R(B,distance_max,Alpha,D,noise,P,d0,K,X,Y,R_max,R_min,threshold); 119 | end 120 | end 121 | end 122 | end 123 | 124 | 125 | 126 | function[] = make_config(T_square,T_square_compare_min,T_square_compare_max,T_square_average,w_min,w_gap,w_max) 127 | x = w_min:w_gap:w_max; 128 | plot(x,T_square,'-*b',x,T_square_compare_min,'-*r',x,T_square_compare_max,'-*k',x,T_square_average,'-*g'); 129 | % scatter(x,flip(t_max_h(1:6)),'*','r',x,flip(t_min_h(1:6)),'*','r',x,flip(t_max_h(7:12)),'*','r',x,flip(t_min_h(7:12)),'or',x,flip(t_max_h(13:18)),'xr',x,flip(t_min_h(13:18)),'xr'); 130 | legend('T.square','T.square.compare.Rmin','T.square.compare.Rmax','T.square.average'); 131 | xlabel('长度/m') 132 | ylabel('时间/s') 133 | end -------------------------------------------------------------------------------- /new_single_area_multi_users.m: -------------------------------------------------------------------------------- 1 | 2 | t_compare_h = []; 3 | t_average_h = []; 4 | t_max_h = []; 5 | t_min_h = []; 6 | for threshold = 5:5:15 7 | for h = 110:-10:50 8 | distance_max = (P*K/(threshold*noise))^(1/Alpha)*d0; 9 | r = sqrt(distance_max^2 - h^2); 10 | T_compare = Time_compare(B,Alpha,D,noise,P,d0,K,distance_max,r,density); 11 | t_compare_h(end+1) = T_compare; 12 | T = Time_average(B,Alpha,D,noise,P,d0,K,h,r,density); 13 | T_max = T(1); 14 | T_average = T(2); 15 | T_min = T(3); 16 | t_max_h(end+1) = T_max; 17 | t_average_h(end+1) = T_average; 18 | t_min_h(end+1) = T_min; 19 | end 20 | end 21 | 22 | make_config(t_compare_h,t_max_h,t_average_h,t_min_h) 23 | 24 | function[T_compare] = Time_compare(B,Alpha,D,noise,P,d0,K,distance_max,r,density) 25 | % h = 100; 26 | p = 2/Alpha*log(P*K*d0^Alpha/noise); 27 | % 积分函数 28 | prefix = 2*D*log(2)/(Alpha*B)/r^2; 29 | func = @(x) 1./(p-log(x)); 30 | T_compare = prefix * integral(func,distance_max^2-r^2,distance_max^2)*pi*r^2*density; 31 | %*int(1/(p-log(x)*(1/r^2)),distance_max^2-r^2,distance_max^2) 32 | end 33 | 34 | function[T] = Time_average(B,Alpha,D,noise,P,d0,K,h,r,density) 35 | x_point = []; 36 | y_point = []; 37 | time = []; 38 | UE_number = 1000; 39 | UE_real_number = floor(pi*r^2*density); 40 | %UE_real_number 41 | for i = 1:UE_number 42 | this_t = 0; 43 | for j = 1:UE_real_number 44 | theta = rand(1,1)*2*pi; 45 | R = unifrnd(0,r^2,1); 46 | x0 = cos(theta)*sqrt(R); 47 | y0 = sin(theta)*sqrt(R); 48 | distance = sqrt(x0^2 + y0^2+h^2); 49 | Pr = P*K*(d0/distance)^Alpha; 50 | SNR = Pr/noise; 51 | rate = B*log2(1+SNR); 52 | t = D/rate; 53 | this_t = this_t + t; 54 | end 55 | time(end+1) = this_t; 56 | end 57 | T_max = max(time); 58 | T_average = mean(time(:)); 59 | T_min = min(time); 60 | T = [T_max,T_average,T_min]; 61 | end 62 | 63 | function[] = make_config(t_compare_h,t_max_h,t_average_h,t_min_h) 64 | x = 50:10:110; 65 | plot(x,flip(t_compare_h(1:7)),'-*b',x,flip(t_average_h(1:7)),'--*r',x,flip(t_compare_h(8:14)),'-ob',x,flip(t_average_h(8:14)),'--or',x,flip(t_compare_h(15:21)),'-xb',x,flip(t_average_h(15:21)),'--xr'); 66 | % scatter(x,flip(t_max_h(1:6)),'*','r',x,flip(t_min_h(1:6)),'*','r',x,flip(t_max_h(7:12)),'*','r',x,flip(t_min_h(7:12)),'or',x,flip(t_max_h(13:18)),'xr',x,flip(t_min_h(13:18)),'xr'); 67 | legend('\gamma=5,T_v','\gamma=5,T_a','\gamma=10,T_v','\gamma=10,T_a','\gamma=15,T_v','\gamma=15,T_a'); 68 | xlabel('高度/m') 69 | ylabel('时间/s') 70 | end -------------------------------------------------------------------------------- /new_single_area_single_user.m: -------------------------------------------------------------------------------- 1 | t_compare_h = []; 2 | t_average_h = []; 3 | t_max_h = []; 4 | t_min_h = []; 5 | %theta_all = []; 6 | for threshold = 5:5:15 7 | for h = 110:-10:50 8 | distance_max = (P*K/(threshold*noise))^(1/Alpha)*d0; 9 | r = sqrt(distance_max^2 - h^2); 10 | %theta = asin(h/distance_max)*180/pi; 11 | %theta_all(end+1) = theta; 12 | T_compare = Time_compare(B,Alpha,D,noise,P,d0,K,distance_max,r); 13 | t_compare_h(end+1) = T_compare; 14 | T = Time_average(B,Alpha,D,noise,P,d0,K,h,r); 15 | T_max = T(1); 16 | T_average = T(2); 17 | T_min = T(3); 18 | t_max_h(end+1) = T_max; 19 | t_average_h(end+1) = T_average; 20 | t_min_h(end+1) = T_min; 21 | end 22 | end 23 | 24 | make_config(t_compare_h,t_max_h,t_average_h,t_min_h) 25 | 26 | function[T_compare] = Time_compare(B,Alpha,D,noise,P,d0,K,distance_max,r) 27 | % h = 100; 28 | p = 2/Alpha*log(P*K*d0^Alpha/noise); 29 | % 积分函数 30 | prefix = 2*D*log(2)/(Alpha*B)/r^2; 31 | func = @(x) 1./(p-log(x)); 32 | T_compare = prefix * integral(func,distance_max^2-r^2,distance_max^2); 33 | %*int(1/(p-log(x)*(1/r^2)),distance_max^2-r^2,distance_max^2) 34 | end 35 | 36 | function[T] = Time_average(B,Alpha,D,noise,P,d0,K,h,r) 37 | x_point = []; 38 | y_point = []; 39 | time = []; 40 | UE_number = 10000; 41 | for i = 1:UE_number 42 | theta = rand(1,1)*2*pi; 43 | R = unifrnd(0,r^2,1); 44 | x = cos(theta)*sqrt(R); 45 | y = sin(theta)*sqrt(R); 46 | x_point(end+1) = x; 47 | y_point(end+1) = y; 48 | end 49 | for i = 1:UE_number 50 | x0 = x_point(i); 51 | y0 = y_point(i); 52 | distance = sqrt(x0^2 + y0^2+h^2); 53 | Pr = P*K*(d0/distance)^Alpha; 54 | SNR = Pr/noise; 55 | rate = B*log2(1+SNR); 56 | t = D/rate; 57 | time(end+1) = t; 58 | end 59 | T_max = max(time); 60 | T_average = mean(time(:)); 61 | T_min = min(time); 62 | T = [T_max,T_average,T_min]; 63 | end 64 | 65 | function[] = make_config(t_compare_h,t_max_h,t_average_h,t_min_h) 66 | x = 50:10:110; 67 | plot(x,flip(t_compare_h(1:7)),'-*b',x,flip(t_average_h(1:7)),'--*r',x,flip(t_compare_h(8:14)),'-ob',x,flip(t_average_h(8:14)),'--or',x,flip(t_compare_h(15:21)),'-xb',x,flip(t_average_h(15:21)),'--xr'); 68 | % scatter(x,flip(t_max_h(1:6)),'*','r',x,flip(t_min_h(1:6)),'*','r',x,flip(t_max_h(7:12)),'*','r',x,flip(t_min_h(7:12)),'or',x,flip(t_max_h(13:18)),'xr',x,flip(t_min_h(13:18)),'xr'); 69 | legend('\gamma=5,T_3','\gamma=5,T_\alpha','\gamma=10,T_3','\gamma=10,T_\alpha','\gamma=15,T_3','\gamma=15,T_\alpha'); 70 | xlabel('高度/m') 71 | ylabel('时间/s') 72 | end -------------------------------------------------------------------------------- /new_traceroute.m: -------------------------------------------------------------------------------- 1 | %% 建立网格图 2 | % 原始数据 3 | x_axis = 0 : X_use; 4 | y_axis = 0 : Y_use; 5 | % 两根线的数据 6 | x1 = [x_axis(1) x_axis(end)]'; 7 | y1 = [y_axis(1) y_axis(end)]'; 8 | % 所有线的xData 9 | x2 = repmat(x1, 1, length(y_axis)-2); 10 | x3 = repmat(x_axis(2) : x_axis(end-1), 2, 1); 11 | xData = [x2 x3]; 12 | % 所有线的yData 13 | y2 = repmat(y1, 1, length(x_axis)-2); 14 | y3 = repmat(y_axis(2) : y_axis(end-1), 2, 1); 15 | yData = [y3 y2]; 16 | % 绘图 17 | h = line(xData, yData); 18 | box on; 19 | set(h, 'Color', 'k'); 20 | 21 | % 涂色方法(涂第i,j个格子) 22 | % i = 5; 23 | % j = 2; 24 | % x = [0 1 1 0] + i - 1; 25 | % y = [0 0 1 1] + j - 1; 26 | % patch('xData', x, 'yData', y, 'FaceColor', 'r'); 27 | 28 | %% 开始选格子 29 | %grid_number = sort(x(2:end)); 30 | grid_number = t(2:end); 31 | index_matrix = zeros(2*length(grid_number),round(max(grid_number))); 32 | grid_select = ones(X_use,Y_use); 33 | color_select = ['r','g','b','y','m','c','k','w']; 34 | for i = 1:X_use 35 | for j = 1:Y_use 36 | grid_select(i,j) = sqrt(i^2 + j^2); 37 | end 38 | end 39 | %grid_select 40 | index = 1; 41 | while (index <= length(grid_number)) 42 | this_number = 1; 43 | max_value = max(max(grid_select)); 44 | [i,j] = find(grid_select==max_value); 45 | %select_i,select_j为本次选定的格子 46 | select_i = i(1); 47 | select_j = j(1); 48 | %记录每次选的格子索引,奇数行为行索引,偶数行记录列索引,用于划线 49 | index_matrix(2*index-1,this_number) = select_i; 50 | index_matrix(2*index,this_number) = select_j; 51 | x_axis = [0 1 1 0] + select_i - 1; 52 | y_axis = [0 0 1 1] + select_j - 1; 53 | patch('xData', x_axis, 'yData', y_axis, 'FaceColor', color_select(index)); 54 | grid_select(select_i,select_j) = 0; 55 | this_number = this_number + 1; 56 | %this_number 57 | grid_this_select = grid_select; 58 | for i =1:X_use 59 | for j = 1:Y_use 60 | if grid_select(i,j) ~= 0 61 | grid_this_select(i,j) = -1; 62 | end 63 | end 64 | end 65 | while this_number <= int8(grid_number(index)) 66 | if select_i + 1 <= X_use && grid_this_select(select_i+1,select_j) == -1 67 | grid_this_select(select_i+1,select_j) = sqrt((select_i+1)^2 + select_j^2); 68 | end 69 | if select_i - 1 >= 1 && grid_this_select(select_i-1,select_j) == -1 70 | grid_this_select(select_i-1,select_j) = sqrt((select_i-1)^2 + select_j^2); 71 | end 72 | if select_j + 1 <= Y_use && grid_this_select(select_i,select_j+1) == -1 73 | grid_this_select(select_i,select_j+1) = sqrt((select_j+1)^2 + select_i^2); 74 | end 75 | if select_j - 1 >= 1 && grid_this_select(select_i,select_j-1) == -1 76 | grid_this_select(select_i,select_j-1) = sqrt((select_j-1)^2 + select_i^2); 77 | end 78 | max_value = max(max(grid_this_select)); 79 | [i,j] = find(grid_this_select==max_value); 80 | select_i = i(1); 81 | select_j = j(1); 82 | index_matrix(2*index-1,this_number) = select_i; 83 | index_matrix(2*index,this_number) = select_j; 84 | x_axis = [0 1 1 0] + select_i - 1; 85 | y_axis = [0 0 1 1] + select_j - 1; 86 | patch('xData', x_axis, 'yData', y_axis, 'FaceColor', color_select(index)); 87 | grid_this_select(select_i,select_j) = 0; 88 | grid_select(select_i,select_j) = 0; 89 | this_number = this_number + 1; 90 | end 91 | %this_number 92 | %grid_select 93 | %index 94 | index = index + 1; 95 | %index 96 | end -------------------------------------------------------------------------------- /new_traceroute_2.m: -------------------------------------------------------------------------------- 1 | x_point = []; 2 | y_point = []; 3 | for i = 1:X_use 4 | for j = 1:Y_use 5 | x_point(end+1) = i-0.25; 6 | x_point(end+1) = i-0.25; 7 | x_point(end+1) = i-0.75; 8 | x_point(end+1) = i-0.75; 9 | y_point(end+1) = j - 0.25; 10 | y_point(end+1) = j - 0.75; 11 | y_point(end+1) = j - 0.25; 12 | y_point(end+1) = j - 0.75; 13 | end 14 | end 15 | plot(x_point,y_point,'+'); 16 | axis([0 X_use 0 Y_use]); 17 | 18 | hold on 19 | %1:length(grid_number) 20 | for species = 1:length(grid_number) 21 | available_number = 1; 22 | start_index = 1; 23 | max_index = size(index_matrix,2); 24 | for this_index = 2:max_index 25 | if index_matrix(2*species-1,this_index) ~= 0 26 | available_number = available_number + 1; 27 | %if sqrt((index_matrix(2*species-1,this_index)-0.75)^2 + (index_matrix(2*species,this_index)-0.75)^2) < sqrt((index_matrix(2*species-1,start_index)-0.75)^2 + (index_matrix(2*species,start_index)-0.75)^2) 28 | if index_matrix(2*species-1,this_index) + index_matrix(2*species,this_index) < index_matrix(2*species-1,start_index) + index_matrix(2*species,start_index) 29 | start_index = this_index; 30 | end 31 | end 32 | end 33 | % index_matrix(2*species-1,start_index) = 0; 34 | % index_matrix(2*species,start_index) = 0; 35 | % available_number = available_number - 1; 36 | start_x = index_matrix(2*species-1,start_index); 37 | start_y = index_matrix(2*species,start_index); 38 | plot(start_x-0.75,start_y-0.75,'ok','MarkerSize',10,'MarkerFaceColor',[0.5,0.5,0.5]); 39 | drawLine_Arrow([start_x-0.75 start_y-0.75],[start_x-0.25,start_y - 0.75],1,species); 40 | drawLine_Arrow([start_x-0.25 start_y-0.75],[start_x-0.25,start_y - 0.25],1,species); 41 | drawLine_Arrow([start_x-0.25 start_y-0.25],[start_x-0.75,start_y - 0.25],1,species); 42 | drawLine_Arrow([start_x-0.75 start_y-0.25],[start_x-0.75,start_y - 0.75],1,species); 43 | index_matrix(2*species-1,start_index) = 0; 44 | index_matrix(2*species,start_index) = 0; 45 | available_number = available_number - 1; 46 | [index_matrix,available_number] = draw(index_matrix,species,max_index,start_x,start_y,available_number); 47 | end 48 | hold off 49 | 50 | function drawLine_Arrow(start_point,end_point,select,species) 51 | % 绘制带箭头的直线 52 | % 从start_point到end_point画一箭头,arrColor箭头颜色,arrSize,箭头大小 53 | %判断参数多少 54 | switch select 55 | case 1 56 | fill_color_select = ['r','g','b','y','m','c','k','w']; 57 | arrColor = fill_color_select(species); 58 | lineColor = 'b'; 59 | arrowSize = 12; 60 | lineWidth = 1; 61 | case 2 62 | arrColor = 'w'; 63 | lineColor = 'w'; 64 | arrowSize = 12; 65 | lineWidth = 1; 66 | end 67 | K= 0.5; % 箭头比例系数 68 | theta= pi / 8; % 箭头角度 69 | A1 = [cos(theta), -sin(theta);sin(theta), cos(theta)]; % 箭头左侧 70 | theta = -theta; 71 | A2 = [cos(theta), -sin(theta);sin(theta), cos(theta)]; % 箭头右侧 72 | arrow = start_point' - end_point'; 73 | %使得箭头跟直线长短无关(固定值) 74 | arrow_1(1) = end_point(1,1)-arrowSize*cos(theta)+K*sin(theta); 75 | arrow_2(1) = end_point(1,1)-arrowSize*cos(theta)-K*sin(theta); 76 | arrow_1(2) = end_point(1,2)-arrowSize*sin(theta)-K*cos(theta); 77 | arrow_2(2) = end_point(1,2)-arrowSize*sin(theta)+K*cos(theta); 78 | arrow_1= A1 * arrow; 79 | arrow_2= A2 * arrow; 80 | arrow_1= K * arrow_1 + end_point'; % 箭头的边的x坐标 81 | arrow_2= K * arrow_2 + end_point'; % 箭头的边的y坐标 82 | hold on; 83 | grid on; 84 | axis equal; 85 | plot([start_point(1), end_point(1)], [start_point(2), end_point(2)],lineColor,'lineWidth',lineWidth); 86 | % 三角箭头(填充) 87 | triangle_x= [end_point(1),arrow_1(1),arrow_2(1),end_point(1)]; 88 | triangle_y= [end_point(2),arrow_1(2),arrow_2(2),end_point(2)]; 89 | this_fill = fill(triangle_x,triangle_y,arrColor); 90 | if arrColor == 'w' 91 | set(this_fill,'edgecolor',arrColor); 92 | end 93 | % 线段箭头(不填充) 94 | % plot([arrow_1(1), end_point(1)], [arrow_1(2), end_point(2)],color,'lineWidth',arrowSize); 95 | % plot([arrow_2(1), end_point(1)], [arrow_2(2), end_point(2)], color,'lineWidth',arrowSize); 96 | hold on; 97 | end 98 | 99 | %%递归函数画路线 100 | function[index_matrix,available_number] = draw(index_matrix,species,max_index,last_x,last_y,available_number) 101 | if available_number == 0 102 | return 103 | end 104 | for this_index = 1:max_index 105 | x = index_matrix(2*species-1,this_index); 106 | y = index_matrix(2*species,this_index); 107 | if x == last_x&&y == last_y+1 || x == last_x && y == last_y-1 || x == last_x-1&&y==last_y || x == last_x+1&&y == last_y 108 | if x == last_x + 1 && y == last_y 109 | drawLine_Arrow([x-0.75 y-0.75],[x-0.25,y - 0.75],1,species); 110 | drawLine_Arrow([x-0.25 y-0.75],[x-0.25,y - 0.25],1,species); 111 | drawLine_Arrow([x-0.25 y-0.25],[x-0.75,y - 0.25],1,species); 112 | %drawLine_Arrow([x-0.75 y-0.25],[x-0.75,y - 0.75],1,species); 113 | drawLine_Arrow([last_x-0.25 last_y-0.75],[x-0.75,y - 0.75],1,species); 114 | drawLine_Arrow([x-0.75 y-0.25],[last_x-0.25,last_y - 0.25],1,species); 115 | drawLine_Arrow([last_x-0.25 last_y-0.75],[last_x-0.25,last_y - 0.25],2,species); 116 | elseif x == last_x && y == last_y + 1 117 | %drawLine_Arrow([x-0.75 y-0.75],[x-0.25,y- 0.75],1,species); 118 | drawLine_Arrow([x-0.25 y-0.75],[x-0.25,y - 0.25],1,species); 119 | drawLine_Arrow([x-0.25 y-0.25],[x-0.75,y - 0.25],1,species); 120 | drawLine_Arrow([x-0.75 y-0.25],[x-0.75,y - 0.75],1,species); 121 | drawLine_Arrow([last_x-0.25 last_y-0.25],[x-0.25,y - 0.75],1,species); 122 | drawLine_Arrow([x-0.75 y-0.75],[last_x-0.75,last_y - 0.25],1,species); 123 | drawLine_Arrow([last_x-0.25 last_y-0.25],[last_x-0.75,last_y - 0.25],2,species); 124 | elseif x == last_x-1 && y == last_y 125 | drawLine_Arrow([x-0.75 y-0.75],[x-0.25,y- 0.75],1,species); 126 | %drawLine_Arrow([x-0.25 y-0.75],[x-0.25,y - 0.25],1,species); 127 | drawLine_Arrow([x-0.25 y-0.25],[x-0.75,y - 0.25],1,species); 128 | drawLine_Arrow([x-0.75 y-0.25],[x-0.75,y - 0.75],1,species); 129 | drawLine_Arrow([last_x-0.75 last_y-0.25],[x-0.25,y - 0.25],1,species); 130 | drawLine_Arrow([x-0.25 y-0.75],[last_x-0.75,last_y - 0.75],1,species); 131 | drawLine_Arrow([last_x-0.75 last_y-0.25],[last_x-0.75,last_y - 0.75],2,species); 132 | elseif x == last_x && y == last_y - 1 133 | drawLine_Arrow([x-0.75 y-0.75],[x-0.25,y- 0.75],1,species); 134 | drawLine_Arrow([x-0.25 y-0.75],[x-0.25,y - 0.25],1,species); 135 | %drawLine_Arrow([x-0.25 y-0.25],[x-0.75,y - 0.25],1,species); 136 | drawLine_Arrow([x-0.75 y-0.25],[x-0.75,y - 0.75],1,species); 137 | drawLine_Arrow([last_x-0.75 last_y-0.75],[x-0.75,y - 0.25],1,species); 138 | drawLine_Arrow([x-0.25 y-0.25],[last_x-0.25,last_y - 0.75],1,species); 139 | drawLine_Arrow([last_x-0.75 last_y-0.75],[last_x-0.25,last_y - 0.75],2,species); 140 | end 141 | index_matrix(2*species-1,this_index) = 0; 142 | index_matrix(2*species,this_index) = 0; 143 | available_number = available_number - 1; 144 | last_x = x; 145 | last_y = y; 146 | [index_matrix,available_number] = draw(index_matrix,species,max_index,last_x,last_y,available_number); 147 | end 148 | end 149 | end --------------------------------------------------------------------------------