-
Notifications
You must be signed in to change notification settings - Fork 1
/
Test.py
130 lines (104 loc) · 3.75 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import subprocess
from multiprocessing.dummy import Pool as ThreadPool
import codecs
import argparse
import platform
import random
import shutil
# Arguments for the ic20 tool
ARGUMENTS = []
# Seeds to play
SEEDS = []
# URL to connect to
URL = "http://localhost:50123"
# URL to AWS
AWS_URL = "https://udi8pt9vo9.execute-api.us-east-1.amazonaws.com/default/"
# Number of threads running at the same time
N_WORKERS = 4
# Location of executable
PATH = './ic20_linux'
# Parse arguments given to script
parser = argparse.ArgumentParser(description = 'Test the winrate of a server.')
parser.add_argument('-aws', action = 'store_true', help = 'If the flag is set AWS is tested, else localhost.')
parser.add_argument('-consistency', action = 'store_true', help = 'If the flag is set the given seeds are played multiple times to check for consitency.')
parser.add_argument('-file', action = 'store_true', help = 'If the flag is set the seeds.txt file is used. Overrides range.')
parser.add_argument('--threads', default = 4, type = int, help = 'Sets the amout of threads.')
parser.add_argument('--range', default = [1, 100], nargs = 2, type = int, help = 'Sets the range of seeds to play.')
args = parser.parse_args()
# Set URL to AWS URL
if(args.aws):
URL = AWS_URL
# Set seeds
SEEDS = list(range(args.range[0], args.range[1] + 1))
# Parse seeds out of seeds file
if(args.file):
SEEDS = []
with codecs.open('seeds.txt', mode='r', encoding='utf_8') as f:
for line in f.read().split("\n"):
if(line != ""):
SEEDS.append(int(line))
# Set number of worker threads
N_WORKERS = args.threads
# Update path for windows
if(platform.system() == 'Windows'):
PATH = 'ic20_windows.exe'
# Set full arguments list
ARGUMENTS = [PATH, '-u', URL, '-t', '0']
wins = 0
loss = 0
def calculateWinrate(outcomes):
wins = 0
games = 0
for outcome in outcomes:
if(outcome[1] == "win"):
wins += 1
if(outcome[1] != None):
games += 1
return wins / games
def playGame(seed):
global wins
global loss
global ARGUMENTS
args = ARGUMENTS[:]
args.extend(['-s', str(seed)])
result = subprocess.run(args, stdout=subprocess.PIPE).stdout.decode('utf-8')
if("win" in result):
wins += 1
print("Game: %d Seed: %d Outcome: Win \t Winrate %f%%" % ((wins + loss), seed, wins * 100 / (wins + loss)), end = "\r")
return (seed, "win")
elif("loss" in result):
loss += 1
print("Game: %d Seed: %d Outcome: Loss \t Winrate %f%%" % ((wins + loss), seed, wins * 100 / (wins + loss)), end = "\r")
return (seed, "loss")
# Make the Pool of workers
pool = ThreadPool(N_WORKERS)
# Open the URLs in their own threads
# and return the results
results = pool.map(playGame, SEEDS)
print(results)
print("%f%% winrate" % (calculateWinrate(results) * 100))
loss = 0
wins = 0
if(args.consistency):
oldWinrate = calculateWinrate(results)
while(1):
oldResults = results
results = pool.map(playGame, SEEDS)
print(results)
print("%f%% winrate" % (calculateWinrate(results) * 100))
differentSeed = set(results).symmetric_difference(set(oldResults))
print("Seeds that changed: " + str(differentSeed))
loss = 0
wins = 0
if args.train:
if(calculateWinrate(results) > oldWinrate):
# Save new best winrate
print("Saving constants")
oldWinrate = calculateWinrate(results)
else:
# Restore better constants
print("Restoring old constants")
shutil.copy('src/resources/oldConstants.txt', 'src/resources/constants.txt')
# Close the pool and wait for the work to finish
pool.close()
pool.join()