-
Notifications
You must be signed in to change notification settings - Fork 0
/
DQNAgent.py
232 lines (163 loc) · 8.2 KB
/
DQNAgent.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
import torch
import numpy as np
import torch.optim as optim
from collections import deque, namedtuple
from torch.utils.tensorboard import SummaryWriter
from DeepQNetwork import DeepQNetwork
from DuelingDeepQNetwork import DuelingDeepQNetwork
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class History:
def __init__(self, plot):
self.ave_rewards = list()
self.episode_rewards = list()
self.writer = SummaryWriter() if plot else None
def __len__(self):
return len(self.episode_rewards)
def append(self, reward):
self.episode_rewards.append(reward)
mean_reward = round(np.mean(self.episode_rewards), 3)
self.ave_rewards.append(mean_reward)
if self.writer:
self.writer.add_scalar('Reward/Episode', reward, len(self.episode_rewards))
self.writer.add_scalar('Average_Reward/Last_5_Episodes', mean_reward, len(self.ave_rewards))
def restore(self):
if self.writer:
for i, reward in enumerate(self.episode_rewards):
self.writer.add_scalar('Reward/Episode', reward, len(self.episode_rewards))
self.writer.add_scalar('Average_Reward/Last_5_Episodes', self.ave_rewards[i], len(self.ave_rewards))
def flush(self):
if self.writer:
self.writer.flush()
Replay = namedtuple('Replay', field_names=['state', 'action', 'reward', 'next_state', 'done'])
class ReplayMemory:
def __init__(self, capacity):
self.buffer = deque(maxlen=capacity)
def __len__(self):
return len(self.buffer)
def append(self, experience):
self.buffer.append(experience)
def sample(self, batch_size):
indices = np.random.choice(len(self.buffer), batch_size, replace=False)
states, actions, rewards, next_states, dones = zip(*[self.buffer[idx] for idx in indices])
return np.array(states), np.array(actions, dtype=np.int64), np.array(rewards, dtype=np.float32), np.array(next_states, dtype=np.float32), np.array(dones, dtype=np.uint8)
class DQNAgent():
def __init__(self, env, hyper_parameters, training_mode, algorithms='DQN', network='Dueling', plot=True):
self.env = env
self.alg = algorithms
self.episode = 0
self.training_mode = training_mode
self.batch_size = hyper_parameters['batch_size']
self.history = History(plot) if training_mode else None
self.replay_memory = ReplayMemory(10000) if training_mode else None
self.gamma = hyper_parameters['gamma']
self.epsilon = hyper_parameters['epsilon'] if training_mode else -1
self.epsilon_min = hyper_parameters['epsilon_min']
self.epsilon_decay = hyper_parameters['epsilon_decay']
if network == 'Dueling':
self.model = DuelingDeepQNetwork(env.action_space.n, env.observation_space.shape[0]).to(device)
self.target_model = DuelingDeepQNetwork(env.action_space.n, env.observation_space.shape[0]).to(device).eval()
else:
self.model = DeepQNetwork(env.action_space.n, env.observation_space.shape[0]).to(device)
self.target_model = DeepQNetwork(env.action_space.n, env.observation_space.shape[0]).to(device).eval()
self.clip_grad_norm = 5
self.learning_rate = hyper_parameters['learning_rate']
self.optimizer = optim.Adam(self.model.parameters(), lr=self.learning_rate)
self.reset()
def reset(self):
self.state, _ = self.env.reset(seed=42)
self.steps = 0
self.episode_reward = 0
def update_epsilon(self):
self.epsilon = max(self.epsilon_min, self.epsilon * self.epsilon_decay)
def act(self):
if self.training_mode and np.random.random() < self.epsilon:
action = self.env.action_space.sample()
else:
state = torch.as_tensor(self.state, dtype=torch.float32, device=device).to(device)
with torch.no_grad():
q_value = self.model(state)
self.model.train()
action = torch.argmax(q_value).item()
return action
def step(self):
action = self.act()
next_state, reward, terminated, truncated, _ = self.env.step(action)
done = (terminated or truncated)
self.replay_memory.append(Replay(self.state, action, reward, next_state, done))
self.steps += 1
self.state = next_state
self.episode_reward += reward
if done:
self.episode += 1
self.history.append(self.episode_reward)
print(f'episode: {self.episode}, steps {self.steps}, reward: {round(self.episode_reward,2)} mean reward: {self.history.ave_rewards[-1]}\n')
self.reset()
return done
def update_weights(self):
if len(self.replay_memory) >= self.batch_size:
states, actions, rewards, next_states, dones = self.replay_memory.sample(self.batch_size)
states = torch.tensor(states, dtype=torch.float32).to(device)
next_states = torch.tensor(next_states).to(device)
actions = torch.tensor(actions).to(device)
rewards = torch.tensor(rewards).to(device)
dones = torch.BoolTensor(dones).to(device)
q_value = self.model(states).gather(dim=1, index=actions.unsqueeze(-1)).squeeze(-1)
if self.alg == 'DQN':
next_q_value = self.target_model(torch.as_tensor(next_states, dtype=torch.float32, device=device)).max(1)[0]
next_q_value = next_q_value.detach()
next_q_value[dones] = 0.0
else: # Double DQN
next_action_values = self.model(next_states).max(1)[1].unsqueeze(-1)
next_q_value = self.target_model(next_states).gather(1, next_action_values).detach().squeeze(-1)
expected_q_value = rewards + next_q_value * self.gamma
loss = torch.nn.MSELoss()(q_value, expected_q_value)
self.optimizer.zero_grad()
loss.backward()
# Clip the gradients to prevent exploding gradients
torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.clip_grad_norm)
self.optimizer.step()
def update_target_network(self):
self.target_model.load_state_dict(self.model.state_dict())
print(f"episode {self.episode}: target model weights updated")
def demo(self, model_path):
# Load the weights of the test_network
self.model.load_state_dict(torch.load(model_path))
self.model.eval()
self.reset()
terminated = False
truncated = False
while not terminated and not truncated:
action = self.act()
self.state, reward, terminated, truncated, _ = self.env.step(action)
self.episode_reward += reward
self.steps += 1
print(f'Steps: {self.steps}, Reward: {self.episode_reward:.2f}, ')
def save_checkpoint(self, episode):
checkpoint = {
'episode': episode,
'model_state_dict': self.model.state_dict(),
'target_state_dict': self.target_model.state_dict(),
'optimizer_state_dict': self.optimizer.state_dict(),
'replay_memory': self.replay_memory,
'epsilon': self.epsilon,
'episode_rewards': np.array(self.history.episode_rewards),
'ave_rewards': np.array(self.history.ave_rewards)
}
torch.save(checkpoint, 'model/checkpoint.pt')
print(f'Checkpoint saved at episode {episode}')
def load_checkpoint(self, filename):
checkpoint = torch.load(filename)
self.model.load_state_dict(checkpoint['model_state_dict'])
self.target_model.load_state_dict(checkpoint['target_state_dict'])
self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
self.replay_memory = checkpoint['replay_memory']
self.epsilon = checkpoint['epsilon']
self.history.ave_rewards = checkpoint['ave_rewards'].tolist()
self.history.episode_rewards = checkpoint['episode_rewards'].tolist()
self.history.restore()
return checkpoint['episode']
def save(self, path):
torch.save(self.model.state_dict(), path)
print(f'Save Model file to {path}')
def flush(self):
self.history.flush()