-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.py
More file actions
95 lines (66 loc) · 2.2 KB
/
Copy pathTest.py
File metadata and controls
95 lines (66 loc) · 2.2 KB
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
import logging
import sys
import gym
from gym.wrappers import Monitor
import gym_ple
from Agents import DQN
def transformGameState(state):
import numpy as np
player_y = state['player_y']
player_vel = state['player_vel']
next_pipe_dist_to_player = state['next_pipe_dist_to_player']
next_pipe_top_y = state['next_pipe_top_y']
next_pipe_bottom_y = state['next_pipe_bottom_y']
next_next_pipe_dist_to_player = state['next_next_pipe_dist_to_player']
next_next_pipe_top_y = state['next_next_pipe_top_y']
next_next_pipe_bottom_y = state['next_next_pipe_bottom_y']
delta_top = player_y - next_pipe_top_y
delta_bottom = player_y - next_pipe_bottom_y
return np.array([delta_bottom, delta_top, player_vel, next_pipe_dist_to_player])
if __name__ == '__main__':
env = gym.make('FlappyBird-v0' if len(sys.argv) < 2 else sys.argv[1])
outdir = './tmp'
env = Monitor(env, directory=outdir, force=True)
env.seed(1)
dqn = DQN(env)
import torch
dqn.eval_net.load_state_dict(torch.load("standard.pt",map_location=torch.device('cpu')))
episode_count = 100
reward = 0
done = False
too_low = False
rewards = list()
for i in range(episode_count):
print('\nTesting weights...')
s = env.reset()
s = transformGameState(env.reset())
import pygame
clock = pygame.time.Clock()
ep_r = 0
while True:
a = dqn.choose_action(s)
if too_low:
a = 0
# take action
s_, r, done, info = env.step(a)
too_low = (s_['player_y'] > 330)
s_ = transformGameState(s_)
if r > 0:
ep_r += r
if done:
print('Ep: ', i,
'| Ep_r: ', round(ep_r, 2))
rewards.append(ep_r)
break
s = s_
clock.tick(30)
env.render()
print("episode: " + str(i) + " DONE")
# Dump result info to disk
env.close()
# Save results
import numpy as np
rewards = np.array(rewards)
mean = rewards.mean()
high = rewards.max()
print("Mean score: {}, Highest score: {}".format(mean, high))