/
pit.py
61 lines (47 loc) · 1.42 KB
/
pit.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
import Arena
from MCTS import MCTS
from azul.AzulGame import AzulGame
from azul.AzulPlayers import RandomPlayer, HumanAzulPlayer
from azul.pytorch.NNet import NNetWrapper as NNet
import cProfile, pstats, io
from pstats import SortKey
import numpy as np
from utils import *
"""
use this script to play any two agents against each other, or play manually with
any agent.
"""
human_vs_cpu = False
ai = False
profiling = True
g = AzulGame(shouldRandomize=False)
# all players
rp = RandomPlayer(g).play
rp2 = RandomPlayer(g).play
hp = HumanAzulPlayer(g).play
if human_vs_cpu:
player2 = hp
else:
if (ai):
n2 = NNet(g)
n2.load_checkpoint('./temp/', 'best.pth.tar')
args2 = dotdict({'numMCTSSims': 50, 'cpuct': 1.0})
mcts2 = MCTS(g, n2, args2)
n2p = lambda x: np.argmax(mcts2.getActionProb(x, temp=0))
player2 = n2p # Player 2 is neural network if it's cpu vs cpu.
else:
player2 = rp2
if profiling:
_output_file = 'perfTesting.prof'
pr = cProfile.Profile()
pr.enable()
arena = Arena.Arena(rp, player2, g, display=AzulGame.display)
print(arena.playGames(100, verbose=False))
if profiling:
pr.disable()
pr.dump_stats(_output_file)
with open(_output_file, 'w') as f:
ps = pstats.Stats(pr, stream=f)
ps.strip_dirs()
ps.sort_stats(SortKey.CUMULATIVE)
ps.print_stats()