forked from algolab-inc/tf-dqn-simple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
80 lines (60 loc) · 2.04 KB
/
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
from __future__ import division
import argparse
import os
import matplotlib.animation as animation
import matplotlib.pyplot as plt
from catch_ball import CatchBall
from dqn_agent import DQNAgent
def init():
img.set_array(state_t_1)
plt.axis("off")
return img,
def animate(step):
global win, lose
global state_t_1, reward_t, terminal
if terminal:
env.reset()
# for log
if reward_t == 1:
win += 1
elif reward_t == -1:
lose += 1
print("WIN: {:03d}/{:03d} ({:.1f}%)".format(win, win + lose, 100 * win / (win + lose)))
else:
state_t = state_t_1
# execute action in environment
action_t = agent.select_action(state_t, 0.0)
env.execute_action(action_t)
# observe environment
state_t_1, reward_t, terminal = env.observe()
# animate
img.set_array(state_t_1)
plt.axis("off")
return img,
if __name__ == "__main__":
# args
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--model_path")
parser.add_argument("-s", "--save", dest="save", action="store_true")
parser.set_defaults(save=False)
args = parser.parse_args()
# environmet, agent
env = CatchBall()
agent = DQNAgent(env.enable_actions, env.name)
agent.load_model(args.model_path)
# variables
win, lose = 0, 0
state_t_1, reward_t, terminal = env.observe()
# animate
fig = plt.figure(figsize=(env.screen_n_rows / 2, env.screen_n_cols / 2))
fig.canvas.set_window_title("{}-{}".format(env.name, agent.name))
img = plt.imshow(state_t_1, interpolation="none", cmap="gray")
ani = animation.FuncAnimation(fig, animate, init_func=init, interval=(1000 / env.frame_rate), blit=True)
if args.save:
# save animation (requires ImageMagick)
ani_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "tmp", "demo-{}.gif".format(env.name))
ani.save(ani_path, writer="imagemagick", fps=env.frame_rate)
else:
# show animation
plt.show()