-
Notifications
You must be signed in to change notification settings - Fork 1
/
rl_landers.py
313 lines (227 loc) · 10.1 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
import gym
import torch
import collections
import os
import numpy as np
from utils import *
from exp_replay_memory import ReplayMemory
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)
n_visits = collections.defaultdict(int)
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()
action = epsilon_greedy(q_states, curr_state, epsilon, num_actions)
observation, reward, done, _ = env.step(action)
qstate = curr_state + (action, )
episode_qstates.append(qstate)
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))
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)
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
curr_state = discretize_state(env.reset())
action = epsilon_greedy(q_states, curr_state, epsilon, num_actions)
while True:
if render:
env.render()
qstate = curr_state + (action, )
observation, reward, done, _ = env.step(action)
next_state = discretize_state(observation)
next_action = epsilon_greedy(q_states, next_state, epsilon, num_actions)
new_qstate = next_state + (next_action, )
if not done:
q_states[qstate] += lr * (reward + gamma * q_states[new_qstate] - q_states[qstate])
else:
q_states[qstate] += lr * (reward - q_states[qstate])
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)
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
curr_state = discretize_state(env.reset())
while True:
if render:
env.render()
action = epsilon_greedy(q_states, curr_state, epsilon, num_actions)
qstate = curr_state + (action, )
observation, reward, done, _ = env.step(action)
next_state = discretize_state(observation)
if not done:
q_states[qstate] += lr * (reward + gamma * greedy(q_states, next_state, num_actions) - q_states[qstate])
else:
q_states[qstate] += lr * (reward - q_states[qstate])
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):
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
loss_function = torch.nn.MSELoss()
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()
action = epsilon_greedy(qnet, curr_state.to(device), epsilon, num_actions)
next_state, reward, done, _ = env.step(action)
#next_frame = get_frame(env)
next_state = lmn_input(next_state)
replay_memory.store(curr_state, action, float(reward), next_state, float(done))
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)
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