forked from johnnycode8/gym_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrozen_lake_dql_cnn.py
317 lines (248 loc) · 12.5 KB
/
frozen_lake_dql_cnn.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
import gymnasium as gym
import numpy as np
import matplotlib.pyplot as plt
from collections import deque
import random
import torch
from torch import nn
import torch.nn.functional as F
import math
# Define model
class DQN(nn.Module):
def __init__(self, input_shape, out_actions):
super().__init__()
# https://poloclub.github.io/cnn-explainer/
self.conv_block1 = nn.Sequential(
nn.Conv2d(in_channels=input_shape, out_channels=10, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=10, out_channels=10, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2)
)
self.conv_block2 = nn.Sequential(
nn.Conv2d(in_channels=10, out_channels=10, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=10, out_channels=10, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2)
)
self.layer_stack = nn.Sequential(
nn.Flatten(), # flatten inputs into a single vector
# After flattening the matrix into a vector, pass it to the output layer. To determine the input shape, use the print() statement in forward()
nn.Linear(in_features=10*1*1, out_features=out_actions)
)
def forward(self, x):
x = self.conv_block1(x)
x = self.conv_block2(x)
# print(x.shape) # Use this to determine input shape of the output layer.
x = self.layer_stack(x)
return x
# Use this to check if DQN is valid
# temp_dqn = DQN(3, 4) # (3 channels, 4 actions)
# temp_tensor = torch.randn(1,3,4,4) # (batch, channel, row, column)
# temp_dqn(temp_tensor)
# Define memory for Experience Replay
class ReplayMemory():
def __init__(self, maxlen):
self.memory = deque([], maxlen=maxlen)
def append(self, transition):
self.memory.append(transition)
def sample(self, sample_size):
return random.sample(self.memory, sample_size)
def __len__(self):
return len(self.memory)
# FrozeLake Deep Q-Learning
class FrozenLakeDQL():
# Hyperparameters (adjustable)
learning_rate_a = 0.001 # learning rate (alpha)
discount_factor_g = 0.9 # discount rate (gamma)
network_sync_rate = 10 # number of steps the agent takes before syncing the policy and target network
replay_memory_size = 1000 # size of replay memory
mini_batch_size = 32 # size of the training data set sampled from the replay memory
# Neural Network
loss_fn = nn.MSELoss() # NN Loss function. MSE=Mean Squared Error can be swapped to something else.
optimizer = None # NN Optimizer. Initialize later.
ACTIONS = ['L','D','R','U'] # for printing 0,1,2,3 => L(eft),D(own),R(ight),U(p)
# Train the FrozeLake environment
def train(self, episodes, render=False, is_slippery=False):
# Create FrozenLake instance
env = gym.make('FrozenLake-v1', map_name="4x4", is_slippery=is_slippery, render_mode='human' if render else None)
num_states = env.observation_space.n
num_actions = env.action_space.n
epsilon = 1 # 1 = 100% random actions
memory = ReplayMemory(self.replay_memory_size)
# Create policy and target network.
policy_dqn = DQN(input_shape=3, out_actions=num_actions)
target_dqn = DQN(input_shape=3, out_actions=num_actions)
# Make the target and policy networks the same (copy weights/biases from one network to the other)
target_dqn.load_state_dict(policy_dqn.state_dict())
print('Policy (random, before training):')
self.print_dqn(policy_dqn)
# Policy network optimizer. "Adam" optimizer can be swapped to something else.
self.optimizer = torch.optim.Adam(policy_dqn.parameters(), lr=self.learning_rate_a)
# List to keep track of rewards collected per episode. Initialize list to 0's.
rewards_per_episode = np.zeros(episodes)
# List to keep track of epsilon decay
epsilon_history = []
# Track number of steps taken. Used for syncing policy => target network.
step_count=0
for i in range(episodes):
state = env.reset()[0] # Initialize to state 0
terminated = False # True when agent falls in hole or reached goal
truncated = False # True when agent takes more than 200 actions
# Agent navigates map until it falls into hole/reaches goal (terminated), or has taken 200 actions (truncated).
while(not terminated and not truncated):
# Select action based on epsilon-greedy
if random.random() < epsilon:
# select random action
action = env.action_space.sample() # actions: 0=left,1=down,2=right,3=up
else:
# select best action
with torch.no_grad():
action = policy_dqn(self.state_to_dqn_input(state)).argmax().item()
# Execute action
new_state,reward,terminated,truncated,_ = env.step(action)
# Save experience into memory
memory.append((state, action, new_state, reward, terminated))
# Move to the next state
state = new_state
# Increment step counter
step_count+=1
# Keep track of the rewards collected per episode.
if reward == 1:
rewards_per_episode[i] = 1
# Check if enough experience has been collected and if at least 1 reward has been collected
if len(memory)>self.mini_batch_size and np.sum(rewards_per_episode)>0:
mini_batch = memory.sample(self.mini_batch_size)
self.optimize(mini_batch, policy_dqn, target_dqn)
# Decay epsilon
epsilon = max(epsilon - 1/episodes, 0)
epsilon_history.append(epsilon)
# Copy policy network to target network after a certain number of steps
if step_count > self.network_sync_rate:
target_dqn.load_state_dict(policy_dqn.state_dict())
step_count=0
# Close environment
env.close()
# Save policy
torch.save(policy_dqn.state_dict(), "frozen_lake_dql_cnn.pt")
# Create new graph
plt.figure(1)
# Plot average rewards (Y-axis) vs episodes (X-axis)
sum_rewards = np.zeros(episodes)
for x in range(episodes):
sum_rewards[x] = np.sum(rewards_per_episode[max(0, x-100):(x+1)])
plt.subplot(121) # plot on a 1 row x 2 col grid, at cell 1
plt.plot(sum_rewards)
# Plot epsilon decay (Y-axis) vs episodes (X-axis)
plt.subplot(122) # plot on a 1 row x 2 col grid, at cell 2
plt.plot(epsilon_history)
# Save plots
plt.savefig('frozen_lake_dql_cnn.png')
# Optimize policy network
def optimize(self, mini_batch, policy_dqn, target_dqn):
current_q_list = []
target_q_list = []
for state, action, new_state, reward, terminated in mini_batch:
if terminated:
# Agent either reached goal (reward=1) or fell into hole (reward=0)
# When in a terminated state, target q value should be set to the reward.
target = torch.FloatTensor([reward])
else:
# Calculate target q value
with torch.no_grad():
target = torch.FloatTensor(
reward + self.discount_factor_g * target_dqn(self.state_to_dqn_input(new_state)).max()
)
# Get the current set of Q values
current_q = policy_dqn(self.state_to_dqn_input(state))
current_q_list.append(current_q)
# Get the target set of Q values
target_q = target_dqn(self.state_to_dqn_input(state))
# Adjust the specific action to the target that was just calculated.
# Target_q[batch][action], hardcode batch to 0 because there is only 1 batch.
target_q[0][action] = target
target_q_list.append(target_q)
# Compute loss for the whole minibatch
loss = self.loss_fn(torch.stack(current_q_list), torch.stack(target_q_list))
# Optimize the model
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
'''
Converts a state (int) to a tensor representation for input into CNN: tensor[batch][channel][row][column].
The FrozenLake 4x4 map has 4x4=16 states numbered from 0 to 15.
Example:
Input: state=1
Return: tensor([[[[0., .9, 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
repeat above matrix 2 more times
]])
where .9 is the normalize color code of Red
'''
def state_to_dqn_input(self, state:int)->torch.Tensor:
# create empty tensor[batch][channel][row][column].
# Converting 1 state, so batch is always 1.
# Channels = 3, Red Green Blue (RGB).
# FrozenLake map has 4 rows x 4 columns.
input_tensor = torch.zeros(1,3,4,4)
# convert state to row and column
r=math.floor(state/4)
c=state-r*4
# Set color for each channel. Normalize it. The color selected here is (R,G,B) = (245,66,120)
input_tensor[0][0][r][c] = 245/255
input_tensor[0][1][r][c] = 66/255
input_tensor[0][2][r][c] = 120/255
# To see what the image looks like, uncomment out the following lines AND put a break point at the return statement.
# pic = input_tensor.squeeze() # input_tensor[batch][channel][row][column] - use squeeze to remove the batch level
# pic = torch.movedim(pic, 0, 2) # rearrange from [channel][row][column] to [row][column][channel]
# plt.imshow(pic) # plot the image
# plt.show() # show the image
return input_tensor
# Run the FrozeLake environment with the learned policy
def test(self, episodes, is_slippery=False):
# Create FrozenLake instance
env = gym.make('FrozenLake-v1', map_name="4x4", is_slippery=is_slippery, render_mode='human')
num_states = env.observation_space.n
num_actions = env.action_space.n
# Load learned policy
policy_dqn = DQN(input_shape=3, out_actions=num_actions)
policy_dqn.load_state_dict(torch.load("frozen_lake_dql_cnn.pt"))
policy_dqn.eval() # switch model to evaluation mode
print('Policy (trained):')
self.print_dqn(policy_dqn)
for i in range(episodes):
state = env.reset()[0] # Initialize to state 0
terminated = False # True when agent falls in hole or reached goal
truncated = False # True when agent takes more than 200 actions
# Agent navigates map until it falls into a hole (terminated), reaches goal (terminated), or has taken 200 actions (truncated).
while(not terminated and not truncated):
# Select best action
with torch.no_grad():
action = policy_dqn(self.state_to_dqn_input(state)).argmax().item()
# Execute action
state,reward,terminated,truncated,_ = env.step(action)
env.close()
# Print DQN: state, best action, q values
def print_dqn(self, dqn):
# Loop each state and print policy to console
for s in range(16):
# Format q values for printing
q_values = ''
for q in dqn(self.state_to_dqn_input(s))[0].tolist():
q_values += "{:+.2f}".format(q)+' ' # Concatenate q values, format to 2 decimals
q_values=q_values.rstrip() # Remove space at the end
# Map the best action to L D R U
best_action = self.ACTIONS[dqn(self.state_to_dqn_input(s)).argmax()]
# Print policy in the format of: state, action, q values
# The printed layout matches the FrozenLake map.
print(f'{s:02},{best_action},[{q_values}]', end=' ')
if (s+1)%4==0:
print() # Print a newline every 4 states
if __name__ == '__main__':
frozen_lake = FrozenLakeDQL()
is_slippery = False
frozen_lake.train(1000, is_slippery=is_slippery)
frozen_lake.test(10, is_slippery=is_slippery)