-
Notifications
You must be signed in to change notification settings - Fork 1
/
lunarlander.py
executable file
·257 lines (211 loc) · 10.8 KB
/
lunarlander.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
# Matt Corsaro
# Brown University CS 2951X Final Project
# Skill chaining for continuous Lunar Lander
# Original DQN code from:
# https://gist.github.com/heerad/d2b92c2f3a83b5e4be395546c17b274c#file-dqn-lunarlander-v2-py
import numpy as np
import gym
from gym import wrappers
import tensorflow as tf
import time
import datetime
import os
from os import path
import sys
import random
from collections import deque
import argparse
def main():
parser = argparse.ArgumentParser(description = "Lunar Lander")
parser.add_argument('--visualize', dest='visualize', action='store_true')
parser.add_argument('--no-visualize', dest='visualize', action='store_false')
parser.set_defaults(visualize=False)
parser.add_argument("--model", type=str, default="")
args = parser.parse_args()
# DQN Params
gamma = 0.99
# Hidden layer sizes
h1 = 200
h2 = 200
h3 = 200
lr = 5e-5
# decay per episode
lr_decay = 1
l2_reg = 1e-6
dropout = 0
num_episodes = 1000
# gym cuts off after 1000, anyway
max_steps_ep = 1000
update_slow_target_every = 100
train_every = 1
replay_memory_capacity = int(1e6)
minibatch_size = 1024
epsilon_start = 1.0
epsilon_end = 0.05
epsilon_decay_length = 10000
epsilon_decay_exp = 0.98
# game parameters
env = gym.make("LunarLander-v2")
state_dim = np.prod(np.array(env.observation_space.shape))
n_actions = env.action_space.n
####################################################################################################################
## Tensorflow
tf.reset_default_graph()
# placeholders
state_ph = tf.placeholder(dtype=tf.float32, shape=[None,state_dim]) # input to Q network
next_state_ph = tf.placeholder(dtype=tf.float32, shape=[None,state_dim]) # input to slow target network
action_ph = tf.placeholder(dtype=tf.int32, shape=[None]) # action indices (indices of Q network output)
reward_ph = tf.placeholder(dtype=tf.float32, shape=[None]) # rewards (go into target computation)
is_not_terminal_ph = tf.placeholder(dtype=tf.float32, shape=[None]) # indicators (go into target computation)
is_training_ph = tf.placeholder(dtype=tf.bool, shape=()) # for dropout
episode_reward = tf.Variable(0.)
tf.summary.scalar("Episode Reward", episode_reward)
r_summary_placeholder = tf.placeholder("float")
update_ep_reward = episode_reward.assign(r_summary_placeholder)
plot_epsilon = tf.Variable(0.)
tf.summary.scalar("Epsilon", plot_epsilon)
eps_summary_placeholder = tf.placeholder("float")
update_plot_epsilon = plot_epsilon.assign(eps_summary_placeholder)
# episode counter
episodes = tf.Variable(0.0, trainable=False, name='episodes')
episode_inc_op = episodes.assign_add(1)
# will use this to initialize both Q network and slowly-changing target network with same structure
def generate_network(s, trainable, reuse):
hidden = tf.layers.dense(s, h1, activation = tf.nn.relu, trainable = trainable, name = 'dense', reuse = reuse)
hidden_drop = tf.layers.dropout(hidden, rate = dropout, training = trainable & is_training_ph)
hidden_2 = tf.layers.dense(hidden_drop, h2, activation = tf.nn.relu, trainable = trainable, name = 'dense_1', \
reuse = reuse)
hidden_drop_2 = tf.layers.dropout(hidden_2, rate = dropout, training = trainable & is_training_ph)
hidden_3 = tf.layers.dense(hidden_drop_2, h3, activation = tf.nn.relu, trainable = trainable, name = 'dense_2',\
reuse = reuse)
hidden_drop_3 = tf.layers.dropout(hidden_3, rate = dropout, training = trainable & is_training_ph)
action_values = tf.squeeze(tf.layers.dense(hidden_drop_3, n_actions, trainable = trainable, name = 'dense_3', \
reuse = reuse))
return action_values
with tf.variable_scope('q_network') as scope:
# Q network applied to state_ph
q_action_values = generate_network(state_ph, trainable = True, reuse = False)
# Q network applied to next_state_ph (for double Q learning)
q_action_values_next = tf.stop_gradient(generate_network(next_state_ph, trainable = False, reuse = True))
# slow target network
with tf.variable_scope('slow_target_network', reuse=False):
# use stop_gradient to treat the output values as constant targets when doing backprop
slow_target_action_values = tf.stop_gradient(generate_network(next_state_ph, trainable = False, reuse = False))
# isolate vars for each network
q_network_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='q_network')
slow_target_network_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='slow_target_network')
# update values for slowly-changing target network to match current critic network
update_slow_target_ops = []
for i, slow_target_var in enumerate(slow_target_network_vars):
update_slow_target_op = slow_target_var.assign(q_network_vars[i])
update_slow_target_ops.append(update_slow_target_op)
update_slow_target_op = tf.group(*update_slow_target_ops, name='update_slow_target')
targets = reward_ph + is_not_terminal_ph * gamma * \
tf.gather_nd(slow_target_action_values, tf.stack((tf.range(minibatch_size), \
tf.cast(tf.argmax(q_action_values_next, axis=1), tf.int32)), axis=1))
# Estimated Q values for (s,a) from experience replay
estim_taken_action_vales = tf.gather_nd(q_action_values, tf.stack((tf.range(minibatch_size), action_ph), axis=1))
# loss function (with regularization)
loss = tf.reduce_mean(tf.square(targets - estim_taken_action_vales))
for var in q_network_vars:
if not 'bias' in var.name:
loss += l2_reg * 0.5 * tf.nn.l2_loss(var)
# optimizer
train_op = tf.train.AdamOptimizer(lr*lr_decay**episodes).minimize(loss)
# initialize session
sess = tf.Session()
sess.run(tf.global_variables_initializer())
timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y_%m_%d_%H_%M_%S')
board_name = "board_" + timestamp
saver = tf.train.Saver()
start_time = time.time()
if args.model == '':
#####################################################################################################
## Training
print "Training a new model."
writer = tf.summary.FileWriter(board_name)
writer.add_graph(sess.graph)
total_steps = 0
experience = deque(maxlen=replay_memory_capacity)
epsilon = epsilon_start
epsilon_linear_step = (epsilon_start-epsilon_end)/epsilon_decay_length
for ep in range(num_episodes):
total_reward = 0
steps_in_ep = 0
observation = env.reset()
for t in range(max_steps_ep):
# choose action according to epsilon-greedy policy wrt Q
if np.random.random() < epsilon:
action = np.random.randint(n_actions)
else:
q_s = sess.run(q_action_values,
feed_dict = {state_ph: observation[None], is_training_ph: False})
action = np.argmax(q_s)
# take step
next_observation, reward, done, _info = env.step(action)
if args.visualize:
env.render()
total_reward += reward
# add this to experience replay buffer
experience.append((observation, action, reward, next_observation, 0.0 if done else 1.0))
# update the slow target's weights to match the latest q network if it's time to do so
if total_steps%update_slow_target_every == 0:
_ = sess.run(update_slow_target_op)
# update network weights to fit a minibatch of experience
if total_steps%train_every == 0 and len(experience) >= minibatch_size:
# grab N (s,a,r,s') tuples from experience
minibatch = random.sample(experience, minibatch_size)
# do a train_op with all the inputs required
_ = sess.run(train_op,
feed_dict = {
state_ph: np.asarray([elem[0] for elem in minibatch]),
action_ph: np.asarray([elem[1] for elem in minibatch]),
reward_ph: np.asarray([elem[2] for elem in minibatch]),
next_state_ph: np.asarray([elem[3] for elem in minibatch]),
is_not_terminal_ph: np.asarray([elem[4] for elem in minibatch]),
is_training_ph: True})
observation = next_observation
total_steps += 1
steps_in_ep += 1
# linearly decay epsilon from epsilon_start to epsilon_end over epsilon_decay_length steps
if total_steps < epsilon_decay_length:
epsilon -= epsilon_linear_step
# then exponentially decay it every episode
elif done:
epsilon *= epsilon_decay_exp
if total_steps == epsilon_decay_length:
print('--------------------------------MOVING TO EXPONENTIAL EPSILON DECAY-----------------------------------------')
if done:
# Increment episode counter
_ = sess.run(episode_inc_op)
break
sess.run(update_ep_reward, feed_dict={r_summary_placeholder: total_reward})
sess.run(update_plot_epsilon, feed_dict={eps_summary_placeholder: epsilon})
summary_str = sess.run(tf.summary.merge_all())
writer.add_summary(summary_str, ep)
print('Episode %2i, Reward: %7.3f, Steps: %i, Next eps: %7.3f, Minutes: %7.3f'%\
(ep,total_reward,steps_in_ep, epsilon, (time.time() - start_time)/60))
saver.save(sess, os.getcwd() + '/' + timestamp + ".ckpt")
else:
print "Loading trained model from", args.model
saver.restore(sess, os.getcwd() + '/' + args.model)
attempts = 10
print "Load successful, playing for", attempts, "games."
for _ in range(attempts):
observation = env.reset()
total_reward = 0
steps = 0
for t in range(max_steps_ep):
q_s = sess.run(q_action_values, feed_dict = {state_ph: observation[None], is_training_ph: False})
action = np.argmax(q_s)
next_observation, reward, done, _info = env.step(action)
observation = next_observation
total_reward += reward
env.render()
steps += 1
if done:
break
print "Reward:", total_reward, "in", steps, "steps."
env.close()
if __name__ == '__main__':
main()