-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDQN.py
666 lines (495 loc) · 26.1 KB
/
DQN.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
import os
import gc
import torch
import pygame
import warnings
import numpy as np
import torch.nn as nn
import gymnasium as gym
import torch.optim as optim
from collections import deque
import matplotlib.pyplot as plt
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
gc.collect()
torch.cuda.empty_cache()
os.environ['CUDA_LAUNCH_BLOCKING'] = '1' # Used for debugging; CUDA related errors shown immediately.
# Seed everything for reproducible results
seed = 2024
np.random.seed(seed)
np.random.default_rng(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
class ReplayMemory:
def __init__(self, capacity):
"""
Experience Replay Memory defined by deques to store transitions/agent experiences
"""
self.capacity = capacity
self.states = deque(maxlen=capacity)
self.actions = deque(maxlen=capacity)
self.next_states = deque(maxlen=capacity)
self.rewards = deque(maxlen=capacity)
self.dones = deque(maxlen=capacity)
def store(self, state, action, next_state, reward, done):
"""
Append (store) the transitions to their respective deques
"""
self.states.append(state)
self.actions.append(action)
self.next_states.append(next_state)
self.rewards.append(reward)
self.dones.append(done)
def sample(self, batch_size):
"""
Randomly sample transitions from memory, then convert sampled transitions
to tensors and move to device (CPU or GPU).
"""
indices = np.random.choice(len(self), size=batch_size, replace=False)
states = torch.stack([torch.as_tensor(self.states[i], dtype=torch.float32, device=device) for i in indices]).to(device)
actions = torch.as_tensor([self.actions[i] for i in indices], dtype=torch.long, device=device)
next_states = torch.stack([torch.as_tensor(self.next_states[i], dtype=torch.float32, device=device) for i in indices]).to(device)
rewards = torch.as_tensor([self.rewards[i] for i in indices], dtype=torch.float32, device=device)
dones = torch.as_tensor([self.dones[i] for i in indices], dtype=torch.bool, device=device)
return states, actions, next_states, rewards, dones
def __len__(self):
"""
To check how many samples are stored in the memory. self.dones deque
represents the length of the entire memory.
"""
return len(self.dones)
class DQN_Network(nn.Module):
"""
The Deep Q-Network (DQN) model for reinforcement learning.
This network consists of Fully Connected (FC) layers with ReLU activation functions.
"""
def __init__(self, num_actions, input_dim):
"""
Initialize the DQN network.
Parameters:
num_actions (int): The number of possible actions in the environment.
input_dim (int): The dimensionality of the input state space.
"""
super(DQN_Network, self).__init__()
self.FC = nn.Sequential(
nn.Linear(input_dim, 12),
nn.ReLU(inplace=True),
nn.Linear(12, 8),
nn.ReLU(inplace=True),
nn.Linear(8, num_actions)
)
# Initialize FC layer weights using He initialization
for layer in [self.FC]:
for module in layer:
if isinstance(module, nn.Linear):
nn.init.kaiming_uniform_(module.weight, nonlinearity='relu')
def forward(self, x):
"""
Forward pass of the network to find the Q-values of the actions.
Parameters:
x (torch.Tensor): Input tensor representing the state.
Returns:
Q (torch.Tensor): Tensor containing Q-values for each action.
"""
Q = self.FC(x)
return Q
class DQN_Agent:
"""
DQN Agent Class. This class defines some key elements of the DQN algorithm,
such as the learning method, hard update, and action selection based on the
Q-value of actions or the epsilon-greedy policy.
"""
def __init__(self, env, epsilon_max, epsilon_min, epsilon_decay,
clip_grad_norm, learning_rate, discount, memory_capacity):
# To save the history of network loss
self.loss_history = []
self.running_loss = 0
self.learned_counts = 0
# RL hyperparameters
self.epsilon_max = epsilon_max
self.epsilon_min = epsilon_min
self.epsilon_decay = epsilon_decay
self.discount = discount
self.action_space = env.action_space
self.action_space.seed(seed) # Set the seed to get reproducible results when sampling the action space
self.observation_space = env.observation_space
self.replay_memory = ReplayMemory(memory_capacity)
# Initiate the network models
input_dim = self.observation_space.shape[0]
output_dim = self.action_space.n
self.main_network = DQN_Network(num_actions=output_dim, input_dim=input_dim).to(device)
self.target_network = DQN_Network(num_actions=output_dim, input_dim=input_dim).to(device).eval()
self.target_network.load_state_dict(self.main_network.state_dict())
self.clip_grad_norm = clip_grad_norm # For clipping exploding gradients caused by high reward value
self.critertion = nn.MSELoss()
self.optimizer = optim.Adam(self.main_network.parameters(), lr=learning_rate)
def select_action(self, state):
"""
Selects an action using epsilon-greedy strategy OR based on the Q-values.
Parameters:
state (torch.Tensor): Input tensor representing the state.
Returns:
action (int): The selected action.
"""
# Exploration: epsilon-greedy
if np.random.random() < self.epsilon_max:
return self.action_space.sample()
# Exploitation: the action is selected based on the Q-values.
# Check if the state is a tensor or not. If not, make it a tensor
if not torch.is_tensor(state):
state = torch.as_tensor(state, dtype=torch.float32, device=device)
with torch.no_grad():
Q_values = self.main_network(state)
action = torch.argmax(Q_values).item()
return action
def learn(self, batch_size, done):
"""
Train the main network using a batch of experiences sampled from the replay memory.
Parameters:
batch_size (int): The number of experiences to sample from the replay memory.
done (bool): Indicates whether the episode is done or not. If done,
calculate the loss of the episode and append it in a list for plot.
"""
# Sample a batch of experiences from the replay memory
states, actions, next_states, rewards, dones = self.replay_memory.sample(batch_size)
# # The following prints are for debugging. Use them to indicate the correct shape of the tensors.
# print('Before--------Before')
# print("states:", states.shape)
# print("actions:", actions.shape)
# print("next_states:", next_states.shape)
# print("rewards:", rewards.shape)
# print("dones:", dones.shape)
# # Preprocess the data for training
# states = states.unsqueeze(1)
# next_states = next_states.unsqueeze(1)
actions = actions.unsqueeze(1)
rewards = rewards.unsqueeze(1)
dones = dones.unsqueeze(1)
# # The following prints are for debugging. Use them to indicate the correct shape of the tensors.
# print()
# print('After--------After')
# print("states:", states.shape)
# print("actions:", actions.shape)
# print("next_states:", next_states.shape)
# print("rewards:", rewards.shape)
# print("dones:", dones.shape)
predicted_q = self.main_network(states) # forward pass through the main network to find the Q-values of the states
predicted_q = predicted_q.gather(dim=1, index=actions) # selecting the Q-values of the actions that were actually taken
# Compute the maximum Q-value for the next states using the target network
with torch.no_grad():
next_target_q_value = self.target_network(next_states).max(dim=1, keepdim=True)[0] # not argmax (cause we want the maxmimum q-value, not the action that maximize it)
next_target_q_value[dones] = 0 # Set the Q-value for terminal states to zero
y_js = rewards + (self.discount * next_target_q_value) # Compute the target Q-values
loss = self.critertion(predicted_q, y_js) # Compute the loss
# Update the running loss and learned counts for logging and plotting
self.running_loss += loss.item()
self.learned_counts += 1
if done:
episode_loss = self.running_loss / self.learned_counts # The average loss for the episode
self.loss_history.append(episode_loss) # Append the episode loss to the loss history for plotting
# Reset the running loss and learned counts
self.running_loss = 0
self.learned_counts = 0
self.optimizer.zero_grad() # Zero the gradients
loss.backward() # Perform backward pass and update the gradients
# # Uncomment the following two lines to find the best value for clipping gradient (Comment torch.nn.utils.clip_grad_norm_ while uncommenting the following two lines)
# grad_norm_before_clip = torch.nn.utils.clip_grad_norm_(self.main_network.parameters(), float('inf'))
# print("Gradient norm before clipping:", grad_norm_before_clip)
# Clip the gradients to prevent exploding gradients
torch.nn.utils.clip_grad_norm_(self.main_network.parameters(), self.clip_grad_norm)
self.optimizer.step() # Update the parameters of the main network using the optimizer
def hard_update(self):
"""
Navie update: Update the target network parameters by directly copying
the parameters from the main network.
"""
self.target_network.load_state_dict(self.main_network.state_dict())
def update_epsilon(self):
"""
Update the value of epsilon for epsilon-greedy exploration.
This method decreases epsilon over time according to a decay factor, ensuring
that the agent becomes less exploratory and more exploitative as training progresses.
"""
self.epsilon_max = max(self.epsilon_min, self.epsilon_max * self.epsilon_decay)
def save(self, path):
"""
Save the parameters of the main network to a file with .pth extention.
"""
torch.save(self.main_network.state_dict(), path)
class step_wrapper(gym.Wrapper):
"""
A wrapper class for modifying the state and reward functions of the
MountainCar-v0 environment.
"""
def __init__(self, env):
"""
Initializes the StepWrapper. This is the main class for wrapping the environment with it.
Args:
env (gym.Env): The environment to be wrapped.
Attributes:
observation_wrapper (Inherit from ObservationWrapper):
An instance of the ObservationWrapper class for modifying observations.
reward_wrapper (Inherit from RewardWrapper):
An instance of the RewardWrapper class for modifying rewards.
"""
super().__init__(env) # We give the env here to initialize the gym.Wrapper superclass (inherited).
self.observation_wrapper = observation_wrapper(env)
self.reward_wrapper = reward_wrapper(env)
def step(self, action):
"""
Executes a step in the environment with the provided action.The reason
behind using this method is to have access to the state and reward functions return.
Args:
action (int): The action to be taken.
"""
state, reward, done, truncation, info = self.env.step(action) # Same as before as usual
modified_state = self.observation_wrapper.observation(state) # Give the state to another Wrapper, which returns a modified version of state
modified_reward = self.reward_wrapper.reward(modified_state) # Give the modified state to another Wrapper to return the modified reward
return modified_state, modified_reward, done, truncation, info # The same returns as usual but with modified versions of the state and reward functions
def reset(self, seed):
state, info = self.env.reset(seed=seed) # Same as before as usual
modified_state = self.observation_wrapper.observation(state) # Give the state to another Wrapper, which returns a modified version of state
return modified_state, info # Same as before as usual but with returning the modified version of the state
class observation_wrapper(gym.ObservationWrapper):
"""
Wrapper class for modifying observations in the MountainCar-v0 environment.
Args:
env (gym.Env): The environment to wrap.
Attributes:
min_value (numpy.ndarray): Array of minimum observation values (2 dimensions because of the state.).
max_value (numpy.ndarray): Array of maximum observation values (2 dimensions because of the state.).
"""
def __init__(self, env):
super().__init__(env)
self.min_value = env.observation_space.low
self.max_value = env.observation_space.high
def observation(self, state):
"""
Modifies the observation by clipping the values and normalizing it.
Args:
state (numpy.ndarray): The original observation from the environment.
Returns:
numpy.ndarray: The modified and normalized observation.
"""
normalized_state = (state - self.min_value) / (self.max_value - self.min_value) # Min-max normalization
return normalized_state
class reward_wrapper(gym.RewardWrapper):
"""
Wrapper class for modifying rewards in the MountainCar-v0 environment.
Args:
env (gym.Env): The environment to wrap.
"""
def __init__(self, env):
super().__init__(env)
def reward(self, state):
"""
Modifies the reward based on the current state of the environment.
Args:
state (numpy.ndarray): The current state of the environment.
Returns:
float: The modified reward.
"""
current_position, current_velocity = state # extract the position and current velocity based on the state
# Interpolate the value to the desired range (because the velocity normalized value would be in range of 0 to 1 and now it would be in range of -0.5 to 0.5)
current_velocity = np.interp(current_velocity, np.array([0, 1]), np.array([-0.5, 0.5]))
# (1) Calculate the modified reward based on the current position and velocity of the car.
degree = current_position * 360
degree2radian = np.deg2rad(degree)
modified_reward = 0.2 * (np.cos(degree2radian) + 2 * np.abs(current_velocity))
# (2) Step limitation
modified_reward -= 0.5 # Subtract 0.5 to adjust the base reward (to limit useless steps).
# (3) Check if the car has surpassed a threshold of the path and is closer to the goal
if current_position > 0.98:
modified_reward += 20 # Add a bonus reward (Reached the goal)
elif current_position > 0.92:
modified_reward += 10 # So close to the goal
elif current_position > 0.82:
modified_reward += 6 # car is closer to the goal
elif current_position > 0.65:
modified_reward += 1 - np.exp(-2 * current_position) # car is getting close. Thus, giving reward based on the position and the further it reached
# (4) Check if the car is coming down with velocity from left and goes with full velocity to right
initial_position = 0.40842572 # Normalized value of initial position of the car which is extracted manually
if current_velocity > 0.3 and current_position > initial_position + 0.1:
modified_reward += 1 + 2 * current_position # Add a bonus reward for this desired behavior
return modified_reward
class Model_TrainTest():
def __init__(self, hyperparams):
# Define RL Hyperparameters
self.train_mode = hyperparams["train_mode"]
self.RL_load_path = hyperparams["RL_load_path"]
self.save_path = hyperparams["save_path"]
self.save_interval = hyperparams["save_interval"]
self.clip_grad_norm = hyperparams["clip_grad_norm"]
self.learning_rate = hyperparams["learning_rate"]
self.discount_factor = hyperparams["discount_factor"]
self.batch_size = hyperparams["batch_size"]
self.update_frequency = hyperparams["update_frequency"]
self.max_episodes = hyperparams["max_episodes"]
self.max_steps = hyperparams["max_steps"]
self.render = hyperparams["render"]
self.epsilon_max = hyperparams["epsilon_max"]
self.epsilon_min = hyperparams["epsilon_min"]
self.epsilon_decay = hyperparams["epsilon_decay"]
self.memory_capacity = hyperparams["memory_capacity"]
self.render_fps = hyperparams["render_fps"]
# Define Env
self.env = gym.make('MountainCar-v0', max_episode_steps=self.max_steps,
render_mode="human" if self.render else None)
self.env.metadata['render_fps'] = self.render_fps # For max frame rate make it 0
"""
The 'MountainCar-v0' environment in the 'gymnasium' library generates
UserWarnings about deprecated methods.
These warnings are related to the 'size' and 'shape' methods which are
being phased out in a future version of the library.
Even though we are not directly using these methods in our code, the
warnings are still displayed.
To keep our output clean and focused on our own program's execution,
the following line of code is for ignoring these warnings.
"""
warnings.filterwarnings("ignore", category=UserWarning)
# Apply RewardWrapper
self.env = step_wrapper(self.env)
# Define the agent class
self.agent = DQN_Agent(env = self.env,
epsilon_max = self.epsilon_max,
epsilon_min = self.epsilon_min,
epsilon_decay = self.epsilon_decay,
clip_grad_norm = self.clip_grad_norm,
learning_rate = self.learning_rate,
discount = self.discount_factor,
memory_capacity = self.memory_capacity)
def train(self):
"""
Reinforcement learning training loop.
"""
total_steps = 0
self.reward_history = []
# Training loop over episodes
for episode in range(1, self.max_episodes+1):
state, _ = self.env.reset(seed=seed)
done = False
truncation = False
step_size = 0
episode_reward = 0
while not done and not truncation:
action = self.agent.select_action(state)
next_state, reward, done, truncation, _ = self.env.step(action)
self.agent.replay_memory.store(state, action, next_state, reward, done)
if len(self.agent.replay_memory) > self.batch_size:
self.agent.learn(self.batch_size, (done or truncation))
# Update target-network weights
if total_steps % self.update_frequency == 0:
self.agent.hard_update()
state = next_state
episode_reward += reward
step_size +=1
# Appends for tracking history
self.reward_history.append(episode_reward) # episode reward
total_steps += step_size
# Decay epsilon at the end of each episode
self.agent.update_epsilon()
#-- based on interval
if episode % self.save_interval == 0:
self.agent.save(self.save_path + '_' + f'{episode}' + '.pth')
if episode != self.max_episodes:
self.plot_training(episode)
print('\n~~~~~~Interval Save: Model saved.\n')
result = (f"Episode: {episode}, "
f"Total Steps: {total_steps}, "
f"Ep Step: {step_size}, "
f"Raw Reward: {episode_reward:.2f}, "
f"Epsilon: {self.agent.epsilon_max:.2f}")
print(result)
self.plot_training(episode)
def test(self, max_episodes):
"""
Reinforcement learning policy evaluation.
"""
# Load the weights of the test_network
self.agent.main_network.load_state_dict(torch.load(self.RL_load_path))
self.agent.main_network.eval()
# Testing loop over episodes
for episode in range(1, max_episodes+1):
state, _ = self.env.reset(seed=seed)
done = False
truncation = False
step_size = 0
episode_reward = 0
while not done and not truncation:
action = self.agent.select_action(state)
next_state, reward, done, truncation, _ = self.env.step(action)
state = next_state
episode_reward += reward
step_size += 1
# Print log
result = (f"Episode: {episode}, "
f"Steps: {step_size:}, "
f"Reward: {episode_reward:.2f}, ")
print(result)
pygame.quit() # close the rendering window
def plot_training(self, episode):
# Calculate the Simple Moving Average (SMA) with a window size of 50
sma = np.convolve(self.reward_history, np.ones(50)/50, mode='valid')
# Clip max (high) values for better plot analysis
reward_history = np.clip(self.reward_history, a_min=None, a_max=100)
sma = np.clip(sma, a_min=None, a_max=100)
plt.figure()
plt.title("Obtained Rewards")
plt.plot(reward_history, label='Raw Reward', color='#4BA754', alpha=1)
plt.plot(sma, label='SMA 50', color='#F08100')
plt.xlabel("Episode")
plt.ylabel("Rewards")
plt.legend()
# Only save as file if last episode
if episode == self.max_episodes:
plt.savefig('./reward_plot.png', format='png', dpi=600, bbox_inches='tight')
plt.tight_layout()
plt.grid(True)
plt.show()
plt.clf()
plt.close()
plt.figure()
plt.title("Network Loss")
plt.plot(self.agent.loss_history, label='Loss', color='#8921BB', alpha=1)
plt.xlabel("Episode")
plt.ylabel("Loss")
# Only save as file if last episode
if episode == self.max_episodes:
plt.savefig('./Loss_plot.png', format='png', dpi=600, bbox_inches='tight')
plt.tight_layout()
plt.grid(True)
plt.show()
if __name__ == '__main__':
# Parameters:
train_mode = False
render = not train_mode
RL_hyperparams = {
"train_mode" : train_mode,
"RL_load_path" : './final_weights' + '_' + '1000' + '.pth',
"save_path" : './final_weights',
"save_interval" : 100,
"clip_grad_norm" : 5,
"learning_rate" : 75e-5,
"discount_factor" : 0.96,
"batch_size" : 64,
"update_frequency" : 20,
"max_episodes" : 1000 if train_mode else 2,
"max_steps" : 200,
"render" : render,
"epsilon_max" : 0.999 if train_mode else -1,
"epsilon_min" : 0.01,
"epsilon_decay" : 0.997,
"memory_capacity" : 125_000 if train_mode else 0,
"render_fps" : 60,
}
# Run
DRL = Model_TrainTest(RL_hyperparams) # Define the instance
# Train
if train_mode:
DRL.train()
else:
# Test
DRL.test(max_episodes = RL_hyperparams['max_episodes'])