forked from lazavgeridis/LunarLander-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rl_landers.py
357 lines (275 loc) · 13 KB
/
rl_landers.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
"""
Environment: LunarLander-v2
Reward for moving from the top of the screen to the landing pad and zero speed is about 100..140 points.
If the lander moves away from the landing pad it loses reward. The episode finishes if the lander crashes or
comes to rest, receiving an additional -100 or +100 points. Each leg with ground contact is +10 points.
Firing the main engine is -0.3 points each frame. Firing the side engine is -0.03 points each frame.
Solved is 200 points.
> https://github.com/openai/gym/blob/master/gym/envs/box2d/lunar_lander.py <
"""
import gym
import torch
import collections
import os
import numpy as np
from utils import *
from exp_replay_memory import ReplayMemory
"""
render_freq: int
render the environment every 'render_freq' episodes
print_freq: int
print out training progress every 'print_freq' episodes
"""
def random_lander(env, n_episodes, print_freq=500, render_freq=500):
return_per_ep = [0.0]
for i in range(n_episodes):
state = env.reset()
t = 0
if (i + 1) % render_freq == 0:
render = True
else:
render = False
while True:
if render:
env.render()
action = env.action_space.sample()
observation, reward, done, _ = env.step(action)
return_per_ep[-1] += reward
if done:
if (i + 1) % print_freq == 0:
print("Episode finished after {} timesteps".format(t + 1))
print("Episode {}: Total return {}\n".format(i + 1, return_per_ep[-1]))
return_per_ep.append(0.0)
break
state = observation
t += 1
return return_per_ep
def mc_lander(env, n_episodes, gamma, min_eps, print_freq=500, render_freq=500):
q_states = collections.defaultdict(float) # note that the first insertion of a key initializes its value to 0.0
n_visits = collections.defaultdict(int) # note that the first insertion of a key initializes its value to 0
return_per_ep = [0.0]
episode_qstates = []
episode_return = []
epsilon = 1.0
num_actions = env.action_space.n
for i in range(n_episodes):
t = 0
curr_state = discretize_state(env.reset())
if (i + 1) % render_freq == 0:
render = True
else:
render = False
while True:
if render:
env.render()
# choose action A using ε-greedy policy
action = epsilon_greedy(q_states, curr_state, epsilon, num_actions)
# take action A, earn immediate reward and land into next state S'
observation, reward, done, _ = env.step(action)
qstate = curr_state + (action, )
episode_qstates.append(qstate)
# increment visit count = N(state, action)
n_visits[qstate] += 1
return_per_ep[-1] += reward
episode_return.append(reward)
if done:
if (i + 1) % print_freq == 0:
print("\nEpisode finished after {} timesteps".format(t+1))
print("Episode {}: Total return = {}".format(i + 1, return_per_ep[-1]))
print("Total keys in q_states dictionary = {}".format(len(q_states)))
print("Total keys in n_visits dictionary = {}".format(len(n_visits)))
if (i + 1) % 100 == 0:
mean_100ep_reward = round(np.mean(return_per_ep[-101:-1]), 1)
print("Last 100 episodes mean reward: {}".format(mean_100ep_reward))
############# policy evaluation step ######################################
# improve policy only when episode has terminated
for step, qstate in enumerate(episode_qstates):
q_states[qstate] += (discounted_return(episode_return[step: ], gamma) - q_states[qstate]) / n_visits[qstate]
###########################################################################
epsilon = decay_epsilon(epsilon, min_eps)
return_per_ep.append(0.0)
episode_qstates.clear()
episode_return.clear()
break
curr_state = discretize_state(observation)
t += 1
return return_per_ep
def sarsa_lander(env, n_episodes, gamma, lr, min_eps, print_freq=500, render_freq=500):
q_states = collections.defaultdict(float) # note that the first insertion of a key initializes its value to 0.0
return_per_ep = [0.0]
epsilon = 1.0
num_actions = env.action_space.n
for i in range(n_episodes):
t = 0
if (i + 1) % render_freq == 0:
render = True
else:
render = False
# Initial episode state: S
curr_state = discretize_state(env.reset())
# Choose A from S using policy π
action = epsilon_greedy(q_states, curr_state, epsilon, num_actions)
while True:
if render:
env.render()
# Create (S, A) pair
qstate = curr_state + (action, )
# Take action A, earn immediate reward R and land into next state S'
# S --> A --> R --> S'
observation, reward, done, _ = env.step(action)
next_state = discretize_state(observation)
# Next State: S'
# Choose A' from S' using policy π
next_action = epsilon_greedy(q_states, next_state, epsilon, num_actions)
# create (S', A') pair
new_qstate = next_state + (next_action, )
###################################################################
# Policy evaluation step
if not done:
q_states[qstate] += lr * (reward + gamma * q_states[new_qstate] - q_states[qstate]) # (S', A') non terminal state
else:
q_states[qstate] += lr * (reward - q_states[qstate]) # (S', A') terminal state
###################################################################
return_per_ep[-1] += reward
if done:
if (i + 1) % print_freq == 0:
print("\nEpisode finished after {} timesteps".format(t + 1))
print("Episode {}: Total Return = {}".format(i + 1, return_per_ep[-1]))
print("Total keys in q_states dictionary = {}".format(len(q_states)))
if (i + 1) % 100 == 0:
mean_100ep_reward = round(np.mean(return_per_ep[-101:-1]), 1)
print("Last 100 episodes mean reward: {}".format(mean_100ep_reward))
epsilon = decay_epsilon(epsilon, min_eps)
return_per_ep.append(0.0)
break
curr_state = next_state
action = next_action
t += 1
return return_per_ep
def qlearning_lander(env, n_episodes, gamma, lr, min_eps, print_freq=500, render_freq=500):
q_states = collections.defaultdict(float) # note that the first insertion of a key initializes its value to 0.0
return_per_ep = [0.0]
epsilon = 1.0
num_actions = env.action_space.n
for i in range(n_episodes):
t = 0
if (i + 1) % render_freq == 0:
render = True
else:
render = False
# Initial episode state: S
curr_state = discretize_state(env.reset())
while True:
if render:
env.render()
# choose action A from S using behaviour policy -> ε-greedy
action = epsilon_greedy(q_states, curr_state, epsilon, num_actions)
# Create (S, A) pair
qstate = curr_state + (action, )
# Take action A, earn immediate reward R and land into next state S'
# S --> A --> R --> S'
observation, reward, done, _ = env.step(action)
next_state = discretize_state(observation)
###################################################################
# Policy evaluation step
if not done:
q_states[qstate] += lr * (reward + gamma * greedy(q_states, next_state, num_actions) - q_states[qstate]) # (S', A') non terminal state
else:
q_states[qstate] += lr * (reward - q_states[qstate]) # (S', A') terminal state
###################################################################
return_per_ep[-1] += reward
if done:
if (i + 1) % print_freq == 0:
print("\nEpisode finished after {} timesteps".format(t + 1))
print("Episode {}: Total Return = {}".format(i + 1, return_per_ep[-1]))
print("Total keys in q_states dictionary = {}".format(len(q_states)))
if (i + 1) % 100 == 0:
mean_100ep_reward = round(np.mean(return_per_ep[-101:-1]), 1)
print("Last 100 episodes mean reward: {}".format(mean_100ep_reward))
epsilon = decay_epsilon(epsilon, min_eps)
return_per_ep.append(0.0)
break
curr_state = next_state
t += 1
return return_per_ep
def dqn_lander(env, n_episodes, gamma, lr, min_eps, \
batch_size=32, memory_capacity=50000, \
network='linear', learning_starts=1000, \
train_freq=1, target_network_update_freq=1000, \
print_freq=500, render_freq=500, save_freq=1000):
# set device to run on
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
loss_function = torch.nn.MSELoss()
# path to save checkpoints
PATH = "./models"
if not os.path.isdir(PATH):
os.mkdir(PATH)
num_actions = env.action_space.n
input_shape = env.observation_space.shape[-1]
qnet, qnet_optim = build_qnetwork(num_actions, lr, input_shape, network, device)
qtarget_net, _ = build_qnetwork(num_actions, lr, input_shape, network, device)
qtarget_net.load_state_dict(qnet.state_dict())
qnet.train()
qtarget_net.eval()
replay_memory = ReplayMemory(memory_capacity)
epsilon = 1.0
return_per_ep = [0.0]
saved_mean_reward = None
t = 0
for i in range(n_episodes):
curr_state = lmn_input(env.reset())
if (i + 1) % render_freq == 0:
render = True
else:
render = False
while True:
if render:
env.render()
# choose action A using behaviour policy -> ε-greedy; use q-network
action = epsilon_greedy(qnet, curr_state.to(device), epsilon, num_actions)
# take action A, earn immediate reward R and land into next state S'
next_state, reward, done, _ = env.step(action)
#next_frame = get_frame(env)
next_state = lmn_input(next_state)
# store transition (S, A, R, S', Done) in replay memory
replay_memory.store(curr_state, action, float(reward), next_state, float(done))
# if replay memory currently stores > 'learning_starts' transitions,
# sample a random mini-batch and update q_network's parameters
if t > learning_starts and t % train_freq == 0:
states, actions, rewards, next_states, dones = replay_memory.sample_minibatch(batch_size)
#loss =
fit(qnet, \
qnet_optim, \
qtarget_net, \
loss_function, \
states, \
actions, \
rewards, \
next_states, \
dones, \
gamma, \
num_actions,
device)
# periodically update q-target network's parameters
if t > learning_starts and t % target_network_update_freq == 0:
update_target_network(qnet, qtarget_net)
t += 1
return_per_ep[-1] += reward
if done:
if (i + 1) % print_freq == 0:
print("\nEpisode: {}".format(i + 1))
print("Episode return : {}".format(return_per_ep[-1]))
print("Total time-steps: {}".format(t))
if (i + 1) % 100 == 0:
mean_100ep_reward = round(np.mean(return_per_ep[-101:-1]), 1)
print("\nLast 100 episodes mean reward: {}".format(mean_100ep_reward))
if t > learning_starts and (i + 1) % save_freq == 0:
if saved_mean_reward is None or mean_100ep_reward > saved_mean_reward:
print("\nSaving model due to mean reward increase: {} -> {}".format(saved_mean_reward, mean_100ep_reward))
save_model(qnet, i + 1, PATH)
saved_mean_reward = mean_100ep_reward
return_per_ep.append(0.0)
epsilon = decay_epsilon(epsilon, min_eps)
break
curr_state = next_state
return return_per_ep