forked from Flenji/traffic_light_optimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddqn.py
331 lines (244 loc) · 11.3 KB
/
ddqn.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
# -*- coding: utf-8 -*-
"""
To learn how to implement Deep-Q-Learning in Python, an online course
"deep-q-learning-from-paper-to-code" by Phil Tabor was followed.
The code follows the structure of his implementtion which can be found on
GitHub: https://github.com/philtabor/Deep-Q-Learning-Paper-To-Code
This class contains classes for the DDQN networks and the agent and the memory of the agent.
Authors: AAU CS (IT) 07 - 03
"""
import random
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch as T
import gym
import matplotlib.pyplot as plt
import os
import pickle
class Memory():
"""
Handles the storation of transitions (state, action, reward, state_, done)
and the sampling of memories for the learning of the DDQN
"""
def __init__(self, memory, input_shape):
self.memory = memory #number of maximal transitions stored
self.idx = 0
shape = (memory,*input_shape) #input_shape is the shape of the observation
self.states = np.zeros(shape, dtype = np.float32)
self.states_ = np.zeros(shape,dtype = np.float32) #new states after taking an action
self.actions = np.zeros([memory],dtype = np.int64)
self.rewards = np.zeros([memory], dtype = np.float32)
self.terminal = np.zeros([memory],dtype = np.uint8) #information about when an environment is done
def sample_memories(self,batch_size):
"""
Samples a number of memories corresponding to the batch_size. Returns np.arrays for states,
actions, rewards, new states and dones.
"""
max_mem = min(self.idx, self.memory)
sample_idx = np.random.choice(max_mem, size=batch_size, replace=False) #for choosing random transitions
sample_states = self.states[sample_idx]
sample_actions = self.actions[sample_idx]
sample_rewards = self.rewards[sample_idx]
sample_states_ = self.states_[sample_idx]
sample_dones = self.terminal[sample_idx]
return sample_states, sample_actions, sample_rewards, sample_states_,\
sample_dones
def store_transition(self,state, action, reward, state_, done):
"""
Stores an experience consisting of <state,action,reward, new state, done>
in the memory.
"""
idx = self.idx%self.memory
self.states[idx] = state
self.actions[idx] = action
self.rewards[idx] = reward
self.states_[idx] = state_
self.terminal[idx] = done
self.idx += 1
class DDQN(nn.Module):
"""
This class implement the basic functionality of the Double-Deep-Q-Network
"""
def __init__(self, learning_rate, input_dim, n_actions, name, checkpoint_dir):
super(DDQN,self).__init__()
self.name = name
self.checkpoint_dir = checkpoint_dir
self.checkpoint_file = os.path.join(self.checkpoint_dir,name)
self.learning_rate = learning_rate
self.input_dim = input_dim
self.n_actions = n_actions
#Network architecture
self.layer1 = nn.Linear(*input_dim, 128)
self.layer2 = nn.Linear(128, 128)
self.layer3 = nn.Linear(128, n_actions)
self.optimizer = optim.Adam(self.parameters(), lr=learning_rate)
self.loss = nn.MSELoss()
self.device = T.device("cuda:0" if T.cuda.is_available() else "cpu")
self.to(self.device)
def forward(self, x):
"""
Calculates values of output nodes for given observation x
"""
x = F.relu(self.layer1(x))
x = F.relu(self.layer2(x))
x = self.layer3(x)
return x
def save(self):
#print(f"--- saving model: {self.name} ---")
T.save(self.state_dict(),self.checkpoint_file)
def load(self):
#print(f"--- loading model {self.name} ---")
self.load_state_dict(T.load(self.checkpoint_file))
class DDQN_deeper(nn.Module):
"""
This class implement the basic functionality of the Double-Deep-Q-Network
"""
def __init__(self, learning_rate, input_dim, n_actions, name, checkpoint_dir):
super(DDQN_deeper,self).__init__()
self.name = name
self.checkpoint_dir = checkpoint_dir
self.checkpoint_file = os.path.join(self.checkpoint_dir,name)
self.learning_rate = learning_rate
self.input_dim = input_dim
self.n_actions = n_actions
#Network architecture
self.layer1 = nn.Linear(*input_dim, 128)
self.dropout1 = nn.Dropout(p=0.5)
self.layer2 = nn.Linear(128, 256)
self.dropout2 = nn.Dropout(p=0.5)
self.layer3 = nn.Linear(256, 128)
self.dropout3 = nn.Dropout(p=0.5)
self.layer4 = nn.Linear(128, n_actions)
self.optimizer = optim.Adam(self.parameters(), lr=learning_rate)
self.loss = nn.MSELoss()
self.device = T.device("cuda:0" if T.cuda.is_available() else "cpu")
self.to(self.device)
def forward(self, x):
"""
Calculates values of output nodes for given observation x
"""
x = F.relu(self.layer1(x))
x = self.dropout1(x)
x = F.relu(self.layer2(x))
x = self.dropout2(x)
x = F.relu(self.layer3(x))
x = self.dropout3(x)
x = self.layer4(x)
return x
def save(self):
#print(f"--- saving model: {self.name} ---")
T.save(self.state_dict(),self.checkpoint_file)
def load(self):
#print(f"--- loading model {self.name} ---")
self.load_state_dict(T.load(self.checkpoint_file))
class Agent():
"""
This class implements the agent's functionalities.
"""
def __init__(self, learning_rate, input_dim, n_actions, mem_size, batch_size,\
name, checkpoint_dir, gamma = 0.99, eps_max = 1, eps_min = 0.01,\
eps_dec = 5e-6, replace = 1000, deeper=False):
self.learning_rate = learning_rate
self.n_actions = n_actions
self.actions = [i for i in range(n_actions)]
self.input_dim = input_dim
self.mem_size = mem_size
self.batch_size = batch_size
self.name = name
self.checkpoint_dir = checkpoint_dir
self.gamma = gamma
self.epsilon = eps_max
self.eps_min = eps_min
self.eps_dec = eps_dec
self.replace = replace #after how many learning steps should the target-network be updated
self.learning_counter = 0
if deeper:
self.online_q = DDQN_deeper(learning_rate,input_dim,n_actions,name,checkpoint_dir) #two networks for deciding which action to take
self.target_q = DDQN_deeper(learning_rate,input_dim,n_actions,name,checkpoint_dir) # for calculating the target_value
else:
self.online_q = DDQN(learning_rate,input_dim,n_actions,name,checkpoint_dir) #two networks for deciding which action to take
self.target_q = DDQN(learning_rate,input_dim,n_actions,name,checkpoint_dir) # for calculating the target_value
self.memory = Memory(mem_size,input_dim)
def get_action(self, state):
"""
Depending on the epsilon value, this function returns either a random action
or the best learned one for the given observation.
"""
if np.random.random() < self.epsilon: #select random action with regards to epsilon
action = np.random.choice(self.actions)
else:
state = T.tensor(state, dtype=T.float).to(self.online_q.device)
action = T.argmax(self.online_q.forward(state)).item()
return action
def get_test_action(self,state):
"""
This function returns always the best calculated action. No exploring.
"""
state = T.tensor(state, dtype=T.float).to(self.online_q.device)
action = T.argmax(self.online_q.forward(state)).item()
return action
def store_transition(self,s,a,r,s_,d):
self.memory.store_transition(s, a, r, s_, d)
def sample_memory(self):
s, a, r, s_, d = self.memory.sample_memories(self.batch_size)
#convert numpy array to Torch tensor
states = T.tensor(s).to(self.online_q.device)
actions = T.tensor(a).to(self.online_q.device)
rewards = T.tensor(r).to(self.online_q.device)
states_ = T.tensor(s_).to(self.online_q.device)
dones = T.tensor(d, dtype = T.bool).to(self.online_q.device)
return states, actions, rewards, states_, dones
def save_model(self):
print(f"--- saving model: {self.name} ---")
self.online_q.save()
self.target_q.save()
# saving epsilon
checkpoint_epsilon = os.path.join(self.checkpoint_dir,self.name+"_epsilon")
with open (checkpoint_epsilon, "wb") as file:
pickle.dump(self.epsilon, file)
def load_model(self):
print(f"--- loading model: {self.name} ---")
self.online_q.load()
self.target_q.load()
#loading epsilon
checkpoint_epsilon = os.path.join(self.checkpoint_dir,self.name+"_epsilon")
with open(checkpoint_epsilon, 'rb') as file:
self.epsilon = pickle.load(file)
def update_target_network(self):
"""
Updates the parameters of the target network if there have been |replace| learning
steps.
"""
if self.learning_counter % self.replace == 0: #update target network only every replace-steps
state_dict = self.online_q.state_dict()
self.target_q.load_state_dict(state_dict)
def decrement_epsilon(self):
"""
Decrements epsilon if the current epsilon is greater than the minimum set epsilon.
"""
self.epsilon = self.epsilon - self.eps_dec if self.epsilon - self.eps_dec > self.eps_min\
else self.eps_min
def learn(self,s,a,r,s_,d):
"""
Learning function for updating the agent's parameter.
"""
self.store_transition(s, a, r, s_, d)
if self.memory.idx < self.batch_size:
return
self.online_q.optimizer.zero_grad() #reset the gradients to zero to avoid wrong gradient calculation
self.update_target_network()
states, actions, rewards, states_, dones = self.sample_memory()
indices = np.arange(self.batch_size)
q_pred = self.online_q.forward(states)[indices, actions]
q_next = self.target_q.forward(states_)[indices]
q_eval = self.online_q.forward(states_)[indices]
eval_actions = q_eval.argmax(dim=1) #chooses best action according to the onlone network
q_next[dones] = 0.0
q_target = rewards + self.gamma * q_next[indices,eval_actions]
loss = self.online_q.loss(q_pred,q_target).to(self.online_q.device)
loss.backward()
self.online_q.optimizer.step()
self.learning_counter += 1
self.decrement_epsilon()