-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
53 lines (44 loc) · 1.88 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
from collections import deque
from time import sleep
import numpy as np
import torch
from unityagents import UnityEnvironment
from agents.dqn_agent import DQNAgent
from utils.config import generate_configuration_qnet
def test(env, agent, n_ep_train, n_episodes=10, sleep_t=0.0):
# Get the default brain
brain_name = env.brain_names[0]
scores = []
scores_window = deque(maxlen=100)
for i_episode in range(1, n_episodes + 1):
# Reset the environment and the score
env_info = env.reset(train_mode=False)[brain_name]
state = env_info.vector_observations[0]
score = 0
while True:
action = agent.act(state)
env_info = env.step(action)[brain_name]
sleep(sleep_t)
next_state, reward, done = env_info.vector_observations[0], env_info.rewards[0], env_info.local_done[0]
state = next_state
score += reward
if done:
break
scores_window.append(score)
scores.append(score)
print('\rTest Episode {}\tLast Score: {:.2f}\tAverage Score: {:.2f}'.format(i_episode, score,
np.mean(scores_window)),
end="")
print('\rTest after {} episode mean {:.2f}'.format(n_ep_train, np.mean(scores_window)))
return np.mean(scores_window)
if __name__ == '__main__':
env = UnityEnvironment(file_name="./Banana_Linux/Banana.x86_64")
brain_name = env.brain_names[0]
brain = env.brains[brain_name]
env_info = env.reset(train_mode=True)[brain_name]
action_size = brain.vector_action_space_size
state_size = len(env_info.vector_observations[0])
config = generate_configuration_qnet(action_size, state_size)
agent = DQNAgent(config)
agent.load_weights("./checkpoint.pth")
print(test(env, agent, 0, n_episodes=100, sleep_t=0))