├── README.md ├── data_generator.py ├── data_set_5c.npz └── main.py /README.md: -------------------------------------------------------------------------------- 1 | # Supervised-Deep-Learning-for-Radio-Resource-Allocation 2 | ### Data Generation 3 | The training and testing data is generated through simulation. To generate data, run 4 | ``` 5 | python data_generator.py 6 | ``` 7 | ### Training and Testing 8 | To train and test the model, run 9 | ``` 10 | python main.py 11 | ``` 12 | -------------------------------------------------------------------------------- /data_generator.py: -------------------------------------------------------------------------------- 1 | # library 2 | 3 | import sys 4 | 5 | import math as mt 6 | import numpy as np 7 | 8 | import itertools 9 | import matplotlib.pyplot as plt 10 | 11 | 12 | # function definition 13 | 14 | def circle(x, y,r): 15 | ang=np.arange(0, 2*mt.pi, 0.01) 16 | xp=r*np.cos(ang) 17 | yp=r*np.sin(ang) 18 | plt.plot(x+xp,y+yp) 19 | return 20 | 21 | def random_beam(): 22 | x=[] 23 | x.append(60) 24 | x.append((60+120)%360) 25 | x.append((60-120)%360) 26 | return x 27 | 28 | def point_d(thita,x1,y1,l): 29 | x2=[] 30 | y2=[] 31 | if thita==90: 32 | x2.append(x1) 33 | x2.append(x1) 34 | y2.append(y1+l) 35 | y2.append(y1-l) 36 | if (thita>180 and thita<270) or (thita<=180 and thita>90): 37 | x2.append(l*np.cos(np.deg2rad(thita)) + x1) 38 | y2.append(l * np.sin(np.deg2rad(thita)) + y1) 39 | x2.append(-l * np.cos(np.deg2rad(thita)) + x1) 40 | y2.append(-l * np.sin(np.deg2rad(thita)) + y1) 41 | else: 42 | x2.append(-l*np.cos(np.deg2rad(thita)) + x1) 43 | y2.append(-l * np.sin(np.deg2rad(thita)) + y1) 44 | x2.append(l * np.cos(np.deg2rad(thita)) + x1) 45 | y2.append(l * np.sin(np.deg2rad(thita)) + y1) 46 | 47 | 48 | return x2, y2 49 | 50 | 51 | 52 | def random_allocation(c_x,c_y,angl,d,rng,num): 53 | X=[] 54 | Y=[] 55 | x=np.random.randint(d,rng+1,num) 56 | y=np.random.randint(0,angl,num) 57 | for i in range(num) : 58 | [a,b]=point_d(y[i],c_x,c_y,x[i]) 59 | if y[i]<90 or y[i]>270 : 60 | X.append(a[1]) 61 | Y.append(b[1]) 62 | else: 63 | X.append(a[0]) 64 | Y.append(b[0]) 65 | return X,Y 66 | 67 | 68 | 69 | def find_points_in_angle(x2,y2,x3,y3,angl,q,w): 70 | result_x=[] 71 | result_y=[] 72 | ang=[] 73 | global U_neighbor 74 | global U_neighbor_sector 75 | for i in range(len(x3)): 76 | if (x3[i]-x2)==0: 77 | if y3[i]>=y2: 78 | ang.append(90) 79 | else: 80 | ang.append(270) 81 | else: 82 | ang.append(np.rad2deg(np.arctan2((y3[i]-y2),(x3[i]-x2)))%360) 83 | angl1=angl+60 84 | angl2=angl-60 85 | if angl1>=360 and ang[i]>=0 and ang[i]<90: 86 | ang[i]=ang[i]+360 87 | if angl2<0: 88 | angl2=angl2%360 89 | if angl2>angl1: 90 | angl1=angl1+360 91 | if ang[i]>=0 and ang[i]<180: 92 | ang[i]=ang[i]+360 93 | r=ang[i] 94 | if angl2<=r and r<=angl1: 95 | t=1 96 | else: 97 | t=0 98 | if t==1: 99 | result_x.append(x3[i]) 100 | result_y.append(y3[i]) 101 | U_neighbor[i].extend([q]) 102 | U_neighbor_sector[i].extend([w]) 103 | t=0 104 | 105 | return result_x, result_y 106 | 107 | def find_dis(x1,y1,x0,y0): 108 | return np.sqrt((x0-x1)**2+(y0-y1)**2) 109 | 110 | def find_angl(x2,y2,x3,y3,angl): 111 | z=[] 112 | if (x3-x2)==0: 113 | if y3>=y2: 114 | ang=90 115 | z=ang 116 | else: 117 | ang=270 118 | z=ang 119 | else: 120 | ang=np.rad2deg(np.arctan2((y3-y2),(x3-x2)))%360 121 | if angl>=0 and angl<90 and ang>270 and ang < 360 : 122 | z=360-(ang-angl) 123 | elif ang >= 0 and ang < 90 and angl > 270 and angl < 360: 124 | z=360-(angl-ang) 125 | else: 126 | z=np.abs(angl-ang) 127 | 128 | return z 129 | 130 | def power_watt_dbm(power): 131 | return 10*np.log10(power*1000) 132 | 133 | def transmit_power(thita,thita_3db,Am, power_dbm): 134 | x=-np.min([12*(thita/thita_3db)**2, Am]) 135 | g_x=10**(x/10) 136 | return (10**(power_dbm/10))/1000*g_x 137 | 138 | def pw_m_hata(p,d): 139 | pl=128.1+37.6*np.log10(d/1000) 140 | G=10**(-pl/10) 141 | return G*p 142 | 143 | 144 | 145 | def profile_power(subbands,slot,p_setting,power): 146 | ln=len(p_setting) 147 | 148 | pset=[] 149 | for i in range(len(slot)): 150 | 151 | p=power+1 152 | t=0 153 | k=[] 154 | while (p>=power): 155 | p=0 156 | t=t+1 157 | l=[] 158 | for j in range(len(subbands)): 159 | 160 | l.append(np.random.randint(0,ln,1)) 161 | p=p+(subbands[j]*p_setting[l[j][0]]) 162 | k.append(l) 163 | 164 | for j in range(len(subbands)): 165 | pset.append(p_setting[k[t - 1][j][0]]) 166 | 167 | return pset 168 | 169 | 170 | def convert_subband_power(sub,x): 171 | z=[] 172 | for i in range(len(x)) : 173 | z.append(sub[i]*x[i]) 174 | 175 | return z 176 | 177 | 178 | def generate_power_matrix_macro(): 179 | global thita 180 | global U_x 181 | global num_M 182 | global N_subband 183 | global P_macro_subband 184 | global thita_3db 185 | global A_m 186 | global U_received_power_subband 187 | U_received_power_subband=[] 188 | 189 | for i in range(len(U_x)): 190 | U_received_power_subband.append([]) 191 | 192 | for j in range(num_M): 193 | U_received_power_subband[i].append([]) 194 | 195 | for a in range(N_subband): 196 | pw=P_macro_subband[j][a] 197 | pw_R= transmit_power(thita[i][j], thita_3db, A_m, power_watt_dbm(pw)) 198 | U_received_power_subband[i][j].append(pw_m_hata(pw_R,U_macro_distance[i][j])) 199 | 200 | return 201 | 202 | def sinr_db(p,pn): 203 | 204 | return 10* np.log10(p/pn) 205 | 206 | def db_sinr(val): 207 | 208 | return 10**(val/10) 209 | 210 | def sinr_to_cqi(i): 211 | 212 | if i<=-5: 213 | out=1 214 | 215 | elif i <=-2.5 and i> -5: 216 | out=2 217 | 218 | elif i <=0 and i> -2.5: 219 | out=3 220 | 221 | elif i <=2.4 and i> 0: 222 | out=4 223 | 224 | elif i <=4 and i>2.4: 225 | out=5 226 | 227 | elif i <=6 and i> 4: 228 | out=6 229 | 230 | elif i <=8 and i> 6: 231 | out=7 232 | 233 | elif i <=10 and i> 8: 234 | out=8 235 | 236 | elif i <=13 and i> 10: 237 | out=9 238 | 239 | elif i <=16 and i> 13: 240 | out=10 241 | 242 | elif i <=18 and i> 16: 243 | out=11 244 | 245 | elif i <=20.5 and i> 18: 246 | out=12 247 | 248 | elif i <=24 and i> 20.5: 249 | out=13 250 | 251 | elif i <=26.4 and i> 24: 252 | out=14 253 | 254 | elif i> 26.4: 255 | out=15 256 | 257 | return out 258 | 259 | def shanon_formula(p,pn): 260 | alpha=1 261 | del_f=12*15*10**3 262 | N0=(10**(-174/10))/1000 263 | z1=p/(N0*del_f+pn) 264 | 265 | return del_f*np.log2(1+alpha*z1) 266 | 267 | 268 | def throughput(): 269 | global U_received_power_subband 270 | global U_throughput_subband 271 | global U_SINR_subband 272 | global U_CQI_subband 273 | global P_own 274 | global P_intr 275 | global sub 276 | global U_x 277 | global U_association_macro 278 | global N_subband 279 | global num_M 280 | 281 | U_throughput_subband=[] 282 | U_SINR_subband=[] 283 | U_CQI_subband=[] 284 | 285 | for i in range(len(U_x)): 286 | U_throughput_subband.append([]) 287 | U_SINR_subband.append([]) 288 | U_CQI_subband.append([]) 289 | q=U_association_macro[i] 290 | 291 | if q>0: 292 | id_cell=q-1 293 | P_own=[] 294 | P_intr=[] 295 | 296 | for X in range(N_subband): 297 | P_own.append(U_received_power_subband[i][id_cell][X]) 298 | P_intr.append(0) 299 | 300 | for H in range(num_M): 301 | 302 | if H !=id_cell: 303 | P_intr[X] = P_intr[X] + U_received_power_subband[i][H][X] 304 | 305 | U_SINR_subband[i].append(sinr_db(P_own[X], P_intr[X])) 306 | U_CQI_subband[i].append(sinr_to_cqi(U_SINR_subband[i][X])) 307 | U_throughput_subband[i].append(sub[X] * shanon_formula(P_own[X], P_intr[X]) / (1024 * 1024)) 308 | 309 | return 310 | 311 | 312 | 313 | def calculate_sinr(cell_m,user_i,subband_x): 314 | 315 | return db_sinr(U_SINR_subband[(num_U*cell_m)+user_i][subband_x]) 316 | 317 | 318 | def calculate_gain(cell_m,user_i): 319 | 320 | d=U_macro_distance[(num_U*cell_m)+user_i][cell_m] 321 | 322 | pl=128.1+37.6*np.log10(d/1000) 323 | G=10**(-pl/10) 324 | #print('G :',G) 325 | return G 326 | 327 | def calculate_power(cell_m,cell_n,user_i,subband_x): 328 | 329 | pw = P_macro_subband[cell_m][subband_x] 330 | pw_R = transmit_power(thita[(num_U*cell_n)+user_i][subband_x], thita_3db, A_m, power_watt_dbm(pw)) 331 | #print(pw,pw_R) 332 | return pw_R 333 | 334 | 335 | 336 | def optimize_resource_ga(): 337 | global ga_comb 338 | global ga_comb2 339 | global ga_fit_comb 340 | global P_macro_subband 341 | global N_subband 342 | global O_macro_pos 343 | global O_macro_no 344 | global O_macro_sum 345 | global num_M 346 | global num_U 347 | global sub 348 | global slots_slots 349 | global MC_power 350 | global P_max_macro 351 | 352 | O_macro_sum=[] 353 | ga_comb_temp=[] 354 | 355 | for itr1 in range(6000): 356 | x=[] 357 | 358 | for i in range(num_M): 359 | x.extend(profile_power(sub,slots_slots,MC_power,P_max_macro)) 360 | 361 | ga_comb_temp.append(x) 362 | 363 | ga_comb=np.unique(ga_comb_temp,axis=0).tolist() 364 | 365 | 366 | for itr1 in range(len(ga_comb)): 367 | 368 | for i in range(len(M_x)): 369 | P_macro_subband[i]=ga_comb[itr1][i*N_subband: (i+1)*N_subband] 370 | 371 | #print(ga_comb_temp[itr1],P_macro_subband) 372 | generate_power_matrix_macro() 373 | throughput() 374 | 375 | O_macro_no = [] 376 | O_macro_pos = [] 377 | 378 | for i in range(len(M_x)): 379 | [a,b]=calc_max(i*num_U,((i+1)*num_U)) 380 | O_macro_no.append(a) 381 | O_macro_pos.append(b) 382 | 383 | O_macro_sum.append(np.sum(O_macro_no)) 384 | 385 | q=np.sort(O_macro_sum).tolist() 386 | Q = np.sort(O_macro_sum).tolist() 387 | w = np.argsort(O_macro_sum).tolist() 388 | W = np.argsort(O_macro_sum).tolist() 389 | 390 | O_macro_no=[] 391 | O_macro_pos=[] 392 | O_macro_sum=[] 393 | ga_fit_comb=[[],[],[],[],[],[],[],[],[],[],[],[]] 394 | ga_comb2=ga_comb 395 | 396 | LEN=len(ga_comb2) 397 | sys.stdout.flush() 398 | for uu in range(3000): 399 | O_macro_no = [] 400 | O_macro_pos = [] 401 | O_macro_sum=[] 402 | #prev_config=[] 403 | 404 | ga_fit_comb[0]=ga_comb[w[-1]] 405 | ga_fit_comb[1] = ga_comb[w[-2]] 406 | ga_fit_comb[2] = ga_comb[w[-3]] 407 | ga_fit_comb[3] = ga_comb[w[-4]] 408 | 409 | ga_comb=[] 410 | w=[] 411 | q=[] 412 | 413 | [ga_fit_comb[4],ga_fit_comb[5]]=split_merge_ga(ga_fit_comb[0],ga_fit_comb[1]) 414 | ga_fit_comb[4]=mutation_ga(ga_fit_comb[4]) 415 | ga_fit_comb[5] = mutation_ga(ga_fit_comb[5]) 416 | 417 | [ga_fit_comb[6], ga_fit_comb[7]] = split_merge_ga(ga_fit_comb[0], ga_fit_comb[2]) 418 | ga_fit_comb[6] = mutation_ga(ga_fit_comb[6]) 419 | ga_fit_comb[7] = mutation_ga(ga_fit_comb[7]) 420 | 421 | [ga_fit_comb[8], ga_fit_comb[9]] = split_merge_ga(ga_fit_comb[0], ga_fit_comb[3]) 422 | ga_fit_comb[8] = mutation_ga(ga_fit_comb[8]) 423 | ga_fit_comb[9] = mutation_ga(ga_fit_comb[9]) 424 | 425 | 426 | [ga_fit_comb[2], ga_fit_comb[3]] = split_merge_ga(ga_comb2[W[np.random.randint(LEN-10, LEN,1)[0]]], ga_comb2[W[np.random.randint(LEN-10, LEN,1)[0]]]) 427 | ga_fit_comb[2] = mutation_ga(ga_fit_comb[2]) 428 | ga_fit_comb[3] = mutation_ga(ga_fit_comb[3]) 429 | 430 | [ga_fit_comb[10], ga_fit_comb[11]] = split_merge_ga(ga_comb2[W[np.random.randint(LEN - 50, LEN, 1)[0]]],ga_comb2[W[np.random.randint(LEN - 50, LEN, 1)[0]]]) 431 | ga_fit_comb[10] = mutation_ga(ga_fit_comb[10]) 432 | ga_fit_comb[11] = mutation_ga(ga_fit_comb[11]) 433 | 434 | ga_comb = np.unique(ga_fit_comb, axis=0).tolist() 435 | 436 | OM_position=[] 437 | 438 | for itr1 in range(len(ga_comb)): 439 | 440 | for i in range(len(M_x)): 441 | P_macro_subband[i] = ga_comb[itr1][i * N_subband: (i + 1) * N_subband] 442 | 443 | # print(ga_comb_temp[itr1],P_macro_subband) 444 | generate_power_matrix_macro() 445 | throughput() 446 | 447 | O_macro_no = [] 448 | O_macro_pos = [] 449 | 450 | for i in range(len(M_x)): 451 | [a, b] = calc_max(i * num_U, ((i + 1) * num_U)) 452 | O_macro_no.extend(a) 453 | O_macro_pos.extend(b) 454 | 455 | #print(O_macro_pos) 456 | OM_position.append(O_macro_pos) 457 | O_macro_sum.append(np.sum(O_macro_no)) 458 | 459 | #print(O_macro_sum) 460 | 461 | q = np.sort(O_macro_sum).tolist() 462 | w = np.argsort(O_macro_sum).tolist() 463 | 464 | current_config = ga_comb[w[-1]] 465 | 466 | if uu==0: 467 | prev_config= ga_comb[w[-1]] 468 | cont_itr=0 469 | 470 | if current_config==prev_config : 471 | cont_itr=cont_itr+1 472 | else: 473 | cont_itr=0 474 | 475 | prev_config=ga_comb[w[-1]] 476 | 477 | 478 | if cont_itr >500: 479 | break 480 | 481 | power_config=ga_comb[w[-1]] 482 | ff=np.digitize(power_config,MC_power).tolist() 483 | ff.extend(OM_position[w[-1]]) 484 | #print("power_config",ff) 485 | #g_ga_th.append(q[-1]) 486 | #print("throughput_ga :", q[-1]) 487 | return power_config,ff,q[-1] 488 | 489 | def calc_max(xi,xe): 490 | global U_throughput_subband 491 | 492 | return np.amax(U_throughput_subband[xi:xe],axis=0).tolist() , np.argmax(U_throughput_subband[xi:xe],axis=0).tolist() 493 | 494 | 495 | def split_merge_ga(v1,v2): 496 | 497 | z1=[] 498 | z2=[] 499 | a=np.random.randint(1,num_M+1,1) 500 | b = np.random.randint(0, num_M+1 - a, 1) 501 | 502 | temp_v11=v1[0:(a[0]-1)*N_subband] 503 | temp_v12 = v1[(a[0]-1) * N_subband: (a[0]+b[0])*N_subband] 504 | temp_v13 = v1[(a[0] + b[0]) * N_subband:] 505 | 506 | temp_v21 = v2[0:(a[0] - 1) * N_subband] 507 | temp_v22 = v2[(a[0] - 1) * N_subband: (a[0] + b[0]) * N_subband] 508 | temp_v23 = v2[(a[0] + b[0]) * N_subband:] 509 | 510 | z1.extend(temp_v11) 511 | z1.extend(temp_v22) 512 | z1.extend(temp_v13) 513 | 514 | z2.extend(temp_v21) 515 | z2.extend(temp_v12) 516 | z2.extend(temp_v23) 517 | 518 | return z1, z2 519 | 520 | def mutation_ga(v): 521 | 522 | global MC_power 523 | z=[] 524 | 525 | temp=v[:] 526 | 527 | a=np.random.randint(0,7,1) 528 | if a[0]==1: 529 | ln=len(v) 530 | p1=np.random.randint(0,ln,1) 531 | q1=np.random.randint(0,len(MC_power),1) 532 | 533 | p2 = np.random.randint(0, ln, 1) 534 | q2 = np.random.randint(0, len(MC_power), 1) 535 | 536 | temp[p1[0]]=MC_power[q1[0]] 537 | temp[p2[0]] = MC_power[q2[0]] 538 | 539 | right=check_max(temp) 540 | 541 | if right <0.5: 542 | z=temp 543 | else: 544 | z=v 545 | 546 | elif a[0] == 2: 547 | ln = len(v) 548 | p1 = np.random.randint(0, ln, 1) 549 | q1 = np.random.randint(0, len(MC_power), 1) 550 | 551 | p2 = np.random.randint(0, ln, 1) 552 | q2 = np.random.randint(0, len(MC_power), 1) 553 | 554 | p3 = np.random.randint(0, ln, 1) 555 | q3 = np.random.randint(0, len(MC_power), 1) 556 | 557 | temp[p1[0]] = MC_power[q1[0]] 558 | temp[p2[0]] = MC_power[q2[0]] 559 | temp[p3[0]] = MC_power[q3[0]] 560 | 561 | right = check_max(temp) 562 | 563 | if right < 0.5: 564 | z = temp 565 | else: 566 | z = v 567 | 568 | elif a[0] == 3: 569 | ln = len(v) 570 | p1 = np.random.randint(0, ln, 1) 571 | q1 = np.random.randint(0, len(MC_power), 1) 572 | 573 | p2 = np.random.randint(0, ln, 1) 574 | q2 = np.random.randint(0, len(MC_power), 1) 575 | 576 | p3 = np.random.randint(0, ln, 1) 577 | q3 = np.random.randint(0, len(MC_power), 1) 578 | 579 | p4 = np.random.randint(0, ln, 1) 580 | q4 = np.random.randint(0, len(MC_power), 1) 581 | 582 | temp[p1[0]] = MC_power[q1[0]] 583 | temp[p2[0]] = MC_power[q2[0]] 584 | temp[p3[0]] = MC_power[q3[0]] 585 | temp[p4[0]] = MC_power[q4[0]] 586 | 587 | right = check_max(temp) 588 | 589 | if right < 0.5: 590 | z = temp 591 | else: 592 | z = v 593 | 594 | elif a[0] == 4: 595 | ln = len(v) 596 | p1 = np.random.randint(0, ln, 1) 597 | q1 = np.random.randint(0, len(MC_power), 1) 598 | 599 | p2 = np.random.randint(0, ln, 1) 600 | q2 = np.random.randint(0, len(MC_power), 1) 601 | 602 | p3 = np.random.randint(0, ln, 1) 603 | q3 = np.random.randint(0, len(MC_power), 1) 604 | 605 | p4 = np.random.randint(0, ln, 1) 606 | q4 = np.random.randint(0, len(MC_power), 1) 607 | 608 | p5 = np.random.randint(0, ln, 1) 609 | q5 = np.random.randint(0, len(MC_power), 1) 610 | 611 | temp[p1[0]] = MC_power[q1[0]] 612 | temp[p2[0]] = MC_power[q2[0]] 613 | temp[p3[0]] = MC_power[q3[0]] 614 | temp[p4[0]] = MC_power[q4[0]] 615 | temp[p5[0]] = MC_power[q5[0]] 616 | 617 | right = check_max(temp) 618 | 619 | if right < 0.5: 620 | z = temp 621 | else: 622 | z = v 623 | 624 | 625 | else : 626 | ln = len(v) 627 | p = np.random.randint(0, ln, 1) 628 | q = np.random.randint(0, len(MC_power), 1) 629 | 630 | temp[p[0]] = MC_power[q[0]] 631 | 632 | right = check_max(temp) 633 | 634 | if right < 0.5: 635 | z = temp 636 | else: 637 | z = v 638 | 639 | 640 | return z 641 | 642 | 643 | def check_max(v): 644 | 645 | z=0 646 | for irr in range(num_M): 647 | 648 | if np.sum(v[irr*N_subband:(irr+1)*N_subband]) <= 2.5 : 649 | z=(z | 0) 650 | else: 651 | z= (z | 1) 652 | 653 | return z 654 | 655 | def check_ga_with_drl(action): 656 | global g_total_call 657 | global g_total_corrected 658 | global final_comb 659 | global M_x 660 | global ln_fl_comb 661 | action_drl=[] 662 | if (g_cntr-1) < g_data_len: 663 | #print("running ---------------") 664 | action_ga=data_set['arr_2'][g_cntr-1].tolist() 665 | ga_th=data_set['arr_3'][g_cntr-1].tolist() 666 | # action_wmmse = data_set['arr_4'][g_cntr - 1].tolist() 667 | # wmmse_th = data_set['arr_5'][g_cntr - 1].tolist() 668 | 669 | else: 670 | 671 | action_ga,_,ga_th=optimize_resource_ga() 672 | g_data_action_ga.append(action_ga) 673 | g_data_ga_th.append(ga_th) 674 | # action_wmmse, wmmse_th = optimize_resource_wmmse() 675 | # g_data_action_wmmse.append(action_wmmse) 676 | # g_data_wmmse_th.append(wmmse_th) 677 | 678 | 679 | g_ga_th.append(ga_th) 680 | # g_wmmse_th.append(wmmse_th) 681 | 682 | #print("throughput_ga :", ga_th) 683 | 684 | #for i in range(len(M_x)): 685 | # action_drl.extend(final_comb[action[i]-i*ln_fl_comb]) 686 | #answer=np.double(action_drl==action_ga) 687 | #print(answer,action_drl,action_ga) 688 | #g_total_call=g_total_call+1 689 | #g_total_corrected=g_total_corrected+answer 690 | #g_drl_th.append(g_drl_th_val) 691 | 692 | #g_eql_th.append(g_eql_th_val) 693 | #g_rnd_th.append(g_rnd_th_val) 694 | 695 | 696 | 697 | # print("Throughput GA: ",ga_th,' DRL: ', g_drl_th_val,' EQL: ', g_eql_th_val,' RND: ',g_rnd_th_val,' WMMSE: ',wmmse_th) 698 | np.savez("result_5c_2h_5p_lr2.npz", g_ga_th) 699 | np.save("counter_v2_5c_2h_5p_lr2.npy",g_cntr) 700 | if g_cntr > g_data_len: 701 | np.savez("data_set_5c.npz",g_data_x,g_data_y,g_data_action_ga,g_data_ga_th) 702 | 703 | 704 | return 705 | 706 | 707 | ######################################################################################################################## 708 | 709 | 710 | 711 | def optimize_resource_wmmse(): 712 | 713 | #v_k_0=np.random.rand(0,np.sqrt(0.8),num_M) 714 | global P_macro_subband 715 | global N_subband 716 | global num_U 717 | global num_M 718 | global ln_fl_comb 719 | global final_comb 720 | global k_m_n 721 | global v_k_old 722 | global u_k_old 723 | global w_k_old 724 | 725 | 726 | global v_k_new 727 | global u_k_new 728 | global w_k_new 729 | 730 | del_f = 12 * 15 * 10 ** 3 731 | N0 = (10 ** (-174 / 10)) / 1000 732 | 733 | 734 | for i in range(num_M): 735 | P_macro_subband[i] = final_comb[np.random.randint(0, ln_fl_comb)] 736 | 737 | v_k_old=np.sqrt(P_macro_subband).tolist() 738 | 739 | generate_power_matrix_macro() 740 | throughput() 741 | 742 | K_macro_no = [] 743 | k_m_n = [] 744 | 745 | # for calculating user association 746 | 747 | for i in range(len(M_x)): 748 | [a, b] = calc_max(i * num_U, ((i + 1) * num_U)) 749 | # K_macro_no.append(a) 750 | k_m_n.append(b) 751 | #print(k_m_n) 752 | 753 | # for u_k_old 754 | u_k_old=[] 755 | 756 | for i in range(len(M_x)): # u_k 757 | u_k_old.append([]) 758 | 759 | for X in range(N_subband): # u_k^n 760 | 761 | sum_den = 0 762 | for j in range(len(M_x)): # j=1 763 | 764 | sum_den = sum_den + (calculate_gain(j, k_m_n[i][X]) * calculate_power(j, i, k_m_n[i][X], X) ) 765 | 766 | sum_den=sum_den+(N0*del_f) 767 | 768 | u_k_old[i].append(np.sqrt(calculate_gain(i,k_m_n[i][X])) * np.sqrt(calculate_power(i, i, k_m_n[i][X], X))/ sum_den) 769 | 770 | # for u_k_old 771 | w_k_old=[] 772 | 773 | for i in range(len(M_x)): # w_k 774 | w_k_old.append([]) 775 | 776 | for X in range(N_subband): # w_k^n 777 | 778 | w_k_old[i].append(1/ (u_k_old[i][X] * np.sqrt(calculate_gain(i,k_m_n[i][X])) * np.sqrt(calculate_power(i, i, k_m_n[i][X], X)))) 779 | for itr_wmmse in range(40): 780 | 781 | update_v_k() 782 | update_u_k() 783 | update_w_k() 784 | if u_k_old==u_k_new: 785 | break 786 | u_k_old=u_k_new 787 | w_k_old=w_k_new 788 | 789 | generate_power_matrix_macro() 790 | throughput() 791 | 792 | O_macro_no = [] 793 | O_macro_pos = [] 794 | 795 | for i in range(len(M_x)): 796 | [a, b] = calc_max(i * num_U, ((i + 1) * num_U)) 797 | O_macro_no.append(a) 798 | O_macro_pos.append(b) 799 | 800 | 801 | 802 | #print('Final :',P_macro_subband) 803 | 804 | 805 | 806 | return P_macro_subband, np.sum(O_macro_no) # action , throughput 807 | 808 | def update_v_k(): 809 | global P_macro_subband 810 | global v_k_new 811 | global u_k_old 812 | global w_k_old 813 | global k_m_n 814 | # for v_k_new 815 | v_k_new = [] 816 | 817 | for i in range(len(M_x)): # v_k 818 | v_k_new.append([]) 819 | 820 | for X in range(N_subband): # v_k^n 821 | 822 | sum_den = 0 823 | for j in range(len(M_x)): # j=1 824 | 825 | sum_den = sum_den + (calculate_gain(j, k_m_n[i][X]) * (( u_k_old[j][X])**2) * w_k_old[j][X]) 826 | 827 | #sum_den = sum_den + 1 828 | 829 | v_k_new[i].append( 830 | np.sqrt(calculate_gain(i, k_m_n[i][X])) * w_k_old[i][X] * u_k_old[i][X] / sum_den) 831 | 832 | P_macro_subband=np.square(v_k_new).tolist() 833 | 834 | normalized_check_max() 835 | 836 | generate_power_matrix_macro() 837 | throughput() 838 | 839 | k_m_n = [] 840 | 841 | # for calculating user association 842 | 843 | for i in range(len(M_x)): 844 | [a, b] = calc_max(i * num_U, ((i + 1) * num_U)) 845 | # K_macro_no.append(a) 846 | k_m_n.append(b) 847 | 848 | #print(np.square(v_k_new).tolist()) 849 | #print(P_macro_subband) 850 | 851 | return 852 | 853 | def update_u_k(): 854 | global u_k_new 855 | 856 | del_f = 12 * 15 * 10 ** 3 857 | N0 = (10 ** (-174 / 10)) / 1000 858 | u_k_new = [] 859 | 860 | for i in range(len(M_x)): # u_k 861 | u_k_new.append([]) 862 | 863 | for X in range(N_subband): # u_k^n 864 | 865 | sum_den = 0 866 | for j in range(len(M_x)): # j=1 867 | 868 | sum_den = sum_den + (calculate_gain(j, k_m_n[i][X]) * calculate_power(j, i, k_m_n[i][X], X)) 869 | 870 | sum_den = sum_den + (N0 * del_f) 871 | 872 | u_k_new[i].append( 873 | np.sqrt(calculate_gain(i, k_m_n[i][X])) * np.sqrt(calculate_power(i, i, k_m_n[i][X], X)) / sum_den) 874 | 875 | #print(u_k_new) 876 | 877 | return 878 | 879 | def update_w_k(): 880 | global w_k_new 881 | w_k_new = [] 882 | 883 | for i in range(len(M_x)): # w_k 884 | w_k_new.append([]) 885 | 886 | for X in range(N_subband): # w_k^n 887 | 888 | w_k_new[i].append(1 / (u_k_new[i][X] * np.sqrt(calculate_gain(i, k_m_n[i][X])) * np.sqrt( 889 | calculate_power(i, i, k_m_n[i][X], X)))) 890 | 891 | return 892 | 893 | def normalized_check_max(): 894 | 895 | 896 | for i in range(len(M_x)): # 897 | 898 | for X in range(N_subband): # 899 | P_macro_subband[i][X]= normalized_power(P_macro_subband[i][X]) 900 | 901 | if np.sum(P_macro_subband[i][0:N_subband]) >2.41: 902 | P_macro_subband[i]=(0.8*np.ones(N_subband)).tolist() 903 | 904 | 905 | 906 | 907 | return 908 | 909 | 910 | def normalized_power(inpt): 911 | 912 | if inpt <(MC_power[0]+MC_power[1])/2: 913 | output=MC_power[0] 914 | elif (MC_power[0]+MC_power[1])/2 <= inpt < (MC_power[1]+MC_power[2])/2: 915 | output=MC_power[1] 916 | elif (MC_power[1]+MC_power[2])/2 <= inpt < (MC_power[2]+MC_power[3])/2: 917 | output=MC_power[2] 918 | elif (MC_power[2]+MC_power[3])/2 <= inpt < (MC_power[3]+MC_power[4])/2: 919 | output=MC_power[3] 920 | elif (MC_power[3]+MC_power[4])/2 <= inpt : 921 | output=MC_power[4] 922 | 923 | return output 924 | 925 | 926 | 927 | 928 | # -------------------- ENVIRONMENT --------------------- 929 | 930 | #s,s_ini = self.env_reset() 931 | 932 | def env_reset(): 933 | global thita 934 | global g_drl_th_val 935 | global g_eql_th_val 936 | global g_rnd_th_val 937 | global thita_3db 938 | global P_max_macro 939 | global MC_power 940 | global slots_slots 941 | global sub 942 | global A_m 943 | global R_m 944 | global num_U 945 | global num_M 946 | global M_x 947 | global M_y 948 | global M_cell_beam 949 | global M_cell_associated_user_id 950 | global U_x 951 | global U_y 952 | global U_association_macro 953 | global U_neighbor 954 | global U_neighbor_sector 955 | global M_cell_region_x 956 | global M_cell_region_y 957 | global U_macro_distance 958 | global U_macro_power 959 | global M_cell_txblock_power 960 | global P_macro_subband 961 | global U_received_power_subband 962 | global U_throughput_subband 963 | global U_SINR_subband 964 | global U_CQI_subband 965 | global g_action 966 | global g_cntr 967 | 968 | global current_throughput 969 | global previous_throughput 970 | 971 | global g_counter 972 | 973 | global k_m_n 974 | global v_k_old 975 | global u_k_old 976 | global w_k_old 977 | 978 | global v_k_new 979 | global u_k_new 980 | global w_k_new 981 | 982 | k_m_n = [] 983 | v_k_old = [] 984 | u_k_old = [] 985 | w_k_old = [] 986 | 987 | v_k_new = [] 988 | u_k_new = [] 989 | w_k_new = [] 990 | 991 | g_counter = 0 992 | current_throughput = [] 993 | previous_throughput = 0 994 | g_drl_th_val = 0 995 | g_eql_th_val = 0 996 | g_rnd_th_val = 0 997 | 998 | M_cell_beam = [] 999 | M_cell_associated_user_id = [] 1000 | M_cell_region_x = [] 1001 | M_cell_region_y = [] 1002 | M_cell_txblock_power = [] 1003 | 1004 | U_x = [] 1005 | U_y = [] 1006 | U_association_macro = [] 1007 | U_neighbor = [] 1008 | U_neighbor_sector = [] 1009 | U_macro_distance = [] 1010 | U_macro_power = [] 1011 | U_throughputt = [] 1012 | 1013 | P_macro_subband = [] 1014 | U_received_power_subband = [] 1015 | U_throughput_subband = [] 1016 | 1017 | U_SINR_subband = [] 1018 | U_CQI_subband = [] 1019 | 1020 | itr = 0 1021 | for i in range(len(M_x)): 1022 | M_cell_beam.append(random_beam()) 1023 | M_cell_associated_user_id.append([]) 1024 | 1025 | for j in range(num_U): 1026 | M_cell_associated_user_id[i].extend([itr + 1]) 1027 | U_association_macro.append(i + 1) 1028 | itr = itr + 1 1029 | 1030 | # [x, y] = random_allocation(M_x[i], M_y[i], 360, 0.2 * R_m, R_m, num_U) 1031 | # U_x.extend(x) 1032 | # U_y.extend(y) 1033 | 1034 | # *********************************************************************************************************************** 1035 | 1036 | # g_data_x.append(U_x) 1037 | # g_data_y.append(U_y) 1038 | if g_cntr < g_data_len: 1039 | U_x = data_set['arr_0'][g_cntr].tolist() 1040 | # print(U_x) 1041 | U_y = data_set['arr_1'][g_cntr].tolist() 1042 | 1043 | else: 1044 | for i in range(len(M_x)): 1045 | [x, y] = random_allocation(M_x[i], M_y[i], 360, 0.2 * R_m, R_m, num_U) 1046 | U_x.extend(x) 1047 | U_y.extend(y) 1048 | 1049 | g_data_x.append(U_x) 1050 | g_data_y.append(U_y) 1051 | 1052 | g_cntr = g_cntr + 1 1053 | print(g_cntr) 1054 | 1055 | # *********************************************************************************************************************** 1056 | for i in range(len(U_x)): 1057 | U_neighbor.append([]) 1058 | U_neighbor_sector.append([]) 1059 | 1060 | for i in range(len(M_x)): 1061 | M_cell_region_x.append([[], [], []]) 1062 | M_cell_region_y.append([[], [], []]) 1063 | 1064 | for j in range(3): 1065 | [a, b] = find_points_in_angle(M_x[i], M_y[i], U_x, U_y, M_cell_beam[i][j], i + 1, j + 1) 1066 | M_cell_region_x[i][j].extend(a) 1067 | M_cell_region_y[i][j].extend(b) 1068 | 1069 | thita = np.zeros((len(U_x), len(M_x))) 1070 | 1071 | for i in range(len(U_x)): 1072 | U_macro_distance.append([]) 1073 | U_macro_power.append([]) 1074 | 1075 | for j in range(len(M_x)): 1076 | U_macro_distance[i].append(find_dis(U_x[i], U_y[i], M_x[j], M_y[j])) 1077 | angl = M_cell_beam[j][U_neighbor_sector[i][j] - 1] 1078 | 1079 | thita[i][j] = find_angl(M_x[j], M_y[j], U_x[i], U_y[i], angl) 1080 | a = transmit_power(thita[i][j], thita_3db, A_m, power_watt_dbm(P_max_macro)) 1081 | U_macro_power[i].append(pw_m_hata(a, U_macro_distance[i][j])) 1082 | 1083 | for i in range(len(M_x)): 1084 | M_cell_txblock_power.append(profile_power(sub, slots_slots, MC_power, P_max_macro)) 1085 | P_macro_subband.append(M_cell_txblock_power[i]) 1086 | 1087 | generate_power_matrix_macro() 1088 | 1089 | throughput() 1090 | 1091 | temp_var = [] 1092 | 1093 | for i in range(num_M * num_U): 1094 | temp_var.extend(U_CQI_subband[i]) 1095 | temp_var.append(np.double(U_macro_distance[i][U_association_macro[i] - 1] / R_m >= 0.5)) 1096 | 1097 | return np.array(temp_var) 1098 | 1099 | 1100 | ######################################################################################################################## 1101 | 1102 | # variable definition 1103 | 1104 | current_throughput=[] 1105 | previous_throughput=0 1106 | 1107 | g_counter=0 1108 | g_action=[] 1109 | g_total_call=0 1110 | g_total_corrected=0 1111 | g_ga_th=[] 1112 | g_wmmse_th=[] 1113 | g_drl_th_val=0 1114 | g_drl_th=[] 1115 | g_eql_th_val=0 1116 | g_rnd_th_val=0 1117 | g_eql_th=[] 1118 | g_rnd_th=[] 1119 | 1120 | g_cntr=0 1121 | g_data_len=0 1122 | 1123 | g_data_x=[] 1124 | g_data_y=[] 1125 | 1126 | g_data_action_ga=[] 1127 | g_data_vector_ga=[] 1128 | g_data_ga_th=[] 1129 | 1130 | g_data_action_wmmse=[] 1131 | g_data_wmmse_th=[] 1132 | 1133 | 1134 | 1135 | M_cell_beam=[] 1136 | M_cell_associated_user_id=[] 1137 | M_cell_cor_x=[] 1138 | M_cell_cor_y=[] 1139 | M_cell_region_x=[] 1140 | M_cell_region_y=[] 1141 | M_cell_txblock_power=[] 1142 | 1143 | U_x=[] 1144 | U_y=[] 1145 | U_association_macro=[] 1146 | U_neighbor=[] 1147 | U_neighbor_sector=[] 1148 | U_macro_distance=[] 1149 | U_macro_power=[] 1150 | U_throughputt=[] 1151 | 1152 | P_macro_subband=[] 1153 | 1154 | U_received_power_subband=[] 1155 | U_throughput_subband=[] 1156 | U_SINR_subband=[] 1157 | U_CQI_subband=[] 1158 | 1159 | ga_comb=[] 1160 | ga_comb2=[] 1161 | ga_fit_comb=[] 1162 | 1163 | O_macro_no=[] 1164 | O_macro_pos=[] 1165 | O_macro_sum=[] 1166 | 1167 | thita=[] 1168 | 1169 | 1170 | thita_3db=70 1171 | A_m=35 1172 | P_max_macro=40 1173 | alpha=25 1174 | r=1000 1175 | R_m=500 1176 | min_RM=600 1177 | num_M=5 1178 | num_U=5 1179 | N=9000 1180 | N_subband=3 1181 | 1182 | MC_power_1=[0.4, 0.6, 0.8,1.0,1.2] 1183 | MC_power=[0.4,0.6, 0.8,1.0,1.2] 1184 | 1185 | N_RB=48 1186 | N_slot=1 1187 | N_subframes=1 1188 | sub=[] 1189 | slots_slots=[1] 1190 | sub.append(mt.floor(N_RB/N_subband)) 1191 | sub.append(mt.floor(N_RB/N_subband)) 1192 | 1193 | # for 5 cell system 1194 | M_x=[272, -270, -72, 490, -804] 1195 | M_y=[668, 136, -839, -100, -405] 1196 | 1197 | # for 10 cell system 1198 | #M_x=[-507.325029580438,1002.33404665068,145.312062677023,200.78357265330,1638.02723651541,1373.51667496699,-505.567176719993,89.0624746033709,888.768341569969,700.98746226480] 1199 | #M_y=[622.439530990585,1204.09269092070,-857.441644095846,0.776230721301,-266.682269021143,409.885301601770,-276.654621800917,971.99443198228,-404.073417200223,500.42153804114] 1200 | 1201 | # for 15 cell system 1202 | #M_x=[-507.325029580438,1002.33404665068,145.312062677023,1008.94157032003,200.78357265330,1638.02723651541,1373.51667496699,-505.567176719993,89.0624746033709,208.3844792471699,-1205.45088276488,-1203.86653509258,888.768341569969,-623.017861398548,700.98746226480] 1203 | #M_y=[622.439530990585,1204.09269092070,-857.441644095846,-1254.30978634305,0.776230721301,-566.682269021143,309.885301601770,-276.654621800917,971.99443198228,-1708.16795562791,281.640997912069,-609.705714626437,-404.073417200223,-1209.60615767022,500.42153804114] 1204 | 1205 | 1206 | sub.append(N_RB-sub[N_subband-2]*(N_subband-1)) 1207 | #print(sub) 1208 | final_comb=[] 1209 | P_own=[] 1210 | P_intr=[] 1211 | P_SINR=[] 1212 | 1213 | 1214 | # final_comb : combination of all the actions for a particular cell 1215 | 1216 | power_comb = list(map(list, itertools.product(MC_power_1, repeat=N_subband))) 1217 | 1218 | for i in power_comb: 1219 | temp_sum=0.0 1220 | for j in range(N_subband): 1221 | temp_sum=temp_sum+ i[j]*sub[j] 1222 | if temp_sum <=40 : 1223 | final_comb.append(i) 1224 | 1225 | ln_fl_comb=len(final_comb) 1226 | 1227 | #print(final_comb[12]) 1228 | 1229 | plt.figure(1) 1230 | plt.plot(M_x,M_y,'b*') 1231 | 1232 | for i in range(len(M_x)): 1233 | circle(M_x[i],M_y[i],R_m) 1234 | #plt.show() # to plot the figure 1235 | 1236 | 1237 | 1238 | 1239 | 1240 | 1241 | 1242 | 1243 | try: 1244 | 1245 | g_cntr=0 1246 | 1247 | ######################################################################################################################## 1248 | #g_cntr=np.load("counter_5c_1.npy").tolist() 1249 | #data_set = np.load("data_set_5c_1.npz") 1250 | #g_data_len = len(data_set['arr_0']) 1251 | #g_data_x = data_set['arr_0'].tolist() 1252 | #g_data_y = data_set['arr_1'].tolist() 1253 | #g_data_action_ga = data_set['arr_2'].tolist() 1254 | #g_data_ga_th = data_set['arr_3'].tolist() 1255 | #g_data_action_wmmse = data_set['arr_4'].tolist() 1256 | #g_data_wmmse_th = data_set['arr_5'].tolist() 1257 | #g_data_vector_ga=data_set['arr_6'].tolist() 1258 | #print(g_data_len,g_cntr) 1259 | #print(g_data_ga_th) 1260 | 1261 | 1262 | 1263 | ######################################################################################################################## 1264 | 1265 | while True: 1266 | s=env_reset().tolist() 1267 | #print(s) 1268 | #a,b,c=optimize_resource_ga() 1269 | #print(a,b,c) 1270 | 1271 | if (g_cntr) > g_data_len: 1272 | action_ga, vector_ga, ga_th = optimize_resource_ga() 1273 | #print(s) 1274 | s.extend(vector_ga) 1275 | vector_ga=s 1276 | #print(vector_ga) 1277 | g_data_action_ga.append(action_ga) 1278 | g_data_ga_th.append(ga_th) 1279 | g_data_vector_ga.append(vector_ga) 1280 | action_wmmse, wmmse_th = optimize_resource_wmmse() 1281 | g_data_action_wmmse.append(action_wmmse) 1282 | g_data_wmmse_th.append(wmmse_th) 1283 | 1284 | np.save("counter_5c_1", g_cntr) 1285 | if g_cntr > g_data_len: 1286 | np.savez("data_set_5c_1.npz", g_data_x, g_data_y, g_data_action_ga, g_data_ga_th, g_data_action_wmmse, g_data_wmmse_th,g_data_vector_ga) 1287 | 1288 | 1289 | finally: 1290 | # agent.brain.model.save("dqn_5c_2h_5p_lr2.h5") 1291 | #np.save("data_5c_2h_5p_lr2.npy", agent.memory.samples) 1292 | 1293 | print("total call vs corrected answer :", g_total_call,g_total_corrected) 1294 | 1295 | 1296 | 1297 | 1298 | 1299 | 1300 | 1301 | 1302 | -------------------------------------------------------------------------------- /data_set_5c.npz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ishfaq06/Supervised-Deep-Learning-for-Radio-Resource-Allocation/fcab5e2302d3e61b17a94bd8dedf75a26c5f64fc/data_set_5c.npz -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | 2 | from keras.models import Model 3 | from keras.layers import Input, Dense 4 | from keras.layers import Input, Convolution2D, Activation, MaxPooling2D, \ 5 | Dense, BatchNormalization, Dropout 6 | 7 | from keras.optimizers import SGD 8 | import numpy as np 9 | 10 | import math as mt 11 | import itertools 12 | 13 | 14 | num_M=5 15 | num_U=5 16 | N=9000 17 | N_subband=3 18 | 19 | MC_power_1=[0.4, 0.6, 0.8,1.0,1.2] 20 | MC_power=[0.4,0.6, 0.8,1.0,1.2] 21 | 22 | N_RB=48 23 | N_slot=1 24 | N_subframes=1 25 | sub=[] 26 | slots_slots=[1] 27 | sub.append(mt.floor(N_RB/N_subband)) 28 | sub.append(mt.floor(N_RB/N_subband)) 29 | sub.append(N_RB-sub[N_subband-2]*(N_subband-1)) 30 | #print(sub) 31 | final_comb=[] 32 | power_comb = list(map(list, itertools.product(MC_power_1, repeat=N_subband))) 33 | 34 | for i in power_comb: 35 | temp_sum=0.0 36 | for j in range(N_subband): 37 | temp_sum=temp_sum+ i[j]*sub[j] 38 | if temp_sum <=40 : 39 | final_comb.append(i) 40 | 41 | ln_fl_comb=len(final_comb) 42 | print(final_comb) 43 | aw=final_comb.index([1.2,0.8,0.4]) 44 | print("aw----",aw,ln_fl_comb) 45 | 46 | 47 | data_set = np.load("data_set_5c.npz") 48 | 49 | g_data_x = data_set['arr_0'].tolist() 50 | g_data_y = data_set['arr_1'].tolist() 51 | g_data_action_ga = data_set['arr_2'].tolist() 52 | g_data_ga_th = data_set['arr_3'].tolist() 53 | g_data_action_wmmse = data_set['arr_4'].tolist() 54 | g_data_wmmse_th = data_set['arr_5'].tolist() 55 | g_data_vector_ga=data_set['arr_6'] 56 | 57 | print(g_data_action_ga[0]) 58 | 59 | data_x=[] 60 | data_y=[] 61 | for i in range(len(g_data_vector_ga)): 62 | temp_x=g_data_vector_ga[i][0:100] 63 | temp_y = g_data_vector_ga[i][100:115] 64 | data_x.append(temp_x) 65 | data_y.append(temp_y) 66 | data_x=np.array(data_x) 67 | data_y=np.array(data_y) 68 | data_y=data_y.astype('int') 69 | final=[] 70 | c=[] 71 | for i in range(len(data_y)): 72 | 73 | for j in range(len(data_y[i])): 74 | # f=[int(x) for x in list('{0:3b}'.format(a[[i],[j]][0]))] 75 | #f=[int(x) for x in bin(data_y[[i],[j]][0])[2:].zfill(3)] 76 | f_=[int(x) for x in bin(data_y[[i],[j]][0])[2:].zfill(3)] 77 | f= (1-np.array(f_)).tolist() 78 | f_final=[] 79 | for k in range(len(f_)): 80 | f_final.append(f_[k]) 81 | f_final.append(f[k]) 82 | #print(f) 83 | final.extend(f_final) 84 | c.append(final) 85 | final=[] 86 | 87 | print(c[0:3]) 88 | data_y=np.array(c) 89 | print(data_y) 90 | 91 | data_x=data_x.astype('float32') 92 | data_y=data_y.astype('float32') 93 | 94 | 95 | print(len(data_x[0]),len(data_y[0])) 96 | 97 | X_train=data_x[0:round(0.8*len(data_x))] 98 | Y_train=data_y[0:round(0.8*len(data_x))] 99 | X_test=data_x[round(0.8*len(data_x)):len(data_x)] 100 | Y_test=data_y[round(0.8*len(data_x)):len(data_x)] 101 | 102 | print(len(Y_test),len(Y_train)) 103 | print(Y_test) 104 | 105 | # Layer_1 106 | input_img = Input(shape = (100, )) 107 | #distorted_input1 = Dropout(.1)(input_img) 108 | encoded1 = Dense(800, activation = 'sigmoid')(input_img) 109 | encoded1_bn = BatchNormalization()(encoded1) 110 | 111 | decoded1 = Dense(100, activation = 'sigmoid')(encoded1_bn) 112 | autoencoder1 = Model(input_img, decoded1) 113 | encoder1 = Model(input_img, encoded1_bn) 114 | 115 | # Layer 2 116 | encoded1_input = Input(shape = (800,)) 117 | #distorted_input2 = Dropout(.2)(encoded1_input) 118 | encoded2 = Dense(400, activation = 'sigmoid')(encoded1_input) 119 | encoded2_bn = BatchNormalization()(encoded2) 120 | decoded2 = Dense(800, activation = 'sigmoid')(encoded2_bn) 121 | 122 | autoencoder2 = Model(encoded1_input, decoded2) 123 | encoder2 = Model(encoded1_input, encoded2_bn) 124 | 125 | # Layer 3 - which we won't end up fitting in the interest of time 126 | encoded2_input = Input(shape = (400,)) 127 | #distorted_input3 = Dropout(.3)(encoded2_input) 128 | encoded3 = Dense(200, activation = 'sigmoid')(encoded2_input) 129 | encoded3_bn = BatchNormalization()(encoded3) 130 | decoded3 = Dense(400, activation = 'sigmoid')(encoded3_bn) 131 | 132 | autoencoder3 = Model(encoded2_input, decoded3) 133 | encoder3 = Model(encoded2_input, encoded3_bn) 134 | 135 | # Layer_4 136 | encoded3_input = Input(shape = (200,)) 137 | encoded4 = Dense(15*3*2, activation = 'sigmoid')(encoded3_input) 138 | 139 | softmax1 = Model(encoded3_input, encoded4) 140 | 141 | # Not as Deep Autoencoder 142 | nad_encoded1_da = Dense(800, activation = 'sigmoid')(input_img) 143 | nad_encoded1_da_bn = BatchNormalization()(nad_encoded1_da) 144 | nad_encoded2_da = Dense(400, activation = 'sigmoid')(nad_encoded1_da_bn) 145 | nad_encoded2_da_bn = BatchNormalization()(nad_encoded2_da) 146 | nad_encoded3_da = Dense(200, activation = 'sigmoid')(nad_encoded2_da_bn) 147 | nad_encoded3_da_bn = BatchNormalization()(nad_encoded3_da) 148 | 149 | 150 | dense1 = Dense(15*3*2, activation='sigmoid')(nad_encoded3_da_bn) 151 | 152 | nad_deep_autoencoder = Model(input_img, dense1) 153 | 154 | sgd1 = SGD(lr = 5, decay = 0.5, momentum = .85, nesterov = True) 155 | sgd2 = SGD(lr = 5, decay = 0.5, momentum = .85, nesterov = True) 156 | sgd3 = SGD(lr = 5, decay = 0.5, momentum = .85, nesterov = True) 157 | 158 | autoencoder1.compile(loss='mse', optimizer = sgd1) 159 | autoencoder2.compile(loss='mse', optimizer = sgd2) 160 | autoencoder3.compile(loss='mse', optimizer = sgd3) 161 | 162 | encoder1.compile(loss='mse', optimizer = sgd1) 163 | encoder2.compile(loss='mse', optimizer = sgd1) 164 | encoder3.compile(loss='mse', optimizer = sgd1) 165 | softmax1.compile(loss='mse', optimizer= sgd1) 166 | 167 | nad_deep_autoencoder.compile(loss='mse', optimizer = sgd1) 168 | 169 | autoencoder1.fit(X_train, X_train, 170 | epochs = 8, batch_size = 512, 171 | validation_split = 0.25, 172 | shuffle = True) 173 | first_layer_code = encoder1.predict(X_train) 174 | print(first_layer_code.shape) 175 | 176 | autoencoder2.fit(first_layer_code, first_layer_code, 177 | epochs = 8, batch_size = 512, 178 | validation_split = 0.25, 179 | shuffle = True) 180 | 181 | second_layer_code = encoder2.predict(first_layer_code) 182 | print(second_layer_code.shape) 183 | 184 | autoencoder3.fit(second_layer_code, second_layer_code, 185 | epochs = 8, batch_size = 512, 186 | validation_split = 0.25, 187 | shuffle = True) 188 | third_layer_code = encoder3.predict(second_layer_code) 189 | print(third_layer_code.shape) 190 | 191 | softmax1.fit(third_layer_code, Y_train, 192 | epochs = 8, batch_size = 512, 193 | validation_split = 0.25, 194 | shuffle = True) 195 | 196 | # Setting up the weights of the not-as-deep autoencoder 197 | nad_deep_autoencoder.layers[1].set_weights(autoencoder1.layers[1].get_weights()) # first dense layer 198 | nad_deep_autoencoder.layers[2].set_weights(autoencoder1.layers[2].get_weights()) # first bn layer 199 | nad_deep_autoencoder.layers[3].set_weights(autoencoder2.layers[1].get_weights()) # second dense layer 200 | nad_deep_autoencoder.layers[4].set_weights(autoencoder2.layers[2].get_weights()) # second bn layer 201 | nad_deep_autoencoder.layers[5].set_weights(autoencoder3.layers[1].get_weights()) # third dense layer 202 | nad_deep_autoencoder.layers[6].set_weights(autoencoder3.layers[2].get_weights()) # third bn layer 203 | nad_deep_autoencoder.layers[7].set_weights(softmax1.layers[1].get_weights()) # fourth dense layer 204 | 205 | val_preds = nad_deep_autoencoder.predict(X_test) 206 | #val_preds=np.round(val_preds) 207 | 208 | ###################################### 209 | for j in range(len(val_preds)): 210 | for i in range(15*3): 211 | if val_preds[j][2*i]>val_preds[j][(2*i)+1]: 212 | val_preds[j][2*i]=1 213 | val_preds[j][(2*i)+1]=0 214 | else: 215 | val_preds[j][2 * i] = 0 216 | val_preds[j][(2 * i) + 1] = 1 217 | 218 | n_correct = np.sum(np.equal(val_preds, Y_test).astype(int)) 219 | total = float(len(val_preds)) 220 | print("Test Accuracy:", n_correct / total) 221 | 222 | nad_deep_autoencoder.fit(X_train, Y_train, 223 | epochs=20, batch_size=500, 224 | validation_split=0.25, 225 | shuffle=True) 226 | 227 | val_preds = nad_deep_autoencoder.predict(X_test) 228 | print(val_preds[0]) 229 | #val_preds=np.round(val_preds) 230 | 231 | ###################################### 232 | for j in range(len(val_preds)): 233 | for i in range(15*3): 234 | if val_preds[j][2*i]>val_preds[j][(2*i)+1]: 235 | val_preds[j][2*i]=1 236 | val_preds[j][(2*i)+1]=0 237 | else: 238 | val_preds[j][2 * i] = 0 239 | val_preds[j][(2 * i) + 1] = 1 240 | 241 | 242 | n_correct = np.sum(np.equal(val_preds, Y_test).astype(int)) 243 | total = float(len(val_preds)) 244 | print("Test Accuracy:", n_correct / total) 245 | 246 | #print(np.round(val_preds[0]),Y_test[0]) --------------------------------------------------------------------------------