-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
169 lines (145 loc) · 5.53 KB
/
test.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
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 3 17:15:52 2018
@author: r17baena
"""
import numpy as np
import math as m
import random
from scipy.spatial import distance
filename = "dat.npz"
def spread_users_one_antenna(RRH_pos,Radius,K):#RETURN LOCATION OF USERS
user_pos = np.zeros((K,2))
# magic numbers!
a = m.sqrt(3)
b = a*Radius
x = 2*Radius*(np.random.rand(K,1)-0.5); # X & Y relative to the eNode's center
y = 2* Radius*a*(np.random.rand(K,1)-0.5)
user_pos = np.zeros((K,2))
user_pos[:,0] = x[:,0]
user_pos[:,1] = y[:,0]
R=1
# odd is the distance to the closest antenna
odd = np.zeros((R,K))
for t in range(K):
odd[:,t] = m.sqrt((user_pos[t,0]-RRH_pos[0])**2 +(user_pos[t,1]-RRH_pos[1])**2 )
for t in range(K):
# while user is badly spread (too far or too close), regenerate data
while (abs(user_pos[t,0])*m.sqrt(3)+ abs(user_pos[t,1]) >b ) or ( odd [0,t] < 5):
user_pos[t,0] = 2*Radius*(random.random()-0.5)
user_pos[t,1] = 0.5*Radius*a*(random.random()-0.5)
odd[:,t] = (m.sqrt((user_pos[t,0]-RRH_pos[0])**2 +(user_pos[t,1]-RRH_pos[1])**2 ))
return user_pos
def channel(K,N,N_time): #RETURN CHANNEL STATE in three dimension (K,N,N_time)
#K users,N subcarriers, N_times slot.
R=500 #radius of the cell
H=np.zeros((N_time,K,N)) #H[t,k,n] is the channel gain at time it on subband n for user k
B=10 #
chan_Trms = 500; #Root mean square delay spread du canal en nanoseconde
chan_v = 32 # longueur du canal en durée symbole ??
chan_path = 10 # 10 paths (multipath channel)
RRH_pos=np.array([0,0])
for t in range(N_time):
user_pos = spread_users_one_antenna(RRH_pos,R,K)
for k in range(K):
path = np.sort(np.vstack([np.array([0]), np.floor(np.random.rand(chan_path-1,1)*chan_v)]))
h = (np.random.randn(chan_path,1)+1j*np.random.randn(chan_path,1))*np.exp(-path*(1000/B)/chan_Trms/2)
h = h/np.linalg.norm(h) #normalisation du canal
H[t, k, :] = np.hstack(abs(np.fft.fft(h,n=N,axis=0)))
#Large scalefading depending on the distance user- antenna
d = distance.euclidean(user_pos[k,:],RRH_pos)
path_loss_db=128.1+37.6*m.log10(d/1000)+ np.random.normal(0,8)
path_loss=10**(-path_loss_db/10)
H[t, k, :]= H[t, k, :]*m.sqrt(path_loss)
return H
def PF_scheduler(H,P_total):#ALLOCATION ALGORITHM USE FOR FAIRNESS (LABEL)
#A_t[k,s] timeslot,user,nsubcarrier
#H_T: Channel matrix of size K*S.
#Bc: Bandwidth per subband
#Tk_t: average throughput achieved so far
N_time=np.size(H,0) #nb timeslot
K=np.size(H,1) #nb user
S=np.size(H,2) #nb subcarrier
N0 = 4e-21
Bc=10E6/S
tc=20
P=P_total/S
A=np.zeros((N_time,K,S))#allocation matrice
R_possible=np.zeros((N_time,K,S))#possible data rate
Tk=np.zeros((N_time,K))
pf=np.zeros((N_time,K,S))
R=np.zeros((N_time,K))
R_possible=gains_to_datarate(H,P_total)
for t in range(N_time):
for s in range(S):
for k in range(K):
if float(abs(Tk[t,k]))>np.finfo(float).eps:
pf[t,k,s]=R_possible[t,k,s]/Tk[t,k]
else:
pf[t,k,s]=R_possible[t,k,s]
k_b=np.argmax(pf[t,:,s])#seeking for user who max pf forsubcarrier s
A[t,k_b,s]=1
if t>1:
Tk[t,k_b]=Tk[t-1,k_b]*(1-1/tc)+R_possible[t,k_b,s]/tc
else:
Tk[t,k_b]=R_possible[t,k_b,s]/tc
R[t,k_b]+=R_possible[t,k_b,s]
Tk[t,:]=np.zeros(K)
if t>1:
Tk[t,:]=Tk[t-1,:]*(1-1/tc)
Tk[t,:]=Tk[t,:]+R[t,:]/tc
return A
def gains_to_datarate(H, P_total):#RETURN THE DATA RATE TENSOR(K,N,N_time)
N = H.shape[1]
P = P_total/N
Bc = 1e6 / N #Subbandwidth
N0 = 4e-21
return Bc*np.log2(1+P*np.square(H)/(N0*Bc))#FORMULA OF DATA RATE
def generate_dataset(K, N, N_time, P_total, N_examples):
x = np.zeros((N_examples, K * N * N_time))
y=np.zeros((N_examples, K * N_time*N))#label
r=np.zeros((N_examples,K* N_time*N))
for i in range(N_examples):
H=channel(K, N, N_time)
r[i,:]=gains_to_datarate(H,P_total).reshape(K*N*N_time)
x[i,:] = gains_to_datarate(H, P_total).reshape(K*N*N_time)
y[i,:]=PF_scheduler(H,P_total).reshape(K*N*N_time)
if (i+1) % 1000 == 0:
print(i)
return x,r,y
nb_users = 10
nb_carriers = 10
nb_timeslots = 10
power = 10
n_examples = 10000
n_tests = 1000
###TRAIN DATA SET
x,r,l = generate_dataset(nb_users, nb_carriers, nb_timeslots, power, n_examples)
filename = "data_input.npz"
source_tensor = x,r
np.savez(filename,data=source_tensor)
filename = "data_label.npz"
source_tensor = l
np.savez(filename,data=source_tensor)
###TEST DATA SET
x,r,l = generate_dataset(nb_users, nb_carriers, nb_timeslots, power, n_tests)
filename = "data_input_test.npz"
source_tensor = x,r
np.savez(filename,data=source_tensor)
filename = "data_label_test.npz"
source_tensor = l
np.savez(filename,data=source_tensor)
#
###RETURN THE TRUE DATA RATE WITH ALLOCATION TENSOR AND DATA RATE TENSOR
def Tk_Network_Output(R,A):
N_time=np.size(R,0) #nb timeslot
K=np.size(R,1)
Tk=np.zeros((N_time,K))
tc=20
for t in range(N_time):
A_t=np.transpose(A[t,:,:])
Rtot=np.diagonal(np.matmul(R[t,:,:],A_t))
if t>1:
Tk[t,:]=Tk[t-1,:]*(1-1/tc)
Tk[t,:]=Tk[t,:]+Rtot/tc
return Tk