-
Notifications
You must be signed in to change notification settings - Fork 66
/
agent.py
369 lines (332 loc) · 18.8 KB
/
agent.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
from __future__ import print_function, division
import os
import time
import random
import numpy as np
from base import BaseModel
from replay_memory import ReplayMemory
from utils import save_pkl, load_pkl
import tensorflow as tf
import matplotlib.pyplot as plt
class Agent(BaseModel):
def __init__(self, config, environment, sess):
self.sess = sess
self.weight_dir = 'weight'
self.env = environment
#self.history = History(self.config)
model_dir = './Model/a.model'
self.memory = ReplayMemory(model_dir)
self.max_step = 100000
self.RB_number = 20
self.num_vehicle = len(self.env.vehicles)
self.action_all_with_power = np.zeros([self.num_vehicle, 3, 2],dtype = 'int32') # this is actions that taken by V2V links with power
self.action_all_with_power_training = np.zeros([20, 3, 2],dtype = 'int32') # this is actions that taken by V2V links with power
self.reward = []
self.learning_rate = 0.01
self.learning_rate_minimum = 0.0001
self.learning_rate_decay = 0.96
self.learning_rate_decay_step = 500000
self.target_q_update_step = 100
self.discount = 0.5
self.double_q = True
self.build_dqn()
self.V2V_number = 3 * len(self.env.vehicles) # every vehicle need to communicate with 3 neighbors
self.training = True
#self.actions_all = np.zeros([len(self.env.vehicles),3], dtype = 'int32')
def merge_action(self, idx, action):
self.action_all_with_power[idx[0], idx[1], 0] = action % self.RB_number
self.action_all_with_power[idx[0], idx[1], 1] = int(np.floor(action/self.RB_number))
def get_state(self, idx):
# ===============
# Get State from the environment
# =============
vehicle_number = len(self.env.vehicles)
V2V_channel = (self.env.V2V_channels_with_fastfading[idx[0],self.env.vehicles[idx[0]].destinations[idx[1]],:] - 80)/60
V2I_channel = (self.env.V2I_channels_with_fastfading[idx[0], :] - 80)/60
V2V_interference = (-self.env.V2V_Interference_all[idx[0],idx[1],:] - 60)/60
NeiSelection = np.zeros(self.RB_number)
for i in range(3):
for j in range(3):
if self.training:
NeiSelection[self.action_all_with_power_training[self.env.vehicles[idx[0]].neighbors[i], j, 0 ]] = 1
else:
NeiSelection[self.action_all_with_power[self.env.vehicles[idx[0]].neighbors[i], j, 0 ]] = 1
for i in range(3):
if i == idx[1]:
continue
if self.training:
if self.action_all_with_power_training[idx[0],i,0] >= 0:
NeiSelection[self.action_all_with_power_training[idx[0],i,0]] = 1
else:
if self.action_all_with_power[idx[0],i,0] >= 0:
NeiSelection[self.action_all_with_power[idx[0],i,0]] = 1
time_remaining = np.asarray([self.env.demand[idx[0],idx[1]] / self.env.demand_amount])
load_remaining = np.asarray([self.env.individual_time_limit[idx[0],idx[1]] / self.env.V2V_limit])
#print('shapes', time_remaining.shape,load_remaining.shape)
return np.concatenate((V2I_channel, V2V_interference, V2V_channel, NeiSelection, time_remaining, load_remaining))#,time_remaining))
#return np.concatenate((V2I_channel, V2V_interference, V2V_channel, time_remaining, load_remaining))#,time_remaining))
def predict(self, s_t, step, test_ep = False):
# ==========================
# Select actions
# ======================
ep = 1/(step/1000000 + 1)
if random.random() < ep and test_ep == False: # epsion to balance the exporation and exploition
action = np.random.randint(60)
else:
action = self.q_action.eval({self.s_t:[s_t]})[0]
return action
def observe(self, prestate, state, reward, action):
# -----------
# Collect Data for Training
# ---------
self.memory.add(prestate, state, reward, action) # add the state and the action and the reward to the memory
#print(self.step)
if self.step > 0:
if self.step % 50 == 0:
#print('Training')
self.q_learning_mini_batch() # training a mini batch
#self.save_weight_to_pkl()
if self.step % self.target_q_update_step == self.target_q_update_step - 1:
#print("Update Target Q network:")
self.update_target_q_network() # ?? what is the meaning ??
def train(self):
num_game, self.update_count, ep_reward = 0, 0, 0.
total_reward, self.total_loss, self.total_q = 0.,0.,0.
max_avg_ep_reward = 0
ep_reward, actions = [], []
mean_big = 0
number_big = 0
mean_not_big = 0
number_not_big = 0
self.env.new_random_game(20)
for self.step in (range(0, 40000)): # need more configuration
if self.step == 0: # initialize set some varibles
num_game, self.update_count,ep_reward = 0, 0, 0.
total_reward, self.total_loss, self.total_q = 0., 0., 0.
ep_reward, actions = [], []
# prediction
# action = self.predict(self.history.get())
if (self.step % 2000 == 1):
self.env.new_random_game(20)
print(self.step)
state_old = self.get_state([0,0])
#print("state", state_old)
self.training = True
for k in range(1):
for i in range(len(self.env.vehicles)):
for j in range(3):
state_old = self.get_state([i,j])
action = self.predict(state_old, self.step)
#self.merge_action([i,j], action)
self.action_all_with_power_training[i, j, 0] = action % self.RB_number
self.action_all_with_power_training[i, j, 1] = int(np.floor(action/self.RB_number))
reward_train = self.env.act_for_training(self.action_all_with_power_training, [i,j])
state_new = self.get_state([i,j])
self.observe(state_old, state_new, reward_train, action)
if (self.step % 2000 == 0) and (self.step > 0):
# testing
self.training = False
number_of_game = 10
if (self.step % 10000 == 0) and (self.step > 0):
number_of_game = 50
if (self.step == 38000):
number_of_game = 100
V2I_Rate_list = np.zeros(number_of_game)
Fail_percent_list = np.zeros(number_of_game)
for game_idx in range(number_of_game):
self.env.new_random_game(self.num_vehicle)
test_sample = 200
Rate_list = []
print('test game idx:', game_idx)
for k in range(test_sample):
action_temp = self.action_all_with_power.copy()
for i in range(len(self.env.vehicles)):
self.action_all_with_power[i,:,0] = -1
sorted_idx = np.argsort(self.env.individual_time_limit[i,:])
for j in sorted_idx:
state_old = self.get_state([i,j])
action = self.predict(state_old, self.step, True)
self.merge_action([i,j], action)
if i % (len(self.env.vehicles)/10) == 1:
action_temp = self.action_all_with_power.copy()
reward, percent = self.env.act_asyn(action_temp) #self.action_all)
Rate_list.append(np.sum(reward))
#print("actions", self.action_all_with_power)
V2I_Rate_list[game_idx] = np.mean(np.asarray(Rate_list))
Fail_percent_list[game_idx] = percent
#print("action is", self.action_all_with_power)
print('failure probability is, ', percent)
#print('action is that', action_temp[0,:])
self.save_weight_to_pkl()
print ('The number of vehicle is ', len(self.env.vehicles))
print ('Mean of the V2I rate is that ', np.mean(V2I_Rate_list))
print('Mean of Fail percent is that ', np.mean(Fail_percent_list))
#print('Test Reward is ', np.mean(test_result))
def q_learning_mini_batch(self):
# Training the DQN model
# ------
#s_t, action,reward, s_t_plus_1, terminal = self.memory.sample()
s_t, s_t_plus_1, action, reward = self.memory.sample()
#print()
#print('samples:', s_t[0:10], s_t_plus_1[0:10], action[0:10], reward[0:10])
t = time.time()
if self.double_q: #double Q learning
pred_action = self.q_action.eval({self.s_t: s_t_plus_1})
q_t_plus_1_with_pred_action = self.target_q_with_idx.eval({self.target_s_t: s_t_plus_1, self.target_q_idx: [[idx, pred_a] for idx, pred_a in enumerate(pred_action)]})
target_q_t = self.discount * q_t_plus_1_with_pred_action + reward
else:
q_t_plus_1 = self.target_q.eval({self.target_s_t: s_t_plus_1})
max_q_t_plus_1 = np.max(q_t_plus_1, axis=1)
target_q_t = self.discount * max_q_t_plus_1 +reward
_, q_t, loss,w = self.sess.run([self.optim, self.q, self.loss, self.w], {self.target_q_t: target_q_t, self.action:action, self.s_t:s_t, self.learning_rate_step: self.step}) # training the network
print('loss is ', loss)
self.total_loss += loss
self.total_q += q_t.mean()
self.update_count += 1
def build_dqn(self):
# --- Building the DQN -------
self.w = {}
self.t_w = {}
initializer = tf. truncated_normal_initializer(0, 0.02)
activation_fn = tf.nn.relu
n_hidden_1 = 500
n_hidden_2 = 250
n_hidden_3 = 120
n_input = 82
n_output = 60
def encoder(x):
weights = {
'encoder_h1': tf.Variable(tf.truncated_normal([n_input, n_hidden_1],stddev=0.1)),
'encoder_h2': tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2],stddev=0.1)),
'encoder_h3': tf.Variable(tf.truncated_normal([n_hidden_2, n_hidden_3],stddev=0.1)),
'encoder_h4': tf.Variable(tf.truncated_normal([n_hidden_3, n_output],stddev=0.1)),
'encoder_b1': tf.Variable(tf.truncated_normal([n_hidden_1],stddev=0.1)),
'encoder_b2': tf.Variable(tf.truncated_normal([n_hidden_2],stddev=0.1)),
'encoder_b3': tf.Variable(tf.truncated_normal([n_hidden_3],stddev=0.1)),
'encoder_b4': tf.Variable(tf.truncated_normal([n_output],stddev=0.1)),
}
layer_1 = tf.nn.relu(tf.add(tf.matmul(x, weights['encoder_h1']), weights['encoder_b1']))
layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1, weights['encoder_h2']), weights['encoder_b2']))
layer_3 = tf.nn.relu(tf.add(tf.matmul(layer_2, weights['encoder_h3']), weights['encoder_b3']))
layer_4 = tf.nn.relu(tf.add(tf.matmul(layer_3, weights['encoder_h4']), weights['encoder_b4']))
return layer_4, weights
with tf.variable_scope('prediction'):
self.s_t = tf.placeholder('float32',[None, n_input])
self.q, self.w = encoder(self.s_t)
self.q_action = tf.argmax(self.q, dimension = 1)
with tf.variable_scope('target'):
self.target_s_t = tf.placeholder('float32', [None, n_input])
self.target_q, self.target_w = encoder(self.target_s_t)
self.target_q_idx = tf.placeholder('int32', [None,None], 'output_idx')
self.target_q_with_idx = tf.gather_nd(self.target_q, self.target_q_idx)
with tf.variable_scope('pred_to_target'):
self.t_w_input = {}
self.t_w_assign_op = {}
for name in self.w.keys():
print('name in self w keys', name)
self.t_w_input[name] = tf.placeholder('float32', self.target_w[name].get_shape().as_list(),name = name)
self.t_w_assign_op[name] = self.target_w[name].assign(self.t_w_input[name])
def clipped_error(x):
try:
return tf.select(tf.abs(x) < 1.0, 0.5 * tf.square(x), tf.abs(x) - 0.5)
except:
return tf.where(tf.abs(x) < 1.0, 0.5 * tf.square(x), tf.abs(x) - 0.5)
with tf.variable_scope('optimizer'):
self.target_q_t = tf.placeholder('float32', None, name='target_q_t')
self.action = tf.placeholder('int32',None, name = 'action')
action_one_hot = tf.one_hot(self.action, n_output, 1.0, 0.0, name='action_one_hot')
q_acted = tf.reduce_sum(self.q * action_one_hot, reduction_indices = 1, name='q_acted')
self.delta = self.target_q_t - q_acted
self.global_step = tf.Variable(0, trainable=False)
self.loss = tf.reduce_mean(tf.square(self.delta), name = 'loss')
self.learning_rate_step = tf.placeholder('int64', None, name='learning_rate_step')
self.learning_rate_op = tf.maximum(self.learning_rate_minimum, tf.train.exponential_decay(self.learning_rate, self.learning_rate_step, self.learning_rate_decay_step, self.learning_rate_decay, staircase=True))
self.optim = tf.train.RMSPropOptimizer(self.learning_rate_op, momentum=0.95, epsilon=0.01).minimize(self.loss)
tf.initialize_all_variables().run()
self.update_target_q_network()
def update_target_q_network(self):
for name in self.w.keys():
self.t_w_assign_op[name].eval({self.t_w_input[name]: self.w[name].eval()})
def save_weight_to_pkl(self):
if not os.path.exists(self.weight_dir):
os.makedirs(self.weight_dir)
for name in self.w.keys():
save_pkl(self.w[name].eval(), os.path.join(self.weight_dir,"%s.pkl" % name))
def load_weight_from_pkl(self):
with tf.variable_scope('load_pred_from_pkl'):
self.w_input = {}
self.w_assign_op = {}
for name in self.w.keys():
self.w_input[name] = tf.placeholder('float32')
self.w_assign_op[name] = self.w[name].assign(self.w_input[name])
for name in self.w.keys():
self.w_assign_op[name].eval({self.w_input[name]:load_pkl(os.path.join(self.weight_dir, "%s.pkl" % name))})
self.update_target_q_network()
def play(self, n_step = 100, n_episode = 100, test_ep = None, render = False):
number_of_game = 100
V2I_Rate_list = np.zeros(number_of_game)
Fail_percent_list = np.zeros(number_of_game)
self.load_weight_from_pkl()
self.training = False
for game_idx in range(number_of_game):
self.env.new_random_game(self.num_vehicle)
test_sample = 200
Rate_list = []
print('test game idx:', game_idx)
print('The number of vehicle is ', len(self.env.vehicles))
time_left_list = []
power_select_list_0 = []
power_select_list_1 = []
power_select_list_2 = []
for k in range(test_sample):
action_temp = self.action_all_with_power.copy()
for i in range(len(self.env.vehicles)):
self.action_all_with_power[i, :, 0] = -1
sorted_idx = np.argsort(self.env.individual_time_limit[i, :])
for j in sorted_idx:
state_old = self.get_state([i, j])
time_left_list.append(state_old[-1])
action = self.predict(state_old, 0, True)
'''
if state_old[-1] <=0:
continue
power_selection = int(np.floor(action/self.RB_number))
if power_selection == 0:
power_select_list_0.append(state_old[-1])
if power_selection == 1:
power_select_list_1.append(state_old[-1])
if power_selection == 2:
power_select_list_2.append(state_old[-1])
'''
self.merge_action([i, j], action)
if i % (len(self.env.vehicles) / 10) == 1:
action_temp = self.action_all_with_power.copy()
reward, percent = self.env.act_asyn(action_temp) # self.action_all)
Rate_list.append(np.sum(reward))
# print("actions", self.action_all_with_power)
'''
number_0, bin_edges = np.histogram(power_select_list_0, bins = 10)
number_1, bin_edges = np.histogram(power_select_list_1, bins = 10)
number_2, bin_edges = np.histogram(power_select_list_2, bins = 10)
p_0 = number_0 / (number_0 + number_1 + number_2)
p_1 = number_1 / (number_0 + number_1 + number_2)
p_2 = number_2 / (number_0 + number_1 + number_2)
plt.plot(bin_edges[:-1]*0.1 + 0.01, p_0, 'b*-', label='Power Level 23 dB')
plt.plot(bin_edges[:-1]*0.1 + 0.01, p_1, 'rs-', label='Power Level 10 dB')
plt.plot(bin_edges[:-1]*0.1 + 0.01, p_2, 'go-', label='Power Level 5 dB')
plt.xlim([0,0.12])
plt.xlabel("Time left for V2V transmission (s)")
plt.ylabel("Probability of power selection")
plt.legend()
plt.grid()
plt.show()
'''
V2I_Rate_list[game_idx] = np.mean(np.asarray(Rate_list))
Fail_percent_list[game_idx] = percent
print('Mean of the V2I rate is that ', np.mean(V2I_Rate_list[0:game_idx] ))
print('Mean of Fail percent is that ',percent, np.mean(Fail_percent_list[0:game_idx]))
# print('action is that', action_temp[0,:])
print('The number of vehicle is ', len(self.env.vehicles))
print('Mean of the V2I rate is that ', np.mean(V2I_Rate_list))
print('Mean of Fail percent is that ', np.mean(Fail_percent_list))
# print('Test Reward is ', np.mean(test_result))