-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_script_stage_1.py
72 lines (58 loc) · 2.07 KB
/
eval_script_stage_1.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
# PyTorch imports
import numpy as np
import torch
import onnx
from onnx2pytorch import ConvertModel
import argparse
import sys
from gym import logger as gymlogger
# Environment import and set logger level to display error only
gymlogger.set_level(40) # error only
# ignore prints to stdout of imports
save_stdout = sys.stdout
sys.stdout = open('trash', 'w')
import os
os.system("git clone https://github.com/MultiAgentLearning/playground ./pommer_setup")
os.system("pip install -U ./pommer_setup")
os.system('rm -rf ./pommer_setup')
os.system("git clone https://github.com/RLCommunity/graphic_pomme_env ./graphic_pomme_env")
os.system("pip install -U ./graphic_pomme_env")
os.system('rm -rf ./graphic_pomme_env')
sys.stdout = save_stdout
from graphic_pomme_env import graphic_pomme_env
from graphic_pomme_env.wrappers import PommerEnvWrapperFrameSkip2
# Seed random number generators
if os.path.exists("seed.rnd"):
with open("seed.rnd", "r") as f:
seed = int(f.readline().strip())
np.random.seed(seed)
torch.manual_seed(seed)
else:
seed = None
if __name__ == "__main__":
N_EPISODES = 50
parser = argparse.ArgumentParser()
parser.add_argument("--submission", type=str)
args = parser.parse_args()
model_file = args.submission
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Network
net = ConvertModel(onnx.load(model_file), experimental=True)
net = net.to(device)
net.eval()
win_count = 0.0
env = PommerEnvWrapperFrameSkip2(num_stack=5, start_pos=0, board='GraphicOVOCompact-v0')
for i in range(N_EPISODES):
if seed is not None:
seed = np.random.randint(1e7)
done = False
obs, opponent_obs = env.reset()
while not done:
obs = torch.from_numpy(np.array(obs)).float().to(device)
net_out = net(obs).detach().cpu().numpy()
action = np.argmax(net_out)
agent_step, opponent_step = env.step(action)
obs, r, done, info = agent_step
if r > 0:
win_count += 1
print(win_count / N_EPISODES)