-
Notifications
You must be signed in to change notification settings - Fork 19
/
JSAC_OPT_final_in_feasible.py
1528 lines (1203 loc) · 72 KB
/
JSAC_OPT_final_in_feasible.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import tensorflow as tf
import numpy as np
import math
import os
import time
import pandas as pd
from numpy import linalg as LA
np.set_printoptions(formatter={'float': '{: 0.3f}'.format})
''' WMMSE results
'''
def WMMSE_sum_rate(p_int, H, Pmax, int_cell):
K = np.size(p_int)
vnew = 0
b = np.sqrt(p_int)
f = np.zeros(K)
w = np.zeros(K)
for i in range(K):
f[i] = H[i, i] * b[i] / ( np.matmul(np.square(H[i, :]), np.square(b)) + int_cell[i])
w[i] = 1 / (1 - f[i] * b[i] * H[i, i])
vnew = vnew + np.log(w[i])
VV = np.zeros(100)
for iter in range(100):
vold = vnew
for i in range(K):
btmp = w[i] * f[i] * H[i, i] / sum(w * np.square(f) * np.square(H[:, i]))
b[i] = np.minimum(btmp, np.sqrt(Pmax)) + np.maximum(btmp, 0) - btmp
vnew = 0
for i in range(K):
f[i] = H[i, i] * b[i] / ( np.matmul(np.square(H[i, :]), np.square(b) ) + int_cell[i])
w[i] = 1 / (1 - f[i] * b[i] * H[i, i] + 1e-12)
vnew = vnew + np.log(w[i])
VV[iter] = vnew
if vnew - vold <= 1e-3:
break
p_opt = np.square(b)
return p_opt
## Calculate SINR, interference, out_prob for given values
def cal_performance(tx_pow, inter_threshold, num_band, ch_val):
SINR_mat = []
out_prob_mat = []
int_CUE_mat = []
for i in range(num_band):
ch_val_band = np.array(ch_val[i], copy=True)
ch_w = np.multiply(tx_pow[i], ch_val_band)
sig = np.array(np.diag(ch_w), copy=True)
int = np.sum(ch_w, 1)
int_2 = np.sum(np.transpose(ch_w), 1)
cap = np.divide(sig, int - sig + N0W)
out_prob = np.mean((int - sig)[-1:] > inter_threshold)
int_CUE = (int - sig)[-1:]
SINR_mat.append(cap)
int_CUE_mat.append(int_CUE)
out_prob_mat.append(out_prob)
return SINR_mat, int_CUE_mat, out_prob_mat
## Calculate SINR of eq given channel pt and num d2d
## Assume that the PU use the maximum transmit power
def sinr_eq(p_t, inter_threshold, num_d2d, num_band, ch_val, rate_thr):
tx_pow = 1.0/num_band * p_t * np.ones((num_band, num_d2d+1))
tx_pow[:,-1:] = p_t
SINR_mat, int_CUE_mat, out_prob_mat = cal_performance(tx_pow, inter_threshold, num_band, ch_val)
return_cap = np.sum(np.log2(1+np.array(SINR_mat)), 0)
return_CUI = np.mean(int_CUE_mat, 0)
return_OUT_prob = np.mean(out_prob_mat, 0)
return_tx_pow = tx_pow
##########################
## FIND whether the QoS constraint is satisfied
##########################
rate_temp = return_cap[:-1]
rate_satisfied = np.array(rate_temp) < rate_thr
return_OUT_DUE = np.mean(rate_satisfied.astype(float))
return return_cap, return_CUI, return_OUT_prob, return_OUT_DUE, return_tx_pow
## Calculate SINR of eq given channel pt and num d2d
def sinr_dnn(tx_pow, inter_threshold, num_d2d, num_band, ch_val, rate_thr):
SINR_mat, int_CUE_mat, out_prob_mat = cal_performance(tx_pow, inter_threshold, num_band, ch_val)
return_cap = np.sum(np.log2(1 + np.array(SINR_mat)), 0)
return_CUI = np.mean(int_CUE_mat, 0)
return_OUT_prob = np.mean(out_prob_mat, 0)
return_tx_pow = tx_pow
##########################
## FIND whether the QoS constraint is satisfied
##########################
rate_temp = return_cap[:-1]
rate_satisfied = np.array(rate_temp) < rate_thr
return_OUT_DUE = np.mean(rate_satisfied.astype(float))
return return_cap, return_CUI, return_OUT_prob, return_OUT_DUE, return_tx_pow
## Calculate SINR of eq given channel pt and num d2d
def sinr_dnn_infeasible(tx_pow, inter_threshold, num_d2d, num_band, ch_val, rate_thr):
SINR_mat, int_CUE_mat, out_prob_mat = cal_performance(tx_pow, inter_threshold, num_band, ch_val)
return_cap = np.sum(np.log2(1 + np.array(SINR_mat)), 0)
return_CUI = int_CUE_mat
return_OUT_prob = np.mean(out_prob_mat, 0)
return_tx_pow = tx_pow
##########################
## FIND whether the QoS constraint is satisfied
##########################
rate_temp = return_cap[:-1]
rate_satisfied = np.array(rate_temp) < rate_thr
return_OUT_DUE = np.mean(rate_satisfied.astype(float))
return return_cap, return_CUI, return_OUT_prob, return_OUT_DUE, return_tx_pow
def sinr_conv_opt(p_t, inter_threshold, num_d2d, num_band, ch_val):
tx_pow = 1.0 / num_band * p_t * np.ones((num_band, num_d2d + 1))
tx_pow[:, -1:] = p_t
rate_cur = -1
print_power = 0
for i_1 in range(granu):
for i_2 in range(granu):
for i_3 in range(granu):
for i_4 in range(granu):
# tx power of first
tx_pow[0, 0] = p_t * i_1/(granu-1)
tx_pow[0, 1] = p_t * i_2/(granu-1)
tx_pow[1, 0] = p_t * i_3/(granu-1)
tx_pow[1, 1] = p_t * i_4/(granu-1)
if np.any(tx_pow.sum(axis=0)[:-1] > p_t) == False:
SINR_mat_temp, int_CUE_mat_temp, out_prob_mat_temp = cal_performance(tx_pow, inter_threshold, num_band, ch_val)
#print(" ")
#print('int = ', np.log10(int_CUE_mat_temp))
#print('rate = ', np.log2(1+np.array(SINR_mat_temp)))
#print('threshodl = ', np.array(int_CUE_mat_temp)>inter_threshold)
if np.any(np.array(int_CUE_mat_temp)>inter_threshold) == False:
rate_temp = np.sum(np.log2(1 + np.array(SINR_mat_temp)[:,:-1]))
if rate_temp > rate_cur:
rate_cur = np.array(rate_temp, copy=True)
SINR_mat = np.array(SINR_mat_temp, copy=True)
int_CUE_mat = np.array(int_CUE_mat_temp, copy=True)
out_prob_mat = np.array(out_prob_mat_temp, copy=True)
print_power = np.array(tx_pow, copy=True)
return np.sum(np.log2(1 + np.array(SINR_mat)), 0), np.mean(int_CUE_mat, 0), np.mean(out_prob_mat, 0), print_power
def sinr_conv_opt_ee(p_t, inter_threshold, num_d2d, num_band, ch_val):
tx_pow = 1.0 / num_band * p_t * np.ones((num_band, num_d2d + 1))
tx_pow[:, -1:] = p_t
ee_cur = -1
print_power = 0
for i_1 in range(granu):
for i_2 in range(granu):
for i_3 in range(granu):
for i_4 in range(granu):
# tx power of first
tx_pow[0, 0] = p_t * i_1/(granu-1)
tx_pow[0, 1] = p_t * i_2/(granu-1)
tx_pow[1, 0] = p_t * i_3/(granu-1)
tx_pow[1, 1] = p_t * i_4/(granu-1)
if np.any(tx_pow.sum(axis=0)[:-1] > p_t) == False:
SINR_mat_temp, int_CUE_mat_temp, out_prob_mat_temp = cal_performance(tx_pow, inter_threshold, num_band, ch_val)
if np.any(np.array(int_CUE_mat_temp)>inter_threshold) == False:
ee_temp = np.mean(np.divide(np.sum(np.log2(1 + np.array(SINR_mat_temp)[:, :-1]), 0),
(np.sum(tx_pow, 0)[:-1] + p_c)))
if ee_temp > ee_cur:
ee_cur = np.array(ee_temp, copy=True)
SINR_mat = np.array(SINR_mat_temp, copy=True)
int_CUE_mat = np.array(int_CUE_mat_temp, copy=True)
out_prob_mat = np.array(out_prob_mat_temp, copy=True)
print_power = np.array(tx_pow, copy=True)
return np.sum(np.log2(1 + np.array(SINR_mat)), 0), np.mean(int_CUE_mat, 0), np.mean(out_prob_mat, 0), print_power
#################################################################################
### Aggretate all conventional schemes - Rate maximize, EE maximize, TX minimize
#################################################################################
def sinr_conv_opt_all(p_t, inter_threshold, num_d2d, num_band, ch_val, rate_thr):
### Initialize tx power
tx_pow = 1.0 / num_band * p_t * np.ones((num_band, num_d2d + 1))
tx_pow[:, -1:] = p_t
## Initial value of tx power, rate, ee
tx_pow_cur = 100000000
rate_cur = -1
ee_cur = -1
feasible_result = 0
###################################
### Initialization for returned valules
##################################
SINR_rate = np.zeros((2, 3))
int_CUE_rate = np.zeros((2, 1))
out_prob_rate = np.zeros((1, 1))
tx_power_rate = np.zeros((2, 3))
out_DUE_rate = np.zeros((1, 1))
SINR_ee = np.zeros((2, 3))
int_CUE_ee = np.zeros((2, 1))
out_prob_ee = np.zeros((1, 1))
tx_power_ee = np.zeros((2, 3))
out_DUE_ee = np.zeros((1, 1))
SINR_tx = np.zeros((2, 3))
int_CUE_tx = np.zeros((2, 1))
out_prob_tx = np.zeros((1, 1))
tx_power_tx = np.zeros((2, 3))
out_DUE_tx = np.zeros((1, 1))
for i_1 in range(granu):
for i_2 in range(granu):
for i_3 in range(granu):
for i_4 in range(granu):
# tx power of first
tx_pow[0, 0] = p_t * i_1/(granu-1)
tx_pow[0, 1] = p_t * i_2/(granu-1)
tx_pow[1, 0] = p_t * i_3/(granu-1)
tx_pow[1, 1] = p_t * i_4/(granu-1)
if np.any(tx_pow.sum(axis=0)[:-1] > p_t) == False:
SINR_mat_temp, int_CUE_mat_temp, out_prob_mat_temp = cal_performance(tx_pow, inter_threshold, num_band, ch_val)
if np.any(np.array(int_CUE_mat_temp)>inter_threshold) == False:
rate_temp = np.sum(np.log2(1 + np.array(SINR_mat_temp)), 0)[:-1]
if np.all(rate_temp>rate_thr) == True:
ee_temp = np.mean(np.divide(np.sum(np.log2(1 + np.array(SINR_mat_temp)[:, :-1]), 0),
(np.sum(tx_pow, 0)[:-1] + p_c)))
tx_pow_temp = tx_pow[0, 0] + tx_pow[0, 1] + tx_pow[1, 0] + tx_pow[1, 1]
feasible_result = 1
###########################
### Maximize of RATE ######
###########################
if np.sum(rate_temp) > rate_cur:
rate_cur = np.array(np.sum(rate_temp), copy=True)
SINR_rate = np.array(SINR_mat_temp, copy=True)
int_CUE_rate = np.array(int_CUE_mat_temp, copy=True)
out_prob_rate = np.array(out_prob_mat_temp, copy=True)
tx_power_rate = np.array(tx_pow, copy=True)
out_DUE_rate = np.zeros((1, 1))
###########################
### Maximize of EE ######
###########################
if ee_temp > ee_cur:
ee_cur = np.array(ee_temp, copy=True)
SINR_ee = np.array(SINR_mat_temp, copy=True)
int_CUE_ee = np.array(int_CUE_mat_temp, copy=True)
out_prob_ee = np.array(out_prob_mat_temp, copy=True)
tx_power_ee = np.array(tx_pow, copy=True)
out_DUE_ee = np.zeros((1, 1))
###########################
### Tx minimize ######
###########################
if tx_pow_cur > tx_pow_temp:
tx_pow_cur = np.array(tx_pow_temp, copy=True)
SINR_tx = np.array(SINR_mat_temp, copy=True)
int_CUE_tx = np.array(int_CUE_mat_temp, copy=True)
out_prob_tx = np.array(out_prob_mat_temp, copy=True)
tx_power_tx = np.array(tx_pow, copy=True)
out_DUE_tx = np.zeros((1, 1))
########
## Initialize return value
########
return_val_rate = []
return_val_ee = []
return_val_tx = []
return_val_tot = []
return_val_rate.append(np.sum(np.log2(1 + np.array(SINR_rate)), 0))
return_val_ee.append(np.sum(np.log2(1 + np.array(SINR_ee)), 0))
return_val_tx.append(np.sum(np.log2(1 + np.array(SINR_tx)), 0))
return_val_rate.append(np.mean(int_CUE_rate, 0))
return_val_ee.append(np.mean(int_CUE_ee, 0))
return_val_tx.append(np.mean(int_CUE_tx, 0))
return_val_rate.append(np.mean(out_prob_rate, 0))
return_val_ee.append(np.mean(out_prob_ee, 0))
return_val_tx.append(np.mean(out_prob_tx, 0))
return_val_rate.append(np.mean(out_DUE_rate, 0))
return_val_ee.append(np.mean(out_DUE_ee, 0))
return_val_tx.append(np.mean(out_DUE_tx, 0))
return_val_rate.append(tx_power_rate)
return_val_ee.append(tx_power_ee)
return_val_tx.append(tx_power_tx)
return_val_tot.append(return_val_rate)
return_val_tot.append(return_val_ee)
return_val_tot.append(return_val_tx)
return return_val_tot, feasible_result
'''
Building DNN model
'''
def model(X, w1, w2, w3, w4, w5, w1_1, w2_1, w3_1, w4_1, w5_1, wo, wp, b1, b2, b3, b4, b5, b1_1, b2_1, b3_1, b4_1, b5_1,bo, bp, p_keep_conv, num_d2d, num_band):
# Determines
l1 = tf.nn.relu(tf.matmul(X, w1) + b1)
l1 = tf.nn.dropout(l1, p_keep_conv)
l2 = tf.nn.relu(tf.matmul(l1, w2) + b2)
l2 = tf.nn.dropout(l2, p_keep_conv)
l3 = tf.nn.relu(tf.matmul(l2, w3) + b3)
l3 = tf.nn.dropout(l3, p_keep_conv)
l4 = tf.nn.relu(tf.matmul(l3, w4) + b4)
l4 = tf.nn.dropout(l4, p_keep_conv)
l5 = tf.nn.relu(tf.matmul(l4, w5) + b5)
l5 = tf.nn.dropout(l5, p_keep_conv)
## lra represents the resource allocation for each band
lra = tf.matmul(l5, wo) + bo
lra = tf.nn.dropout(lra, p_keep_conv)
l1_1 = tf.nn.relu(tf.matmul(X, w1_1) + b1_1)
l1_1 = tf.nn.dropout(l1_1, p_keep_conv)
l2_1 = tf.nn.relu(tf.matmul(l1_1, w2_1) + b2_1)
l2_1 = tf.nn.dropout(l2_1, p_keep_conv)
l3_1 = tf.nn.relu(tf.matmul(l2_1, w3_1) + b3_1)
l3_1 = tf.nn.dropout(l3_1, p_keep_conv)
l4_1 = tf.nn.relu(tf.matmul(l3_1, w4_1) + b4_1)
l4_1 = tf.nn.dropout(l4_1, p_keep_conv)
l5_1 = tf.nn.relu(tf.matmul(l4_1, w5_1) + b5_1)
l5_1 = tf.nn.dropout(l5_1, p_keep_conv)
## lp represents the transmit power of users
lp = tf.matmul(l5_1, wp) + bp
lp = tf.nn.dropout(lp, p_keep_conv)
## Calculate the resource allocation for each users
lo_resource_alloc = tf.reshape(lra, [-1, num_d2d, num_band])
#lo_resource_arg = tf.argmax(lo_resource_alloc, 2)
#lo_temp = tf.one_hot(lo_resource_arg, depth=num_band)
lo_temp = tf.nn.softmax(lo_resource_alloc)
## Calculate the resource allocation for each users
lo_power_alloc = tf.reshape(lp, [-1, num_d2d, 1])
pyx = tf.multiply(lo_temp , tf.nn.sigmoid(lo_power_alloc))
return pyx
'''
Initialization of location information
'''
def loc_init(size_area, d2d_dist, num_d2d):
rx_loc = size_area * (np.random.rand(num_d2d + 1, 2) - 0.5)
tx_loc = np.zeros((num_d2d + 1, 2))
for i in range(num_d2d+1):
temp_dist = d2d_dist * (np.random.rand(1, 2) - 0.5)
temp_chan = rx_loc[i, :] + temp_dist
while (np.max(abs(temp_chan)) > size_area / 2) | (np.linalg.norm(temp_dist) > d2d_dist):
temp_dist = d2d_dist * (np.random.rand(1, 2) - 0.5)
temp_chan = rx_loc[i, :] + temp_dist
tx_loc[i, :] = temp_chan
return rx_loc, tx_loc
'''
For the returned matrix, pu_ch_gain[0, : ] indicates the channel of RX 1
'''
def ch_gen(size_area, d2d_dist, num_d2d, num_samples, num_band):
ch_w_fading = []
for i in range(num_samples):
rx_loc, tx_loc = loc_init(size_area, d2d_dist, num_d2d)
ch_w_temp_band = []
for j in range(num_band):
rx_loc_temp = size_area * (np.random.rand(1, 2) - 0.5)
temp_dist = d2d_dist * (np.random.rand(1, 2) - 0.5)
temp_chan = rx_loc_temp + temp_dist
while (np.max(abs(temp_chan)) > size_area / 2) | (np.linalg.norm(temp_dist) > d2d_dist):
temp_dist = d2d_dist * (np.random.rand(1, 2) - 0.5)
temp_chan = rx_loc_temp + temp_dist
rx_loc[num_d2d, :] = rx_loc_temp
tx_loc[num_d2d, :] = temp_chan
## generate distance_vector
dist_vec = rx_loc.reshape(num_d2d + 1, 1, 2) - tx_loc
dist_vec = np.linalg.norm(dist_vec, axis=2)
dist_vec = np.maximum(dist_vec, 1)
# find path loss // shadowing is not considered
pu_ch_gain_db = - pl_const - pl_alpha * np.log10(dist_vec)
pu_ch_gain = 10 ** (pu_ch_gain_db / 10)
multi_fading = 0.5 * np.random.randn(num_d2d+1, num_d2d+1) ** 2 + 0.5 * np.random.randn(num_d2d+1, num_d2d+1) ** 2
final_ch = np.maximum(pu_ch_gain * multi_fading, np.exp(-30))
ch_w_temp_band.append(final_ch)
ch_w_fading.append(ch_w_temp_band)
return np.array(ch_w_fading)
def ch_gen_test(size_area, d2d_dist, num_d2d, num_samples, num_band):
ch_w_fading = []
for i in range(num_samples):
rx_loc, tx_loc = loc_init(size_area, d2d_dist, num_d2d)
ch_w_temp_band = []
for j in range(num_band):
rx_loc_temp = size_area * (np.random.rand(1, 2) - 0.5)
temp_dist = d2d_dist * (np.random.rand(1, 2) - 0.5)
temp_chan = rx_loc_temp + temp_dist
while (np.max(abs(temp_chan)) > size_area / 2) | (np.linalg.norm(temp_dist) > d2d_dist):
temp_dist = d2d_dist * (np.random.rand(1, 2) - 0.5)
temp_chan = rx_loc_temp + temp_dist
rx_loc[num_d2d, :] = rx_loc_temp
tx_loc[num_d2d, :] = temp_chan
## generate distance_vector
dist_vec = rx_loc.reshape(num_d2d + 1, 1, 2) - tx_loc
dist_vec = np.linalg.norm(dist_vec, axis=2)
dist_vec = np.maximum(dist_vec, 1)
# find path loss // shadowing is not considered
pu_ch_gain_db = - pl_const_test - pl_alpha_test * np.log10(dist_vec)
pu_ch_gain = 10 ** (pu_ch_gain_db / 10)
multi_fading = 0.5 * np.random.randn(num_d2d+1, num_d2d+1) ** 2 + 0.5 * np.random.randn(num_d2d+1, num_d2d+1) ** 2
final_ch = np.maximum(pu_ch_gain * multi_fading, np.exp(-30))
ch_w_temp_band.append(final_ch)
ch_w_fading.append(ch_w_temp_band)
return np.array(ch_w_fading)
'''
For the returned matrix, pu_ch_gain[0, : ] indicates the channel of RX 1
'''
def per_eval(batch_size, inter_threshold, num_band, rate_thr, learning_rate_init = 0.00001, target=0):
cap_mat_te_DUE = np.zeros((5, 1))
cap_mat_tr_DUE = np.zeros((5, 1))
cap_mat_eq_DUE = np.zeros((5, 1))
cap_mat_inter_DUE = np.zeros((5, 1))
cap_mat_opt_rate_DUE = np.zeros((5, 1))
cap_mat_opt_ee_DUE = np.zeros((5, 1))
cap_mat_opt_tx_DUE = np.zeros((5, 1))
cap_mat_te_CUE = np.zeros((5, 1))
cap_mat_tr_CUE = np.zeros((5, 1))
cap_mat_eq_CUE = np.zeros((5, 1))
cap_mat_inter_CUE = np.zeros((5, 1))
cap_mat_opt_rate_CUE = np.zeros((5, 1))
cap_mat_opt_ee_CUE = np.zeros((5, 1))
cap_mat_opt_tx_CUE = np.zeros((5, 1))
out_mat_te = np.zeros((5, 1))
out_mat_tr = np.zeros((5, 1))
out_mat_eq = np.zeros((5, 1))
out_mat_inter = np.zeros((5, 1))
out_mat_opt_rate = np.zeros((5, 1))
out_mat_opt_ee = np.zeros((5, 1))
out_mat_opt_tx = np.zeros((5, 1))
out_DUE_mat_te = np.zeros((5, 1))
out_DUE_mat_tr = np.zeros((5, 1))
out_DUE_mat_eq = np.zeros((5, 1))
out_DUE_mat_inter = np.zeros((5, 1))
out_DUE_mat_opt_rate = np.zeros((5, 1))
out_DUE_mat_opt_ee = np.zeros((5, 1))
out_DUE_mat_opt_tx = np.zeros((5, 1))
inter_mat_te = np.zeros((5, 1)) + 1e-50
inter_mat_tr = np.zeros((5, 1)) + 1e-50
inter_mat_eq = np.zeros((5, 1)) + 1e-50
inter_mat_inter = np.zeros((5, 1)) + 1e-50
inter_mat_opt_rate = np.zeros((5, 1)) + 1e-50
inter_mat_opt_ee = np.zeros((5, 1)) + 1e-50
inter_mat_opt_tx = np.zeros((5, 1)) + 1e-50
ee_mat_te = np.zeros((5, 1))
ee_mat_tr = np.zeros((5, 1))
ee_mat_eq = np.zeros((5, 1))
ee_mat_inter = np.zeros((5, 1))
ee_mat_opt_rate = np.zeros((5, 1))
ee_mat_opt_ee = np.zeros((5, 1))
ee_mat_opt_tx = np.zeros((5, 1))
tx_mat_te = np.zeros((5, 1))
tx_mat_tr = np.zeros((5, 1))
tx_mat_eq = np.zeros((5, 1))
tx_mat_inter = np.zeros((5, 1))
tx_mat_opt_rate = np.zeros((5, 1))
tx_mat_opt_ee = np.zeros((5, 1))
tx_mat_opt_tx = np.zeros((5, 1))
saver = tf.train.Saver()
lam_1_val_mat = [1e3, 0.9, 0.92, 0.95, 0.99]
lam_2_val_mat = [1e3, 0.9, 0.92, 0.95, 0.99]
for k in range(5):
print("iteration: ", k)
size_area = 30.0+10*k
lam_1_val = gamma_1
lam_2_val = gamma_2
learning_rate = learning_rate_init
for l in range(iter_num):
cap_te_DUE, cap_tr_DUE, cap_eq_DUE, cap_inter_DUE, cap_opt_rate_DUE, cap_opt_ee_DUE, cap_opt_tx_DUE = 0, 0, 0, 0, 0, 0, 0
cap_te_CUE, cap_tr_CUE, cap_eq_CUE, cap_inter_CUE, cap_opt_rate_CUE, cap_opt_ee_CUE, cap_opt_tx_CUE = 0, 0, 0, 0, 0, 0, 0
out_te, out_tr, out_eq, out_inter, out_opt_rate, out_opt_ee, out_opt_tx = 0, 0, 0, 0, 0, 0, 0
out_DUE_te, out_DUE_tr, out_DUE_eq, out_DUE_inter, out_DUE_opt_rate, out_DUE_opt_ee, out_DUE_opt_tx = 0, 0, 0, 0, 0, 0, 0
inter_te, inter_tr, inter_eq, inter_inter, inter_opt_rate, inter_opt_ee, inter_opt_tx = 1e-50, 1e-50, 1e-50, 1e-50, 1e-50, 1e-50, 1e-50
tx_te, tx_tr, tx_eq, tx_inter, tx_opt_rate, tx_opt_ee, tx_opt_tx = 0, 0, 0, 0, 0, 0, 0
ee_te, ee_tr, ee_eq, ee_inter, ee_opt_rate, ee_opt_ee, ee_opt_tx = 0, 0, 0, 0, 0, 0, 0
power_1, power_2, power_3 = 0, 0, 0
#### Reltaed to channel sample
## Generating channel values
ch_val_tot_train = ch_gen(size_area, d2d_dist, num_d2d, num_samples, num_band)
ch_val_tot_test = ch_gen_test(size_area, d2d_dist, num_d2d, test_size, num_band)
ch_val_tot = np.concatenate((ch_val_tot_train, ch_val_tot_test))
_sample_ch = np.log10(ch_val_tot)
avg_val = np.mean(_sample_ch)
std_val = np.sqrt(np.var(_sample_ch))
_sample_ch = (_sample_ch - avg_val) / std_val
sample_ch = np.array(_sample_ch, copy=True)
ch_val = sample_ch[:num_samples]
ch_val_test = sample_ch[num_samples:]
## Calculate the
ch_val_diag = []
for j_2 in range(len(ch_val)):
ch_val_diag_band = []
for j_3 in range(num_band):
sig_diag = np.array(np.diag(ch_val[j_2, j_3]), copy=True)
ch_val_diag_band.append(sig_diag)
ch_val_diag.append(ch_val_diag_band)
ch_val_diag = np.array(ch_val_diag)
ch_val_test_diag = []
for j_2 in range(len(ch_val_test)):
ch_val_diag_band = []
for j_3 in range(num_band):
sig_test_diag = np.array(np.diag(ch_val_test[j_2, j_3]), copy=True)
ch_val_diag_band.append(sig_test_diag)
ch_val_test_diag.append(ch_val_diag_band)
ch_val_test_diag = np.array(ch_val_test_diag)
with tf.Session() as sess:
tf.initialize_all_variables().run()
ref_opt_rate_rate = 0
ref_opt_rate_ee = 0
ref_opt_rate_out = 0
ref_opt_rate_out_DUE = 0
ref_opt_rate_CUI = 1e-20
ref_opt_rate_tx = 1e-20
ref_opt_ee_rate = 0
ref_opt_ee_ee = 0
ref_opt_ee_out = 0
ref_opt_ee_out_DUE = 0
ref_opt_ee_CUI = 1e-20
ref_opt_ee_tx = 1e-20
ref_opt_tx_rate = 0
ref_opt_tx_ee = 0
ref_opt_tx_out = 0
ref_opt_tx_out_DUE = 0
ref_opt_tx_CUI = 1e-20
ref_opt_tx_tx = 1e-20
ref_eq_rate = 0
ref_eq_ee = 0
ref_eq_out = 0
ref_eq_out_DUE = 0
ref_eq_CUI = 1e-20
ref_eq_tx = 1e-20
ref_inter_rate = 0
ref_inter_ee = 0
ref_inter_out = 0
ref_inter_out_DUE = 0
ref_inter_CUI = 1e-20
ref_inter_tx = 1e-20
#############################
## If Reuse is 0, reuse the saved model for DNN. NO TRAINING
## If Reuse is 1, JUST INFERENCE
## Test_size is the number of samples to be examined in the training for comparison
#############################
if reuse == 0:
test_size_init = test_size
test_size_init = -1
else:
test_size_init = -1
##############
## correct_ch_num is the number of feasible channel vaules
correct_ch_num = 0
for j_2 in range(test_size_init):
if j_2%50 == 0:
print("Test phase = ", j_2)
##############
## Test for opt rate
###############
conv_result, feasible_check = sinr_conv_opt_all(p_t, inter_threshold, num_d2d, num_band,
10 ** (avg_val + std_val * ch_val[j_2]), rate_thr)
temp_cap_opt_rate = conv_result[0][0]
temp_CUI_opt_rate = conv_result[0][1]
temp_OUT_prob_opt_rate = conv_result[0][2]
temp_OUT_DUE_opt_rate = conv_result[0][3]
temp_tx_pow_opt_rate = conv_result[0][4]
ref_opt_rate_rate = ref_opt_rate_rate + np.mean(temp_cap_opt_rate[:-1])
ref_opt_rate_ee = ref_opt_rate_ee + np.mean(np.divide(temp_cap_opt_rate[:-1], (np.sum(temp_tx_pow_opt_rate, 0)[:-1] + p_c) ))
ref_opt_rate_out = ref_opt_rate_out + temp_OUT_prob_opt_rate
ref_opt_rate_out_DUE = ref_opt_rate_out_DUE + temp_OUT_DUE_opt_rate
ref_opt_rate_CUI = ref_opt_rate_CUI + temp_CUI_opt_rate
ref_opt_rate_tx = ref_opt_rate_tx + np.sum(np.sum(temp_tx_pow_opt_rate, 0)[:-1]) / num_d2d
#############################
## correct_ch_num holds the number of channel samples which are feasible
##############################
correct_ch_num = correct_ch_num + feasible_check
##############
## Test for opt ee
###############3
temp_cap_opt_ee = conv_result[1][0]
temp_CUI_opt_ee = conv_result[1][1]
temp_OUT_prob_opt_ee = conv_result[1][2]
temp_OUT_DUE_opt_ee = conv_result[1][3]
temp_tx_pow_opt_ee = conv_result[1][4]
ref_opt_ee_rate = ref_opt_ee_rate + np.mean(temp_cap_opt_ee[:-1])
ref_opt_ee_ee = ref_opt_ee_ee + np.mean(np.divide(temp_cap_opt_ee[:-1], (np.sum(temp_tx_pow_opt_ee, 0)[:-1] + p_c) ))
ref_opt_ee_out = ref_opt_ee_out + temp_OUT_prob_opt_ee
ref_opt_ee_out_DUE = ref_opt_ee_out_DUE + temp_OUT_DUE_opt_ee
ref_opt_ee_CUI = ref_opt_ee_CUI + temp_CUI_opt_ee
ref_opt_ee_tx = ref_opt_ee_tx + np.sum(np.sum(temp_tx_pow_opt_ee, 0)[:-1]) / num_d2d
##############
## Test for opt tx
###############3
temp_cap_opt_tx = conv_result[2][0]
temp_CUI_opt_tx = conv_result[2][1]
temp_OUT_prob_opt_tx = conv_result[2][2]
temp_OUT_DUE_opt_tx = conv_result[2][3]
temp_tx_pow_opt_tx = conv_result[2][4]
ref_opt_tx_rate = ref_opt_tx_rate + np.mean(temp_cap_opt_tx[:-1])
ref_opt_tx_ee = ref_opt_tx_ee + np.mean(np.divide(temp_cap_opt_tx[:-1], (np.sum(temp_tx_pow_opt_tx, 0)[:-1] + p_c) ))
ref_opt_tx_out = ref_opt_tx_out + temp_OUT_prob_opt_tx
ref_opt_tx_out_DUE = ref_opt_tx_out_DUE + temp_OUT_DUE_opt_tx
ref_opt_tx_CUI = ref_opt_tx_CUI + temp_CUI_opt_tx
ref_opt_tx_tx = ref_opt_tx_tx + np.sum(np.sum(temp_tx_pow_opt_tx, 0)[:-1]) / num_d2d
##############
## Test for EQ
###############3
cap_eq, CUI_eq, OUT_prob_eq, OUT_DUE_eq, tx_pow_eq = sinr_eq(p_t, inter_threshold, num_d2d, num_band, 10 ** (avg_val + std_val * ch_val[j_2]), rate_thr)
ref_eq_rate = ref_eq_rate + np.mean(cap_eq[:-1])/ test_size
ref_eq_ee = ref_eq_ee + np.mean(np.divide(cap_eq[:-1], (np.sum(tx_pow_eq, 0)[:-1] + p_c)))/ test_size
ref_eq_out = ref_eq_out + OUT_prob_eq/ test_size
ref_eq_out_DUE = ref_eq_out_DUE + OUT_DUE_eq / test_size
ref_eq_CUI = ref_eq_CUI + CUI_eq/ test_size
ref_eq_tx = ref_eq_tx + np.sum(np.sum(tx_pow_eq, 0)[:-1]) / num_d2d
correct_ch_num = np.maximum(correct_ch_num, 1)
print("feasible percentageg = %0.0f" %(correct_ch_num*1.0/test_size*100))
ref_opt_rate_rate = ref_opt_rate_rate / correct_ch_num
ref_opt_rate_out = ref_opt_rate_out / correct_ch_num
ref_opt_rate_out_DUE = ref_opt_rate_out_DUE / correct_ch_num
ref_opt_rate_CUI = ref_opt_rate_CUI / correct_ch_num
ref_opt_rate_tx = ref_opt_rate_tx / correct_ch_num
ref_opt_ee_rate = ref_opt_ee_rate / correct_ch_num
ref_opt_ee_out = ref_opt_ee_out / correct_ch_num
ref_opt_ee_out_DUE = ref_opt_ee_out_DUE / correct_ch_num
ref_opt_ee_CUI = ref_opt_ee_CUI / correct_ch_num
ref_opt_ee_tx = ref_opt_ee_tx / correct_ch_num
ref_opt_tx_rate = ref_opt_tx_rate / correct_ch_num
ref_opt_tx_out = ref_opt_tx_out / correct_ch_num
ref_opt_tx_out_DUE = ref_opt_tx_out_DUE / correct_ch_num
ref_opt_tx_CUI = ref_opt_tx_CUI / correct_ch_num
ref_opt_tx_tx = ref_opt_tx_tx / correct_ch_num
##########################################
### If reuse == 0, restore the saved model
##########################################
if reuse != 0:
tot_epoch = -1
else:
tot_epoch = tot_epoch_real
##########################################
### If target = 0 => RATE MAXIMIZATION
### If target = 1 => EE MAXIMIZATION
### If target = 2 => TX MINIMIZATION
##########################################
if target == 0:
opt_target = train_op_rate
cost_target = cost_rate
elif target == 1:
opt_target = train_op_ee
cost_target = cost_ee
else:
opt_target = train_op_tx
cost_target = cost_tx
## Initialize the previousr results for EE
a_prev, b_prev, c_prev, d_prev, e_prev, f_prev, g_prev = 0, 0, 0, 0, 0, 0, 0
for i in range(tot_epoch):
if i%200 == 0:
lam_1_val = lam_1_val*2
lam_2_val = lam_2_val*2
learning_rate = learning_rate / 1.5
print("update gamma = ", lam_1_val)
rand_perm = np.random.permutation(len(ch_val))
ch_val[:] = ch_val[rand_perm]
ch_val_diag[:] = ch_val_diag[rand_perm]
for start, end in zip(range(0, len(ch_val), batch_size), range(batch_size, len(ch_val), batch_size)):
feed_vec = ch_val[start:end]
feed_diag = ch_val_diag[start:end]
feed_vec = feed_vec.reshape(-1, (num_d2d+1)**2*num_band)
#################################
### Important part - Training
################################
sess.run(opt_target,
feed_dict={X: feed_vec, X2:ch_val[start:end], S_Diag: feed_diag, p_keep_conv: 1.0, avg_val_dnn: avg_val,
std_val_dnn: std_val, lr: learning_rate, lambda_1_dnn:lam_1_val, lambda_2_dnn:lam_2_val})
if i%100 == 0:
feed_vec = ch_val[:batch_size]
feed_vec = feed_vec.reshape(-1, (num_d2d+1)**2*num_band)
feed_diag = ch_val_diag[:batch_size]
######################
## a : Cost (train)
## b : Rate (train)
## c : Interference (train)
## d : EE (train)
## e: TX power (train)
## f: CUE constraint (train)
## g: DUE constriant (train)
######################
a = -sess.run(cost_target,
feed_dict={X: feed_vec, X2:ch_val[:batch_size], S_Diag: feed_diag, p_keep_conv: 1.0, avg_val_dnn: avg_val,
std_val_dnn: std_val, lambda_1_dnn:lam_1_val, lambda_2_dnn:lam_2_val})
b = -sess.run(tf_rate,
feed_dict={X: feed_vec, X2:ch_val[:batch_size], S_Diag: feed_diag, p_keep_conv: 1.0, avg_val_dnn: avg_val,
std_val_dnn: std_val, lambda_1_dnn:lam_1_val})
b = b/np.log(2)
c = sess.run(CUE_inter_plot,
feed_dict={X: feed_vec, X2:ch_val[:batch_size], S_Diag: feed_diag, p_keep_conv: 1.0, avg_val_dnn: avg_val,
std_val_dnn: std_val, lambda_1_dnn:lam_1_val}) + 1e-15
d = -sess.run(tf_ee,
feed_dict={X: feed_vec, X2: ch_val[:batch_size], S_Diag: feed_diag,
p_keep_conv: 1.0, avg_val_dnn: avg_val,
std_val_dnn: std_val, lambda_1_dnn: lam_1_val})
d = d / np.log(2)
e = sess.run(py_x_temp,
feed_dict={X: feed_vec, X2: ch_val[:batch_size], S_Diag: feed_diag,
p_keep_conv: 1.0, avg_val_dnn: avg_val,
std_val_dnn: std_val, lambda_1_dnn: lam_1_val})
e = np.sum(e) / batch_size / num_d2d
f = sess.run(CUE_inter,
feed_dict={X: feed_vec, X2: ch_val[:batch_size], S_Diag: feed_diag,
p_keep_conv: 1.0, avg_val_dnn: avg_val,
std_val_dnn: std_val, lambda_1_dnn: lam_1_val})
f = np.sum((np.array(f) > 0).astype(float)) / batch_size / num_band
g = sess.run(DUE_out,
feed_dict={X: feed_vec, X2: ch_val[:batch_size], S_Diag: feed_diag,
p_keep_conv: 1.0, avg_val_dnn: avg_val,
std_val_dnn: std_val, lambda_1_dnn: lam_1_val})
g = np.sum((np.array(g) > 0).astype(float)) / batch_size / num_d2d
feed_vec_1 = ch_val_test[:batch_size]
feed_vec_1 = feed_vec_1.reshape(-1, (num_d2d+1)**2*num_band)
feed_diag_1 = ch_val_test_diag[:batch_size]
######################
## a_1 : Cost (test)
## b_1 : Rate (test)
## c_1 : Interference (test)
## d_1 : EE (test)
## e_1 : TX power (test)
## f_1 : CUE constraint (test)
## g_1 : DUE constriant (test)
######################
a_1 = -sess.run(cost_target, feed_dict={X: feed_vec_1, X2:ch_val_test[:batch_size], S_Diag: feed_diag_1, p_keep_conv: 1.0,
avg_val_dnn: avg_val, std_val_dnn: std_val, lambda_1_dnn:lam_1_val, lambda_2_dnn:lam_2_val})
b_1 = -sess.run(tf_rate, feed_dict={X: feed_vec_1, X2:ch_val_test[:batch_size],S_Diag: feed_diag_1, p_keep_conv: 1.0,
avg_val_dnn: avg_val, std_val_dnn: std_val, lambda_1_dnn:lam_1_val})
b_1 = b_1 / np.log(2)
c_1 = sess.run(CUE_inter_plot, feed_dict={X: feed_vec_1, X2:ch_val_test[:batch_size],S_Diag: feed_diag_1, p_keep_conv: 1.0,
avg_val_dnn: avg_val, std_val_dnn: std_val, lambda_1_dnn:lam_1_val}) + 1e-15
d_1 = -sess.run(tf_ee,
feed_dict={X: feed_vec_1, X2: ch_val_test[:batch_size], S_Diag: feed_diag_1,
p_keep_conv: 1.0, avg_val_dnn: avg_val,
std_val_dnn: std_val, lambda_1_dnn: lam_1_val})
d_1 = d_1 / np.log(2)
e_1 = sess.run(py_x_temp,
feed_dict={X: feed_vec_1, X2: ch_val_test[:batch_size], S_Diag: feed_diag_1,
p_keep_conv: 1.0, avg_val_dnn: avg_val,
std_val_dnn: std_val, lambda_1_dnn: lam_1_val})
e_1 = np.sum(e_1) / batch_size / num_d2d
f_1 = sess.run(CUE_inter,
feed_dict={X: feed_vec_1, X2: ch_val_test[:batch_size], S_Diag: feed_diag_1,
p_keep_conv: 1.0, avg_val_dnn: avg_val,
std_val_dnn: std_val, lambda_1_dnn: lam_1_val})
f_1 = np.sum((np.array(f_1) > 0).astype(float)) / batch_size / num_band
g_1 = sess.run(DUE_out,
feed_dict={X: feed_vec_1, X2: ch_val_test[:batch_size], S_Diag: feed_diag_1,
p_keep_conv: 1.0, avg_val_dnn: avg_val,
std_val_dnn: std_val, lambda_1_dnn: lam_1_val})
g_1 = np.sum((np.array(g_1) > 0).astype(float)) / batch_size / num_d2d
print("")
print("Second iter: %d" %i)
print("COST: %0.3f, impr = %0.3f in percentage = %0.0f" % (
a, (a - a_prev), (a - a_prev) / a * 100))
print("RATE: %0.3f, EQ = %0.3f, opt(rate) = %0.3f, opt(ee) = %0.3f, opt(tx) = %0.3f" % (
b, ref_eq_rate, ref_opt_rate_rate, ref_opt_ee_rate, ref_opt_tx_rate))
print("EE : %0.3f, EQ = %0.3f, opt(rate) = %0.3f, opt(ee) = %0.3f, opt(tx) = %0.3f" % (
d*1e3, ref_eq_ee*1e3, ref_opt_rate_ee*1e3, ref_opt_ee_ee*1e3, ref_opt_tx_ee*1e3))
print("TX : %0.0f, EQ = %0.0f, opt(rate) = %0.0f, opt(ee) = %0.0f, opt(tx) = %0.0f" % (
e, ref_eq_tx, ref_opt_rate_tx, ref_opt_ee_tx, ref_opt_tx_tx))
print("Inter: %0.3f, EQ = %0.3f, opt(rate) = %0.3f, opt(ee) = %0.3f, opt(tx) = %0.3f" % (
10*np.log10(c), 10*np.log10(ref_eq_CUI),
10*np.log10(ref_opt_rate_CUI), 10*np.log10(ref_opt_ee_CUI), 10*np.log10(ref_opt_tx_CUI)))
print("OUT(CUE): %0.1f, EQ = %0.1f, opt(rate) = %0.1f, opt(ee) = %0.1f, opt(tx) = %0.1f" % (
f*1e2, ref_eq_out*1e2, ref_opt_rate_out*1e2, ref_opt_ee_out*1e2, ref_opt_tx_out*1e2))
print("OUT(DUE): %0.1f, EQ = %0.1f, opt(rate) = %0.1f, opt(ee) = %0.1f, opt(tx) = %0.1f" % (
g*1e2, ref_eq_out_DUE*1e2, ref_opt_rate_out_DUE*1e2, ref_opt_ee_out_DUE*1e2, ref_opt_tx_out_DUE*1e2))
print("<Test results> COST= %0.3f, RATE= %0.3f, EE= %0.3f, Inter= %0.3f, "
"TX = %0.1f, OUT(CUE) = %0.1f, OUT(DUE) = %0.1f"
% (a_1, b_1, d_1*1e3, 10*np.log10(c_1), e_1, f_1*1e2, g_1*1e2))
a_prev = a
b_prev = b
c_prev = c
d_prev = d
e_prev = e
f_prev = f
g_prev = g
print("**" * 40)
if reuse == 0:
save_path = saver.save(sess, "/tmp/model.ckpt")
print("Model saved in path: %s" % save_path)
else:
saver.restore(sess, "/tmp/model.ckpt")
print("Model restored.")
###############################################
#### evaluation phase #######################
###############################################
## For test data set
time_temp = 0
time_index = 0 # Hold the number of iteration for test dataset
diff_pow_val = 0
cap_infea_num = 0
int_infea_num = 0
diff_pow_list = np.zeros(1)
#################################################################
####### Given that the performance of proposed scheme should be examined
####### for individual sample, we first take one batch of channel samples
####### and make iteration for single batch of samples.
#################################################################
for start, end in zip(range(0, len(ch_val_test), batch_size), range(batch_size, len(ch_val_test), batch_size)):
#######################################################
### Determining the transmit power of DNN based scheme
#######################################################
feed_vec = ch_val_test[start:end]
feed_vec = feed_vec.reshape(-1, (num_d2d + 1) ** 2 * num_band)
feed_diag = ch_val_test_diag[start:end]
#######################################################
## pw_temp contains the transmit power of DNN based scheme
#######################################################
pw_temp = sess.run(py_x_t, feed_dict={X: feed_vec, X2:ch_val_test[start:end], S_Diag: feed_diag, p_keep_conv: 1.0,
avg_val_dnn: avg_val, std_val_dnn: std_val})
## Call channel vector again since in the upper code, its shape has been changed
feed_vec_temp = ch_val_test[start:end]
for j in range(batch_size):
time_temp = time_temp + 1
###############################################
## Calculate the performance of optimal scheme
###############################################
conv_result, feasible_check = sinr_conv_opt_all(p_t, inter_threshold, num_d2d, num_band,
10 ** (avg_val + std_val * feed_vec_temp[j]), rate_thr)
###############################################
## Calculate the performance only when it is possible to find feasible solution
###############################################
if feasible_check == 0:
###################################################
## Calculate the performance of RATE MAXIMIZE
###################################################
temp_cap_opt_rate = conv_result[0][0]
temp_CUI_opt_rate = conv_result[0][1]
temp_OUT_prob_opt_rate = conv_result[0][2]
temp_OUT_DUE_opt_rate = conv_result[0][3]
temp_tx_pow_opt_rate = conv_result[0][4]
cap_opt_rate_DUE = cap_opt_rate_DUE + np.mean(temp_cap_opt_rate[:-1])
ee_opt_rate = ee_opt_rate + np.mean(np.divide(temp_cap_opt_rate[:-1], (np.sum(temp_tx_pow_opt_rate, 0)[:-1] + p_c)))
out_opt_rate = out_opt_rate + temp_OUT_prob_opt_rate
out_DUE_opt_rate = out_DUE_opt_rate + temp_OUT_DUE_opt_rate
inter_opt_rate = inter_opt_rate + temp_CUI_opt_rate
tx_opt_rate = tx_opt_rate + np.sum(np.sum(temp_tx_pow_opt_rate, 0)[:-1]) / num_d2d