├── FullSearchMethod.py ├── OptimumAndHoveringMethod.py ├── ProposedMethod.py ├── README.md ├── RandomAndHoveringMethod.py ├── RandomMethod.py └── SDPC.py /FullSearchMethod.py: -------------------------------------------------------------------------------- 1 | import random 2 | import math 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | from matplotlib.ticker import MultipleLocator 6 | import time 7 | import copy 8 | from sklearn.datasets._samples_generator import make_blobs 9 | from itertools import product, combinations 10 | import scipy.stats as st 11 | import SDPC 12 | 13 | n_components = 3 14 | X_surface, truth_surface = make_blobs(n_samples=300, centers=[[0.72, 0.72], [0.6, 1.4], [1.4, 1.20]], 15 | cluster_std=[0.24, 0.12, 0.18], random_state=42) 16 | x_surface = X_surface[:, 0] 17 | y_surface = X_surface[:, 1] 18 | xx_surface, yy_surface = np.mgrid[-0.02:1.98:51j, -0.02:1.98:51j] 19 | positions_surface = np.vstack([xx_surface.ravel(), yy_surface.ravel()]) 20 | values_surface = np.vstack([x_surface, y_surface]) 21 | kernel_surface = st.gaussian_kde(values_surface) 22 | f_surface = np.reshape(kernel_surface(positions_surface).T, xx_surface.shape) 23 | zz_surface = f_surface * 0.35 24 | 25 | X_sensor, y_true_sensor = make_blobs(n_samples=20000,centers=[[0.1, 0.1], [0.5, 0.5], [0.6, 1.25], [1.3, 0.6], [1.52, 1.4]], 26 | cluster_std=[0.2, 0.16, 0.204, 0.18, 0.2], random_state=0) 27 | x_sensors = X_sensor[:, 0] 28 | y_sensors = X_sensor[:, 1] 29 | xx_sensors, yy_sensors = np.mgrid[0:1.96:50j, 0:1.96:50j] 30 | positions_sensors = np.vstack([xx_sensors.ravel(), yy_sensors.ravel()]) 31 | values_sensor = np.vstack([x_sensors, y_sensors]) 32 | kernel_sensor = st.gaussian_kde(values_sensor) 33 | f_sensor = np.reshape(kernel_sensor(positions_sensors).T, xx_sensors.shape) 34 | zz_sensor = f_sensor * 37.865 # 20 000 sensors 35 | vector_sensor = np.vectorize(np.int32) 36 | zz_sensor = vector_sensor(zz_sensor) 37 | 38 | people_number = zz_sensor 39 | z_mountain = np.delete(zz_surface, 0, 40 | 0) # delete the first line of -0.02. It was added in order to male '0' as a center of the cell 41 | 42 | prohibited_zone = [] 43 | for a in range(35, 38): 44 | for b in range(35, 38): 45 | for c in np.arange(0.7, 0.8): 46 | prohibited_zone.append([a, b, c]) 47 | 48 | 49 | def distance(x1, y1, z1, x2, y2, z2): 50 | dist = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2 + (z1 - z2) ** 2) * 40 51 | return dist 52 | 53 | 54 | def check_coverage(d_u_s_max, theta, x, y, z): 55 | covered_cells_1 = [] 56 | covered_cells_2 = [] 57 | covered_cells_3 = [] 58 | covered_cells_final = [] 59 | r_max = d_u_s_max * math.sin(theta / 2) 60 | for x_cell in range(0, 50): 61 | for y_cell in range(0, 50): 62 | r_k_i = math.sqrt((x - x_cell) ** 2 + (y - y_cell) ** 2) * 40 63 | if r_max >= r_k_i: 64 | covered_cells_1.append([x_cell, y_cell]) 65 | for cell in covered_cells_1: 66 | d_k_i = distance(x, y, z, cell[0], cell[1], z_mountain[cell[0]][cell[1]]) 67 | if d_u_s_max >= d_k_i: 68 | covered_cells_2.append([cell[0], cell[1]]) 69 | for cell in covered_cells_2: 70 | d_k_i = distance(x, y, z, cell[0], cell[1], z_mountain[cell[0]][cell[1]]) 71 | d = ((z - z_mountain[cell[0]][cell[1]]) * 40) / math.cos(theta / 2) 72 | if (d_k_i <= d) and (z_mountain[cell[0]][cell[1]] <= z): 73 | covered_cells_3.append([cell[0], cell[1]]) 74 | for cell in covered_cells_3: 75 | cell_j = SDPC.coordinate_and_compare_height(x, y, cell[0], cell[1]) 76 | for cell_j_axis in cell_j: 77 | z_j_LOS_k_i = (z - z_mountain[cell[0]][cell[1]]) / ( 78 | math.sqrt((x - cell[0]) ** 2 + (y - cell[1]) ** 2)) * math.sqrt( 79 | (cell_j_axis[0] - cell[0]) ** 2 + (cell_j_axis[1] - cell[1]) ** 2) + z_mountain[cell[0]][cell[1]] 80 | if z_mountain[cell_j_axis[0]][cell_j_axis[1]] < z_j_LOS_k_i: 81 | if [cell[0], cell[1]] not in covered_cells_final: 82 | covered_cells_final.append([cell[0], cell[1]]) 83 | return covered_cells_final 84 | 85 | 86 | def SNR(x_uav, y_uav, z_uav, x_c_i, y_c_i): 87 | d_k_i = distance(x_uav, y_uav, z_uav, x_c_i, y_c_i, z_mountain[x_c_i][y_c_i]) 88 | cal_snr = (10 * math.log10(10 / (9 * 16 * (math.pi ** 2) * (d_k_i ** 2)))) - noise 89 | return cal_snr 90 | 91 | 92 | def sensing_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z): 93 | d_k_m = distance(UAV_x, UAV_y, UAV_z, Part_Position_x, Part_Position_y, Part_Position_z) 94 | T_moving = d_k_m / moving_speed / 60 95 | v_value_copy = copy.deepcopy(v_value) 96 | for x_value in range(50): 97 | for y_value in range(50): 98 | if abs((time_of_each_UAV[current_uav] + T_moving) - prev_time[x_value][y_value]) <= R: 99 | v_value_copy[x_value][y_value] = a_coef * math.exp( 100 | (time_of_each_UAV[current_uav] + T_moving) - prev_time[x_value][y_value]) + b_coef 101 | else: 102 | v_value_copy[x_value][y_value] = ValueMax 103 | return v_value_copy 104 | 105 | 106 | def number_of_sensor_function(): 107 | for i in range(50): 108 | for j in range(50): 109 | if people_number[i][j] < 0: 110 | sensor_num[i][j] = 0 111 | elif 1 <= people_number[i][j] <= sensor_num_Max: 112 | sensor_num[i][j] = (((sensor_num_Max - sensor_num_Min) * people_number[i][j]) + ( 113 | sensor_num_Max * (sensor_num_Min - 1))) / (sensor_num_Max - 1) 114 | elif people_number[i][j] > sensor_num_Max: 115 | sensor_num[i][j] = sensor_num_Max 116 | 117 | 118 | def F_S_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z): 119 | copy_value = sensing_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z) 120 | all_covered_cells = [] 121 | for all_uav_current_position in UAV_current_Position: 122 | if all_uav_current_position != UAV_current_Position[current_uav]: # not sure 123 | each_uav_covered_cell = check_coverage(d_u_s_max, theta, all_uav_current_position[0], 124 | all_uav_current_position[1], all_uav_current_position[2]) 125 | all_covered_cells.append(each_uav_covered_cell) 126 | PSO_particle_covered_cells = check_coverage(d_u_s_max, theta, Part_Position_x, Part_Position_y, Part_Position_z) 127 | particle_covered_cells = set() 128 | covered_cells = set() 129 | for i in PSO_particle_covered_cells: 130 | particle_covered_cells.add((i[0], i[1])) 131 | for uav_covered_cells in all_covered_cells: 132 | for j in uav_covered_cells: 133 | covered_cells.add((j[0], j[1])) 134 | not_included_in_other_uav = particle_covered_cells - covered_cells 135 | sum_F_S = 0 136 | for x_y_not_included_in_other_uav in not_included_in_other_uav: 137 | sum_F_S += copy_value[x_y_not_included_in_other_uav[0]][x_y_not_included_in_other_uav[1]] * \ 138 | sensor_num[x_y_not_included_in_other_uav[0]][x_y_not_included_in_other_uav[1]] 139 | F_S = sum_F_S / NOR_S 140 | return F_S 141 | 142 | 143 | def F_T_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z): 144 | d_k_m = distance(UAV_x, UAV_y, UAV_z, Part_Position_x, Part_Position_y, Part_Position_z) 145 | T_moving = d_k_m / moving_speed 146 | F_T_cell = check_coverage(d_u_s_max, theta, Part_Position_x, Part_Position_y, Part_Position_z) 147 | T_stay = 0 148 | for cell in F_T_cell: 149 | snr1 = SNR(x_uav=UAV_x, y_uav=UAV_y, z_uav=UAV_z, x_c_i=cell[0], y_c_i=cell[1]) 150 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr1 - 0.6)) 151 | T_stay += people_number[cell[0]][cell[1]] * 0.02 * (1 / (1 - p_g_c_i)) 152 | F_T = (T_moving + T_stay) / NOR_T 153 | return F_T 154 | 155 | 156 | def F_E_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z): 157 | d_k_m = distance(UAV_x, UAV_y, UAV_z, Part_Position_x, Part_Position_y, Part_Position_z) 158 | E_k_M = d_k_m / moving_speed * em 159 | E_k_S_cell = check_coverage(d_u_s_max, theta, Part_Position_x, Part_Position_y, Part_Position_z) 160 | E_k_S_cell_sum = 0 161 | E_k_C_sum = 0 162 | for cell in E_k_S_cell: 163 | snr1 = SNR(x_uav=UAV_x, y_uav=UAV_y, z_uav=UAV_z, x_c_i=cell[0], y_c_i=cell[1]) 164 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr1 - 0.6)) 165 | E_k_S_cell_sum += people_number[cell[0]][cell[1]] * 0.02 * (1 / (1 - p_g_c_i)) 166 | E_k_C_sum += people_number[cell[0]][cell[1]] * (1 / (1 - p_g_c_i)) * ec 167 | E_k_S = E_k_S_cell_sum * es 168 | F_E = (E_k_M + E_k_S + E_k_C_sum) / NOR_E 169 | return F_E 170 | 171 | 172 | def fitness_function(x, y, z, UAV_x, UAV_y, UAV_z): 173 | F_S = F_S_function(x, y, z, UAV_x, UAV_y, UAV_z) 174 | F_T = F_T_function(x, y, z, UAV_x, UAV_y, UAV_z) 175 | F_E = F_E_function(x, y, z, UAV_x, UAV_y, UAV_z) 176 | F = w1 * F_S - w2 * F_T - w3 * F_E 177 | return F 178 | 179 | 180 | def residual_energy(UAV_x1, UAV_y1, UAV_z1, UAV_x2, UAV_y2, UAV_z2): 181 | d_k_m = distance(UAV_x1, UAV_y1, UAV_z1, UAV_x2, UAV_y2, UAV_z2) 182 | E_k_M = d_k_m / moving_speed * em 183 | E_k_S_cell = check_coverage(d_u_s_max, theta, UAV_x2, UAV_y2, UAV_z2) 184 | E_k_S_cell_sum = 0 185 | E_k_C_sum = 0 186 | for cell in E_k_S_cell: 187 | snr1 = SNR(x_uav=UAV_x1, y_uav=UAV_y1, z_uav=UAV_z1, x_c_i=cell[0], y_c_i=cell[1]) 188 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr1 - 0.6)) 189 | E_k_S_cell_sum += people_number[cell[0]][cell[1]] * 0.02 * (1 / (1 - p_g_c_i)) 190 | E_k_C_sum += people_number[cell[0]][cell[1]] * (1 / (1 - p_g_c_i)) * ec 191 | E_k_S = E_k_S_cell_sum * es 192 | F_E = (E_k_M + E_k_S + E_k_C_sum) 193 | return F_E 194 | 195 | 196 | def check_sdpc_function(cell_j, x1, y1, z1, x2, y2, z2): 197 | check_sdpc = [] 198 | for cell_j_axis in cell_j: 199 | z_j_LOS_uav_gbs = (z1 - z2) / (math.sqrt( 200 | (x1 - x2) ** 2 + (y1 - y2) ** 2)) * math.sqrt((cell_j_axis[0] - x2) ** 2 + (cell_j_axis[1] - y2) ** 2) + z2 201 | if z_mountain[cell_j_axis[0]][cell_j_axis[1]] < z_j_LOS_uav_gbs: 202 | check_sdpc.append(1) 203 | else: 204 | check_sdpc.append(0) 205 | return check_sdpc 206 | 207 | 208 | def check_connection_with_other_UAVs_in_PSO(x1, y1, z1, x2, y2, z2): 209 | if (0 < distance(x1, y1, z1, x2, y2, z2) <= d_u_u_max) and ( 210 | all(check_sdpc_function( 211 | SDPC.coordinate_and_compare_height(x1, y1, x2, y2), x1, y1, z1, x2, y2, z2)) == 1): 212 | return True 213 | 214 | 215 | plot_sensing = [] 216 | plot_energy = [] 217 | plot_uav_num = [] 218 | plot_time = [] 219 | plot_uav_num_time = [] 220 | 221 | # start_time = time.time() 222 | NumUAV = 1 223 | MAX_time = 15 224 | MIN_energy = 20 225 | energy_consumption = [400 for _ in range(NumUAV)] 226 | 227 | theta = math.pi / 3 228 | noise = -80 229 | moving_speed = 10 230 | ValueMax = 2.0 231 | ValueMin = 0.2 232 | sensor_num_Max = 10 233 | sensor_num_Min = 2 234 | d_u_s_max = 265 # 1025 235 | d_u_u_max = 1327 # change to 1327 or 839 236 | w1, w2, w3 = 0.3, 0.35, 0.35 237 | # w1, w2, w3 = 0.4, 0.1, 0.5 238 | 239 | R = 3.5 # 120 secs to 2 mins 240 | # R = 2 241 | NOR_S = 124 242 | NOR_T = 43.9 243 | NOR_E = 75 244 | # NOR_T = 157.23 245 | # NOR_E = 33.27 246 | em = 2 247 | es = 1 248 | ec = 0.03 249 | a_coef = (ValueMax - ValueMin) / ((math.e ** R) - 1) 250 | b_coef = ValueMin - a_coef 251 | 252 | GBS_position = [0, 0, 0] 253 | 254 | v_value = np.ones((50, 50), float) 255 | v_value.fill(2.0) 256 | prev_time = np.zeros((50, 50), float) 257 | prev_time.fill(100) 258 | sensor_num = np.zeros((50, 50), int) 259 | number_of_sensor_function() 260 | UAV_current_Position = [[5, 5, 5] for _ in range(NumUAV)] 261 | 262 | time_of_each_UAV = [0 for _ in range(NumUAV)] 263 | copy_time_of_each_UAV = [0 for _ in range(NumUAV)] 264 | 265 | uav_terminates = [] 266 | UAV_position_history = [[] for _ in range(NumUAV)] 267 | 268 | average_f_value = 0 269 | accumulative_sensing_value_list = [] 270 | accumulative_total_value_list = [] 271 | average_s_value = 0 272 | average_t_value = 0 273 | average_e_value = 0 274 | movement_uav = 0 275 | movement_uav_list = [] 276 | covered_cell_check = np.zeros((50, 50), float) 277 | accumulated_covered_cell = [] 278 | movement = 0 279 | old_current_uav = 6 280 | while movement < 1: 281 | 282 | if all(time_of_each_UAV) != 0: 283 | copy_time_of_each_UAV2 = [] 284 | for more_zero in copy_time_of_each_UAV: 285 | if more_zero != 0: 286 | copy_time_of_each_UAV2.append(more_zero) 287 | min_time = min(every_time for every_time in copy_time_of_each_UAV2) 288 | current_uav = time_of_each_UAV.index(min_time) 289 | else: 290 | current_uav = time_of_each_UAV.index(0) 291 | 292 | if old_current_uav < NumUAV: 293 | for x_value in range(50): 294 | for y_value in range(50): 295 | if abs(time_of_each_UAV[old_current_uav] - prev_time[x_value][y_value]) <= R: 296 | v_value[x_value][y_value] = a_coef * math.exp( 297 | time_of_each_UAV[old_current_uav] - prev_time[x_value][y_value]) + b_coef 298 | else: 299 | v_value[x_value][y_value] = ValueMax 300 | check_coverage_old_uav = check_coverage(d_u_s_max, theta, UAV_current_Position[old_current_uav][0], 301 | UAV_current_Position[old_current_uav][1], 302 | UAV_current_Position[old_current_uav][2]) 303 | for cov in check_coverage_old_uav: 304 | v_value[cov[0]][cov[1]] = 0.2 305 | prev_time[cov[0]][cov[1]] = time_of_each_UAV[old_current_uav] 306 | UAV_current_Position[old_current_uav] = [0, 0, 0] 307 | old_current_uav = 6 308 | 309 | for x_value in range(50): 310 | for y_value in range(50): 311 | if abs(time_of_each_UAV[current_uav] - prev_time[x_value][y_value]) <= R: 312 | v_value[x_value][y_value] = a_coef * math.exp( 313 | time_of_each_UAV[current_uav] - prev_time[x_value][y_value]) + b_coef 314 | else: 315 | v_value[x_value][y_value] = ValueMax 316 | 317 | check_coverage_current_position = check_coverage(d_u_s_max, theta, UAV_current_Position[current_uav][0], 318 | UAV_current_Position[current_uav][1], 319 | UAV_current_Position[current_uav][2]) 320 | 321 | for cov in check_coverage_current_position: 322 | v_value[cov[0]][cov[1]] = 0.2 323 | prev_time[cov[0]][cov[1]] = time_of_each_UAV[current_uav] 324 | 325 | best_value_to_compare = -10 326 | best_position = [] 327 | z_array = np.arange(0.0, 25.0, 0.5) 328 | for full_x in range(0, 50): 329 | for full_y in range(0, 50): 330 | for full_z in z_array: 331 | new_position_list = [full_x, full_y, full_z] 332 | 333 | if (new_position_list not in prohibited_zone) and (new_position_list not in UAV_current_Position) and ( 334 | new_position_list[2] > z_mountain[new_position_list[0]][new_position_list[1]]) and ( 335 | new_position_list not in UAV_position_history[current_uav]): 336 | GBS_UAV_connection = [] 337 | for num_gbs_uav in range(NumUAV): 338 | if num_gbs_uav != current_uav: 339 | func1 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[num_gbs_uav][0], 340 | UAV_current_Position[num_gbs_uav][1], 341 | UAV_current_Position[num_gbs_uav][2], 342 | GBS_position[0], GBS_position[1], 343 | GBS_position[2]) 344 | if func1 == True: 345 | GBS_UAV_connection.append(num_gbs_uav) 346 | if len(GBS_UAV_connection) >= 1: 347 | exitFlag = False 348 | for connect_to_pso in GBS_UAV_connection: 349 | func2 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[connect_to_pso][0], 350 | UAV_current_Position[connect_to_pso][1], 351 | UAV_current_Position[connect_to_pso][2], 352 | new_position_list[0], 353 | new_position_list[1], 354 | new_position_list[2]) 355 | if func2 == True: 356 | fit_value = fitness_function(new_position_list[0], 357 | new_position_list[1], 358 | new_position_list[2], 359 | UAV_current_Position[current_uav][0], 360 | UAV_current_Position[current_uav][1], 361 | UAV_current_Position[current_uav][2]) 362 | if fit_value > best_value_to_compare: 363 | best_value_to_compare = fit_value 364 | best_position = [new_position_list[0], new_position_list[1], new_position_list[2]] 365 | 366 | break 367 | else: 368 | exitFlag2 = False 369 | for connect_to_pso2 in range(5): 370 | if (connect_to_pso2 != connect_to_pso) and (connect_to_pso2 != current_uav): 371 | func3 = check_connection_with_other_UAVs_in_PSO( 372 | UAV_current_Position[connect_to_pso][0], 373 | UAV_current_Position[connect_to_pso][1], 374 | UAV_current_Position[connect_to_pso][2], 375 | UAV_current_Position[connect_to_pso2][0], 376 | UAV_current_Position[connect_to_pso2][1], 377 | UAV_current_Position[connect_to_pso2][2]) 378 | if func3 == True: 379 | func4 = check_connection_with_other_UAVs_in_PSO( 380 | UAV_current_Position[connect_to_pso2][0], 381 | UAV_current_Position[connect_to_pso2][1], 382 | UAV_current_Position[connect_to_pso2][2], 383 | new_position_list[0], new_position_list[1], 384 | new_position_list[2]) 385 | if func4 == True: 386 | fit_value = fitness_function(new_position_list[0], new_position_list[1], 387 | new_position_list[2], 388 | UAV_current_Position[current_uav][0], 389 | UAV_current_Position[current_uav][1], 390 | UAV_current_Position[current_uav][2]) 391 | if fit_value > best_value_to_compare: 392 | best_value_to_compare = fit_value 393 | best_position = [new_position_list[0], new_position_list[1], 394 | new_position_list[2]] 395 | exitFlag = True 396 | break 397 | else: 398 | exitFlag3 = False 399 | for connect_to_pso3 in range(NumUAV): 400 | if (connect_to_pso3 != current_uav) and ( 401 | connect_to_pso3 != connect_to_pso2) and ( 402 | connect_to_pso3 != connect_to_pso): 403 | func5 = check_connection_with_other_UAVs_in_PSO( 404 | UAV_current_Position[connect_to_pso3][0], 405 | UAV_current_Position[connect_to_pso3][1], 406 | UAV_current_Position[connect_to_pso3][2], 407 | UAV_current_Position[connect_to_pso2][0], 408 | UAV_current_Position[connect_to_pso2][1], 409 | UAV_current_Position[connect_to_pso2][2]) 410 | if func5 == True: 411 | func6 = check_connection_with_other_UAVs_in_PSO( 412 | UAV_current_Position[connect_to_pso3][0], 413 | UAV_current_Position[connect_to_pso3][1], 414 | UAV_current_Position[connect_to_pso3][2], 415 | new_position_list[0], 416 | new_position_list[1], 417 | new_position_list[2]) 418 | if func6 == True: 419 | fit_value = fitness_function(full_x, full_y, full_z, 420 | UAV_current_Position[ 421 | current_uav][0], 422 | UAV_current_Position[ 423 | current_uav][1], 424 | UAV_current_Position[ 425 | current_uav][2]) 426 | if fit_value > best_value_to_compare: 427 | best_value_to_compare = fit_value 428 | best_position = [full_x, full_y, full_z] 429 | 430 | exitFlag = True 431 | exitFlag2 = True 432 | break 433 | else: 434 | for connect_to_pso4 in range(NumUAV): 435 | if (connect_to_pso4 != current_uav) and ( 436 | connect_to_pso4 != connect_to_pso2) and ( 437 | connect_to_pso4 != connect_to_pso) and ( 438 | connect_to_pso4 != connect_to_pso3): 439 | func7 = check_connection_with_other_UAVs_in_PSO( 440 | UAV_current_Position[ 441 | connect_to_pso3][0], 442 | UAV_current_Position[connect_to_pso3][1], 443 | UAV_current_Position[connect_to_pso3][2], 444 | UAV_current_Position[connect_to_pso4][0], 445 | UAV_current_Position[connect_to_pso4][1], 446 | UAV_current_Position[connect_to_pso4][2]) 447 | if func7 == True: 448 | func8 = check_connection_with_other_UAVs_in_PSO( 449 | UAV_current_Position[connect_to_pso4][ 450 | 0], 451 | UAV_current_Position[connect_to_pso4][ 452 | 1], 453 | UAV_current_Position[connect_to_pso4][ 454 | 2], 455 | new_position_list[0], 456 | new_position_list[1], 457 | new_position_list[2]) 458 | if func8 == True: 459 | fit_value = fitness_function( 460 | new_position_list[0], 461 | new_position_list[1], 462 | new_position_list[2], 463 | UAV_current_Position[ 464 | current_uav][ 465 | 0], 466 | UAV_current_Position[ 467 | current_uav][ 468 | 1], 469 | UAV_current_Position[ 470 | current_uav][ 471 | 2]) 472 | if fit_value > best_value_to_compare: 473 | best_value_to_compare = fit_value 474 | best_position = [ 475 | new_position_list[0], 476 | new_position_list[1], 477 | new_position_list[2]] 478 | 479 | exitFlag = True 480 | exitFlag2 = True 481 | exitFlag3 = True 482 | break 483 | if (exitFlag3): 484 | break 485 | if (exitFlag2): 486 | break 487 | if (exitFlag): 488 | break 489 | else: 490 | func9 = check_connection_with_other_UAVs_in_PSO(new_position_list[0], 491 | new_position_list[1], 492 | new_position_list[2], GBS_position[0], 493 | GBS_position[1], 494 | GBS_position[2]) 495 | if func9 == True: 496 | old_uav_to_other_uav_con = [] 497 | for second_check in range(NumUAV): 498 | if second_check != current_uav: 499 | func10 = check_connection_with_other_UAVs_in_PSO( 500 | UAV_current_Position[second_check][0], 501 | UAV_current_Position[second_check][1], 502 | UAV_current_Position[second_check][2], 503 | UAV_current_Position[current_uav][0], 504 | UAV_current_Position[current_uav][1], 505 | UAV_current_Position[current_uav][2]) 506 | if func10 == True: 507 | old_uav_to_other_uav_con.append(second_check) 508 | 509 | if (all((distance(UAV_current_Position[con_elem][0], 510 | UAV_current_Position[con_elem][1], 511 | UAV_current_Position[con_elem][2], new_position_list[0], 512 | new_position_list[1], new_position_list[2])) <= d_u_u_max for 513 | con_elem in old_uav_to_other_uav_con)) and (all 514 | (all(check_sdpc_function( 515 | SDPC.coordinate_and_compare_height( 516 | UAV_current_Position[con_elem][0], 517 | UAV_current_Position[con_elem][1], 518 | new_position_list[0], new_position_list[1]), 519 | UAV_current_Position[con_elem][0], 520 | UAV_current_Position[con_elem][1], 521 | UAV_current_Position[con_elem][2], 522 | new_position_list[0], new_position_list[1], 523 | new_position_list[2])) == 1 for con_elem in old_uav_to_other_uav_con)): 524 | 525 | fit_value = fitness_function(full_x, full_y, full_z, 526 | UAV_current_Position[current_uav][0], 527 | UAV_current_Position[current_uav][1], 528 | UAV_current_Position[current_uav][2]) 529 | if fit_value > best_value_to_compare: 530 | best_value_to_compare = fit_value 531 | best_position = [full_x, full_y, full_z] 532 | 533 | next_uav_position = best_position 534 | moving_time = (distance(UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 535 | UAV_current_Position[current_uav][2], next_uav_position[0], next_uav_position[1], 536 | next_uav_position[2]) / moving_speed) / 60 537 | F_S = F_S_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 538 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 539 | UAV_current_Position[current_uav][2]) 540 | average_s_value += F_S 541 | accumulative_sensing_value_list.append(average_s_value) 542 | F_T = F_T_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 543 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 544 | UAV_current_Position[current_uav][2]) 545 | average_t_value += F_T 546 | F_E = F_E_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 547 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 548 | UAV_current_Position[current_uav][2]) 549 | average_e_value += F_E 550 | F = fitness_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 551 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 552 | UAV_current_Position[current_uav][2]) 553 | average_f_value += F 554 | accumulative_total_value_list.append(average_f_value) 555 | check_coverage_new_position = check_coverage(d_u_s_max, theta, next_uav_position[0], next_uav_position[1], 556 | next_uav_position[2]) 557 | 558 | for x_value in range(50): 559 | for y_value in range(50): 560 | if abs((time_of_each_UAV[current_uav] + moving_time) - prev_time[x_value][y_value]) <= R: 561 | v_value[x_value][y_value] = a_coef * math.exp( 562 | (time_of_each_UAV[current_uav] + moving_time) - prev_time[x_value][y_value]) + b_coef 563 | else: 564 | v_value[x_value][y_value] = ValueMax 565 | 566 | new_position_stay_time_sum = 0 567 | for cov_next in check_coverage_new_position: 568 | covered_cell_check[cov_next[0]][cov_next[1]] = 1 569 | snr = SNR(UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 570 | UAV_current_Position[current_uav][2], cov_next[0], cov_next[1]) 571 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr - 0.6)) 572 | next_stay_time_c_i = people_number[cov_next[0]][cov_next[1]] * 0.02 * 1 / (1 - p_g_c_i) 573 | new_position_stay_time_sum += next_stay_time_c_i 574 | new_position_stay_time = new_position_stay_time_sum / 60 575 | 576 | num_covered = 0 577 | for ii in range(50): 578 | for jj in range(50): 579 | if covered_cell_check[ii][jj] == 1: 580 | num_covered += 1 581 | accumulated_covered_cell.append(num_covered) 582 | 583 | time_update_in_movement = time_of_each_UAV[current_uav] + moving_time + new_position_stay_time 584 | copy_time_of_each_UAV[current_uav] = time_update_in_movement 585 | time_of_each_UAV[current_uav] = time_update_in_movement 586 | plot_time.append(time_of_each_UAV[current_uav]) 587 | 588 | residual_e = residual_energy(UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 589 | UAV_current_Position[current_uav][2], next_uav_position[0], next_uav_position[1], 590 | next_uav_position[2]) 591 | energy_consumption[current_uav] = energy_consumption[current_uav] - residual_e 592 | 593 | if (energy_consumption[current_uav] < MIN_energy) or (time_of_each_UAV[current_uav] > MAX_time): 594 | copy_time_of_each_UAV[current_uav] = 0 595 | uav_terminates.append(current_uav) 596 | 597 | print(f"there will be minus 1 uav {current_uav}") 598 | # UAV_current_Position[current_uav] = [0, 0, 0] 599 | plot_uav_num_time.append(time_of_each_UAV[current_uav]) 600 | old_current_uav = current_uav 601 | UAV_position_history[current_uav].append(next_uav_position) 602 | UAV_current_Position[current_uav] = next_uav_position 603 | 604 | print(f'sensing {F, F_S, F_T, F_E}') 605 | print(f"current UAV number {current_uav}: current position {next_uav_position}") 606 | print(movement_uav) 607 | movement_uav += 1 608 | # if len(uav_terminates) == NumUAV: 609 | # print(f"There is no UAV at movement time ") 610 | movement = 10 611 | # print(f"fitness value {average_f_value / movement_uav}") 612 | # print(f"accumulative total fitness value{average_f_value}") 613 | # print( 614 | # f"average sensing, time, energy {average_s_value / movement_uav, average_t_value / movement_uav, average_e_value / movement_uav}") 615 | # print("max sensing, time, energy value", average_s_value, average_t_value, average_e_value) 616 | # print(f"Num of movements {movement_uav}") 617 | # print(f"Max number of covered cells {max(accumulated_covered_cell)}") 618 | # print(f"plot last uav terminated its work {max(plot_uav_num_time)}") 619 | # print(f"plot uav terminated its work {plot_uav_num_time}") 620 | # 621 | # print(f" time {plot_time}") 622 | # print(f"accumulateve total value{accumulative_total_value_list}") 623 | # print(f" accumulative sensing value {accumulative_sensing_value_list}") 624 | # print(f"accumulated covered cells {accumulated_covered_cell}") 625 | 626 | 627 | 628 | -------------------------------------------------------------------------------- /ProposedMethod.py: -------------------------------------------------------------------------------- 1 | import copy 2 | import random 3 | import math 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from matplotlib.lines import Line2D 7 | from matplotlib.ticker import MultipleLocator 8 | 9 | import time 10 | from sklearn.datasets._samples_generator import make_blobs 11 | import scipy.stats as st 12 | import SDPC 13 | n_components = 3 14 | X_surface, truth_surface = make_blobs(n_samples=300, centers=[[0.72, 0.72], [0.6, 1.4], [1.4, 1.20]], cluster_std=[0.24, 0.12, 0.18], random_state=42) 15 | x_surface = X_surface[:, 0] 16 | y_surface = X_surface[:, 1] 17 | xx_surface, yy_surface = np.mgrid[-0.02:1.98:51j, -0.02:1.98:51j] 18 | positions_surface = np.vstack([xx_surface.ravel(), yy_surface.ravel()]) 19 | values_surface = np.vstack([x_surface, y_surface]) 20 | kernel_surface = st.gaussian_kde(values_surface) 21 | f_surface = np.reshape(kernel_surface(positions_surface).T, xx_surface.shape) 22 | zz_surface = f_surface * 0.35 23 | 24 | X_sensor, y_true_sensor = make_blobs(n_samples=20000,centers=[[0.1, 0.1], [0.5, 0.5], [0.6, 1.25], [1.3, 0.6], [1.52, 1.4]], 25 | cluster_std=[0.2, 0.16, 0.204, 0.18, 0.2], random_state=0) 26 | x_sensors = X_sensor[:, 0] 27 | y_sensors = X_sensor[:, 1] 28 | xx_sensors, yy_sensors = np.mgrid[0:1.96:50j, 0:1.96:50j] 29 | positions_sensors = np.vstack([xx_sensors.ravel(), yy_sensors.ravel()]) 30 | values_sensor = np.vstack([x_sensors, y_sensors]) 31 | kernel_sensor = st.gaussian_kde(values_sensor) 32 | f_sensor = np.reshape(kernel_sensor(positions_sensors).T, xx_sensors.shape) 33 | zz_sensor = f_sensor * 37.865 # 20 000 sensors 34 | 35 | # zz_sensor = f_sensor * 19.919 # 10 000 sensors 36 | # zz_sensor = f_sensor * 10.859# 5 000 sensors 37 | 38 | vector_sensor = np.vectorize(np.int32) 39 | zz_sensor = vector_sensor(zz_sensor) 40 | 41 | people_number = zz_sensor 42 | z_mountain = np.delete(zz_surface, 0, 0) # delete the first line of -0.02. It was added in order to male '0' as a center of the cell 43 | 44 | 45 | 46 | prohibited_zone = [] 47 | for a in range(35, 38): 48 | for b in range(35, 38): 49 | for c in np.arange(0.7, 0.8): 50 | prohibited_zone.append([a, b, c]) 51 | 52 | 53 | def distance(x1, y1, z1, x2, y2, z2): 54 | dist = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2 + (z1 - z2) ** 2) * 40 55 | return dist 56 | 57 | 58 | def check_coverage(d_u_s_max, theta, x, y, z): 59 | covered_cells_1 = [] 60 | covered_cells_2 = [] 61 | covered_cells_3 = [] 62 | covered_cells_final = [] 63 | r_max = d_u_s_max * math.sin(theta / 2) 64 | for x_cell in range(0, 50): 65 | for y_cell in range(0, 50): 66 | r_k_i = math.sqrt((x - x_cell) ** 2 + (y - y_cell) ** 2) * 40 67 | if r_max >= r_k_i: 68 | covered_cells_1.append([x_cell, y_cell]) 69 | for cell in covered_cells_1: 70 | d_k_i = distance(x, y, z, cell[0], cell[1], z_mountain[cell[0]][cell[1]]) 71 | if d_u_s_max >= d_k_i: 72 | covered_cells_2.append([cell[0], cell[1]]) 73 | for cell in covered_cells_2: 74 | d_k_i = distance(x, y, z, cell[0], cell[1], z_mountain[cell[0]][cell[1]]) 75 | d = ((z - z_mountain[cell[0]][cell[1]]) * 40) / math.cos(theta / 2) 76 | if (d_k_i <= d) and (z_mountain[cell[0]][cell[1]] <= z): 77 | covered_cells_3.append([cell[0], cell[1]]) 78 | for cell in covered_cells_3: 79 | cell_j = SDPC.coordinate_and_compare_height(x, y, cell[0], cell[1]) 80 | for cell_j_axis in cell_j: 81 | z_j_LOS_k_i = (z - z_mountain[cell[0]][cell[1]]) / ( 82 | math.sqrt((x - cell[0]) ** 2 + (y - cell[1]) ** 2)) * math.sqrt( 83 | (cell_j_axis[0] - cell[0]) ** 2 + (cell_j_axis[1] - cell[1]) ** 2) + z_mountain[cell[0]][cell[1]] 84 | if z_mountain[cell_j_axis[0]][cell_j_axis[1]] < z_j_LOS_k_i: 85 | if [cell[0], cell[1]] not in covered_cells_final: 86 | covered_cells_final.append([cell[0], cell[1]]) 87 | return covered_cells_final 88 | 89 | 90 | def SNR(x_uav, y_uav, z_uav, x_c_i, y_c_i): 91 | d_k_i = distance(x_uav, y_uav, z_uav, x_c_i, y_c_i, z_mountain[x_c_i][y_c_i]) 92 | cal_snr = (10 * math.log10(10 / (9 * 16 * (math.pi ** 2) * (d_k_i ** 2)))) - noise 93 | return cal_snr 94 | 95 | 96 | def sensing_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z): 97 | d_k_m = distance(UAV_x, UAV_y, UAV_z, Part_Position_x, Part_Position_y, Part_Position_z) 98 | T_moving = d_k_m / moving_speed / 60 99 | v_value_copy = copy.deepcopy(v_value) 100 | for x_value in range(50): 101 | for y_value in range(50): 102 | if abs((time_of_each_UAV[current_uav] + T_moving) - prev_time[x_value][y_value]) <= R: 103 | v_value_copy[x_value][y_value] = a_coef * math.exp( 104 | (time_of_each_UAV[current_uav] + T_moving) - prev_time[x_value][y_value]) + b_coef 105 | else: 106 | v_value_copy[x_value][y_value] = ValueMax 107 | return v_value_copy 108 | 109 | 110 | def number_of_sensor_function(): 111 | for i in range(50): 112 | for j in range(50): 113 | if people_number[i][j] < 0: 114 | sensor_num[i][j] = 0 115 | elif 1 <= people_number[i][j] <= sensor_num_Max: 116 | sensor_num[i][j] = (((sensor_num_Max - sensor_num_Min) * people_number[i][j]) + ( 117 | sensor_num_Max * (sensor_num_Min - 1))) / (sensor_num_Max - 1) 118 | elif people_number[i][j] > sensor_num_Max: 119 | sensor_num[i][j] = sensor_num_Max 120 | 121 | 122 | def F_S_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z): 123 | copy_value = sensing_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z) 124 | all_covered_cells = [] 125 | for all_uav_current_position in UAV_current_Position: 126 | if all_uav_current_position != UAV_current_Position[current_uav]: #not sure 127 | each_uav_covered_cell = check_coverage(d_u_s_max, theta, all_uav_current_position[0], 128 | all_uav_current_position[1], all_uav_current_position[2]) 129 | all_covered_cells.append(each_uav_covered_cell) 130 | PSO_particle_covered_cells = check_coverage(d_u_s_max, theta, Part_Position_x, Part_Position_y, Part_Position_z) 131 | particle_covered_cells = set() 132 | covered_cells = set() 133 | for i in PSO_particle_covered_cells: 134 | particle_covered_cells.add((i[0], i[1])) 135 | for uav_covered_cells in all_covered_cells: 136 | for j in uav_covered_cells: 137 | covered_cells.add((j[0], j[1])) 138 | not_included_in_other_uav = particle_covered_cells - covered_cells 139 | sum_F_S = 0 140 | for x_y_not_included_in_other_uav in not_included_in_other_uav: 141 | sum_F_S += copy_value[x_y_not_included_in_other_uav[0]][x_y_not_included_in_other_uav[1]] * \ 142 | sensor_num[x_y_not_included_in_other_uav[0]][x_y_not_included_in_other_uav[1]] 143 | F_S = sum_F_S / NOR_S 144 | return F_S 145 | 146 | 147 | def F_T_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z): 148 | d_k_m = distance(UAV_x, UAV_y, UAV_z, Part_Position_x, Part_Position_y, Part_Position_z) 149 | T_moving = d_k_m / moving_speed 150 | F_T_cell = check_coverage(d_u_s_max, theta, Part_Position_x, Part_Position_y, Part_Position_z) 151 | T_stay = 0 152 | for cell in F_T_cell: 153 | snr1 = SNR(x_uav=UAV_x, y_uav=UAV_y, z_uav=UAV_z, x_c_i=cell[0], y_c_i=cell[1]) 154 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr1 - 0.6)) 155 | T_stay += people_number[cell[0]][cell[1]] * 0.02 * (1 / (1 - p_g_c_i)) 156 | F_T = (T_moving + T_stay) / NOR_T 157 | return F_T 158 | 159 | 160 | def F_E_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z): 161 | d_k_m = distance(UAV_x, UAV_y, UAV_z, Part_Position_x, Part_Position_y, Part_Position_z) 162 | E_k_M = d_k_m / moving_speed * em 163 | E_k_S_cell = check_coverage(d_u_s_max, theta, Part_Position_x, Part_Position_y, Part_Position_z) 164 | E_k_S_cell_sum = 0 165 | E_k_C_sum = 0 166 | for cell in E_k_S_cell: 167 | snr1 = SNR(x_uav=UAV_x, y_uav=UAV_y, z_uav=UAV_z, x_c_i=cell[0], y_c_i=cell[1]) 168 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr1 - 0.6)) 169 | E_k_S_cell_sum += people_number[cell[0]][cell[1]] * 0.02 * (1 / (1 - p_g_c_i)) 170 | E_k_C_sum += people_number[cell[0]][cell[1]] * (1 / (1 - p_g_c_i)) * ec 171 | E_k_S = E_k_S_cell_sum * es 172 | F_E = (E_k_M + E_k_S + E_k_C_sum) / NOR_E 173 | return F_E 174 | 175 | 176 | def fitness_function(x, y, z, UAV_x, UAV_y, UAV_z): 177 | F_S = F_S_function(x, y, z, UAV_x, UAV_y, UAV_z) 178 | F_T = F_T_function(x, y, z, UAV_x, UAV_y, UAV_z) 179 | F_E = F_E_function(x, y, z, UAV_x, UAV_y, UAV_z) 180 | F = w1 * F_S - w2 * F_T - w3 * F_E 181 | return F 182 | 183 | 184 | def residual_energy(UAV_x1, UAV_y1, UAV_z1, UAV_x2, UAV_y2, UAV_z2): 185 | d_k_m = distance(UAV_x1, UAV_y1, UAV_z1, UAV_x2, UAV_y2, UAV_z2) 186 | E_k_M = d_k_m / moving_speed * em 187 | E_k_S_cell = check_coverage(d_u_s_max, theta, UAV_x2, UAV_y2, UAV_z2) 188 | E_k_S_cell_sum = 0 189 | E_k_C_sum = 0 190 | for cell in E_k_S_cell: 191 | snr1 = SNR(x_uav=UAV_x1, y_uav=UAV_y1, z_uav=UAV_z1, x_c_i=cell[0], y_c_i=cell[1]) 192 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr1 - 0.6)) 193 | E_k_S_cell_sum += people_number[cell[0]][cell[1]] * 0.02 * (1 / (1 - p_g_c_i)) 194 | E_k_C_sum += people_number[cell[0]][cell[1]] * (1 / (1 - p_g_c_i)) * ec 195 | E_k_S = E_k_S_cell_sum * es 196 | F_E = (E_k_M + E_k_S + E_k_C_sum) 197 | return F_E 198 | 199 | 200 | def check_sdpc_function(cell_j, x1, y1, z1, x2, y2, z2): 201 | check_sdpc = [] 202 | for cell_j_axis in cell_j: 203 | z_j_LOS_uav_gbs = (z1 - z2) / (math.sqrt( 204 | (x1 - x2) ** 2 + (y1 - y2) ** 2)) * math.sqrt((cell_j_axis[0] - x2) ** 2 + (cell_j_axis[1] - y2) ** 2) + z2 205 | if z_mountain[cell_j_axis[0]][cell_j_axis[1]] < z_j_LOS_uav_gbs: 206 | check_sdpc.append(1) 207 | else: 208 | check_sdpc.append(0) 209 | return check_sdpc 210 | 211 | def check_connection_with_other_UAVs_in_PSO(x1, y1, z1, x2, y2, z2): 212 | if (0 < distance(x1,y1,z1,x2,y2,z2) <= d_u_u_max) and ( 213 | all(check_sdpc_function( 214 | SDPC.coordinate_and_compare_height(x1, y1, x2, y2),x1, y1, z1, x2, y2, z2)) == 1): 215 | return True 216 | 217 | 218 | def pso_particle_pos_velocity_initialization(NumParticle, NumDimention, VelocityMax, current_uav): 219 | Init_PartPosition = [[[] for _ in range(NumDimention)] for _ in range(NumParticle)] 220 | global GlobalBestValue 221 | global GlobalBestPosition 222 | for i in range(NumParticle): 223 | for j in range(NumDimention): 224 | PartVelocity[i][j] = random.randint(-VelocityMax, VelocityMax) 225 | init_value_while_to_false = 0 226 | while init_value_while_to_false < 2: 227 | Init_PartPosition[i][0] = random.randint(0, 49) 228 | Init_PartPosition[i][1] = random.randint(0, 49) 229 | Init_PartPosition[i][2] = random.uniform(0, 23.9) 230 | z_cell = z_mountain[Init_PartPosition[i][0]][Init_PartPosition[i][1]] 231 | if (Init_PartPosition[i][2] > z_cell) and (Init_PartPosition[i] not in prohibited_zone) and ( 232 | Init_PartPosition[i] not in UAV_current_Position): 233 | GBS_UAV_connection = [] 234 | for num_gbs_uav in range(NumUAV): 235 | if num_gbs_uav != current_uav: 236 | func1 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[num_gbs_uav][0], 237 | UAV_current_Position[num_gbs_uav][1], 238 | UAV_current_Position[num_gbs_uav][2], 239 | GBS_position[0], GBS_position[1], GBS_position[2]) 240 | if func1 == True: 241 | GBS_UAV_connection.append(num_gbs_uav) 242 | if len(GBS_UAV_connection) >= 1: 243 | exitFlag = False 244 | for connect_to_pso in GBS_UAV_connection: 245 | func2 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[connect_to_pso][0], 246 | UAV_current_Position[connect_to_pso][1], 247 | UAV_current_Position[connect_to_pso][2], 248 | Init_PartPosition[i][0], Init_PartPosition[i][1], 249 | Init_PartPosition[i][2]) 250 | if func2 == True: 251 | PartPosition[i] = Init_PartPosition[i] 252 | PartBestPosition[i] = PartPosition[i] 253 | init_value_while_to_false = 5 254 | break 255 | else: 256 | exitFlag2 = False 257 | for connect_to_pso2 in range(NumUAV): 258 | if (connect_to_pso2 != connect_to_pso) and (connect_to_pso2 != current_uav): 259 | func3 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[connect_to_pso][0], 260 | UAV_current_Position[connect_to_pso][1], 261 | UAV_current_Position[connect_to_pso][2], 262 | UAV_current_Position[connect_to_pso2][0], 263 | UAV_current_Position[connect_to_pso2][1], 264 | UAV_current_Position[connect_to_pso2][2]) 265 | if func3 == True: 266 | func4 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[connect_to_pso2][0], 267 | UAV_current_Position[connect_to_pso2][1], 268 | UAV_current_Position[connect_to_pso2][2], 269 | Init_PartPosition[i][0], Init_PartPosition[i][1], 270 | Init_PartPosition[i][2]) 271 | if func4 == True: 272 | PartPosition[i] = Init_PartPosition[i] 273 | PartBestPosition[i] = PartPosition[i] 274 | init_value_while_to_false = 5 275 | exitFlag = True 276 | break 277 | else: 278 | exitFlag3 = False 279 | for connect_to_pso3 in range(NumUAV): 280 | if (connect_to_pso3 != current_uav) and ( 281 | connect_to_pso3 != connect_to_pso2) and ( 282 | connect_to_pso3 != connect_to_pso): 283 | func5 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[connect_to_pso3][0], 284 | UAV_current_Position[connect_to_pso3][1], 285 | UAV_current_Position[connect_to_pso3][2], 286 | UAV_current_Position[connect_to_pso2][0], 287 | UAV_current_Position[connect_to_pso2][1], 288 | UAV_current_Position[connect_to_pso2][2]) 289 | if func5 == True: 290 | func6 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[connect_to_pso3][0], 291 | UAV_current_Position[connect_to_pso3][1], 292 | UAV_current_Position[connect_to_pso3][2], 293 | Init_PartPosition[i][0], 294 | Init_PartPosition[i][1], 295 | Init_PartPosition[i][2]) 296 | if func6 == True: 297 | PartPosition[i] = Init_PartPosition[i] 298 | PartBestPosition[i] = PartPosition[i] 299 | init_value_while_to_false = 5 300 | exitFlag = True 301 | exitFlag2 = True 302 | break 303 | else: 304 | for connect_to_pso4 in range(NumUAV): 305 | if (connect_to_pso4 != current_uav) and ( 306 | connect_to_pso4 != connect_to_pso2) and ( 307 | connect_to_pso4 != connect_to_pso) and ( 308 | connect_to_pso4 != connect_to_pso3): 309 | func7 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[ 310 | connect_to_pso3][0], 311 | UAV_current_Position[connect_to_pso3][1], 312 | UAV_current_Position[connect_to_pso3][2], 313 | UAV_current_Position[connect_to_pso4][0], 314 | UAV_current_Position[connect_to_pso4][1], 315 | UAV_current_Position[connect_to_pso4][2]) 316 | if func7 == True: 317 | func8 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[connect_to_pso4][0], 318 | UAV_current_Position[connect_to_pso4][1], 319 | UAV_current_Position[connect_to_pso4][2], 320 | Init_PartPosition[i][0], 321 | Init_PartPosition[i][1], 322 | Init_PartPosition[i][2]) 323 | if func8 == True: 324 | PartPosition[i] = Init_PartPosition[i] 325 | PartBestPosition[i] = PartPosition[i] 326 | init_value_while_to_false = 5 327 | exitFlag = True 328 | exitFlag2 = True 329 | exitFlag3 = True 330 | break 331 | if (exitFlag3): 332 | break 333 | if (exitFlag2): 334 | break 335 | if (exitFlag): 336 | break 337 | else: 338 | func9 = check_connection_with_other_UAVs_in_PSO(Init_PartPosition[i][0],Init_PartPosition[i][1], Init_PartPosition[i][2], GBS_position[0], GBS_position[1], 339 | GBS_position[2]) 340 | if func9 == True: 341 | old_uav_to_other_uav_con = [] 342 | for second_check in range(NumUAV): 343 | if second_check != current_uav: 344 | func10 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[second_check][0], 345 | UAV_current_Position[second_check][1], 346 | UAV_current_Position[second_check][2], 347 | UAV_current_Position[current_uav][0], 348 | UAV_current_Position[current_uav][1], 349 | UAV_current_Position[current_uav][2]) 350 | if func10 == True: 351 | old_uav_to_other_uav_con.append(second_check) 352 | 353 | if (all((distance(UAV_current_Position[con_elem][0], 354 | UAV_current_Position[con_elem][1], 355 | UAV_current_Position[con_elem][2], Init_PartPosition[i][0], 356 | Init_PartPosition[i][1], Init_PartPosition[i][2])) <= d_u_u_max for 357 | con_elem in old_uav_to_other_uav_con)) and (all 358 | (all(check_sdpc_function( 359 | SDPC.coordinate_and_compare_height( 360 | UAV_current_Position[con_elem][0], 361 | UAV_current_Position[con_elem][1], 362 | Init_PartPosition[i][0], Init_PartPosition[i][1]), 363 | UAV_current_Position[con_elem][0], 364 | UAV_current_Position[con_elem][1], 365 | UAV_current_Position[con_elem][2], 366 | Init_PartPosition[i][0], Init_PartPosition[i][1], 367 | Init_PartPosition[i][2])) == 1 for con_elem in old_uav_to_other_uav_con)): 368 | PartPosition[i] = Init_PartPosition[i] 369 | PartBestPosition[i] = PartPosition[i] 370 | break 371 | PartBestValue[i] = fitness_function(PartPosition[i][0], PartPosition[i][1], PartPosition[i][2], 372 | UAV_current_Position[current_uav][0], 373 | UAV_current_Position[current_uav][1], 374 | UAV_current_Position[current_uav][2]) 375 | Part_dic[PartBestValue[i]] = PartPosition[i] 376 | GlobalBestValue = max(key for key in Part_dic.keys()) 377 | GlobalBestPosition = Part_dic[GlobalBestValue] 378 | 379 | 380 | def pso_iteration(MaxIteration, NumParticle, NumDimention, w_weight, c1, c2, VelocityMax, current_uav): 381 | Init_PartPosition = [[[] for _ in range(NumDimention)] for _ in range(NumParticle)] 382 | global GlobalBestValue 383 | global GlobalBestPosition 384 | for iter in range(MaxIteration): 385 | for i in range(NumParticle): 386 | iter_value_while_to_false = 0 387 | while iter_value_while_to_false < 2: 388 | for j in range(NumDimention): 389 | r1 = random.random() 390 | r2 = random.random() 391 | PartVelocity[i][j] = w_weight * PartVelocity[i][j] + c1 * r1 * ( 392 | PartBestPosition[i][j] - PartPosition[i][j]) + c2 * r2 * ( 393 | GlobalBestPosition[j] - PartPosition[i][j]) 394 | if PartVelocity[i][j] < -VelocityMax: 395 | PartVelocity[i][j] = -VelocityMax 396 | if PartVelocity[i][j] > VelocityMax: 397 | PartVelocity[i][j] = VelocityMax 398 | Init_PartPosition[i][0] = round(PartPosition[i][0] + PartVelocity[i][0]) 399 | Init_PartPosition[i][1] = round(PartPosition[i][1] + PartVelocity[i][1]) 400 | Init_PartPosition[i][2] = PartPosition[i][2] + PartVelocity[i][2] 401 | if (0 <= Init_PartPosition[i][0] <= 49) and (0 <= Init_PartPosition[i][1] <= 49) and ( 402 | 0 <= Init_PartPosition[i][2] <= 24): 403 | z_cell = z_mountain[Init_PartPosition[i][0]][Init_PartPosition[i][1]] 404 | if (Init_PartPosition[i][2] > z_cell) and (Init_PartPosition[i] not in prohibited_zone) and ( 405 | Init_PartPosition[i] not in UAV_current_Position): 406 | GBS_UAV_connection = [] 407 | for num_gbs_uav in range(NumUAV): 408 | if num_gbs_uav != current_uav: 409 | func1 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[num_gbs_uav][0], 410 | UAV_current_Position[num_gbs_uav][1], 411 | UAV_current_Position[num_gbs_uav][2], 412 | GBS_position[0], GBS_position[1], GBS_position[2]) 413 | if func1 == True: 414 | GBS_UAV_connection.append(num_gbs_uav) 415 | if len(GBS_UAV_connection) >= 1: 416 | exitFlag = False 417 | for connect_to_pso in GBS_UAV_connection: 418 | func2 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[connect_to_pso][0], 419 | UAV_current_Position[connect_to_pso][1], 420 | UAV_current_Position[connect_to_pso][2], 421 | Init_PartPosition[i][0], Init_PartPosition[i][1], 422 | Init_PartPosition[i][2]) 423 | if func2 == True: 424 | PartPosition[i] = Init_PartPosition[i] 425 | iter_value_while_to_false = 5 426 | break 427 | else: 428 | exitFlag2 = False 429 | for connect_to_pso2 in range(NumUAV): 430 | if (connect_to_pso2 != connect_to_pso) and (connect_to_pso2 != current_uav): 431 | func3 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[connect_to_pso][0], 432 | UAV_current_Position[connect_to_pso][1], 433 | UAV_current_Position[connect_to_pso][2], 434 | UAV_current_Position[connect_to_pso2][0], 435 | UAV_current_Position[connect_to_pso2][1], 436 | UAV_current_Position[connect_to_pso2][2]) 437 | if func3 == True: 438 | func4 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[connect_to_pso2][0], 439 | UAV_current_Position[connect_to_pso2][1], 440 | UAV_current_Position[connect_to_pso2][2], 441 | Init_PartPosition[i][0], Init_PartPosition[i][1], 442 | Init_PartPosition[i][2]) 443 | if func4 == True: 444 | PartPosition[i] = Init_PartPosition[i] 445 | iter_value_while_to_false = 5 446 | exitFlag = True 447 | break 448 | else: 449 | exitFlag3 = False 450 | for connect_to_pso3 in range(NumUAV): 451 | if (connect_to_pso3 != current_uav) and ( 452 | connect_to_pso3 != connect_to_pso2) and ( 453 | connect_to_pso3 != connect_to_pso): 454 | func5 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[connect_to_pso3][0], 455 | UAV_current_Position[connect_to_pso3][1], 456 | UAV_current_Position[connect_to_pso3][2], 457 | UAV_current_Position[connect_to_pso2][0], 458 | UAV_current_Position[connect_to_pso2][1], 459 | UAV_current_Position[connect_to_pso2][ 460 | 2]) 461 | if func5 == True: 462 | func6 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[connect_to_pso3][0], 463 | UAV_current_Position[connect_to_pso3][1], 464 | UAV_current_Position[connect_to_pso3][2], 465 | Init_PartPosition[i][0], 466 | Init_PartPosition[i][1], 467 | Init_PartPosition[i][2]) 468 | if func6 == True: 469 | PartPosition[i] = Init_PartPosition[i] 470 | iter_value_while_to_false = 5 471 | exitFlag = True 472 | exitFlag2 = True 473 | break 474 | else: 475 | for connect_to_pso4 in range(NumUAV): 476 | if (connect_to_pso4 != current_uav) and ( 477 | connect_to_pso4 != connect_to_pso2) and ( 478 | connect_to_pso4 != connect_to_pso) and ( 479 | connect_to_pso4 != connect_to_pso3): 480 | func7 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[ 481 | connect_to_pso3][0], 482 | UAV_current_Position[connect_to_pso3][1], 483 | UAV_current_Position[connect_to_pso3][2], 484 | UAV_current_Position[connect_to_pso4][0], 485 | UAV_current_Position[connect_to_pso4][1], 486 | UAV_current_Position[connect_to_pso4][2]) 487 | if func7 == True: 488 | func8 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[connect_to_pso4][0], 489 | UAV_current_Position[connect_to_pso4][1], 490 | UAV_current_Position[connect_to_pso4][2], 491 | Init_PartPosition[i][0], 492 | Init_PartPosition[i][1], 493 | Init_PartPosition[i][2]) 494 | if func8 == True: 495 | PartPosition[i] = Init_PartPosition[i] 496 | iter_value_while_to_false = 5 497 | exitFlag = True 498 | exitFlag2 = True 499 | exitFlag3 = True 500 | break 501 | if (exitFlag3): 502 | break 503 | if (exitFlag2): 504 | break 505 | if (exitFlag): 506 | break 507 | else: 508 | func9 = check_connection_with_other_UAVs_in_PSO(Init_PartPosition[i][0], 509 | Init_PartPosition[i][1], Init_PartPosition[i][2], GBS_position[0], GBS_position[1], 510 | GBS_position[2]) 511 | if func9 == True: 512 | old_uav_to_other_uav_con = [] 513 | for second_check in range(NumUAV): 514 | if second_check != current_uav: 515 | func10 = check_connection_with_other_UAVs_in_PSO(UAV_current_Position[second_check][0], 516 | UAV_current_Position[second_check][1], 517 | UAV_current_Position[second_check][2], 518 | UAV_current_Position[current_uav][0], 519 | UAV_current_Position[current_uav][1], 520 | UAV_current_Position[current_uav][2]) 521 | if func10 == True: 522 | old_uav_to_other_uav_con.append(second_check) 523 | if (all((distance(UAV_current_Position[con_elem][0], 524 | UAV_current_Position[con_elem][1], 525 | UAV_current_Position[con_elem][2], Init_PartPosition[i][0], 526 | Init_PartPosition[i][1], Init_PartPosition[i][2])) <= d_u_u_max for 527 | con_elem in old_uav_to_other_uav_con)) and (all 528 | (all(check_sdpc_function( 529 | SDPC.coordinate_and_compare_height( 530 | UAV_current_Position[con_elem][0], 531 | UAV_current_Position[con_elem][1], 532 | Init_PartPosition[i][0], Init_PartPosition[i][1]), 533 | UAV_current_Position[con_elem][0], 534 | UAV_current_Position[con_elem][1], 535 | UAV_current_Position[con_elem][2], 536 | Init_PartPosition[i][0], Init_PartPosition[i][1], 537 | Init_PartPosition[i][2])) == 1 for con_elem in old_uav_to_other_uav_con)): 538 | PartPosition[i] = Init_PartPosition[i] 539 | break 540 | fit_func = fitness_function(PartPosition[i][0], PartPosition[i][1], PartPosition[i][2], 541 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 542 | UAV_current_Position[current_uav][2]) 543 | if fit_func > PartBestValue[i]: 544 | PartBestValue[i] = fit_func 545 | for j in range(NumDimention): 546 | PartBestPosition[i][j] = PartPosition[i][j] 547 | if fit_func > GlobalBestValue: 548 | GlobalBestValue = fit_func 549 | GlobalBestPosition = PartPosition[i] 550 | 551 | global_average_f = 0 552 | global_average_ft = 0 553 | global_average_fs = 0 554 | global_average_fe = 0 555 | global_time = 0 556 | average_accumulated_total_value = 0 557 | average_accumulated_time_value= 0 558 | average_accumulated_energy_value = 0 559 | average_accumulated_sensing_value = 0 560 | average_average_sensing_value = 0 561 | average_max_time = 0 562 | average_number_of_movement = 0 563 | average_number_of_covered_cells = 0 564 | 565 | simulation_time = 1 566 | while simulation_time < 21: 567 | plot_sensing = [] 568 | plot_energy = [] 569 | plot_uav_num = [] 570 | plot_time = [] 571 | plot_uav_num_time = [] 572 | 573 | # start_time = time.time() 574 | NumUAV =5 575 | MAX_time = 15 576 | MIN_energy = 20 577 | energy_consumption = [400 for _ in range(NumUAV)] 578 | 579 | theta = math.pi / 3 580 | noise = -80 581 | moving_speed = 10 582 | ValueMax = 2.0 583 | ValueMin = 0.2 584 | sensor_num_Max = 10 585 | sensor_num_Min = 2 586 | d_u_s_max = 265 # 1025 587 | d_u_u_max = 1327 # change to 1327 or 839 588 | w1, w2, w3 = 0.3, 0.35, 0.35 589 | # w1, w2, w3 = 0.4, 0.1, 0.5 590 | 591 | R = 3.5 # 120 secs to 2 mins 592 | NOR_S = 124 593 | NOR_T = 43.9 594 | NOR_E = 75 595 | # NOR_T = 157.23 596 | # NOR_E = 33.27 597 | em = 2 598 | es = 1 599 | ec = 0.03 600 | a_coef = (ValueMax - ValueMin) / ((math.e ** R) - 1) 601 | b_coef = ValueMin - a_coef 602 | 603 | # PSO parameters: 604 | NumParticle = 5 605 | NumDimention = 3 606 | MaxIteration = 50 607 | VelocityMax = 7 608 | c1, c2 = 2, 2 609 | w_weight = 0.5 610 | GBS_position = [0, 0, 0] 611 | 612 | v_value = np.ones((50, 50), float) 613 | v_value.fill(2.0) 614 | prev_time = np.zeros((50, 50), float) 615 | prev_time.fill(100) 616 | sensor_num = np.zeros((50, 50), int) 617 | number_of_sensor_function() 618 | UAV_current_Position = [[0, 0, 0] for _ in range(NumUAV)] 619 | 620 | time_of_each_UAV = [0 for _ in range(NumUAV)] 621 | copy_time_of_each_UAV = [0 for _ in range(NumUAV)] 622 | uav_terminates = [] 623 | # vsi_x = 15 624 | # vsi_y = 15 625 | # time_list = [0] 626 | # value_list = [2] 627 | average_f_value = 0 628 | accumulative_sensing_value_list = [] 629 | accumulative_total_value_list = [] 630 | average_s_value = 0 631 | average_t_value = 0 632 | average_e_value = 0 633 | movement_uav = 0 634 | movement_uav_list = [] 635 | covered_cell_check = np.zeros((50, 50), float) 636 | accumulated_covered_cell = [] 637 | movement = 0 638 | old_current_uav = 6 639 | list_to_simulate = [] 640 | while movement < 1: 641 | # if movement_uav > 20: 642 | # n_components = 3 643 | # X_surface, truth_surface = make_blobs(n_samples=300, centers=[[0.72, 0.72], [0.6, 1.4], [1.4, 1.20]], 644 | # cluster_std=[0.24, 0.12, 0.18], random_state=42) 645 | # x_surface = X_surface[:, 0] 646 | # y_surface = X_surface[:, 1] 647 | # xx_surface, yy_surface = np.mgrid[-0.02:1.98:51j, -0.02:1.98:51j] 648 | # positions_surface = np.vstack([xx_surface.ravel(), yy_surface.ravel()]) 649 | # values_surface = np.vstack([x_surface, y_surface]) 650 | # kernel_surface = st.gaussian_kde(values_surface) 651 | # f_surface = np.reshape(kernel_surface(positions_surface).T, xx_surface.shape) 652 | # zz_surface = f_surface * 0.35 653 | # 654 | # X_sensor, y_true_sensor = make_blobs(n_samples=20000, 655 | # centers=[[0.44, 0.44], [0.8, 0.8], [0.6, 1.52], [1.4, 0.6], 656 | # [1.52, 1.4]], 657 | # cluster_std=[0.16, 0.16, 0.204, 0.21, 0.2], random_state=0) 658 | # x_sensors = X_sensor[:, 0] 659 | # y_sensors = X_sensor[:, 1] 660 | # xx_sensors, yy_sensors = np.mgrid[0:1.96:50j, 0:1.96:50j] 661 | # positions_sensors = np.vstack([xx_sensors.ravel(), yy_sensors.ravel()]) 662 | # values_sensor = np.vstack([x_sensors, y_sensors]) 663 | # kernel_sensor = st.gaussian_kde(values_sensor) 664 | # f_sensor = np.reshape(kernel_sensor(positions_sensors).T, xx_sensors.shape) 665 | # zz_sensor = f_sensor * 34.41 # 20 000 sensors 666 | # vector_sensor = np.vectorize(np.int32) 667 | # zz_sensor = vector_sensor(zz_sensor) 668 | # fig, ax = plt.subplots(nrows=1, ncols=1) 669 | # ax.set_xlim(xmin=0, xmax=2) 670 | # ax.set_ylim(ymin=0, ymax=2) 671 | # ax.xaxis.set_major_locator(MultipleLocator(base=0.2)) 672 | # ax.yaxis.set_major_locator(MultipleLocator(base=0.2)) 673 | # plt.contour(xx_surface, yy_surface, zz_surface, 20, cmap='RdGy') 674 | # plt.scatter(x_sensors, y_sensors, marker='+', s=3, color='grey', zorder=3) 675 | # # ax.set_title('Contour map') 676 | # ax.set_xlabel('x [km] ', size=15) 677 | # ax.set_ylabel('y [km] ', size=15) 678 | # plt.plot(0, 0, marker='.', color='brown', markersize=26) 679 | # ax.text(0.02, 0.02, "GBS", color='brown', size=17) 680 | # ax.tick_params(which='major', length=5, width=2, direction='in', bottom=True, top=True, left=True, 681 | # right=True) 682 | # ax.grid(linewidth=1, color='lightgrey') 683 | # 684 | # 685 | # for uav_connection in range(NumUAV): 686 | # if uav_connection == 0: 687 | # color = 'red' 688 | # elif uav_connection == 1: 689 | # color = 'green' 690 | # elif uav_connection == 2: 691 | # color = 'magenta' 692 | # elif uav_connection == 3: 693 | # color = 'purple' 694 | # elif uav_connection == 4: 695 | # color = 'navy' 696 | # plt.scatter(UAV_current_Position[uav_connection][0] * 0.04, UAV_current_Position[uav_connection][1] * 0.04, s=100, 697 | # zorder=10, color=color, label=f"UAV {uav_connection+1} " ) 698 | # 699 | # for con_num in range(NumUAV): 700 | # if con_num != uav_connection: 701 | # if distance(UAV_current_Position[uav_connection][0] , UAV_current_Position[uav_connection][1], 702 | # UAV_current_Position[uav_connection][2], 703 | # UAV_current_Position[con_num][0], UAV_current_Position[con_num][1], UAV_current_Position[con_num][2]) <= d_u_u_max: 704 | # cell_j = SDPC.coordinate_and_compare_height(UAV_current_Position[uav_connection][0], 705 | # UAV_current_Position[uav_connection][1], 706 | # UAV_current_Position[con_num][0], UAV_current_Position[con_num][1]) 707 | # sdpc_list_check = check_sdpc_function(cell_j, UAV_current_Position[uav_connection][0], 708 | # UAV_current_Position[uav_connection][1], 709 | # UAV_current_Position[uav_connection][2], 710 | # UAV_current_Position[con_num][0], UAV_current_Position[con_num][1], UAV_current_Position[con_num][2]) 711 | # if all(sdpc_list_check) == 1: 712 | # plt.plot([UAV_current_Position[uav_connection][0] * 0.04, UAV_current_Position[con_num][0] * 0.04], 713 | # [UAV_current_Position[uav_connection][1] * 0.04, UAV_current_Position[con_num][1] * 0.04], 714 | # color='blue', zorder=8, linestyle='solid', linewidth = 1) 715 | # 716 | # if distance(UAV_current_Position[uav_connection][0], UAV_current_Position[uav_connection][1], 717 | # UAV_current_Position[uav_connection][2], 718 | # GBS_position[0], GBS_position[1], GBS_position[2]) <= d_u_u_max: 719 | # cell_j = SDPC.coordinate_and_compare_height(UAV_current_Position[uav_connection][0], 720 | # UAV_current_Position[uav_connection][1], 721 | # GBS_position[0], GBS_position[1]) 722 | # sdpc_list_check = check_sdpc_function(cell_j, UAV_current_Position[uav_connection][0], 723 | # UAV_current_Position[uav_connection][1], 724 | # UAV_current_Position[uav_connection][2], 725 | # GBS_position[0], GBS_position[1], GBS_position[2]) 726 | # if all(sdpc_list_check) == 1: 727 | # plt.plot([UAV_current_Position[uav_connection][0] * 0.04, 0], 728 | # [UAV_current_Position[uav_connection][1]* 0.04, 0], 729 | # color='blue', zorder=8, linestyle='solid', linewidth = 1) 730 | # # handles, labels = plt.gca().get_legend_handles_labels() 731 | # # line = Line2D([0], [0], label='Connection line', color='blue') 732 | # # handles.extend([line]) 733 | # # plt.legend(handles=handles, loc = 'upper left'lower right) 734 | # plt.legend(fontsize=12, loc = 'center left') 735 | # if movement_uav == 3: 736 | # movement = 3 737 | GlobalBestValue = -5 738 | PartVelocity = [[[] for _ in range(NumDimention)] for _ in range(NumParticle)] 739 | PartPosition = [[[] for _ in range(NumDimention)] for _ in range(NumParticle)] 740 | PartBestPosition = [[[] for _ in range(NumDimention)] for _ in range(NumParticle)] 741 | PartBestValue = [[] for _ in range(NumParticle)] 742 | Part_dic = dict() 743 | GlobalBestPosition = [] 744 | 745 | if all(time_of_each_UAV) != 0: 746 | copy_time_of_each_UAV2 = [] 747 | for more_zero in copy_time_of_each_UAV: 748 | if more_zero != 0: 749 | copy_time_of_each_UAV2.append(more_zero) 750 | min_time = min(every_time for every_time in copy_time_of_each_UAV2) 751 | current_uav = time_of_each_UAV.index(min_time) 752 | else: 753 | current_uav = time_of_each_UAV.index(0) 754 | 755 | 756 | if old_current_uav < NumUAV: 757 | for x_value in range(50): 758 | for y_value in range(50): 759 | if abs(time_of_each_UAV[old_current_uav] - prev_time[x_value][y_value]) <= R: 760 | v_value[x_value][y_value] = a_coef * math.exp( 761 | time_of_each_UAV[old_current_uav] - prev_time[x_value][y_value]) + b_coef 762 | else: 763 | v_value[x_value][y_value] = ValueMax 764 | check_coverage_old_uav = check_coverage(d_u_s_max, theta, UAV_current_Position[old_current_uav][0], 765 | UAV_current_Position[old_current_uav][1], 766 | UAV_current_Position[old_current_uav][2]) 767 | for cov in check_coverage_old_uav: 768 | v_value[cov[0]][cov[1]] = 0.2 769 | prev_time[cov[0]][cov[1]] = time_of_each_UAV[old_current_uav] 770 | UAV_current_Position[old_current_uav] = [0, 0, 0] 771 | old_current_uav = 6 772 | 773 | 774 | for x_value in range(50): 775 | for y_value in range(50): 776 | if abs(time_of_each_UAV[current_uav] - prev_time[x_value][y_value]) <= R: 777 | v_value[x_value][y_value] = a_coef * math.exp(time_of_each_UAV[current_uav] - prev_time[x_value][y_value]) + b_coef 778 | else: 779 | v_value[x_value][y_value] = ValueMax 780 | # if movement_uav == 0: 781 | # x_y_vsi = check_coverage_new_position[0] 782 | # vsi_x = x_y_vsi[0] 783 | # vsi_y = x_y_vsi[1] 784 | # value_list.append(v_value[vsi_x][vsi_y]) 785 | # time_list.append(time_of_each_UAV[current_uav] + moving_time) 786 | check_coverage_current_position = check_coverage(d_u_s_max, theta, UAV_current_Position[current_uav][0],UAV_current_Position[current_uav][1], UAV_current_Position[current_uav][2]) 787 | 788 | for cov in check_coverage_current_position: 789 | v_value[cov[0]][cov[1]] = 0.2 790 | prev_time[cov[0]][cov[1]] = time_of_each_UAV[current_uav] 791 | # value_list.append(v_value[vsi_x][vsi_y]) 792 | # time_list.append(time_of_each_UAV[current_uav] + moving_time) 793 | 794 | 795 | 796 | pso_particle_pos_velocity_initialization(NumParticle, NumDimention, VelocityMax, current_uav) 797 | pso_iteration(MaxIteration, NumParticle, NumDimention, w_weight, c1, c2, VelocityMax, current_uav) 798 | next_uav_position = GlobalBestPosition 799 | moving_time = (distance(UAV_current_Position[current_uav][0],UAV_current_Position[current_uav][1], UAV_current_Position[current_uav][2], next_uav_position[0], next_uav_position[1], next_uav_position[2]) / moving_speed) / 60 800 | F_S = F_S_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 801 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 802 | UAV_current_Position[current_uav][2]) 803 | average_s_value += F_S 804 | accumulative_sensing_value_list.append(average_s_value) 805 | F_T = F_T_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 806 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 807 | UAV_current_Position[current_uav][2]) 808 | average_t_value += F_T 809 | F_E = F_E_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 810 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 811 | UAV_current_Position[current_uav][2]) 812 | average_e_value += F_E 813 | F = fitness_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 814 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 815 | UAV_current_Position[current_uav][2]) 816 | average_f_value += F 817 | accumulative_total_value_list.append(average_f_value) 818 | check_coverage_new_position = check_coverage(d_u_s_max, theta, next_uav_position[0],next_uav_position[1],next_uav_position[2]) 819 | 820 | for x_value in range(50): 821 | for y_value in range(50): 822 | if abs((time_of_each_UAV[current_uav] + moving_time) - prev_time[x_value][y_value]) <= R: 823 | v_value[x_value][y_value] = a_coef * math.exp((time_of_each_UAV[current_uav] + moving_time ) - prev_time[x_value][y_value]) + b_coef 824 | else: 825 | v_value[x_value][y_value] = ValueMax 826 | 827 | new_position_stay_time_sum = 0 828 | for cov_next in check_coverage_new_position: 829 | covered_cell_check[cov_next[0]][cov_next[1]] = 1 830 | snr = SNR(UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 831 | UAV_current_Position[current_uav][2], cov_next[0], cov_next[1]) 832 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr - 0.6)) 833 | next_stay_time_c_i = people_number[cov_next[0]][cov_next[1]] * 0.02 * 1 / (1 - p_g_c_i) 834 | new_position_stay_time_sum += next_stay_time_c_i 835 | new_position_stay_time = new_position_stay_time_sum / 60 836 | 837 | num_covered = 0 838 | for ii in range(50): 839 | for jj in range(50): 840 | if covered_cell_check[ii][jj] == 1: 841 | num_covered += 1 842 | accumulated_covered_cell.append(num_covered) 843 | 844 | time_update_in_movement = time_of_each_UAV[current_uav] + moving_time + new_position_stay_time 845 | # if movement_uav > 20: 846 | # list_to_simulate.append(time_update_in_movement) 847 | copy_time_of_each_UAV[current_uav] = time_update_in_movement 848 | time_of_each_UAV[current_uav] = time_update_in_movement 849 | plot_time.append(time_of_each_UAV[current_uav]) 850 | 851 | 852 | residual_e = residual_energy(UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], UAV_current_Position[current_uav][2],next_uav_position[0], next_uav_position[1], next_uav_position[2] ) 853 | energy_consumption[current_uav] = energy_consumption[current_uav] - residual_e 854 | 855 | if (energy_consumption[current_uav] < MIN_energy) or (time_of_each_UAV[current_uav] > MAX_time): 856 | copy_time_of_each_UAV[current_uav] = 0 857 | uav_terminates.append(current_uav) 858 | print(f"there will be minus 1 uav {current_uav}") 859 | # UAV_current_Position[current_uav] = [0, 0, 0] 860 | plot_uav_num_time.append(time_of_each_UAV[current_uav]) 861 | old_current_uav = current_uav 862 | UAV_current_Position[current_uav] = next_uav_position 863 | 864 | 865 | # while_close = [] 866 | # for i_j in copy_time_of_each_UAV: 867 | # if i_j == 0: 868 | # while_close.append(i_j) 869 | 870 | if len(uav_terminates) == NumUAV: 871 | print(f"There is no UAV at movement time ") 872 | movement = 10 873 | print(f'sensing { F, F_S, F_T, F_E}') 874 | print(f"current UAV number {current_uav}: current position {next_uav_position}") 875 | print(movement_uav) 876 | movement_uav += 1 877 | print(f"accumulative total fitness value{average_f_value}") 878 | print(f"average sensing, time, energy {average_s_value/movement_uav, average_t_value/ movement_uav, average_e_value/ movement_uav}") 879 | print("max sensing, time, energy value", average_s_value, average_t_value, average_e_value) 880 | print(f"Num of movements {movement_uav}") 881 | print(f"Max number of covered cells {max(accumulated_covered_cell)}") 882 | print(f"plot last uav terminated its work {max(plot_uav_num_time)}") 883 | print(f"plot uav terminated its work {plot_uav_num_time}") 884 | 885 | print(f" time {plot_time}") 886 | print(f"accumulateve total value{accumulative_total_value_list}") 887 | print(f" accumulative sensing value {accumulative_sensing_value_list}") 888 | print(f"accumulated covered cells {accumulated_covered_cell}") 889 | average_number_of_movement += movement_uav 890 | average_accumulated_total_value += average_f_value 891 | average_accumulated_sensing_value += average_s_value 892 | average_accumulated_time_value += average_t_value 893 | average_accumulated_energy_value += average_e_value 894 | average_average_sensing_value += average_s_value / movement_uav 895 | average_number_of_covered_cells += max(accumulated_covered_cell) 896 | average_max_time += max(plot_time) 897 | 898 | simulation_time += 1 899 | print("simulation:", simulation_time) 900 | print("max total value", average_accumulated_total_value / 20) 901 | print("max sensing value", average_accumulated_sensing_value / 20) 902 | print("max time value", average_accumulated_time_value / 20) 903 | print("max energy value", average_accumulated_energy_value / 20) 904 | 905 | print(f"Num of movements {average_number_of_movement / 20}") 906 | print(f"Max number of covered cells {average_number_of_covered_cells / 20}") 907 | print(f" time {average_max_time / 20}") 908 | print(f" average sensing value {average_average_sensing_value / 20}") 909 | 910 | # print("max sensing value", average_accumulated_sensing_value / 3) 911 | # print(f"Num of movements {average_number_of_movement/3}") 912 | # print(f"Max number of covered cells {average_number_of_covered_cells/3}") 913 | # print(f" time {average_max_time / 3}") 914 | # print(f" average sensing value {average_average_sensing_value / 3}") 915 | 916 | # fig4 = plt.figure(30) 917 | # ax = plt.axes() 918 | # plt.plot(time_list, value_list) 919 | # ax.set(xlabel = 'Time t in minutes',ylabel = 'VSI') 920 | # ax.set_ylim(ymin = 0) 921 | # ax.yaxis.set_major_locator(MultipleLocator(base=0.2)) 922 | # print("time list ", time_list) 923 | # print("value list", value_list) 924 | # for jk in v_value: 925 | # print(jk) 926 | # 927 | # plt.show() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UAV-path-planning-using-PSO-algorithm 2 | The goal is to find an optimal path for UAV to gather more data in WSN. 3 | -------------------------------------------------------------------------------- /RandomAndHoveringMethod.py: -------------------------------------------------------------------------------- 1 | import copy 2 | import random 3 | import math 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from matplotlib.lines import Line2D 7 | from matplotlib.ticker import MultipleLocator 8 | 9 | import time 10 | from sklearn.datasets._samples_generator import make_blobs 11 | import scipy.stats as st 12 | import SDPC 13 | n_components = 3 14 | X_surface, truth_surface = make_blobs(n_samples=300, centers=[[0.72, 0.72], [0.6, 1.4], [1.4, 1.20]], cluster_std=[0.24, 0.12, 0.18], random_state=42) 15 | x_surface = X_surface[:, 0] 16 | y_surface = X_surface[:, 1] 17 | xx_surface, yy_surface = np.mgrid[-0.02:1.98:51j, -0.02:1.98:51j] 18 | positions_surface = np.vstack([xx_surface.ravel(), yy_surface.ravel()]) 19 | values_surface = np.vstack([x_surface, y_surface]) 20 | kernel_surface = st.gaussian_kde(values_surface) 21 | f_surface = np.reshape(kernel_surface(positions_surface).T, xx_surface.shape) 22 | zz_surface = f_surface * 0.35 23 | 24 | X_sensor, y_true_sensor = make_blobs(n_samples=20000,centers=[[0.1, 0.1], [0.5, 0.5], [0.6, 1.25], [1.3, 0.6], [1.52, 1.4]], 25 | cluster_std=[0.2, 0.16, 0.204, 0.18, 0.2], random_state=0) 26 | x_sensors = X_sensor[:, 0] 27 | y_sensors = X_sensor[:, 1] 28 | xx_sensors, yy_sensors = np.mgrid[0:1.96:50j, 0:1.96:50j] 29 | positions_sensors = np.vstack([xx_sensors.ravel(), yy_sensors.ravel()]) 30 | values_sensor = np.vstack([x_sensors, y_sensors]) 31 | kernel_sensor = st.gaussian_kde(values_sensor) 32 | f_sensor = np.reshape(kernel_sensor(positions_sensors).T, xx_sensors.shape) 33 | zz_sensor = f_sensor * 37.865 # 20 000 sensors 34 | 35 | # zz_sensor = f_sensor * 19.919 # 10 000 sensors 36 | # zz_sensor = f_sensor * 10.859# 5 000 sensors 37 | 38 | vector_sensor = np.vectorize(np.int32) 39 | zz_sensor = vector_sensor(zz_sensor) 40 | 41 | people_number = zz_sensor 42 | z_mountain = np.delete(zz_surface, 0, 0) # delete the first line of -0.02. It was added in order to male '0' as a center of the cell 43 | 44 | 45 | 46 | prohibited_zone = [] 47 | for a in range(35, 38): 48 | for b in range(35, 38): 49 | for c in np.arange(0.7, 0.8): 50 | prohibited_zone.append([a, b, c]) 51 | 52 | 53 | def distance(x1, y1, z1, x2, y2, z2): 54 | dist = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2 + (z1 - z2) ** 2) * 40 55 | return dist 56 | 57 | 58 | def check_coverage(d_u_s_max, theta, x, y, z): 59 | covered_cells_1 = [] 60 | covered_cells_2 = [] 61 | covered_cells_3 = [] 62 | covered_cells_final = [] 63 | r_max = d_u_s_max * math.sin(theta / 2) 64 | for x_cell in range(0, 50): 65 | for y_cell in range(0, 50): 66 | r_k_i = math.sqrt((x - x_cell) ** 2 + (y - y_cell) ** 2) * 40 67 | if r_max >= r_k_i: 68 | covered_cells_1.append([x_cell, y_cell]) 69 | for cell in covered_cells_1: 70 | d_k_i = distance(x, y, z, cell[0], cell[1], z_mountain[cell[0]][cell[1]]) 71 | if d_u_s_max >= d_k_i: 72 | covered_cells_2.append([cell[0], cell[1]]) 73 | for cell in covered_cells_2: 74 | d_k_i = distance(x, y, z, cell[0], cell[1], z_mountain[cell[0]][cell[1]]) 75 | d = ((z - z_mountain[cell[0]][cell[1]]) * 40) / math.cos(theta / 2) 76 | if (d_k_i <= d) and (z_mountain[cell[0]][cell[1]] <= z): 77 | covered_cells_3.append([cell[0], cell[1]]) 78 | for cell in covered_cells_3: 79 | cell_j = SDPC.coordinate_and_compare_height(x, y, cell[0], cell[1]) 80 | for cell_j_axis in cell_j: 81 | z_j_LOS_k_i = (z - z_mountain[cell[0]][cell[1]]) / ( 82 | math.sqrt((x - cell[0]) ** 2 + (y - cell[1]) ** 2)) * math.sqrt( 83 | (cell_j_axis[0] - cell[0]) ** 2 + (cell_j_axis[1] - cell[1]) ** 2) + z_mountain[cell[0]][cell[1]] 84 | if z_mountain[cell_j_axis[0]][cell_j_axis[1]] < z_j_LOS_k_i: 85 | if [cell[0], cell[1]] not in covered_cells_final: 86 | covered_cells_final.append([cell[0], cell[1]]) 87 | return covered_cells_final 88 | 89 | 90 | def SNR(x_uav, y_uav, z_uav, x_c_i, y_c_i): 91 | d_k_i = distance(x_uav, y_uav, z_uav, x_c_i, y_c_i, z_mountain[x_c_i][y_c_i]) 92 | cal_snr = (10 * math.log10(10 / (9 * 16 * (math.pi ** 2) * (d_k_i ** 2)))) - noise 93 | return cal_snr 94 | 95 | 96 | def sensing_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z): 97 | d_k_m = distance(UAV_x, UAV_y, UAV_z, Part_Position_x, Part_Position_y, Part_Position_z) 98 | T_moving = d_k_m / moving_speed / 60 99 | v_value_copy = copy.deepcopy(v_value) 100 | for x_value in range(50): 101 | for y_value in range(50): 102 | if abs((time_of_each_UAV[current_uav] + T_moving) - prev_time[x_value][y_value]) <= R: 103 | v_value_copy[x_value][y_value] = a_coef * math.exp( 104 | (time_of_each_UAV[current_uav] + T_moving) - prev_time[x_value][y_value]) + b_coef 105 | else: 106 | v_value_copy[x_value][y_value] = ValueMax 107 | return v_value_copy 108 | 109 | 110 | def number_of_sensor_function(): 111 | for i in range(50): 112 | for j in range(50): 113 | if people_number[i][j] < 0: 114 | sensor_num[i][j] = 0 115 | elif 1 <= people_number[i][j] <= sensor_num_Max: 116 | sensor_num[i][j] = (((sensor_num_Max - sensor_num_Min) * people_number[i][j]) + ( 117 | sensor_num_Max * (sensor_num_Min - 1))) / (sensor_num_Max - 1) 118 | elif people_number[i][j] > sensor_num_Max: 119 | sensor_num[i][j] = sensor_num_Max 120 | 121 | 122 | def F_S_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z): 123 | copy_value = sensing_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z) 124 | all_covered_cells = [] 125 | for all_uav_current_position in UAV_current_Position: 126 | if all_uav_current_position != UAV_current_Position[current_uav]: #not sure 127 | each_uav_covered_cell = check_coverage(d_u_s_max, theta, all_uav_current_position[0], 128 | all_uav_current_position[1], all_uav_current_position[2]) 129 | all_covered_cells.append(each_uav_covered_cell) 130 | PSO_particle_covered_cells = check_coverage(d_u_s_max, theta, Part_Position_x, Part_Position_y, Part_Position_z) 131 | particle_covered_cells = set() 132 | covered_cells = set() 133 | for i in PSO_particle_covered_cells: 134 | particle_covered_cells.add((i[0], i[1])) 135 | for uav_covered_cells in all_covered_cells: 136 | for j in uav_covered_cells: 137 | covered_cells.add((j[0], j[1])) 138 | not_included_in_other_uav = particle_covered_cells - covered_cells 139 | sum_F_S = 0 140 | for x_y_not_included_in_other_uav in not_included_in_other_uav: 141 | sum_F_S += copy_value[x_y_not_included_in_other_uav[0]][x_y_not_included_in_other_uav[1]] * \ 142 | sensor_num[x_y_not_included_in_other_uav[0]][x_y_not_included_in_other_uav[1]] 143 | F_S = sum_F_S / NOR_S 144 | return F_S 145 | 146 | 147 | def F_T_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z): 148 | d_k_m = distance(UAV_x, UAV_y, UAV_z, Part_Position_x, Part_Position_y, Part_Position_z) 149 | T_moving = d_k_m / moving_speed 150 | F_T_cell = check_coverage(d_u_s_max, theta, Part_Position_x, Part_Position_y, Part_Position_z) 151 | T_stay = 0 152 | for cell in F_T_cell: 153 | snr1 = SNR(x_uav=UAV_x, y_uav=UAV_y, z_uav=UAV_z, x_c_i=cell[0], y_c_i=cell[1]) 154 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr1 - 0.6)) 155 | T_stay += people_number[cell[0]][cell[1]] * 0.02 * (1 / (1 - p_g_c_i)) 156 | F_T = (T_moving + T_stay) / NOR_T 157 | return F_T 158 | 159 | 160 | def F_E_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z): 161 | d_k_m = distance(UAV_x, UAV_y, UAV_z, Part_Position_x, Part_Position_y, Part_Position_z) 162 | E_k_M = d_k_m / moving_speed * em 163 | E_k_S_cell = check_coverage(d_u_s_max, theta, Part_Position_x, Part_Position_y, Part_Position_z) 164 | E_k_S_cell_sum = 0 165 | E_k_C_sum = 0 166 | for cell in E_k_S_cell: 167 | snr1 = SNR(x_uav=UAV_x, y_uav=UAV_y, z_uav=UAV_z, x_c_i=cell[0], y_c_i=cell[1]) 168 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr1 - 0.6)) 169 | E_k_S_cell_sum += people_number[cell[0]][cell[1]] * 0.02 * (1 / (1 - p_g_c_i)) 170 | E_k_C_sum += people_number[cell[0]][cell[1]] * (1 / (1 - p_g_c_i)) * ec 171 | E_k_S = E_k_S_cell_sum * es 172 | F_E = (E_k_M + E_k_S + E_k_C_sum) / NOR_E 173 | return F_E 174 | 175 | 176 | def fitness_function(x, y, z, UAV_x, UAV_y, UAV_z): 177 | F_S = F_S_function(x, y, z, UAV_x, UAV_y, UAV_z) 178 | F_T = F_T_function(x, y, z, UAV_x, UAV_y, UAV_z) 179 | F_E = F_E_function(x, y, z, UAV_x, UAV_y, UAV_z) 180 | F = w1 * F_S - w2 * F_T - w3 * F_E 181 | return F 182 | 183 | 184 | def residual_energy(UAV_x1, UAV_y1, UAV_z1, UAV_x2, UAV_y2, UAV_z2): 185 | d_k_m = distance(UAV_x1, UAV_y1, UAV_z1, UAV_x2, UAV_y2, UAV_z2) 186 | E_k_M = d_k_m / moving_speed * em 187 | E_k_S_cell = check_coverage(d_u_s_max, theta, UAV_x2, UAV_y2, UAV_z2) 188 | E_k_S_cell_sum = 0 189 | E_k_C_sum = 0 190 | for cell in E_k_S_cell: 191 | snr1 = SNR(x_uav=UAV_x1, y_uav=UAV_y1, z_uav=UAV_z1, x_c_i=cell[0], y_c_i=cell[1]) 192 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr1 - 0.6)) 193 | E_k_S_cell_sum += people_number[cell[0]][cell[1]] * 0.02 * (1 / (1 - p_g_c_i)) 194 | E_k_C_sum += people_number[cell[0]][cell[1]] * (1 / (1 - p_g_c_i)) * ec 195 | E_k_S = E_k_S_cell_sum * es 196 | F_E = (E_k_M + E_k_S + E_k_C_sum) 197 | return F_E 198 | 199 | 200 | def check_sdpc_function(cell_j, x1, y1, z1, x2, y2, z2): 201 | check_sdpc = [] 202 | for cell_j_axis in cell_j: 203 | z_j_LOS_uav_gbs = (z1 - z2) / (math.sqrt( 204 | (x1 - x2) ** 2 + (y1 - y2) ** 2)) * math.sqrt((cell_j_axis[0] - x2) ** 2 + (cell_j_axis[1] - y2) ** 2) + z2 205 | if z_mountain[cell_j_axis[0]][cell_j_axis[1]] < z_j_LOS_uav_gbs: 206 | check_sdpc.append(1) 207 | else: 208 | check_sdpc.append(0) 209 | return check_sdpc 210 | 211 | def check_connection_with_other_UAVs_in_PSO(x1, y1, z1, x2, y2, z2): 212 | if (0 < distance(x1,y1,z1,x2,y2,z2) <= d_u_u_max) and ( 213 | all(check_sdpc_function( 214 | SDPC.coordinate_and_compare_height(x1, y1, x2, y2),x1, y1, z1, x2, y2, z2)) == 1): 215 | return True 216 | 217 | 218 | 219 | 220 | 221 | 222 | global_average_f = 0 223 | global_average_ft = 0 224 | global_average_fs = 0 225 | global_average_fe = 0 226 | global_time = 0 227 | average_accumulated_total_value = 0 228 | average_accumulated_time_value= 0 229 | average_accumulated_energy_value = 0 230 | average_accumulated_sensing_value = 0 231 | average_average_sensing_value = 0 232 | average_max_time = 0 233 | average_number_of_movement = 0 234 | average_number_of_covered_cells = 0 235 | 236 | simulation_time = 1 237 | while simulation_time < 2: 238 | plot_sensing = [] 239 | plot_energy = [] 240 | plot_uav_num = [] 241 | plot_time = [] 242 | plot_uav_num_time = [] 243 | 244 | # start_time = time.time() 245 | NumUAV =1 246 | MAX_time = 15 247 | MIN_energy = 20 248 | energy_consumption = [400 for _ in range(NumUAV)] 249 | 250 | theta = math.pi / 3 251 | noise = -80 252 | moving_speed = 10 253 | ValueMax = 2.0 254 | ValueMin = 0.2 255 | sensor_num_Max = 10 256 | sensor_num_Min = 2 257 | d_u_s_max = 265 # 1025 258 | d_u_u_max = 1327 # change to 1327 or 839 259 | w1, w2, w3 = 0.3, 0.35, 0.35 260 | # w1, w2, w3 = 0.4, 0.1, 0.5 261 | 262 | R = 3.5 # 120 secs to 2 mins 263 | NOR_S = 124 264 | NOR_T = 43.9 265 | NOR_E = 75 266 | # NOR_T = 157.23 267 | # NOR_E = 33.27 268 | em = 2 269 | es = 1 270 | ec = 0.03 271 | a_coef = (ValueMax - ValueMin) / ((math.e ** R) - 1) 272 | b_coef = ValueMin - a_coef 273 | 274 | # PSO parameters: 275 | NumParticle = 5 276 | NumDimention = 3 277 | MaxIteration = 50 278 | VelocityMax = 7 279 | c1, c2 = 2, 2 280 | w_weight = 0.5 281 | GBS_position = [0, 0, 0] 282 | 283 | v_value = np.ones((50, 50), float) 284 | v_value.fill(2.0) 285 | prev_time = np.zeros((50, 50), float) 286 | prev_time.fill(100) 287 | sensor_num = np.zeros((50, 50), int) 288 | number_of_sensor_function() 289 | UAV_current_Position = [[5, 5, 5] for _ in range(NumUAV)] 290 | 291 | time_of_each_UAV = [0 for _ in range(NumUAV)] 292 | copy_time_of_each_UAV = [0 for _ in range(NumUAV)] 293 | uav_terminates = [] 294 | # vsi_x = 15 295 | # vsi_y = 15 296 | # time_list = [0] 297 | # value_list = [2] 298 | average_f_value = 0 299 | accumulative_sensing_value_list = [] 300 | accumulative_total_value_list = [] 301 | average_s_value = 0 302 | average_t_value = 0 303 | average_e_value = 0 304 | movement_uav = 0 305 | movement_uav_list = [] 306 | covered_cell_check = np.zeros((50, 50), float) 307 | accumulated_covered_cell = [] 308 | movement = 0 309 | old_current_uav = 6 310 | list_to_simulate = [] 311 | while movement < 1: 312 | 313 | 314 | if all(time_of_each_UAV) != 0: 315 | copy_time_of_each_UAV2 = [] 316 | for more_zero in copy_time_of_each_UAV: 317 | if more_zero != 0: 318 | copy_time_of_each_UAV2.append(more_zero) 319 | min_time = min(every_time for every_time in copy_time_of_each_UAV2) 320 | current_uav = time_of_each_UAV.index(min_time) 321 | else: 322 | current_uav = time_of_each_UAV.index(0) 323 | 324 | 325 | if old_current_uav < NumUAV: 326 | for x_value in range(50): 327 | for y_value in range(50): 328 | if abs(time_of_each_UAV[old_current_uav] - prev_time[x_value][y_value]) <= R: 329 | v_value[x_value][y_value] = a_coef * math.exp( 330 | time_of_each_UAV[old_current_uav] - prev_time[x_value][y_value]) + b_coef 331 | else: 332 | v_value[x_value][y_value] = ValueMax 333 | check_coverage_old_uav = check_coverage(d_u_s_max, theta, UAV_current_Position[old_current_uav][0], 334 | UAV_current_Position[old_current_uav][1], 335 | UAV_current_Position[old_current_uav][2]) 336 | for cov in check_coverage_old_uav: 337 | v_value[cov[0]][cov[1]] = 0.2 338 | prev_time[cov[0]][cov[1]] = time_of_each_UAV[old_current_uav] 339 | UAV_current_Position[old_current_uav] = [0, 0, 0] 340 | old_current_uav = 6 341 | 342 | 343 | for x_value in range(50): 344 | for y_value in range(50): 345 | if abs(time_of_each_UAV[current_uav] - prev_time[x_value][y_value]) <= R: 346 | v_value[x_value][y_value] = a_coef * math.exp(time_of_each_UAV[current_uav] - prev_time[x_value][y_value]) + b_coef 347 | else: 348 | v_value[x_value][y_value] = ValueMax 349 | # if movement_uav == 0: 350 | # x_y_vsi = check_coverage_new_position[0] 351 | # vsi_x = x_y_vsi[0] 352 | # vsi_y = x_y_vsi[1] 353 | # value_list.append(v_value[vsi_x][vsi_y]) 354 | # time_list.append(time_of_each_UAV[current_uav] + moving_time) 355 | check_coverage_current_position = check_coverage(d_u_s_max, theta, UAV_current_Position[current_uav][0],UAV_current_Position[current_uav][1], UAV_current_Position[current_uav][2]) 356 | 357 | sensor_number_in_coverage = 0 358 | for cov in check_coverage_current_position: 359 | v_value[cov[0]][cov[1]] = 0.2 360 | prev_time[cov[0]][cov[1]] = time_of_each_UAV[current_uav] 361 | sensor_number_in_coverage += people_number[cov[0]][cov[1]] 362 | # value_list.append(v_value[vsi_x][vsi_y]) 363 | # time_list.append(time_of_each_UAV[current_uav] + moving_time) 364 | 365 | 366 | # if sensor_number_in_coverage < 2: 367 | next_uav_position = [] 368 | init_value_while_to_false = 0 369 | while init_value_while_to_false < 2: 370 | new_position_list = [] 371 | new_position_list.append(random.randint(0, 49)) 372 | new_position_list.append(random.randint(0, 49)) 373 | new_position_list.append(random.uniform(0, 23.9)) 374 | if (new_position_list not in prohibited_zone) and (new_position_list not in UAV_current_Position) and ( 375 | new_position_list[2] > z_mountain[new_position_list[0]][new_position_list[1]]): 376 | GBS_UAV_connection = [] 377 | for num_gbs_uav in range(NumUAV): 378 | if num_gbs_uav != current_uav: 379 | if (distance(UAV_current_Position[num_gbs_uav][0], 380 | UAV_current_Position[num_gbs_uav][1], 381 | UAV_current_Position[num_gbs_uav][2], GBS_position[0], GBS_position[1], 382 | GBS_position[2]) <= d_u_u_max) and (all(check_sdpc_function( 383 | SDPC.coordinate_and_compare_height(UAV_current_Position[num_gbs_uav][0], 384 | UAV_current_Position[num_gbs_uav][1], 385 | GBS_position[0], GBS_position[1]), 386 | UAV_current_Position[num_gbs_uav][0], 387 | UAV_current_Position[num_gbs_uav][1], 388 | UAV_current_Position[num_gbs_uav][2], 389 | GBS_position[0], GBS_position[1], GBS_position[2])) == 1): 390 | GBS_UAV_connection.append(num_gbs_uav) 391 | if len(GBS_UAV_connection) >= 1: 392 | exitFlag = False 393 | for connect_to_pso in GBS_UAV_connection: 394 | if (distance(UAV_current_Position[connect_to_pso][0], 395 | UAV_current_Position[connect_to_pso][1], 396 | UAV_current_Position[connect_to_pso][2], new_position_list[0], 397 | new_position_list[1], new_position_list[2]) <= d_u_u_max) and ( 398 | all(check_sdpc_function( 399 | SDPC.coordinate_and_compare_height(UAV_current_Position[connect_to_pso][0], 400 | UAV_current_Position[connect_to_pso][1], 401 | new_position_list[0], 402 | new_position_list[1]), 403 | UAV_current_Position[connect_to_pso][0], 404 | UAV_current_Position[connect_to_pso][1], 405 | UAV_current_Position[connect_to_pso][2], 406 | new_position_list[0], new_position_list[1], 407 | new_position_list[2])) == 1): 408 | 409 | next_uav_position = new_position_list 410 | init_value_while_to_false = 5 411 | break 412 | else: 413 | exitFlag2 = False 414 | for connect_to_pso2 in range(5): 415 | if (connect_to_pso2 != connect_to_pso) and (connect_to_pso2 != current_uav): 416 | if (distance(UAV_current_Position[connect_to_pso][0], 417 | UAV_current_Position[connect_to_pso][1], 418 | UAV_current_Position[connect_to_pso][2], 419 | UAV_current_Position[connect_to_pso2][0], 420 | UAV_current_Position[connect_to_pso2][1], 421 | UAV_current_Position[connect_to_pso2][2]) <= d_u_u_max) and ( 422 | all(check_sdpc_function( 423 | SDPC.coordinate_and_compare_height( 424 | UAV_current_Position[connect_to_pso][0], 425 | UAV_current_Position[connect_to_pso][1], 426 | UAV_current_Position[connect_to_pso2][0], 427 | UAV_current_Position[connect_to_pso2][ 428 | 1]), 429 | UAV_current_Position[connect_to_pso][0], 430 | UAV_current_Position[connect_to_pso][1], 431 | UAV_current_Position[connect_to_pso][2], 432 | UAV_current_Position[connect_to_pso2][0], 433 | UAV_current_Position[connect_to_pso2][1], 434 | UAV_current_Position[connect_to_pso2][2])) == 1): 435 | 436 | if (distance(UAV_current_Position[connect_to_pso2][0], 437 | UAV_current_Position[connect_to_pso2][1], 438 | UAV_current_Position[connect_to_pso2][2], 439 | new_position_list[0], 440 | new_position_list[1], 441 | new_position_list[2]) <= d_u_u_max) and ( 442 | all(check_sdpc_function( 443 | SDPC.coordinate_and_compare_height( 444 | UAV_current_Position[connect_to_pso2][0], 445 | UAV_current_Position[connect_to_pso2][1], 446 | new_position_list[0], new_position_list[1]), 447 | UAV_current_Position[connect_to_pso2][0], 448 | UAV_current_Position[connect_to_pso2][1], 449 | UAV_current_Position[connect_to_pso2][2], 450 | new_position_list[0], new_position_list[1], 451 | new_position_list[2])) == 1): 452 | 453 | next_uav_position = new_position_list 454 | init_value_while_to_false = 5 455 | exitFlag = True 456 | break 457 | else: 458 | exitFlag3 = False 459 | for connect_to_pso3 in range(5): 460 | if (connect_to_pso3 != current_uav) and ( 461 | connect_to_pso3 != connect_to_pso2) and ( 462 | connect_to_pso3 != connect_to_pso): 463 | if (distance(UAV_current_Position[connect_to_pso3][0], 464 | UAV_current_Position[connect_to_pso3][1], 465 | UAV_current_Position[connect_to_pso3][2], 466 | UAV_current_Position[connect_to_pso2][0], 467 | UAV_current_Position[connect_to_pso2][1], 468 | UAV_current_Position[connect_to_pso2][ 469 | 2]) <= d_u_u_max) and ( 470 | all(check_sdpc_function( 471 | SDPC.coordinate_and_compare_height( 472 | UAV_current_Position[connect_to_pso3][0], 473 | UAV_current_Position[connect_to_pso3][1], 474 | UAV_current_Position[connect_to_pso2][0], 475 | UAV_current_Position[connect_to_pso2][ 476 | 1]), 477 | UAV_current_Position[connect_to_pso3][0], 478 | UAV_current_Position[connect_to_pso3][1], 479 | UAV_current_Position[connect_to_pso3][2], 480 | UAV_current_Position[connect_to_pso2][0], 481 | UAV_current_Position[connect_to_pso2][1], 482 | UAV_current_Position[connect_to_pso2][2])) == 1): 483 | if (distance(UAV_current_Position[connect_to_pso3][0], 484 | UAV_current_Position[connect_to_pso3][1], 485 | UAV_current_Position[connect_to_pso3][2], 486 | new_position_list[0], 487 | new_position_list[1], 488 | new_position_list[2]) <= d_u_u_max) and ( 489 | all(check_sdpc_function( 490 | SDPC.coordinate_and_compare_height( 491 | UAV_current_Position[connect_to_pso3][0], 492 | UAV_current_Position[connect_to_pso3][1], 493 | new_position_list[0], 494 | new_position_list[1]), 495 | UAV_current_Position[connect_to_pso3][0], 496 | UAV_current_Position[connect_to_pso3][1], 497 | UAV_current_Position[connect_to_pso3][2], 498 | new_position_list[0], 499 | new_position_list[1], 500 | new_position_list[2])) == 1): 501 | next_uav_position = new_position_list 502 | init_value_while_to_false = 5 503 | exitFlag = True 504 | exitFlag2 = True 505 | break 506 | else: 507 | for connect_to_pso4 in range(5): 508 | if (connect_to_pso4 != current_uav) and ( 509 | connect_to_pso4 != connect_to_pso2) and ( 510 | connect_to_pso4 != connect_to_pso) and ( 511 | connect_to_pso4 != connect_to_pso3): 512 | if (distance( 513 | UAV_current_Position[connect_to_pso3][ 514 | 0], 515 | UAV_current_Position[connect_to_pso3][ 516 | 1], 517 | UAV_current_Position[connect_to_pso3][ 518 | 2], 519 | UAV_current_Position[connect_to_pso2][ 520 | 0], 521 | UAV_current_Position[connect_to_pso2][ 522 | 1], 523 | UAV_current_Position[connect_to_pso2][ 524 | 2]) <= d_u_u_max) and ( 525 | all(check_sdpc_function( 526 | SDPC.coordinate_and_compare_height( 527 | UAV_current_Position[ 528 | connect_to_pso3][ 529 | 0], 530 | UAV_current_Position[ 531 | connect_to_pso3][ 532 | 1], 533 | UAV_current_Position[ 534 | connect_to_pso4][ 535 | 0], 536 | UAV_current_Position[ 537 | connect_to_pso4][ 538 | 1]), 539 | UAV_current_Position[ 540 | connect_to_pso3][ 541 | 0], 542 | UAV_current_Position[ 543 | connect_to_pso3][ 544 | 1], 545 | UAV_current_Position[ 546 | connect_to_pso3][ 547 | 2], 548 | UAV_current_Position[ 549 | connect_to_pso4][ 550 | 0], 551 | UAV_current_Position[ 552 | connect_to_pso4][ 553 | 1], 554 | UAV_current_Position[ 555 | connect_to_pso4][ 556 | 2])) == 1): 557 | if (distance( 558 | UAV_current_Position[ 559 | connect_to_pso4][ 560 | 0], 561 | UAV_current_Position[ 562 | connect_to_pso4][ 563 | 1], 564 | UAV_current_Position[ 565 | connect_to_pso4][ 566 | 2], 567 | new_position_list[0], 568 | new_position_list[1], 569 | new_position_list[ 570 | 2]) <= d_u_u_max) and ( 571 | all(check_sdpc_function( 572 | SDPC.coordinate_and_compare_height( 573 | UAV_current_Position[ 574 | connect_to_pso4][0], 575 | UAV_current_Position[ 576 | connect_to_pso4][1], 577 | new_position_list[0], 578 | new_position_list[1]), 579 | UAV_current_Position[ 580 | connect_to_pso4][ 581 | 0], 582 | UAV_current_Position[ 583 | connect_to_pso4][ 584 | 1], 585 | UAV_current_Position[ 586 | connect_to_pso4][ 587 | 2], 588 | new_position_list[0], 589 | new_position_list[1], 590 | new_position_list[2])) == 1): 591 | next_uav_position = new_position_list 592 | init_value_while_to_false = 5 593 | exitFlag = True 594 | exitFlag2 = True 595 | exitFlag3 = True 596 | break 597 | if (exitFlag3): 598 | break 599 | if (exitFlag2): 600 | break 601 | if (exitFlag): 602 | break 603 | else: 604 | if (distance(new_position_list[0], 605 | new_position_list[1], 606 | new_position_list[2], GBS_position[0], GBS_position[1], 607 | GBS_position[2]) <= d_u_u_max) and (all(check_sdpc_function( 608 | SDPC.coordinate_and_compare_height(new_position_list[0], 609 | new_position_list[1], 610 | GBS_position[0], GBS_position[1]), 611 | new_position_list[0], new_position_list[1], new_position_list[2], GBS_position[0], 612 | GBS_position[1], 613 | GBS_position[2])) == 1): 614 | old_uav_to_other_uav_con = [] 615 | for second_check in range(NumUAV): 616 | if second_check != current_uav: 617 | if (distance(UAV_current_Position[second_check][0], 618 | UAV_current_Position[second_check][1], 619 | UAV_current_Position[second_check][2], 620 | UAV_current_Position[current_uav][0], 621 | UAV_current_Position[current_uav][1], 622 | UAV_current_Position[current_uav][2]) <= d_u_u_max) and ( 623 | all(check_sdpc_function( 624 | SDPC.coordinate_and_compare_height( 625 | UAV_current_Position[second_check][0], 626 | UAV_current_Position[second_check][1], 627 | UAV_current_Position[current_uav][0], 628 | UAV_current_Position[current_uav][1]), 629 | UAV_current_Position[second_check][0], 630 | UAV_current_Position[second_check][1], 631 | UAV_current_Position[second_check][2], 632 | UAV_current_Position[current_uav][0], 633 | UAV_current_Position[current_uav][1], 634 | UAV_current_Position[current_uav][2])) == 1): 635 | old_uav_to_other_uav_con.append(second_check) 636 | 637 | 638 | if (all((distance(UAV_current_Position[con_elem][0], 639 | UAV_current_Position[con_elem][1], 640 | UAV_current_Position[con_elem][2], new_position_list[0], 641 | new_position_list[1], new_position_list[2])) <= d_u_u_max for con_elem in 642 | old_uav_to_other_uav_con)) and (all 643 | (all(check_sdpc_function( 644 | SDPC.coordinate_and_compare_height( 645 | UAV_current_Position[con_elem][0], 646 | UAV_current_Position[con_elem][1], 647 | new_position_list[0], new_position_list[1]), 648 | UAV_current_Position[con_elem][0], 649 | UAV_current_Position[con_elem][1], 650 | UAV_current_Position[con_elem][2], 651 | new_position_list[0], new_position_list[1], 652 | new_position_list[2])) == 1 for con_elem in old_uav_to_other_uav_con)): 653 | next_uav_position = new_position_list 654 | init_value_while_to_false = 5 655 | break 656 | # else: 657 | # next_uav_position = UAV_current_Position[current_uav] 658 | moving_time = (distance(UAV_current_Position[current_uav][0],UAV_current_Position[current_uav][1], UAV_current_Position[current_uav][2], next_uav_position[0], next_uav_position[1], next_uav_position[2]) / moving_speed) / 60 659 | F_S = F_S_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 660 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 661 | UAV_current_Position[current_uav][2]) 662 | average_s_value += F_S 663 | accumulative_sensing_value_list.append(average_s_value) 664 | F_T = F_T_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 665 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 666 | UAV_current_Position[current_uav][2]) 667 | average_t_value += F_T 668 | F_E = F_E_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 669 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 670 | UAV_current_Position[current_uav][2]) 671 | average_e_value += F_E 672 | F = fitness_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 673 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 674 | UAV_current_Position[current_uav][2]) 675 | average_f_value += F 676 | accumulative_total_value_list.append(average_f_value) 677 | check_coverage_new_position = check_coverage(d_u_s_max, theta, next_uav_position[0],next_uav_position[1],next_uav_position[2]) 678 | 679 | for x_value in range(50): 680 | for y_value in range(50): 681 | if abs((time_of_each_UAV[current_uav] + moving_time) - prev_time[x_value][y_value]) <= R: 682 | v_value[x_value][y_value] = a_coef * math.exp((time_of_each_UAV[current_uav] + moving_time ) - prev_time[x_value][y_value]) + b_coef 683 | else: 684 | v_value[x_value][y_value] = ValueMax 685 | 686 | new_position_stay_time_sum = 0 687 | for cov_next in check_coverage_new_position: 688 | covered_cell_check[cov_next[0]][cov_next[1]] = 1 689 | snr = SNR(UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 690 | UAV_current_Position[current_uav][2], cov_next[0], cov_next[1]) 691 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr - 0.6)) 692 | next_stay_time_c_i = people_number[cov_next[0]][cov_next[1]] * 0.02 * 1 / (1 - p_g_c_i) 693 | new_position_stay_time_sum += next_stay_time_c_i 694 | new_position_stay_time = new_position_stay_time_sum / 60 695 | 696 | num_covered = 0 697 | for ii in range(50): 698 | for jj in range(50): 699 | if covered_cell_check[ii][jj] == 1: 700 | num_covered += 1 701 | accumulated_covered_cell.append(num_covered) 702 | 703 | time_update_in_movement = time_of_each_UAV[current_uav] + moving_time + new_position_stay_time 704 | # if movement_uav > 20: 705 | # list_to_simulate.append(time_update_in_movement) 706 | copy_time_of_each_UAV[current_uav] = time_update_in_movement 707 | time_of_each_UAV[current_uav] = time_update_in_movement 708 | plot_time.append(time_of_each_UAV[current_uav]) 709 | 710 | 711 | residual_e = residual_energy(UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], UAV_current_Position[current_uav][2],next_uav_position[0], next_uav_position[1], next_uav_position[2] ) 712 | energy_consumption[current_uav] = energy_consumption[current_uav] - residual_e 713 | 714 | if (energy_consumption[current_uav] < MIN_energy) or (time_of_each_UAV[current_uav] > MAX_time): 715 | copy_time_of_each_UAV[current_uav] = 0 716 | uav_terminates.append(current_uav) 717 | print(f"there will be minus 1 uav {current_uav}") 718 | # UAV_current_Position[current_uav] = [0, 0, 0] 719 | plot_uav_num_time.append(time_of_each_UAV[current_uav]) 720 | old_current_uav = current_uav 721 | UAV_current_Position[current_uav] = next_uav_position 722 | 723 | 724 | # while_close = [] 725 | # for i_j in copy_time_of_each_UAV: 726 | # if i_j == 0: 727 | # while_close.append(i_j) 728 | 729 | 730 | print(f'sensing { F, F_S, F_T, F_E}') 731 | print(f"current UAV number {current_uav}: current position {next_uav_position}") 732 | # print(movement_uav) 733 | movement_uav += 1 734 | # print(f"accumulative total fitness value{average_f_value}") 735 | # print(f"average sensing, time, energy {average_s_value/movement_uav, average_t_value/ movement_uav, average_e_value/ movement_uav}") 736 | # print("max sensing, time, energy value", average_s_value, average_t_value, average_e_value) 737 | # print(f"Num of movements {movement_uav}") 738 | # print(f"Max number of covered cells {max(accumulated_covered_cell)}") 739 | # print(f"plot last uav terminated its work {max(plot_uav_num_time)}") 740 | # print(f"plot uav terminated its work {plot_uav_num_time}") 741 | # if len(uav_terminates) == NumUAV: 742 | # print(f"There is no UAV at movement time ") 743 | movement = 10 744 | print(f" time {plot_time}") 745 | print(f"accumulateve total value{accumulative_total_value_list}") 746 | print(f" accumulative sensing value {accumulative_sensing_value_list}") 747 | print(f"accumulated covered cells {accumulated_covered_cell}") 748 | average_number_of_movement += movement_uav 749 | average_accumulated_total_value += average_f_value 750 | average_accumulated_sensing_value += average_s_value 751 | average_accumulated_time_value += average_t_value 752 | average_accumulated_energy_value += average_e_value 753 | average_average_sensing_value += average_s_value / movement_uav 754 | average_number_of_covered_cells += max(accumulated_covered_cell) 755 | average_max_time += max(plot_time) 756 | print("max total value", average_accumulated_total_value / simulation_time) 757 | print("max sensing value", average_accumulated_sensing_value / simulation_time) 758 | print("max time value", average_accumulated_time_value / simulation_time) 759 | print("max energy value", average_accumulated_energy_value / simulation_time) 760 | 761 | print(f"Num of movements {average_number_of_movement / simulation_time}") 762 | print(f"Max number of covered cells {average_number_of_covered_cells / simulation_time}") 763 | print(f" time {average_max_time / simulation_time}") 764 | print(f" average sensing value {average_average_sensing_value / simulation_time}") 765 | simulation_time += 1 766 | print("simulation:", simulation_time) 767 | # print("max total value", average_accumulated_total_value / 20) 768 | # print("max sensing value", average_accumulated_sensing_value / 20) 769 | # print("max time value", average_accumulated_time_value / 20) 770 | # print("max energy value", average_accumulated_energy_value / 20) 771 | # 772 | # print(f"Num of movements {average_number_of_movement / 20}") 773 | # print(f"Max number of covered cells {average_number_of_covered_cells / 20}") 774 | # print(f" time {average_max_time / 20}") 775 | # print(f" average sensing value {average_average_sensing_value / 20}") 776 | 777 | # print("max sensing value", average_accumulated_sensing_value / 3) 778 | # print(f"Num of movements {average_number_of_movement/3}") 779 | # print(f"Max number of covered cells {average_number_of_covered_cells/3}") 780 | # print(f" time {average_max_time / 3}") 781 | # print(f" average sensing value {average_average_sensing_value / 3}") 782 | 783 | # fig4 = plt.figure(30) 784 | # ax = plt.axes() 785 | # plt.plot(time_list, value_list) 786 | # ax.set(xlabel = 'Time t in minutes',ylabel = 'VSI') 787 | # ax.set_ylim(ymin = 0) 788 | # ax.yaxis.set_major_locator(MultipleLocator(base=0.2)) 789 | # print("time list ", time_list) 790 | # print("value list", value_list) 791 | # for jk in v_value: 792 | # print(jk) 793 | # 794 | # plt.show() -------------------------------------------------------------------------------- /RandomMethod.py: -------------------------------------------------------------------------------- 1 | import random 2 | import math 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | from matplotlib.ticker import MultipleLocator 6 | import time 7 | import copy 8 | from sklearn.datasets._samples_generator import make_blobs 9 | from itertools import product, combinations 10 | import scipy.stats as st 11 | import SDPC 12 | 13 | X_sensor, y_true_sensor = make_blobs(n_samples=20000,centers=[[11, 11], [20, 20], [15, 38], [35, 15], [38, 35]], 14 | cluster_std=[4, 4, 5.1, 5, 5], random_state=0) 15 | x_sensors = X_sensor[:, 0] 16 | y_sensors = X_sensor[:, 1] 17 | xx_sensors, yy_sensors = np.mgrid[0:50:50j, 0:50:50j] 18 | positions_sensors = np.vstack([xx_sensors.ravel(), yy_sensors.ravel()]) 19 | values_sensor = np.vstack([x_sensors, y_sensors]) 20 | kernel_sensor = st.gaussian_kde(values_sensor) 21 | f_sensor = np.reshape(kernel_sensor(positions_sensors).T, xx_sensors.shape) 22 | zz_sensor = f_sensor * 22305.1 # 20 000 sensors 23 | vector_sensor = np.vectorize(np.int32) 24 | zz_sensor = vector_sensor(zz_sensor) 25 | 26 | n_components = 3 27 | X_surface, truth_surface = make_blobs(n_samples=300, centers=[[18, 18], [15, 35], [35, 30]], cluster_std=[6, 3, 4.5], 28 | random_state=42) 29 | x_surface = X_surface[:, 0] 30 | y_surface = X_surface[:, 1] 31 | xx_surface, yy_surface = np.mgrid[-0.5:49.5:101j, -0.5:49.5:101j] 32 | positions_surface = np.vstack([xx_surface.ravel(), yy_surface.ravel()]) 33 | values_surface = np.vstack([x_surface, y_surface]) 34 | kernel_surface = st.gaussian_kde(values_surface) 35 | f_surface = np.reshape(kernel_surface(positions_surface).T, xx_surface.shape) 36 | zz_surface = f_surface * 5000 37 | z_fin_surface = [] 38 | a_surface = 1 39 | while a_surface <= 99: 40 | z_surface = [] 41 | for i in range(1, 100, 2): 42 | z_surface.append(zz_surface[a_surface][i]) 43 | z_fin_surface.append(z_surface) 44 | a_surface += 2 45 | z_axis_surface = np.array(z_fin_surface) 46 | 47 | people_number = zz_sensor 48 | z_mountain = z_axis_surface 49 | NumUAV = 5 50 | theta = math.pi / 3 51 | noise = -80 52 | moving_speed = 10 53 | ValueMax = 2.0 54 | ValueMin = 0.2 55 | sensor_num_Max = 10 56 | sensor_num_Min = 2 57 | d_u_s_max = 265 # 1025 58 | d_u_u_max = 1327 # change to 1327 or 839 59 | w1, w2, w3 = 0.3, 0.35, 0.35 60 | R = 3.5 # 120 secs to 2 mins 61 | NOR_S = 124 62 | NOR_T = 43.9 63 | NOR_E = 75 64 | em = 2 65 | es = 1 66 | ec = 0.03 67 | a_coef = (ValueMax - ValueMin) / ((math.e ** R) - 1) 68 | b_coef = ValueMin - a_coef 69 | 70 | GBS_position = [0, 0, 0] 71 | global_average_f = 0 72 | global_average_ft = 0 73 | global_average_fs = 0 74 | global_average_fe = 0 75 | global_time = 0 76 | 77 | prohibited_zone = [] 78 | for a in range(40, 43): 79 | for b in range(40, 43): 80 | for c in range(10, 13): 81 | prohibited_zone.append([a, b, c]) 82 | 83 | 84 | def distance(x1, y1, z1, x2, y2, z2): 85 | dist = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2 + (z1 - z2) ** 2) * 40 86 | return dist 87 | 88 | 89 | def check_coverage(d_u_s_max, theta, x, y, z): 90 | covered_cells_1 = [] 91 | covered_cells_2 = [] 92 | covered_cells_3 = [] 93 | covered_cells_final = [] 94 | r_max = d_u_s_max * math.sin(theta / 2) 95 | for x_cell in range(0, 50): 96 | for y_cell in range(0, 50): 97 | r_k_i = math.sqrt((x - x_cell) ** 2 + (y - y_cell) ** 2) * 40 98 | if r_max >= r_k_i: 99 | covered_cells_1.append([x_cell, y_cell]) 100 | for cell in covered_cells_1: 101 | d_k_i = distance(x, y, z, cell[0], cell[1], z_mountain[cell[0]][cell[1]]) 102 | if (d_u_s_max >= d_k_i) and (z_mountain[cell[0]][cell[1]] < z): 103 | covered_cells_2.append([cell[0], cell[1]]) 104 | for cell in covered_cells_2: 105 | d_k_i = distance(x, y, z, cell[0], cell[1], z_mountain[cell[0]][cell[1]]) 106 | d = ((z - z_mountain[cell[0]][cell[1]]) * 40) / math.cos(theta / 2) 107 | if d_k_i <= d: 108 | covered_cells_3.append([cell[0], cell[1]]) 109 | for cell in covered_cells_3: 110 | cell_j = SDPC.coordinate_and_compare_height(x, y, cell[0], cell[1]) 111 | for cell_j_axis in cell_j: 112 | z_j_LOS_k_i = (z - z_mountain[cell[0]][cell[1]]) / ( 113 | math.sqrt((x - cell[0]) ** 2 + (y - cell[1]) ** 2)) * math.sqrt( 114 | (cell_j_axis[0] - cell[0]) ** 2 + (cell_j_axis[1] - cell[1]) ** 2) + z_mountain[cell[0]][cell[1]] 115 | if z_mountain[cell_j_axis[0]][cell_j_axis[1]] < z_j_LOS_k_i: 116 | if [cell[0], cell[1]] not in covered_cells_final: 117 | covered_cells_final.append([cell[0], cell[1]]) 118 | return covered_cells_final 119 | 120 | 121 | def SNR(x_uav, y_uav, z_uav, x_c_i, y_c_i): 122 | d_k_i = distance(x_uav, y_uav, z_uav, x_c_i, y_c_i, z_mountain[x_c_i][y_c_i]) 123 | cal_snr = (10 * math.log10(10 / (9 * 16 * (math.pi ** 2) * (d_k_i ** 2)))) - noise 124 | return cal_snr 125 | 126 | 127 | def sensing_function(): 128 | for x_value in range(50): 129 | for y_value in range(50): 130 | if abs(time_of_each_UAV[current_uav] - prev_time[x_value][y_value]) <= R: 131 | v_value[x_value][y_value] = a_coef * math.exp( 132 | time_of_each_UAV[current_uav] - prev_time[x_value][y_value]) + b_coef 133 | else: 134 | v_value[x_value][y_value] = ValueMax 135 | value_list.append(v_value[vsi_x][vsi_y]) 136 | time_list.append(time_of_each_UAV[current_uav]) 137 | 138 | 139 | def number_of_sensor_function(): 140 | for i in range(50): 141 | for j in range(50): 142 | if people_number[i][j] < 0: 143 | sensor_num[i][j] = 0 144 | elif 1 <= people_number[i][j] <= sensor_num_Max: 145 | sensor_num[i][j] = (((sensor_num_Max - sensor_num_Min) * people_number[i][j]) + ( 146 | sensor_num_Max * (sensor_num_Min - 1))) / (sensor_num_Max - 1) 147 | elif people_number[i][j] > sensor_num_Max: 148 | sensor_num[i][j] = sensor_num_Max 149 | 150 | 151 | def F_S_function(Part_Position_x, Part_Position_y, Part_Position_z): 152 | all_covered_cells = [] 153 | for all_uav_current_position in UAV_current_Position: 154 | each_uav_covered_cell = check_coverage(d_u_s_max, theta, all_uav_current_position[0], 155 | all_uav_current_position[1], all_uav_current_position[2]) 156 | all_covered_cells.append(each_uav_covered_cell) 157 | PSO_particle_covered_cells = check_coverage(d_u_s_max, theta, Part_Position_x, Part_Position_y, Part_Position_z) 158 | particle_covered_cells = set() 159 | covered_cells = set() 160 | for i in PSO_particle_covered_cells: 161 | particle_covered_cells.add((i[0], i[1])) 162 | for uav_covered_cells in all_covered_cells: 163 | for j in uav_covered_cells: 164 | covered_cells.add((j[0], j[1])) 165 | not_included_in_other_uav = particle_covered_cells - covered_cells 166 | sum_F_S = 0 167 | for x_y_not_included_in_other_uav in not_included_in_other_uav: 168 | sum_F_S += v_value[x_y_not_included_in_other_uav[0]][x_y_not_included_in_other_uav[1]] * \ 169 | sensor_num[x_y_not_included_in_other_uav[0]][x_y_not_included_in_other_uav[1]] 170 | F_S = sum_F_S / NOR_S 171 | return F_S 172 | 173 | 174 | def F_T_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z): 175 | d_k_m = distance(UAV_x, UAV_y, UAV_z, Part_Position_x, Part_Position_y, Part_Position_z) 176 | T_moving = d_k_m / moving_speed 177 | F_T_cell = check_coverage(d_u_s_max, theta, Part_Position_x, Part_Position_y, Part_Position_z) 178 | T_stay = 0 179 | for cell in F_T_cell: 180 | snr1 = SNR(x_uav=UAV_x, y_uav=UAV_y, z_uav=UAV_z, x_c_i=cell[0], y_c_i=cell[1]) 181 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr1 - 0.6)) 182 | T_stay += people_number[cell[0]][cell[1]] * 0.02 * (1 / (1 - p_g_c_i)) 183 | F_T = (T_moving + T_stay) / NOR_T 184 | return F_T 185 | 186 | 187 | def F_E_function(Part_Position_x, Part_Position_y, Part_Position_z, UAV_x, UAV_y, UAV_z): 188 | d_k_m = distance(UAV_x, UAV_y, UAV_z, Part_Position_x, Part_Position_y, Part_Position_z) 189 | E_k_M = d_k_m / moving_speed * em 190 | E_k_S_cell = check_coverage(d_u_s_max, theta, Part_Position_x, Part_Position_y, Part_Position_z) 191 | E_k_S_cell_sum = 0 192 | E_k_C_sum = 0 193 | for cell in E_k_S_cell: 194 | snr1 = SNR(x_uav=UAV_x, y_uav=UAV_y, z_uav=UAV_z, x_c_i=cell[0], y_c_i=cell[1]) 195 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr1 - 0.6)) 196 | E_k_S_cell_sum += people_number[cell[0]][cell[1]] * 0.02 * (1 / (1 - p_g_c_i)) 197 | E_k_C_sum += people_number[cell[0]][cell[1]] * (1 / (1 - p_g_c_i)) * ec 198 | E_k_S = E_k_S_cell_sum * es 199 | F_E = (E_k_M + E_k_S + E_k_C_sum) / NOR_E 200 | return F_E 201 | 202 | 203 | def fitness_function(x, y, z, UAV_x, UAV_y, UAV_z): 204 | F_S = F_S_function(x, y, z) 205 | F_T = F_T_function(x, y, z, UAV_x, UAV_y, UAV_z) 206 | F_E = F_E_function(x, y, z, UAV_x, UAV_y, UAV_z) 207 | F = w1 * F_S - w2 * F_T - w3 * F_E 208 | return F 209 | 210 | 211 | def check_sdpc_function(cell_j, x1, y1, z1, x2, y2, z2): 212 | check_sdpc = [] 213 | for cell_j_axis in cell_j: 214 | z_j_LOS_uav_gbs = (z1 - z2) / (math.sqrt( 215 | (x1 - x2) ** 2 + (y1 - y2) ** 2)) * math.sqrt((cell_j_axis[0] - x2) ** 2 + (cell_j_axis[1] - y2) ** 2) + z2 216 | if z_mountain[cell_j_axis[0]][cell_j_axis[1]] < z_j_LOS_uav_gbs: 217 | check_sdpc.append(1) 218 | else: 219 | check_sdpc.append(0) 220 | return check_sdpc 221 | 222 | 223 | simulation_time = 1 224 | while simulation_time <2: 225 | print("simulation # ", simulation_time) 226 | start_time = time.time() 227 | v_value = np.ones((50, 50), float) 228 | v_value.fill(2.0) 229 | prev_time = np.zeros((50, 50), float) 230 | prev_time.fill(100) 231 | sensor_num = np.zeros((50, 50), int) 232 | number_of_sensor_function() 233 | UAV_current_Position = [[0, 0, 0] for _ in range(NumUAV)] 234 | time_of_each_UAV = [0, 0, 0, 0, 0] 235 | vsi_x = 0 236 | vsi_y = 0 237 | time_list = [0] 238 | value_list = [2] 239 | average_f_value = 0 240 | average_s_value = 0 241 | average_t_value = 0 242 | average_e_value = 0 243 | current_time_list= [] 244 | current_time_fitness_value= [] 245 | 246 | for current_uav in range(NumUAV): 247 | next_uav_position = [] 248 | init_value_while_to_false = 0 249 | while init_value_while_to_false < 2: 250 | new_position_list = [] 251 | new_position_list.append(random.randint(0, 49)) 252 | new_position_list.append(random.randint(0, 49)) 253 | new_position_list.append(random.uniform(0, 23.9)) 254 | if (new_position_list not in prohibited_zone) and (new_position_list not in UAV_current_Position) and ( 255 | new_position_list[2] > z_mountain[new_position_list[0]][new_position_list[1]]): 256 | if current_uav == 0: 257 | if (distance(new_position_list[0], new_position_list[1], new_position_list[2], GBS_position[0], 258 | GBS_position[1], GBS_position[2]) <= d_u_u_max) and (all(check_sdpc_function( 259 | SDPC.coordinate_and_compare_height(new_position_list[0], new_position_list[1], 260 | GBS_position[0], GBS_position[1]), 261 | new_position_list[0], new_position_list[1], new_position_list[2], GBS_position[0], 262 | GBS_position[1], GBS_position[2])) == 1): 263 | next_uav_position = new_position_list 264 | break 265 | else: 266 | continue 267 | else: 268 | if (any(((distance(new_position_list[0], new_position_list[1], new_position_list[2], 269 | UAV_current_Position[num_gbs_uav][0], UAV_current_Position[num_gbs_uav][1], 270 | UAV_current_Position[num_gbs_uav][2])) <= 839) and (all(check_sdpc_function( 271 | SDPC.coordinate_and_compare_height(new_position_list[0], 272 | new_position_list[1], 273 | UAV_current_Position[num_gbs_uav][0], 274 | UAV_current_Position[num_gbs_uav][1]), 275 | new_position_list[0], new_position_list[1], new_position_list[2], 276 | UAV_current_Position[num_gbs_uav][0], 277 | UAV_current_Position[num_gbs_uav][1], 278 | UAV_current_Position[num_gbs_uav][2])) == 1) for num_gbs_uav in range(0, current_uav))) or ( 279 | distance(new_position_list[0], new_position_list[1], new_position_list[2], 280 | GBS_position[0], GBS_position[1], GBS_position[2]) <= d_u_u_max) and ( 281 | all(check_sdpc_function( 282 | SDPC.coordinate_and_compare_height(new_position_list[0], new_position_list[1], 283 | GBS_position[0], GBS_position[1]), 284 | new_position_list[0], new_position_list[1], new_position_list[2], 285 | GBS_position[0], GBS_position[1], 286 | GBS_position[2])) == 1): 287 | next_uav_position = new_position_list 288 | break 289 | else: 290 | continue 291 | 292 | # GBS_UAV_connection = [] 293 | # for check_connection in range(5): 294 | # if check_connection != current_uav: 295 | # if distance(UAV_current_Position[check_connection][0], 296 | # UAV_current_Position[check_connection][1], 297 | # UAV_current_Position[check_connection][2], GBS_position[0], GBS_position[1], 298 | # GBS_position[2]) <= d_u_u_max: 299 | # cell_j = SDPC.coordinate_and_compare_height( 300 | # UAV_current_Position[check_connection][0], 301 | # UAV_current_Position[check_connection][1], GBS_position[0], GBS_position[1]) 302 | # sdpc_list_check = check_sdpc_function(cell_j, 303 | # UAV_current_Position[check_connection][0], 304 | # UAV_current_Position[check_connection][1], 305 | # UAV_current_Position[check_connection][2], 306 | # GBS_position[0], GBS_position[1], 307 | # GBS_position[2]) 308 | # if all(sdpc_list_check) == 1: 309 | # GBS_UAV_connection.append(UAV_current_Position[check_connection]) 310 | # if len(GBS_UAV_connection) >= 1: 311 | # for con_num in GBS_UAV_connection: 312 | # if distance(new_position_list[0], new_position_list[1], new_position_list[2], 313 | # con_num[0], con_num[1], con_num[2]) <= d_u_u_max: 314 | # cell_j = SDPC.coordinate_and_compare_height(new_position_list[0], new_position_list[1], 315 | # con_num[0], 316 | # con_num[1]) 317 | # sdpc_list_check = check_sdpc_function(cell_j, new_position_list[0], new_position_list[1], 318 | # new_position_list[2], 319 | # con_num[0], con_num[1], con_num[2]) 320 | # if all(sdpc_list_check) == 1: 321 | # next_uav_position = new_position_list 322 | # init_value_while_to_false = 5 323 | # break 324 | # elif distance(new_position_list[0], new_position_list[1], new_position_list[2], GBS_position[0], 325 | # GBS_position[1], GBS_position[2]) <= d_u_u_max: 326 | # cell_j = SDPC.coordinate_and_compare_height(new_position_list[0], new_position_list[1], 327 | # GBS_position[0], GBS_position[1]) 328 | # sdpc_list_check = check_sdpc_function(cell_j, new_position_list[0], new_position_list[1], 329 | # new_position_list[2], 330 | # GBS_position[0], GBS_position[1], GBS_position[2]) 331 | # if all(sdpc_list_check) == 1: 332 | # next_uav_position = new_position_list 333 | # break 334 | # else: 335 | # continue 336 | # else: 337 | # continue 338 | moving_time = ((math.sqrt((UAV_current_Position[current_uav][0] - next_uav_position[0]) ** 2 + ( 339 | UAV_current_Position[current_uav][1] - next_uav_position[1]) ** 2 + ( 340 | UAV_current_Position[current_uav][2] - next_uav_position[ 341 | 2]) ** 2)) * 40 / moving_speed) / 60 342 | check_coverage_next = check_coverage(d_u_s_max, theta, next_uav_position[0], next_uav_position[1], 343 | next_uav_position[2]) 344 | if current_uav == 0: 345 | if (len(check_coverage_next) > 1) and (check_coverage_next[0] != [0, 0]): 346 | x_y_vsi = check_coverage_next[0] 347 | vsi_x = x_y_vsi[0] 348 | vsi_y = x_y_vsi[1] 349 | next_position_stay_time_sum = 0 350 | for cov_next in check_coverage_next: 351 | snr = SNR(next_uav_position[0], next_uav_position[1], next_uav_position[2], cov_next[0], cov_next[1]) 352 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr - 0.6)) 353 | next_stay_time_c_i = people_number[cov_next[0]][cov_next[1]] * 0.02 * 1 / (1 - p_g_c_i) 354 | next_position_stay_time_sum += next_stay_time_c_i 355 | next_position_stay_time = next_position_stay_time_sum / 60 356 | F_S = F_S_function(next_uav_position[0], next_uav_position[1], next_uav_position[2]) 357 | F_T = F_T_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 358 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 359 | UAV_current_Position[current_uav][2]) 360 | F_E = F_E_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 361 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 362 | UAV_current_Position[current_uav][2]) 363 | F = fitness_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 364 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 365 | UAV_current_Position[current_uav][2]) 366 | 367 | time_of_each_UAV[current_uav] = time_of_each_UAV[current_uav] + moving_time + next_position_stay_time 368 | UAV_current_Position[current_uav] = next_uav_position 369 | # print(f"F_S, F_T, F_E of the next optimal position: {F_S, F_T, F_E}") 370 | # print(f"Total fitness F of the next optimal position: {F}") 371 | 372 | movement = 1 373 | while movement < 51: 374 | # print(f"Movement number: {movement}") 375 | # print(f"UAV_current_Position: {UAV_current_Position}") 376 | # if movement > 0: 377 | # fig, ax = plt.subplots(nrows=1, ncols=1) 378 | # ax.set_xlim(xmin=0, xmax=50) 379 | # ax.set_ylim(ymin=0, ymax=50) 380 | # ax.xaxis.set_major_locator(MultipleLocator(base=5)) 381 | # ax.yaxis.set_major_locator(MultipleLocator(base=5)) 382 | # plt.contour(xx_surface, yy_surface, zz_surface, 20, cmap='RdGy') 383 | # plt.scatter(x_sensors, y_sensors, s=7, color='#495057', zorder=3) 384 | # for uav_connection in range(5): 385 | # if uav_connection == 0: 386 | # color = 'blue' 387 | # elif uav_connection == 1: 388 | # color = 'green' 389 | # elif uav_connection == 2: 390 | # color = 'pink' 391 | # elif uav_connection == 3: 392 | # color = 'purple' 393 | # elif uav_connection == 4: 394 | # color = 'yellow' 395 | # plt.scatter(UAV_current_Position[uav_connection][0], UAV_current_Position[uav_connection][1], s=100, 396 | # zorder=10, color=color) 397 | # for con_num in range(5): 398 | # if con_num != uav_connection: 399 | # if distance(UAV_current_Position[uav_connection][0], UAV_current_Position[uav_connection][1], 400 | # UAV_current_Position[uav_connection][2], 401 | # UAV_current_Position[con_num][0], UAV_current_Position[con_num][1], 402 | # UAV_current_Position[con_num][2]) <= d_u_u_max: 403 | # cell_j = SDPC.coordinate_and_compare_height(UAV_current_Position[uav_connection][0], 404 | # UAV_current_Position[uav_connection][1], 405 | # UAV_current_Position[con_num][0], 406 | # UAV_current_Position[con_num][1]) 407 | # sdpc_list_check = check_sdpc_function(cell_j, UAV_current_Position[uav_connection][0], 408 | # UAV_current_Position[uav_connection][1], 409 | # UAV_current_Position[uav_connection][2], 410 | # UAV_current_Position[con_num][0], 411 | # UAV_current_Position[con_num][1], 412 | # UAV_current_Position[con_num][2]) 413 | # if all(sdpc_list_check) == 1: 414 | # plt.plot([UAV_current_Position[uav_connection][0], UAV_current_Position[con_num][0]], 415 | # [UAV_current_Position[uav_connection][1], UAV_current_Position[con_num][1]], 416 | # color='blue', zorder=8, marker='_', markersize=12) 417 | # if distance(UAV_current_Position[uav_connection][0], UAV_current_Position[uav_connection][1], 418 | # UAV_current_Position[uav_connection][2], 419 | # GBS_position[0], GBS_position[1], GBS_position[2]) <= d_u_u_max: 420 | # cell_j = SDPC.coordinate_and_compare_height(UAV_current_Position[uav_connection][0], 421 | # UAV_current_Position[uav_connection][1], 422 | # GBS_position[0], GBS_position[1]) 423 | # sdpc_list_check = check_sdpc_function(cell_j, UAV_current_Position[uav_connection][0], 424 | # UAV_current_Position[uav_connection][1], 425 | # UAV_current_Position[uav_connection][2], 426 | # GBS_position[0], GBS_position[1], GBS_position[2]) 427 | # if all(sdpc_list_check) == 1: 428 | # plt.plot([UAV_current_Position[uav_connection][0], 0], 429 | # [UAV_current_Position[uav_connection][1], 0], 430 | # color='blue', zorder=8, marker='_', markersize=12) 431 | # 432 | 433 | min_time = min(every_time for every_time in time_of_each_UAV) 434 | current_uav = time_of_each_UAV.index(min_time) 435 | check_coverage_current_uav = check_coverage(d_u_s_max, theta, UAV_current_Position[current_uav][0], 436 | UAV_current_Position[current_uav][1], 437 | UAV_current_Position[current_uav][2]) 438 | sensing_function() 439 | for cov in check_coverage_current_uav: 440 | prev_time[cov[0]][cov[1]] = time_of_each_UAV[current_uav] 441 | sensing_function() 442 | 443 | 444 | next_uav_position = [] 445 | init_value_while_to_false = 0 446 | while init_value_while_to_false < 2: 447 | new_position_list = [] 448 | new_position_list.append(random.randint(0, 49)) 449 | new_position_list.append(random.randint(0, 49)) 450 | new_position_list.append(random.uniform(0, 23.9)) 451 | if (new_position_list not in prohibited_zone) and (new_position_list not in UAV_current_Position) and ( 452 | new_position_list[2] > z_mountain[new_position_list[0]][new_position_list[1]]): 453 | GBS_UAV_connection = [] 454 | for num_gbs_uav in range(5): 455 | if num_gbs_uav != current_uav: 456 | if (distance(UAV_current_Position[num_gbs_uav][0], 457 | UAV_current_Position[num_gbs_uav][1], 458 | UAV_current_Position[num_gbs_uav][2], GBS_position[0], GBS_position[1], 459 | GBS_position[2]) <= d_u_u_max) and (all(check_sdpc_function( 460 | SDPC.coordinate_and_compare_height(UAV_current_Position[num_gbs_uav][0], 461 | UAV_current_Position[num_gbs_uav][1], 462 | GBS_position[0], GBS_position[1]), 463 | UAV_current_Position[num_gbs_uav][0], 464 | UAV_current_Position[num_gbs_uav][1], 465 | UAV_current_Position[num_gbs_uav][2], 466 | GBS_position[0], GBS_position[1], GBS_position[2])) == 1): 467 | GBS_UAV_connection.append(num_gbs_uav) 468 | if len(GBS_UAV_connection) >= 1: 469 | exitFlag = False 470 | for connect_to_pso in GBS_UAV_connection: 471 | if (distance(UAV_current_Position[connect_to_pso][0], 472 | UAV_current_Position[connect_to_pso][1], 473 | UAV_current_Position[connect_to_pso][2], new_position_list[0], 474 | new_position_list[1], new_position_list[2]) <= d_u_u_max) and ( 475 | all(check_sdpc_function( 476 | SDPC.coordinate_and_compare_height(UAV_current_Position[connect_to_pso][0], 477 | UAV_current_Position[connect_to_pso][1], 478 | new_position_list[0], 479 | new_position_list[1]), 480 | UAV_current_Position[connect_to_pso][0], 481 | UAV_current_Position[connect_to_pso][1], 482 | UAV_current_Position[connect_to_pso][2], 483 | new_position_list[0], new_position_list[1], 484 | new_position_list[2])) == 1): 485 | 486 | next_uav_position = new_position_list 487 | init_value_while_to_false = 5 488 | break 489 | else: 490 | exitFlag2 = False 491 | for connect_to_pso2 in range(5): 492 | if (connect_to_pso2 != connect_to_pso) and (connect_to_pso2 != current_uav): 493 | if (distance(UAV_current_Position[connect_to_pso][0], 494 | UAV_current_Position[connect_to_pso][1], 495 | UAV_current_Position[connect_to_pso][2], 496 | UAV_current_Position[connect_to_pso2][0], 497 | UAV_current_Position[connect_to_pso2][1], 498 | UAV_current_Position[connect_to_pso2][2]) <= d_u_u_max) and ( 499 | all(check_sdpc_function( 500 | SDPC.coordinate_and_compare_height( 501 | UAV_current_Position[connect_to_pso][0], 502 | UAV_current_Position[connect_to_pso][1], 503 | UAV_current_Position[connect_to_pso2][0], 504 | UAV_current_Position[connect_to_pso2][ 505 | 1]), 506 | UAV_current_Position[connect_to_pso][0], 507 | UAV_current_Position[connect_to_pso][1], 508 | UAV_current_Position[connect_to_pso][2], 509 | UAV_current_Position[connect_to_pso2][0], 510 | UAV_current_Position[connect_to_pso2][1], 511 | UAV_current_Position[connect_to_pso2][2])) == 1): 512 | 513 | if (distance(UAV_current_Position[connect_to_pso2][0], 514 | UAV_current_Position[connect_to_pso2][1], 515 | UAV_current_Position[connect_to_pso2][2], 516 | new_position_list[0], 517 | new_position_list[1], 518 | new_position_list[2]) <= d_u_u_max) and ( 519 | all(check_sdpc_function( 520 | SDPC.coordinate_and_compare_height( 521 | UAV_current_Position[connect_to_pso2][0], 522 | UAV_current_Position[connect_to_pso2][1], 523 | new_position_list[0], new_position_list[1]), 524 | UAV_current_Position[connect_to_pso2][0], 525 | UAV_current_Position[connect_to_pso2][1], 526 | UAV_current_Position[connect_to_pso2][2], 527 | new_position_list[0], new_position_list[1], 528 | new_position_list[2])) == 1): 529 | 530 | next_uav_position = new_position_list 531 | init_value_while_to_false = 5 532 | exitFlag = True 533 | break 534 | else: 535 | exitFlag3 = False 536 | for connect_to_pso3 in range(5): 537 | if (connect_to_pso3 != current_uav) and ( 538 | connect_to_pso3 != connect_to_pso2) and ( 539 | connect_to_pso3 != connect_to_pso): 540 | if (distance(UAV_current_Position[connect_to_pso3][0], 541 | UAV_current_Position[connect_to_pso3][1], 542 | UAV_current_Position[connect_to_pso3][2], 543 | UAV_current_Position[connect_to_pso2][0], 544 | UAV_current_Position[connect_to_pso2][1], 545 | UAV_current_Position[connect_to_pso2][ 546 | 2]) <= d_u_u_max) and ( 547 | all(check_sdpc_function( 548 | SDPC.coordinate_and_compare_height( 549 | UAV_current_Position[connect_to_pso3][0], 550 | UAV_current_Position[connect_to_pso3][1], 551 | UAV_current_Position[connect_to_pso2][0], 552 | UAV_current_Position[connect_to_pso2][ 553 | 1]), 554 | UAV_current_Position[connect_to_pso3][0], 555 | UAV_current_Position[connect_to_pso3][1], 556 | UAV_current_Position[connect_to_pso3][2], 557 | UAV_current_Position[connect_to_pso2][0], 558 | UAV_current_Position[connect_to_pso2][1], 559 | UAV_current_Position[connect_to_pso2][2])) == 1): 560 | if (distance(UAV_current_Position[connect_to_pso3][0], 561 | UAV_current_Position[connect_to_pso3][1], 562 | UAV_current_Position[connect_to_pso3][2], 563 | new_position_list[0], 564 | new_position_list[1], 565 | new_position_list[2]) <= d_u_u_max) and ( 566 | all(check_sdpc_function( 567 | SDPC.coordinate_and_compare_height( 568 | UAV_current_Position[connect_to_pso3][0], 569 | UAV_current_Position[connect_to_pso3][1], 570 | new_position_list[0], 571 | new_position_list[1]), 572 | UAV_current_Position[connect_to_pso3][0], 573 | UAV_current_Position[connect_to_pso3][1], 574 | UAV_current_Position[connect_to_pso3][2], 575 | new_position_list[0], 576 | new_position_list[1], 577 | new_position_list[2])) == 1): 578 | next_uav_position = new_position_list 579 | init_value_while_to_false = 5 580 | exitFlag = True 581 | exitFlag2 = True 582 | break 583 | else: 584 | for connect_to_pso4 in range(5): 585 | if (connect_to_pso4 != current_uav) and ( 586 | connect_to_pso4 != connect_to_pso2) and ( 587 | connect_to_pso4 != connect_to_pso) and ( 588 | connect_to_pso4 != connect_to_pso3): 589 | if (distance( 590 | UAV_current_Position[connect_to_pso3][ 591 | 0], 592 | UAV_current_Position[connect_to_pso3][ 593 | 1], 594 | UAV_current_Position[connect_to_pso3][ 595 | 2], 596 | UAV_current_Position[connect_to_pso2][ 597 | 0], 598 | UAV_current_Position[connect_to_pso2][ 599 | 1], 600 | UAV_current_Position[connect_to_pso2][ 601 | 2]) <= d_u_u_max) and ( 602 | all(check_sdpc_function( 603 | SDPC.coordinate_and_compare_height( 604 | UAV_current_Position[ 605 | connect_to_pso3][ 606 | 0], 607 | UAV_current_Position[ 608 | connect_to_pso3][ 609 | 1], 610 | UAV_current_Position[ 611 | connect_to_pso4][ 612 | 0], 613 | UAV_current_Position[ 614 | connect_to_pso4][ 615 | 1]), 616 | UAV_current_Position[ 617 | connect_to_pso3][ 618 | 0], 619 | UAV_current_Position[ 620 | connect_to_pso3][ 621 | 1], 622 | UAV_current_Position[ 623 | connect_to_pso3][ 624 | 2], 625 | UAV_current_Position[ 626 | connect_to_pso4][ 627 | 0], 628 | UAV_current_Position[ 629 | connect_to_pso4][ 630 | 1], 631 | UAV_current_Position[ 632 | connect_to_pso4][ 633 | 2])) == 1): 634 | if (distance( 635 | UAV_current_Position[ 636 | connect_to_pso4][ 637 | 0], 638 | UAV_current_Position[ 639 | connect_to_pso4][ 640 | 1], 641 | UAV_current_Position[ 642 | connect_to_pso4][ 643 | 2], 644 | new_position_list[0], 645 | new_position_list[1], 646 | new_position_list[ 647 | 2]) <= d_u_u_max) and ( 648 | all(check_sdpc_function( 649 | SDPC.coordinate_and_compare_height( 650 | UAV_current_Position[ 651 | connect_to_pso4][0], 652 | UAV_current_Position[ 653 | connect_to_pso4][1], 654 | new_position_list[0], 655 | new_position_list[1]), 656 | UAV_current_Position[ 657 | connect_to_pso4][ 658 | 0], 659 | UAV_current_Position[ 660 | connect_to_pso4][ 661 | 1], 662 | UAV_current_Position[ 663 | connect_to_pso4][ 664 | 2], 665 | new_position_list[0], 666 | new_position_list[1], 667 | new_position_list[2])) == 1): 668 | next_uav_position = new_position_list 669 | init_value_while_to_false = 5 670 | exitFlag = True 671 | exitFlag2 = True 672 | exitFlag3 = True 673 | break 674 | if (exitFlag3): 675 | break 676 | if (exitFlag2): 677 | break 678 | if (exitFlag): 679 | break 680 | else: 681 | if (distance(new_position_list[0], 682 | new_position_list[1], 683 | new_position_list[2], GBS_position[0], GBS_position[1], 684 | GBS_position[2]) <= d_u_u_max) and (all(check_sdpc_function( 685 | SDPC.coordinate_and_compare_height(new_position_list[0], 686 | new_position_list[1], 687 | GBS_position[0], GBS_position[1]), 688 | new_position_list[0],new_position_list[1], new_position_list[2], GBS_position[0], GBS_position[1], 689 | GBS_position[2])) == 1): 690 | old_uav_to_other_uav_con = [] 691 | for second_check in range(5): 692 | if second_check != current_uav: 693 | if (distance(UAV_current_Position[second_check][0], 694 | UAV_current_Position[second_check][1], 695 | UAV_current_Position[second_check][2], 696 | UAV_current_Position[current_uav][0], 697 | UAV_current_Position[current_uav][1], 698 | UAV_current_Position[current_uav][2]) <= d_u_u_max) and ( 699 | all(check_sdpc_function( 700 | SDPC.coordinate_and_compare_height( 701 | UAV_current_Position[second_check][0], 702 | UAV_current_Position[second_check][1], 703 | UAV_current_Position[current_uav][0], 704 | UAV_current_Position[current_uav][1]), 705 | UAV_current_Position[second_check][0], 706 | UAV_current_Position[second_check][1], 707 | UAV_current_Position[second_check][2], 708 | UAV_current_Position[current_uav][0], 709 | UAV_current_Position[current_uav][1], 710 | UAV_current_Position[current_uav][2])) == 1): 711 | old_uav_to_other_uav_con.append(second_check) 712 | 713 | # 714 | # only_to_current_uav_connect = [] 715 | # for con_elem in old_uav_to_other_uav_con: 716 | # for uav_num_without_some in range(5): 717 | # if uav_num_without_some != current_uav and uav_num_without_some != con_elem 718 | 719 | # for con_elem in old_uav_to_other_uav_con: 720 | if (all((distance(UAV_current_Position[con_elem][0], 721 | UAV_current_Position[con_elem][1], 722 | UAV_current_Position[con_elem][2], new_position_list[0], 723 | new_position_list[1],new_position_list[2])) <= d_u_u_max for con_elem in old_uav_to_other_uav_con)) and (all 724 | (all(check_sdpc_function( 725 | SDPC.coordinate_and_compare_height( 726 | UAV_current_Position[con_elem][0], 727 | UAV_current_Position[con_elem][1], 728 | new_position_list[0], new_position_list[1]), 729 | UAV_current_Position[con_elem][0], 730 | UAV_current_Position[con_elem][1], 731 | UAV_current_Position[con_elem][2], 732 | new_position_list[0], new_position_list[1], 733 | new_position_list[2])) == 1 for con_elem in old_uav_to_other_uav_con)): 734 | next_uav_position = new_position_list 735 | init_value_while_to_false = 5 736 | break 737 | 738 | 739 | 740 | # GBS_UAV_connection = [] 741 | # for check_connection in range(5): 742 | # if check_connection != current_uav: 743 | # if distance(UAV_current_Position[check_connection][0], 744 | # UAV_current_Position[check_connection][1], 745 | # UAV_current_Position[check_connection][2], GBS_position[0], GBS_position[1], 746 | # GBS_position[2]) <= d_u_u_max: 747 | # cell_j = SDPC.coordinate_and_compare_height( 748 | # UAV_current_Position[check_connection][0], 749 | # UAV_current_Position[check_connection][1], GBS_position[0], GBS_position[1]) 750 | # sdpc_list_check = check_sdpc_function(cell_j, 751 | # UAV_current_Position[check_connection][0], 752 | # UAV_current_Position[check_connection][1], 753 | # UAV_current_Position[check_connection][2], 754 | # GBS_position[0], GBS_position[1], 755 | # GBS_position[2]) 756 | # if all(sdpc_list_check) == 1: 757 | # GBS_UAV_connection.append(UAV_current_Position[check_connection]) 758 | # if len(GBS_UAV_connection) >= 1: 759 | # for con_num in GBS_UAV_connection: 760 | # if distance(new_position_list[0], new_position_list[1], new_position_list[2], 761 | # con_num[0], con_num[1], con_num[2]) <= d_u_u_max: 762 | # cell_j = SDPC.coordinate_and_compare_height(new_position_list[0], new_position_list[1], 763 | # con_num[0], 764 | # con_num[1]) 765 | # sdpc_list_check = check_sdpc_function(cell_j, new_position_list[0], new_position_list[1], 766 | # new_position_list[2], 767 | # con_num[0], con_num[1], con_num[2]) 768 | # if all(sdpc_list_check) == 1: 769 | # next_uav_position = new_position_list 770 | # init_value_while_to_false = 5 771 | # break 772 | # elif distance(new_position_list[0], new_position_list[1], new_position_list[2], GBS_position[0], 773 | # GBS_position[1], GBS_position[2]) <= d_u_u_max: 774 | # cell_j = SDPC.coordinate_and_compare_height(new_position_list[0], new_position_list[1], 775 | # GBS_position[0], GBS_position[1]) 776 | # sdpc_list_check = check_sdpc_function(cell_j, new_position_list[0], new_position_list[1], 777 | # new_position_list[2], 778 | # GBS_position[0], GBS_position[1], GBS_position[2]) 779 | # if all(sdpc_list_check) == 1: 780 | # next_uav_position = new_position_list 781 | # break 782 | # else: 783 | # continue 784 | # else: 785 | # continue 786 | moving_time = ((math.sqrt((UAV_current_Position[current_uav][0] - next_uav_position[0]) ** 2 + ( 787 | UAV_current_Position[current_uav][1] - next_uav_position[1]) ** 2 + ( 788 | UAV_current_Position[current_uav][2] - next_uav_position[ 789 | 2]) ** 2)) * 40 / moving_speed) / 60 790 | check_coverage_next = check_coverage(d_u_s_max, theta, next_uav_position[0], next_uav_position[1], 791 | next_uav_position[2]) 792 | 793 | next_position_stay_time_sum = 0 794 | for cov_next in check_coverage_next: 795 | snr = SNR(next_uav_position[0], next_uav_position[1], next_uav_position[2], cov_next[0], cov_next[1]) 796 | p_g_c_i = 10 ** (-0.7 * math.e ** (0.05 * snr - 0.6)) 797 | next_stay_time_c_i = people_number[cov_next[0]][cov_next[1]] * 0.02 * 1 / (1 - p_g_c_i) 798 | next_position_stay_time_sum += next_stay_time_c_i 799 | next_position_stay_time = next_position_stay_time_sum / 60 800 | F_S = F_S_function(next_uav_position[0], next_uav_position[1], next_uav_position[2]) 801 | average_s_value += F_S 802 | F_T = F_T_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 803 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 804 | UAV_current_Position[current_uav][2]) 805 | average_t_value += F_T 806 | F_E = F_E_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 807 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 808 | UAV_current_Position[current_uav][2]) 809 | average_e_value += F_E 810 | F = fitness_function(next_uav_position[0], next_uav_position[1], next_uav_position[2], 811 | UAV_current_Position[current_uav][0], UAV_current_Position[current_uav][1], 812 | UAV_current_Position[current_uav][2]) 813 | average_f_value += F 814 | time_of_each_UAV[current_uav] = time_of_each_UAV[current_uav] + moving_time + next_position_stay_time 815 | UAV_current_Position[current_uav] = next_uav_position 816 | # print(f"F_S, F_T, F_E of the next optimal position: {F_S, F_T, F_E}") 817 | # print(f"Total fitness F of the next optimal position: {F}") 818 | current_time_list.append(time_of_each_UAV[current_uav]) 819 | current_time_fitness_value.append(F) 820 | movement += 1 821 | print("final average of 50 movement times") 822 | print(f"average fitness value of 50 movement time: {average_f_value / 50}") 823 | print(f"average sensing value {average_s_value / 50}") 824 | print(f"average time value {average_t_value / 50}") 825 | print(f"average energy value {average_e_value / 50}") 826 | end_time = time.time() 827 | run_time_program = end_time - start_time 828 | print(f'Run time of the program is {run_time_program}') 829 | print(current_time_list, "time") 830 | print( current_time_fitness_value, "value") 831 | global_average_f += average_f_value / 50 832 | global_average_fs += average_s_value / 50 833 | global_average_fe += average_e_value / 50 834 | global_average_ft += average_t_value / 50 835 | global_time += run_time_program 836 | simulation_time += 1 837 | # plt.show() 838 | print(f"Average optimal fitness function after 5 simulations: {global_average_f / 5}") 839 | print(f"Average sensing fitness function after 5 simulations: {global_average_fs / 5}") 840 | print(f"Average time fitness function after 5 simulations: {global_average_ft / 5}") 841 | print(f"Average energy fitness function after 5 simulations: {global_average_fe / 5}") 842 | print(f"Average computation time: {global_time / 5}") 843 | 844 | -------------------------------------------------------------------------------- /SDPC.py: -------------------------------------------------------------------------------- 1 | x_cor_list = [] 2 | y_cor_list = [] 3 | 4 | def coordinate_and_compare_height(x1, y1, x2, y2): 5 | global x_cor_list 6 | global y_cor_list 7 | 8 | x_cor = [] 9 | y_cor = [] 10 | y_cor2 = [] 11 | x_cor2 = [] 12 | 13 | 14 | if x1 > x2 and y1 > y2: 15 | for i in range(x1, x2 - 1, -1): 16 | x_cor.append(i) 17 | for j in range(y1, y2 - 1, -1): 18 | y_cor.append(j) 19 | elif x1 > x2 and y1 < y2: 20 | for i in range(x1, x2 - 1, -1): 21 | x_cor.append(i) 22 | for j in range(y1, y2 + 1): 23 | y_cor.append(j) 24 | elif x1 < x2 and y1 > y2: 25 | for i in range(x1, x2 + 1): 26 | x_cor.append(i) 27 | for j in range(y1, y2 - 1, -1): 28 | y_cor.append(j) 29 | elif x1 < x2 and y1 < y2: 30 | for i in range(x1, x2 + 1): 31 | x_cor.append(i) 32 | for j in range(y1, y2 + 1): 33 | y_cor.append(j) 34 | elif x1 == x2 and y1 > y2: 35 | x_cor.append(x1) 36 | for j in range(y1, y2 - 1, -1): 37 | y_cor.append(j) 38 | elif x1 == x2 and y1 < y2: 39 | x_cor.append(x1) 40 | for j in range(y1, y2 + 1): 41 | y_cor.append(j) 42 | elif x1 > x2 and y1 == y2: 43 | y_cor.append(y1) 44 | for i in range(x1, x2 - 1, -1): 45 | x_cor.append(i) 46 | elif x1 < x2 and y1 == y2: 47 | y_cor.append(y1) 48 | for i in range(x1, x2 + 1): 49 | x_cor.append(i) 50 | elif x1 == x2 and y1 == y2: 51 | y_cor.append(x1) 52 | x_cor.append(y1) 53 | # 54 | # print(x_cor, "x coord", len(x_cor)) 55 | # print(y_cor, "y coord", len(y_cor)) 56 | if len(x_cor) > len(y_cor): 57 | coef = int(len(x_cor) / len(y_cor)) 58 | for y_i in y_cor: 59 | for i in range(coef): 60 | y_cor2.append(y_i) 61 | if len(y_cor2) < len(x_cor): 62 | for k in range(len(x_cor) - len(y_cor2)): 63 | y_cor2.append(y_cor[-1]) 64 | y_cor = y_cor2 65 | elif len(x_cor) < len(y_cor): 66 | coef = int(len(y_cor) / len(x_cor)) 67 | for x_i in x_cor: 68 | for i in range(coef): 69 | x_cor2.append(x_i) 70 | if len(x_cor2) < len(y_cor): 71 | for k in range(len(y_cor) - len(x_cor2)): 72 | x_cor2.append(x_cor[-1]) 73 | x_cor = x_cor2 74 | x_cor = x_cor[1: -1] 75 | y_cor = y_cor[1: -1] 76 | x_cor_list = x_cor 77 | y_cor_list = y_cor 78 | cell_axes = [] 79 | for a, b in zip(x_cor_list, y_cor_list): 80 | cell_axes.append([a, b]) 81 | return cell_axes 82 | # print(x_cor, "x coord", len(x_cor)) 83 | # print(y_cor, "y coord", len(y_cor)) 84 | # 85 | # c = coordinate_and_compare_height(38,22,37,15) 86 | # for i in c: 87 | # print(i) 88 | 89 | # print(len(x_cor_list)) 90 | # print(y_cor_list) 91 | # print(x_cor_list) 92 | # coordinate_and_compare_height(38,22,20,19) 93 | # print(len(x_cor_list)) 94 | # print(y_cor_list) 95 | # print(x_cor_list) --------------------------------------------------------------------------------