-
Notifications
You must be signed in to change notification settings - Fork 14
/
Lunar_Lander.py
352 lines (285 loc) · 12.6 KB
/
Lunar_Lander.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
import gym
import numpy as np
import pandas as pd
from collections import deque
import random
from keras import Sequential
from keras.layers import Dense
from keras.activations import relu, linear
from keras.optimizers import Adam
from keras.losses import mean_squared_error
from keras.models import load_model
import pickle
from matplotlib import pyplot as plt
class DQN:
def __init__(self, env, lr, gamma, epsilon, epsilon_decay):
self.env = env
self.action_space = env.action_space
self.observation_space = env.observation_space
self.counter = 0
self.lr = lr
self.gamma = gamma
self.epsilon = epsilon
self.epsilon_decay = epsilon_decay
self.rewards_list = []
self.replay_memory_buffer = deque(maxlen=500000)
self.batch_size = 64
self.epsilon_min = 0.01
self.num_action_space = self.action_space.n
self.num_observation_space = env.observation_space.shape[0]
self.model = self.initialize_model()
def initialize_model(self):
model = Sequential()
model.add(Dense(512, input_dim=self.num_observation_space, activation=relu))
model.add(Dense(256, activation=relu))
model.add(Dense(self.num_action_space, activation=linear))
# Compile the model
model.compile(loss=mean_squared_error,optimizer=Adam(lr=self.lr))
print(model.summary())
return model
def get_action(self, state):
if np.random.rand() < self.epsilon:
return random.randrange(self.num_action_space)
predicted_actions = self.model.predict(state)
return np.argmax(predicted_actions[0])
def add_to_replay_memory(self, state, action, reward, next_state, done):
self.replay_memory_buffer.append((state, action, reward, next_state, done))
def learn_and_update_weights_by_reply(self):
# replay_memory_buffer size check
if len(self.replay_memory_buffer) < self.batch_size or self.counter != 0:
return
# Early Stopping
if np.mean(self.rewards_list[-10:]) > 180:
return
random_sample = self.get_random_sample_from_replay_mem()
states, actions, rewards, next_states, done_list = self.get_attribues_from_sample(random_sample)
targets = rewards + self.gamma * (np.amax(self.model.predict_on_batch(next_states), axis=1)) * (1 - done_list)
target_vec = self.model.predict_on_batch(states)
indexes = np.array([i for i in range(self.batch_size)])
target_vec[[indexes], [actions]] = targets
self.model.fit(states, target_vec, epochs=1, verbose=0)
def get_attribues_from_sample(self, random_sample):
states = np.array([i[0] for i in random_sample])
actions = np.array([i[1] for i in random_sample])
rewards = np.array([i[2] for i in random_sample])
next_states = np.array([i[3] for i in random_sample])
done_list = np.array([i[4] for i in random_sample])
states = np.squeeze(states)
next_states = np.squeeze(next_states)
return np.squeeze(states), actions, rewards, next_states, done_list
def get_random_sample_from_replay_mem(self):
random_sample = random.sample(self.replay_memory_buffer, self.batch_size)
return random_sample
def train(self, num_episodes=2000, can_stop=True):
for episode in range(num_episodes):
state = env.reset()
reward_for_episode = 0
num_steps = 1000
state = np.reshape(state, [1, self.num_observation_space])
for step in range(num_steps):
env.render()
received_action = self.get_action(state)
# print("received_action:", received_action)
next_state, reward, done, info = env.step(received_action)
next_state = np.reshape(next_state, [1, self.num_observation_space])
# Store the experience in replay memory
self.add_to_replay_memory(state, received_action, reward, next_state, done)
# add up rewards
reward_for_episode += reward
state = next_state
self.update_counter()
self.learn_and_update_weights_by_reply()
if done:
break
self.rewards_list.append(reward_for_episode)
# Decay the epsilon after each experience completion
if self.epsilon > self.epsilon_min:
self.epsilon *= self.epsilon_decay
# Check for breaking condition
last_rewards_mean = np.mean(self.rewards_list[-100:])
if last_rewards_mean > 200 and can_stop:
print("DQN Training Complete...")
break
print(episode, "\t: Episode || Reward: ",reward_for_episode, "\t|| Average Reward: ",last_rewards_mean, "\t epsilon: ", self.epsilon )
def update_counter(self):
self.counter += 1
step_size = 5
self.counter = self.counter % step_size
def save(self, name):
self.model.save(name)
def test_already_trained_model(trained_model):
rewards_list = []
num_test_episode = 100
env = gym.make("LunarLander-v2")
print("Starting Testing of the trained model...")
step_count = 1000
for test_episode in range(num_test_episode):
current_state = env.reset()
num_observation_space = env.observation_space.shape[0]
current_state = np.reshape(current_state, [1, num_observation_space])
reward_for_episode = 0
for step in range(step_count):
env.render()
selected_action = np.argmax(trained_model.predict(current_state)[0])
new_state, reward, done, info = env.step(selected_action)
new_state = np.reshape(new_state, [1, num_observation_space])
current_state = new_state
reward_for_episode += reward
if done:
break
rewards_list.append(reward_for_episode)
print(test_episode, "\t: Episode || Reward: ", reward_for_episode)
return rewards_list
def plot_df(df, chart_name, title, x_axis_label, y_axis_label):
plt.rcParams.update({'font.size': 17})
df['rolling_mean'] = df[df.columns[0]].rolling(100).mean()
plt.figure(figsize=(15, 8))
plt.close()
plt.figure()
# plot = df.plot(linewidth=1.5, figsize=(15, 8), title=title)
plot = df.plot(linewidth=1.5, figsize=(15, 8))
plot.set_xlabel(x_axis_label)
plot.set_ylabel(y_axis_label)
# plt.ylim((-400, 300))
fig = plot.get_figure()
plt.legend().set_visible(False)
fig.savefig(chart_name)
def plot_df2(df, chart_name, title, x_axis_label, y_axis_label):
df['mean'] = df[df.columns[0]].mean()
plt.rcParams.update({'font.size': 17})
plt.figure(figsize=(15, 8))
plt.close()
plt.figure()
# plot = df.plot(linewidth=1.5, figsize=(15, 8), title=title)
plot = df.plot(linewidth=1.5, figsize=(15, 8))
plot.set_xlabel(x_axis_label)
plot.set_ylabel(y_axis_label)
plt.ylim((0, 300))
plt.xlim((0, 100))
plt.legend().set_visible(False)
fig = plot.get_figure()
fig.savefig(chart_name)
def plot_experiments(df, chart_name, title, x_axis_label, y_axis_label, y_limit):
plt.rcParams.update({'font.size': 17})
plt.figure(figsize=(15, 8))
plt.close()
plt.figure()
plot = df.plot(linewidth=1, figsize=(15, 8), title=title)
plot.set_xlabel(x_axis_label)
plot.set_ylabel(y_axis_label)
plt.ylim(y_limit)
fig = plot.get_figure()
fig.savefig(chart_name)
def run_experiment_for_gamma():
print('Running Experiment for gamma...')
env = gym.make('LunarLander-v2')
# set seeds
env.seed(21)
np.random.seed(21)
# setting up params
lr = 0.001
epsilon = 1.0
epsilon_decay = 0.995
gamma_list = [0.99, 0.9, 0.8, 0.7]
training_episodes = 1000
rewards_list_for_gammas = []
for gamma_value in gamma_list:
# save_dir = "hp_gamma_"+ str(gamma_value) + "_"
model = DQN(env, lr, gamma_value, epsilon, epsilon_decay)
print("Training model for Gamma: {}".format(gamma_value))
model.train(training_episodes, False)
rewards_list_for_gammas.append(model.rewards_list)
pickle.dump(rewards_list_for_gammas, open("rewards_list_for_gammas.p", "wb"))
rewards_list_for_gammas = pickle.load(open("rewards_list_for_gammas.p", "rb"))
gamma_rewards_pd = pd.DataFrame(index=pd.Series(range(1, training_episodes + 1)))
for i in range(len(gamma_list)):
col_name = "gamma=" + str(gamma_list[i])
gamma_rewards_pd[col_name] = rewards_list_for_gammas[i]
plot_experiments(gamma_rewards_pd, "Figure 4: Rewards per episode for different gamma values",
"Figure 4: Rewards per episode for different gamma values", "Episodes", "Reward", (-600, 300))
def run_experiment_for_lr():
print('Running Experiment for learning rate...')
env = gym.make('LunarLander-v2')
# set seeds
env.seed(21)
np.random.seed(21)
# setting up params
lr_values = [0.0001, 0.001, 0.01, 0.1]
epsilon = 1.0
epsilon_decay = 0.995
gamma = 0.99
training_episodes = 1000
rewards_list_for_lrs = []
for lr_value in lr_values:
model = DQN(env, lr_value, gamma, epsilon, epsilon_decay)
print("Training model for LR: {}".format(lr_value))
model.train(training_episodes, False)
rewards_list_for_lrs.append(model.rewards_list)
pickle.dump(rewards_list_for_lrs, open("rewards_list_for_lrs.p", "wb"))
rewards_list_for_lrs = pickle.load(open("rewards_list_for_lrs.p", "rb"))
lr_rewards_pd = pd.DataFrame(index=pd.Series(range(1, training_episodes + 1)))
for i in range(len(lr_values)):
col_name = "lr="+ str(lr_values[i])
lr_rewards_pd[col_name] = rewards_list_for_lrs[i]
plot_experiments(lr_rewards_pd, "Figure 3: Rewards per episode for different learning rates", "Figure 3: Rewards per episode for different learning rates", "Episodes", "Reward", (-2000, 300))
def run_experiment_for_ed():
print('Running Experiment for epsilon decay...')
env = gym.make('LunarLander-v2')
# set seeds
env.seed(21)
np.random.seed(21)
# setting up params
lr = 0.001
epsilon = 1.0
ed_values = [0.999, 0.995, 0.990, 0.9]
gamma = 0.99
training_episodes = 1000
rewards_list_for_ed = []
for ed in ed_values:
save_dir = "hp_ed_"+ str(ed) + "_"
model = DQN(env, lr, gamma, epsilon, ed)
print("Training model for ED: {}".format(ed))
model.train(training_episodes, False)
rewards_list_for_ed.append(model.rewards_list)
pickle.dump(rewards_list_for_ed, open("rewards_list_for_ed.p", "wb"))
rewards_list_for_ed = pickle.load(open("rewards_list_for_ed.p", "rb"))
ed_rewards_pd = pd.DataFrame(index=pd.Series(range(1, training_episodes+1)))
for i in range(len(ed_values)):
col_name = "epsilon_decay = "+ str(ed_values[i])
ed_rewards_pd[col_name] = rewards_list_for_ed[i]
plot_experiments(ed_rewards_pd, "Figure 5: Rewards per episode for different epsilon(ε) decay", "Figure 5: Rewards per episode for different epsilon(ε) decay values", "Episodes", "Reward", (-600, 300))
if __name__ == '__main__':
env = gym.make('LunarLander-v2')
# set seeds
env.seed(21)
np.random.seed(21)
# setting up params
lr = 0.001
epsilon = 1.0
epsilon_decay = 0.995
gamma = 0.99
training_episodes = 2000
print('St')
model = DQN(env, lr, gamma, epsilon, epsilon_decay)
model.train(training_episodes, True)
# Save Everything
save_dir = "saved_models"
# Save trained model
model.save(save_dir + "trained_model.h5")
# Save Rewards list
pickle.dump(model.rewards_list, open(save_dir + "train_rewards_list.p", "wb"))
rewards_list = pickle.load(open(save_dir + "train_rewards_list.p", "rb"))
# plot reward in graph
reward_df = pd.DataFrame(rewards_list)
plot_df(reward_df, "Figure 1: Reward for each training episode", "Reward for each training episode", "Episode","Reward")
# Test the model
trained_model = load_model(save_dir + "trained_model.h5")
test_rewards = test_already_trained_model(trained_model)
pickle.dump(test_rewards, open(save_dir + "test_rewards.p", "wb"))
test_rewards = pickle.load(open(save_dir + "test_rewards.p", "rb"))
plot_df2(pd.DataFrame(test_rewards), "Figure 2: Reward for each testing episode","Reward for each testing episode", "Episode", "Reward")
print("Training and Testing Completed...!")
# Run experiments for hyper-parameter
run_experiment_for_lr()
run_experiment_for_ed()
run_experiment_for_gamma()