forked from Coolzyh/Globecom2020-ResourceAllocationGNN
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRL_Train_main.py
282 lines (237 loc) · 11.1 KB
/
RL_Train_main.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
# implement the Training main function
import matplotlib.pyplot as plt
from BS_brain import Agent
from Environment import *
import pickle
import random
import numpy as np
import tensorflow as tf
from Sim_Config import RL_Config
import os
import keras
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0" # use GPU 0 to run this code
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
keras.backend.set_session(sess)
def main():
"""
Train the agent
"""
# number of different trainings settings
train_num = [1, 2, 3, 4, 5,
6, 7, 8, 9]
# number of each D2D's Compression Features
num_feedback_set = [16]
gamma_set = [0.5]
batch_set = [512]
# weight for the V2V sum rate
v2v_weight = 1
# weight for the V2I sum rate
v2i_weight = 0.1
num_train_settings = 1
# start training
for train_loop in range(num_train_settings):
# set the current random seed for training
train_seed_sequence = 1001
random.seed(train_seed_sequence)
np.random.seed(train_seed_sequence)
tf.set_random_seed(train_seed_sequence)
# set values for current simulation
curr_RL_Config = RL_Config()
train_show_tra = '-----Start the Number -- ' + str(train_num[train_loop]) + ' -- training -----!'
print(train_show_tra)
# set key parameters for this train
num_feedback = num_feedback_set[0]
gamma = gamma_set[0]
batch_size = batch_set[0]
curr_RL_Config.set_train_value(num_feedback, gamma, batch_size, v2v_weight, v2i_weight)
# start the Environment
Env = start_env()
# run the training process
[Train_Loss, Reward_Per_Train_Step, Reward_Per_Episode,
Train_Q_mean, Train_Q_max_mean, Orig_Train_Q_mean, Orig_Train_Q_max_mean] \
= run_train(Env, curr_RL_Config)
# save the train results
save_flag = save_train_results(Train_Loss, Reward_Per_Train_Step, Reward_Per_Episode,
Train_Q_mean, Train_Q_max_mean, Orig_Train_Q_mean, Orig_Train_Q_max_mean,
curr_RL_Config, Env)
if save_flag:
print('RL Training is finished!')
def start_env():
# start the environment simulator
"""
Generate the Environment
"""
up_lanes = [3.5/2, 3.5/2 + 3.5, 250+3.5/2, 250+3.5+3.5/2, 500+3.5/2, 500+3.5+3.5/2]
down_lanes = [250-3.5-3.5/2, 250-3.5/2, 500-3.5-3.5/2, 500-3.5/2, 750-3.5-3.5/2, 750-3.5/2]
left_lanes = [3.5/2, 3.5/2 + 3.5, 433+3.5/2, 433+3.5+3.5/2, 866+3.5/2, 866+3.5+3.5/2]
right_lanes = [433-3.5-3.5/2, 433-3.5/2, 866-3.5-3.5/2, 866-3.5/2, 1299-3.5-3.5/2, 1299-3.5/2]
width = 750
height = 1299
Env = Environ(down_lanes, up_lanes, left_lanes, right_lanes, width, height)
Env.new_random_game(Env.n_Veh)
return Env
def run_train(Env, curr_RL_Config):
# run the training process
Num_neighbor = Env.n_Neighbor
Num_d2d = Env.n_Veh
Num_CH = Env.n_RB
Num_D2D_feedback = curr_RL_Config.Num_Feedback
# construct a BS agent
BS_Agent = Agent(Num_d2d, Num_CH, Num_neighbor, Num_D2D_feedback, Env, curr_RL_Config)
Num_Episodes = curr_RL_Config.Num_Episodes
Num_Train_Step = curr_RL_Config.Num_Train_Steps
# get the train loss
[Train_Loss, Reward_Per_Train_Step, Reward_Per_Episode,
Train_Q_mean, Train_Q_max_mean, Orig_Train_Q_mean, Orig_Train_Q_max_mean] \
= BS_Agent.train(Num_Episodes, Num_Train_Step)
return [Train_Loss, Reward_Per_Train_Step, Reward_Per_Episode,
Train_Q_mean, Train_Q_max_mean, Orig_Train_Q_mean, Orig_Train_Q_max_mean]
def save_train_results(Train_Loss, Reward_Per_Train_Step, Reward_Per_Episode,
Train_Q_mean, Train_Q_max_mean, Orig_Train_Q_mean, Orig_Train_Q_max_mean,
curr_rl_config, Env):
# plot and save the training results
# get the current training parameter values from curr_rl_config
Batch_Size = curr_rl_config.Batch_Size
Num_Train_Step = curr_rl_config.Num_Train_Steps
Num_Episodes = curr_rl_config.Num_Episodes
Num_D2D_feedback = curr_rl_config.Num_Feedback
GAMMA = curr_rl_config.Gamma
Num_D2D = Env.n_Veh # number of D2D
V2I_Weight = curr_rl_config.v2i_weight
save_flag = False
# record the Target Q value
Train_Loss_per_Episode = np.zeros((Num_D2D, Num_Episodes))
Train_Q_mean_per_Episode = np.zeros((Num_D2D, Num_Episodes))
Train_Q_max_mean_per_Episode = np.zeros((Num_D2D, Num_Episodes))
# record the original Q Value
Orig_Train_Q_mean_per_Episode = np.zeros((Num_D2D, Num_Episodes))
Orig_Train_Q_max_mean_per_Episode = np.zeros((Num_D2D, Num_Episodes))
# calculate for each D2D
for D_loop in range(Num_D2D):
Train_Loss_per_Episode[D_loop, :] = np.sum(Train_Loss[D_loop, :, :], axis=1) / Num_Train_Step
Train_Q_mean_per_Episode[D_loop, :] = np.sum(Train_Q_mean[D_loop, :, :], axis=1) / Num_Train_Step
Train_Q_max_mean_per_Episode[D_loop, :] = np.sum(Train_Q_max_mean[D_loop, :, :], axis=1) / Num_Train_Step
Orig_Train_Q_mean_per_Episode[D_loop, :] = np.sum(Orig_Train_Q_mean[D_loop, :, :], axis=1) / Num_Train_Step
Orig_Train_Q_max_mean_per_Episode[D_loop, :] = np.sum(Orig_Train_Q_max_mean[D_loop, :, :], axis=1) / Num_Train_Step
# save results in their corresponding simulation parameter settings
curr_sim_set = 'Train-Result' + '-RealFB-' + str(Num_D2D_feedback) + '-Batch-' + str(Batch_Size) \
+ '-Gamma-' + str(GAMMA) \
+ '-V2Iweight-' + str(V2I_Weight)
folder = os.getcwd() + '\\' + curr_sim_set + '\\'
if not os.path.exists(folder):
os.makedirs(folder)
print('Create the new folder in train main ', folder)
curr_Result_Dir = folder
# plot loss and Q values for each D2D
for D_loop in range(Num_D2D):
# plot the training loss
x = range(Num_Episodes)
y = Train_Loss_per_Episode[D_loop, :]
plt.figure()
plt.plot(x, y, color='red', label='RL-DNN Train')
plt.xlabel("Number of Training Episodes")
plt.ylabel("Training Loss")
plt.grid(True)
plt.legend()
Curr_OS = os.name
if Curr_OS == 'nt':
print('Current OS is Windows!')
Fig_Dir = curr_Result_Dir
Fig_Name = 'D2D-' + str(D_loop) + '-th-Train-LOSS-' + '-Episode-' + str(Num_Episodes) + '-Step-' \
+ str(Num_Train_Step) + '-Batch-' + str(Batch_Size) + '.png'
Fig_Para = Fig_Dir + Fig_Name
plt.savefig(Fig_Para, dpi=600)
Fig_Name1 = 'D2D-' + str(D_loop) + '-th-Train-LOSS-' + '-Episode-' + str(Num_Episodes) + '-Step-' \
+ str(Num_Train_Step) + '-Batch-' + str(Batch_Size) + '.eps'
Fig_Para1 = Fig_Dir + Fig_Name1
plt.savefig(Fig_Para1)
# plot the Q mean and Q max mean results --- Target Q function
x = range(Num_Episodes)
y = Train_Q_mean_per_Episode[D_loop, :]
y1 = Train_Q_max_mean_per_Episode[D_loop, :]
plt.figure()
plt.plot(x, y, color='red', label='mean Target Value')
plt.plot(x, y1, color='blue', label='max Target Value')
plt.xlabel("Number of Training Episodes")
plt.ylabel("Q Value")
plt.grid(True)
plt.legend()
# save the figure
Fig_Name = 'D2D-' + str(D_loop) + '-th-Target-Q-Func-plot' + '-Episode-' + str(Num_Episodes) + '-Step-' \
+ str(Num_Train_Step) + '-Batch-' + str(Batch_Size) + '.png'
Fig_Para = Fig_Dir + Fig_Name
plt.savefig(Fig_Para, dpi=600)
Fig_Name1 = 'D2D-' + str(D_loop) + '-th-Target-Q-Func-plot' + '-Episode-' + str(Num_Episodes) + '-Step-' \
+ str(Num_Train_Step) + '-Batch-' + str(Batch_Size) + '.eps'
Fig_Para1 = Fig_Dir + Fig_Name1
plt.savefig(Fig_Para1)
# plot the Q mean and Q max mean results --- Original Q function
x = range(Num_Episodes)
y = Orig_Train_Q_mean_per_Episode[D_loop, :]
y1 = Orig_Train_Q_max_mean_per_Episode[D_loop, :]
# plot the results
plt.figure()
# plt.plot(x, y)
plt.plot(x, y, color='red', label='Q mean')
plt.plot(x, y1, color='blue', label='Q-max mean')
plt.xlabel("Number of Training Episodes")
plt.ylabel("Q Value")
# open the grid
plt.grid(True)
# plt.title("Q Function in Training")
plt.legend()
# save the figure
Fig_Name = 'D2D-' + str(D_loop) + '-th-Original-Q-Func-plot' + '-Episode-' + str(Num_Episodes) + '-Step-' \
+ str(Num_Train_Step) + '-Batch-' + str(Batch_Size) + '.png'
Fig_Para = Fig_Dir + Fig_Name
plt.savefig(Fig_Para, dpi=600)
# use for latex
Fig_Name1 = 'D2D-' + str(D_loop) + '-th-Original-Q-Func-plot' + '-Episode-' + str(Num_Episodes) + '-Step-' \
+ str(Num_Train_Step) + '-Batch-' + str(Batch_Size) + '.eps'
Fig_Para1 = Fig_Dir + Fig_Name1
plt.savefig(Fig_Para1)
# plot the training return
x = range(Num_Episodes)
y = Reward_Per_Episode
plt.figure()
plt.plot(x, y, color='blue', label='DNN-RL')
plt.xlabel("Number of Episodes")
plt.ylabel("Return per Episode")
# open the grid
plt.grid(True)
plt.title("Reward per episode in Training")
plt.legend()
# save the figure
Fig_Name = 'Reward-per-Episode-plot' + '-Episode-' + str(Num_Episodes) + '-Step-' + str(Num_Train_Step) \
+ '-Batch-' + str(Batch_Size) + '.png'
Fig_Para = Fig_Dir + Fig_Name
plt.savefig(Fig_Para, dpi=600)
Fig_Name1 = 'Reward-per-Episode-plot' + '-Episode-' + str(Num_Episodes) + '-Step-' + str(Num_Train_Step) \
+ '-Batch-' + str(Batch_Size) + '.eps'
Fig_Para1 = Fig_Dir + Fig_Name1
plt.savefig(Fig_Para1, dpi=600)
# save the results to file
if Curr_OS == 'nt':
# print('Current OS is Windows!')
Data_Dir = curr_Result_Dir
Data_Name = 'Training-Result' + '-Episode-' + str(Num_Episodes) + '-Step-' + str(Num_Train_Step) \
+ '-Batch-' + str(Batch_Size) + '.pkl'
Data_Para = Data_Dir + Data_Name
# open data file
file_to_open = open(Data_Para, 'wb')
# write D2D_Sample to data file
pickle.dump((Train_Loss_per_Episode, Train_Loss,
Train_Q_mean, Train_Q_max_mean,
Train_Q_mean_per_Episode, Train_Q_max_mean_per_Episode,
# record the original Q Value
Orig_Train_Q_mean, Orig_Train_Q_max_mean,
Orig_Train_Q_mean_per_Episode, Orig_Train_Q_max_mean_per_Episode,
Reward_Per_Train_Step, Reward_Per_Episode), file_to_open)
file_to_open.close()
save_flag = True
return save_flag
if __name__ == '__main__':
main()