-
Notifications
You must be signed in to change notification settings - Fork 42
/
tournament.py
executable file
·78 lines (67 loc) · 2.41 KB
/
tournament.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
#!/usr/bin/env python
import sys
import ConfigParser
from cells import Game
config = ConfigParser.RawConfigParser()
def get_mind(name):
full_name = 'minds.' + name
__import__(full_name)
mind = sys.modules[full_name]
mind.name = name
return mind
bounds = None # HACK
symmetric = None
mind_list = None
def main():
global bounds, symmetric, mind_list
try:
config.read('tournament.cfg')
bounds = config.getint('terrain', 'bounds')
symmetric = config.getboolean('terrain', 'symmetric')
minds_str = str(config.get('minds', 'minds'))
except Exception as e:
print 'Got error: %s' % e
config.add_section('minds')
config.set('minds', 'minds', 'mind1,mind2')
config.add_section('terrain')
config.set('terrain', 'bounds', '300')
config.set('terrain', 'symmetric', 'true')
with open('tournament.cfg', 'wb') as configfile:
config.write(configfile)
config.read('tournament.cfg')
bounds = config.getint('terrain', 'bounds')
symmetric = config.getboolean('terrain', 'symmetric')
minds_str = str(config.get('minds', 'minds'))
mind_list = [(n, get_mind(n)) for n in minds_str.split(',')]
# accept command line arguments for the minds over those in the config
try:
if len(sys.argv)>2:
mind_list = [(n,get_mind(n)) for n in sys.argv[1:] ]
except (ImportError, IndexError):
pass
if __name__ == "__main__":
main()
scores = [0 for x in mind_list]
tournament_list = [[mind_list[a], mind_list[b]] for a in range(len(mind_list)) for b in range (a)]
for n in range(4):
for pair in tournament_list:
game = Game(bounds, pair, symmetric, 5000, headless = True)
while game.winner == None:
game.tick()
if game.winner >= 0:
idx = mind_list.index(pair[game.winner])
scores[idx] += 3
if game.winner == -1:
idx = mind_list.index(pair[0])
scores[idx] += 1
idx = mind_list.index(pair[1])
scores[idx] += 1
print scores
print [m[0] for m in mind_list]
names = [m[0] for m in mind_list]
name_score = zip(names,scores)
f = open("scores.csv",'w')
srt = sorted(name_score,key=lambda ns: -ns[1])
for x in srt:
f.write("%s;%s\n" %(x[0],str(x[1])))
f.close()