forked from hunkim/ReinforcementZeroToAll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07_2_dqn_2013_cartpole.py
232 lines (165 loc) · 5.89 KB
/
07_2_dqn_2013_cartpole.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
"""
This code is based on:
https://github.com/hunkim/DeepRL-Agents
CF https://github.com/golbin/TensorFlow-Tutorials
"""
import numpy as np
import tensorflow as tf
import random
import dqn
from collections import deque
import gym
env = gym.make('CartPole-v0')
env = gym.wrappers.Monitor(env, 'gym-results/', force=True)
# Constants defining our neural network
INPUT_SIZE = env.observation_space.shape[0]
OUTPUT_SIZE = env.action_space.n
# Q(s, a) = r + discount_rate * max Q(s_next, a)
DISCOUNT_RATE = 0.99
REPLAY_MEMORY = 50000
MAX_EPISODE = 5000
BATCH_SIZE = 32
# minimum epsilon for epsilon greedy
MIN_E = 0.01
# if epsilon decaying_episode = 100
# epsilon lineary decreases to MIN_E over 100 episodes
EPSILON_DECAYING_EPISODE = MAX_EPISODE * 0.1
def bot_play(mainDQN):
""" Run a single episode with the DQN agent
Parameters
----------
mainDQN : DQN agent
Returns
----------
reward : float
Episode reward is returned
"""
state = env.reset()
reward_sum = 0
while True:
env.render()
action = np.argmax(mainDQN.predict(state))
state, reward, done, _ = env.step(action)
reward_sum += reward
if done:
print("Total score: {}".format(reward_sum))
break
def simple_replay_train(DQN, train_batch):
""" Prepare X_batch, y_batch and train them
Recall our loss function is
target = reward + discount * max Q(s',a)
or reward if done early
Loss function: [target - Q(s, a)]^2
Hence,
X_batch is a state list
y_batch is reward + discount * max Q
or reward if terminated early
Parameters
----------
DQN : DQN Agent
train_batch : list, [item_1, item_2, ..., item_batchsize]
where item_i is also a list
item_i = [state, action, reward, next_state, done]
"""
# We use numpy to vectorize operations
tmp = np.asarray(train_batch)
# state_array.shape = (batch_size, 4)
state_array = np.vstack(tmp[:, 0])
# action_array.shape = (batch_size, )
action_array = tmp[:, 1].astype(np.int32)
# reward_array.shape = (batch_size, )
reward_array = tmp[:, 2]
# next_state_array.shape = (batch_size, 4)
next_state_array = np.vstack(tmp[:, 3])
# done_array.shape = (batch_size, )
done_array = tmp[:, 4].astype(np.int32)
X_batch = state_array
y_batch = DQN.predict(state_array)
# We use a vectorized operation
target = reward_array + DISCOUNT_RATE * np.max(DQN.predict(next_state_array), 1) * (1 - done_array)
y_batch[np.arange(len(X_batch)), action_array] = target
# Train our network using target and predicted Q values on each episode
return DQN.update(X_batch, y_batch)
def annealing_epsilon(episode, min_e, max_e, target_episode):
"""Return an linearly annealed epsilon
Parameters
----------
(epsilon)
|
max_e ---|\
| \
| \
| \
min_e ---|____\_______________(episode)
|
target_episode
slope = (min_e - max_e) / (target_episode)
intercept = max_e
e = slope * episode + intercept
"""
slope = (min_e - max_e) / (target_episode)
intercept = max_e
return max(min_e, slope * episode + intercept)
def main():
"""
pseudocode
For episode = 1, ..., M
s = initital state
For t = 1, ..., T
action = argmax Q(s, a)
Get s2, r, d by playing the action
save [s,a,r,s2,d] to memory
take a minibatch from memory
y = r
or r + d * max Q(s2, ...)
Perform train step on (y - Q(s, a))^2
"""
# store the previous observations in replay memory
replay_buffer = deque()
# Check whether we clear the game
# CartPole Clear Condition
# Avg Reward >= 195 over 100 games
# We will choose more strict condition by setting 199
last_100_game_reward = deque()
with tf.Session() as sess:
mainDQN = dqn.DQN(sess, INPUT_SIZE, OUTPUT_SIZE)
init = tf.global_variables_initializer()
sess.run(init)
for episode in range(MAX_EPISODE):
e = annealing_epsilon(episode, MIN_E, 1.0, EPSILON_DECAYING_EPISODE)
done = False
state = env.reset()
step_count = 0
while not done:
if np.random.rand() < e:
action = env.action_space.sample()
else:
# Choose an action by greedily from the Q-network
action = np.argmax(mainDQN.predict(state))
# Get new state and reward from environment
next_state, reward, done, _ = env.step(action)
if done:
reward = -1
# Save the experience to our buffer
replay_buffer.append((state, action, reward, next_state, done))
if len(replay_buffer) > REPLAY_MEMORY:
replay_buffer.popleft()
state = next_state
step_count += 1
if step_count % 4 == 0 and len(replay_buffer) > BATCH_SIZE:
# Minibatch works better
minibatch = random.sample(replay_buffer, BATCH_SIZE)
simple_replay_train(mainDQN, minibatch)
print("[Episode {:>5}] steps: {:>5} e: {:>5.2f}".format(episode, step_count, e))
last_100_game_reward.append(step_count)
if len(last_100_game_reward) > 100:
last_100_game_reward.popleft()
avg_reward = np.mean(last_100_game_reward)
if avg_reward > 199.0:
print("Game Cleared within {} episodes with avg reward {}".format(episode, avg_reward))
break
# Test run 5 times
for _ in range(5):
bot_play(mainDQN)
if __name__ == "__main__":
main()