-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPA_RL.m
303 lines (281 loc) · 9.05 KB
/
PA_RL.m
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simulation of Power Allocation in femtocell network
% using Reinforcement Learning
%
function Q = PA_RL(fbsCount, NumRealization, QTable)
%% Initialization
% clear all;
clc;
format short
format compact
%% Parameters
Pmin = -20; %dBm
Pmax = 25; %dBm
Npower = 31;
dth = 25;
Kp = 100; % penalty constant for MUE capacity threshold
Gmue = 1.37; % bps/Hz
StepSize = 1.5; % dB
K = 1000;
PBS = 50 ; %dBm
sinr_th = 1.64;%10^(2/10); % I am not sure if it is 2 or 20!!!!!
gamma_th = log2(1+sinr_th);
%% Minimum Rate Requirements for N MUE users
N = 3;
q_mue = 2.0; q_fue=2.0;
%% Q-Learning variables
% Actions
actions = zeros(1,31);
for i=1:31
actions(i) = Pmin + (i-1) * 1.5; % dBm
end
% States
states = allcomb(0:3 , 0:3); % states = (dMUE , dBS)
% Q-Table
% Q = zeros(size(states,1) , size(actions , 2));
Q1 = ones(size(states,1) , size(actions , 2)) * inf;
alpha = 0.5; gamma = 0.9; epsilon = 0.1 ; Iterations = 50000;
%% Generate the UEs
mue(1) = UE(204, 207);
% mue(1) = UE(150, 150);
% mue(1) = UE(-200, 0);
% selectedMUE = mue(mueNumber);
MBS = BaseStation(0 , 0 , 50);
%%
%Generate fbsCount=16 FBSs
FBS_Max = cell(1,16);
for i=1:3
% if i<= fbsCount
FBS_Max{i} = FemtoStation(180+(i-1)*35,150, MBS, mue, 10);
% end
end
for i=1:3
% if i+3<= fbsCount
FBS_Max{i+3} = FemtoStation(165+(i-1)*30,180, MBS, mue, 10);
% end
end
for i=1:4
% if i+6<= fbsCount
FBS_Max{i+6} = FemtoStation(150+(i-1)*35,200, MBS, mue, 10);
% end
end
for i=1:3
% if i+10<= fbsCount
FBS_Max{i+10} = FemtoStation(160+(i-1)*35,240, MBS, mue, 10);
% end
end
for i=1:3
% if i+13<= fbsCount
FBS_Max{i+13} = FemtoStation(150+(i-1)*35,280, MBS, mue, 10);
% end
end
%%
%
FBS = cell(1,fbsCount);
if fbsCount>=1, FBS{1} = FBS_Max{1}; end
if fbsCount>=2, FBS{2} = FBS_Max{3}; end
if fbsCount>=3, FBS{3} = FBS_Max{14}; end
if fbsCount>=4, FBS{4} = FBS_Max{16}; end
if fbsCount>=5, FBS{5} = FBS_Max{9}; end
if fbsCount>=6, FBS{6} = FBS_Max{4}; end
if fbsCount>=7, FBS{7} = FBS_Max{2}; end
if fbsCount>=8, FBS{8} = FBS_Max{15}; end
if fbsCount>=9, FBS{9} = FBS_Max{10}; end
if fbsCount>=10, FBS{10} = FBS_Max{12}; end
if fbsCount>=11, FBS{11} = FBS_Max{5}; end
if fbsCount>=12, FBS{12} = FBS_Max{7}; end
if fbsCount>=13, FBS{13} = FBS_Max{11}; end
if fbsCount>=14, FBS{14} = FBS_Max{6}; end
if fbsCount>=15, FBS{15} = FBS_Max{8}; end
if fbsCount>=16, FBS{16} = FBS_Max{13}; end
%% Initialization and find MUE Capacity
% permutedPowers = npermutek(actions,3);
permutedPowers = randperm(size(actions,2),size(FBS,2));
% y=randperm(size(permutedPowers,1));
for j=1:size(FBS,2)
fbs = FBS{j};
fbs = fbs.setPower(actions(permutedPowers(j)));
fbs = fbs.getDistanceStatus;
FBS{j} = fbs;
end
% selectedMUE.SINR = SINR_MUE(FBS, BS, selectedMUE, -120, 1000);
% selectedMUE.C = log2(1+selectedMUE.SINR);
% if selectedMUE.C < gamma_th
% I = 1;
% else
% I = 0;
% end
%
% for j=1:size(FBS,2)
% fbs = FBS{j};
% fbs.state(1,1) = I;
% FBS{j} = fbs;
% end
%% Calc channel coefficients
fbsNum = size(FBS,2);
G = zeros(fbsNum+1, fbsNum+1); % Matrix Containing small scale fading coefficients
L = zeros(fbsNum+1, fbsNum+1); % Matrix Containing large scale fading coefficients
[G, L] = measure_channel(FBS,MBS,mue,NumRealization);
%% Main Loop
fprintf('Loop for %d number of FBS :\t', fbsCount);
textprogressbar(sprintf('calculating outputs:'));
count = 0;
MUE_C = zeros(1,Iterations);
xx = zeros(1,Iterations);
errorVector = zeros(1,Iterations);
% K1 is distance of selectedMUE from Agents
% k1 = zeros(1,size(FBS,2));
dth = 25; %meter
% Kp = 100;
% for i=1:size(FBS,2)
% k1(i) = (sqrt((FBS{i}.X-selectedMUE.X)^2+(FBS{i}.Y-selectedMUE.Y)^2))/dth;
% end
for episode = 1:Iterations
textprogressbar((episode/Iterations)*100);
permutedPowers = randperm(size(actions,2),size(FBS,2));
if (episode/Iterations)*100 < 80
% Action selection with epsilon=0.1
if rand<epsilon
for j=1:size(FBS,2)
fbs = FBS{j};
fbs = fbs.setPower(actions(permutedPowers(j)));
FBS{j} = fbs;
end
else
for j=1:size(FBS,2)
fbs = FBS{j};
for kk = 1:size(states,1)
if states(kk,:) == fbs.state
break;
end
end
[M, index] = max(QTable(kk,:));
fbs = fbs.setPower(actions(index));
FBS{j} = fbs;
end
end
else
for j=1:size(FBS,2)
fbs = FBS{j};
for kk = 1:size(states,1)
if states(kk,:) == fbs.state
break;
end
end
[M, index] = max(QTable(kk,:));
fbs = fbs.setPower(actions(index));
FBS{j} = fbs;
end
end
% calc FUEs and MUEs capacity
SINR_FUE_Vec = SINR_FUE_2(G, L, FBS, MBS, -120);
C_FUE_Vec = log2(1+SINR_FUE_Vec);
for i=1:size(mue,2)
MUE = mue(i);
MUE.SINR = SINR_MUE_4(G, L, FBS, MBS, MUE, -120);
MUE = MUE.setCapacity(log2(1+MUE.SINR));
mue(i)=MUE;
end
% MUE_C(1,episode) = selectedMUE.C;
xx(1,episode) = episode;
% R = K - (selectedMUE.SINR - sinr_th)^2;
% deviation_FUE=0.0;
% for i=1:size(FBS,2)
% deviation_FUE = deviation_FUE + (fbs.C_FUE-q_M)^2;
% end
dum1 = 1.0;
for i=1:size(mue,2)
dum1 = dum1 * (mue(i).C-q_mue)^2;
end
dum2 = 1.0;
minCFUE = inf;
for j=1:size(FBS,2)
fbs = FBS{j};
fbs = fbs.setCapacity(log2(1+SINR_FUE_Vec(j)));
if minCFUE>fbs.C_FUE
minCFUE = fbs.C_FUE;
end
dum2 = dum2 * (fbs.C_FUE-q_fue)^2;
FBS{j}=fbs;
end
% R = K - dum2*dum1;
for j=1:size(FBS,2)
fbs = FBS{j};
qMax=max(QTable,[],2);
for jjj = 1:31
if actions(1,jjj) == fbs.P
break;
end
end
for kk = 1:size(states,1)
if states(kk,:) == fbs.state
break;
end
end
% CALCULATING NEXT STATE AND REWARD
beta = fbs.dMUE/dth;
if (minCFUE>q_fue) && (mue(1).C>q_mue)
R = beta*fbs.C_FUE*mue(1).C;
% R = beta*fbs.C_FUE;
else
R = beta*fbs.C_FUE - (1/beta)*dum1*dum2;
% R = beta*fbs.C_FUE - (1/beta)*dum1;
end
if R<0
R=0;
end
for nextState=1:size(states,1)
if states(nextState,:) == fbs.state
QTable(kk,jjj) = QTable(kk,jjj) + alpha*(R+gamma*qMax(nextState)-QTable(kk,jjj));
end
end
FBS{j}=fbs;
end
% break if convergence: small deviation on q for 1000 consecutive
errorVector(episode) = sum(sum(abs(Q1-QTable)));
if sum(sum(abs(Q1-QTable)))<0.001 && sum(sum(QTable >0))
if count>1000
episode % report last episode
break % for
else
count=count+1; % set counter if deviation of q is small
end
else
Q1=QTable;
count=0; % reset counter when deviation of q from previous q is large
end
% if selectedMUE.C < gamma_th
% I = 1;
% else
% I = 0;
% end
%
% for j=1:size(FBS,2)
% fbs = FBS{j};
% fbs.state(1,1) = I;
% FBS{j} = fbs;
% end
end
Q = QTable;
answer.mue = mue;
answer.Q = QTable;
answer.Error = errorVector;
answer.FBS = FBS;
min_CFUE = inf;
for j=1:size(FBS,2)
C = FBS{1,j}.C_profile;
c_fue(1,j) = sum(C(40000:size(C,2)))/(-40000+size(C,2));
if min_CFUE > c_fue(1,j)
min_CFUE = c_fue(1,j);
end
end
sum_CFUE = 0.0;
for i=1:size(FBS,2)
sum_CFUE = sum_CFUE + c_fue(1,i);
end
answer.C_FUE = c_fue;
answer.sum_CFUE = sum_CFUE;
answer.min_CFUE = min_CFUE;
QFinal = answer;
save(sprintf('final_3/R_share_beta:%d,Real:%d.mat',fbsCount, NumRealization),'QFinal');
end