-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_DDQN_test.py
258 lines (197 loc) · 7.19 KB
/
maze_DDQN_test.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
from maze_env import Maze
from DDQN_RL_brain import DoubleDQN
from DDQN_RL_brain import DDQNWithPresetReplay
import pickle
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
to_plot_steps = []
to_plot_rewards = []
labels = []
max_episode = 800
def run_maze():
rewards = []
total_reward = 0
step = 0
for episode in range(max_episode):
# initial observation
observation = env.reset()
while True:
# fresh env
# env.render()
# RL choose action based on observation
action = RL.choose_action(observation)
# RL take action and get next observation and reward
observation_, reward, done = env.step(action)
rewards.append(reward)
RL.store_transition(observation, action, reward, observation_)
if (step > 200) and (step % 5 == 0):
RL.learn()
pass
# swap observation
observation = observation_
# break while loop when end of this episode
if done:
print('episode {}'.format(episode))
break
step += 1
# end of game
print('game over')
rewards = np.array(rewards, dtype=np.float32)
steps = np.arange(rewards.shape[0]) + 1
rewards = np.cumsum(rewards)
r = rewards * 1. / steps
to_plot_steps.append(steps)
to_plot_rewards.append(r)
env.destroy()
save_path = 'models/mimic.pickle'
if __name__ == "__main__":
# maze game
env = Maze()
with tf.variable_scope('supervised'):
RL = DoubleDQN(env.n_actions, env.n_features,
learning_rate=0.01,
reward_decay=0.9,
e_greedy=0.9,
replace_target_iter=200,
memory_size=2000,
# output_graph=True
)
with open(save_path, 'rb') as f:
train_data = pickle.load(f)
X = train_data[:, :env.n_features]
Y_ = train_data[:, env.n_features].astype(np.uint32)
Y = np.zeros((Y_.shape[0], env.n_actions))
# Y[np.arange(Y_.shape[0]), Y_] = 1
Y += 1. / (env.n_actions + 1)
Y[np.arange(Y_.shape[0]), Y_] += 1. / (env.n_actions + 1)
RL.mimic_learn(X, Y)
labels.append('soft-supervised')
env.after(100, run_maze)
env.mainloop()
print('second start')
env = Maze()
with tf.variable_scope('ddqn'):
RL = DoubleDQN(env.n_actions, env.n_features,
learning_rate=0.01,
reward_decay=0.9,
e_greedy=0.9,
replace_target_iter=200,
memory_size=2000,
# output_graph=True
)
labels.append('ddqn')
env.after(100, run_maze)
env.mainloop()
print('third start')
env = Maze()
with tf.variable_scope('strong-supervised'):
RL = DoubleDQN(env.n_actions, env.n_features,
learning_rate=0.01,
reward_decay=0.9,
e_greedy=0.9,
replace_target_iter=200,
memory_size=2000,
# output_graph=True
)
with open(save_path, 'rb') as f:
train_data = pickle.load(f)
X = train_data[:, :env.n_features]
Y_ = train_data[:, env.n_features].astype(np.uint32)
Y = np.zeros((Y_.shape[0], env.n_actions))
Y[np.arange(Y_.shape[0]), Y_] = 1
RL.mimic_learn(X, Y)
labels.append('strong-supervised')
env.after(100, run_maze)
env.mainloop()
print('forth start')
env = Maze()
with tf.variable_scope('static-memory-preset'):
RL = DoubleDQN(env.n_actions, env.n_features,
learning_rate=0.01,
reward_decay=0.9,
e_greedy=0.9,
replace_target_iter=200,
memory_size=2000,
# output_graph=True
)
with open(save_path, 'rb') as f:
train_data = pickle.load(f)
for memory in train_data:
RL.store_transition(memory[:env.n_features], memory[env.n_features], memory[env.n_features + 1],
memory[-env.n_features:])
labels.append('static-memory-preset')
env.after(100, run_maze)
env.mainloop()
print('fifth start')
env = Maze()
with tf.variable_scope('prior-memory'):
RL = DDQNWithPresetReplay(env.n_actions, env.n_features,
learning_rate=0.01,
reward_decay=0.9,
e_greedy=0.9,
replace_target_iter=200,
memory_size=2000,
preset_replay_base=0
# output_graph=True
)
with open(save_path, 'rb') as f:
train_data = pickle.load(f)
RL.preset_memory(train_data)
labels.append('prior-memory')
env.after(100, run_maze)
env.mainloop()
# print('forth start :v.2')
# env = Maze()
# with tf.variable_scope('preset-memory-full'):
# RL = DoubleDQN(env.n_actions, env.n_features,
# learning_rate=0.01,
# reward_decay=0.9,
# e_greedy=0.9,
# replace_target_iter=200,
# memory_size=2000,
# # output_graph=True
# )
# with open(save_path, 'rb') as f:
# train_data = pickle.load(f)
#
# while True:
# index = RL.memory_counter if hasattr(RL, 'memory_counter') else 0
# if index == RL.memory_size:
# break
# index = index % len(train_data)
# RL.store_transition(train_data[index, :env.n_features], train_data[index, env.n_features],
# train_data[index, env.n_features + 1], train_data[index, -env.n_features:])
#
# labels.append('preset-memory-v2')
# env.after(100, run_maze)
# env.mainloop()
to_plots = [
['soft-supervised', 'ddqn'],
['strong-supervised', 'ddqn'],
['strong-supervised', 'soft-supervised', 'ddqn'],
['ddqn', 'static-memory-preset'],
['ddqn', 'prior-memory'],
['ddqn', 'static-memory-preset', 'prior-memory'],
['strong-supervised', 'soft-supervised', 'ddqn', 'static-memory-preset', 'prior-memory']
]
def plotttt(lbs):
plt.figure()
for lb in lbs:
idx = labels.index(lb)
plt.plot(to_plot_steps[idx], to_plot_rewards[idx], label=lb)
plt.xlabel('step')
plt.ylabel('average reward')
plt.legend()
plt.grid()
plt.show()
for to_plot in to_plots:
plotttt(to_plot)
# for i, (t, r) in enumerate(zip(to_plot_steps, to_plot_rewards)):
# plt.plot(t, r, label=labels[i])
# print('total step {}, reward {}'.format(t.shape[0], r[-1]))
# plt.xlabel('step')
# plt.ylabel('average reward')
# plt.legend()
# plt.grid()
# plt.show()