11
11
12
12
from .game import Game
13
13
from .match import Match
14
- from .match_generator import RoundRobinMatches , ProbEndRoundRobinMatches , SpatialMatches , ProbEndSpatialMatches
15
- from .result_set import ResultSetFromFile
14
+ from .match_generator import (RoundRobinMatches , ProbEndRoundRobinMatches ,
15
+ SpatialMatches , ProbEndSpatialMatches )
16
+ from .result_set import ResultSetFromFile , ResultSet
16
17
17
18
18
19
class Tournament (object ):
@@ -55,6 +56,11 @@ def __init__(self, players, match_generator=RoundRobinMatches,
55
56
self ._with_morality = with_morality
56
57
self ._logger = logging .getLogger (__name__ )
57
58
59
+ self .interactions_dict = None
60
+ self .outputfile = None
61
+ self .writer = None
62
+ self .filename = None
63
+
58
64
def setup_output_file (self , filename = None ):
59
65
"""Open a CSV writer for tournament output."""
60
66
if filename :
@@ -93,7 +99,13 @@ def play(self, build_results=True, filename=None,
93
99
self .progress_bar = tqdm .tqdm (total = len (self .match_generator ),
94
100
desc = "Playing matches" )
95
101
96
- self .setup_output_file (filename )
102
+ if filename is not None :
103
+ self .outputfile = open (filename , 'w' )
104
+ self .writer = csv .writer (self .outputfile , lineterminator = '\n ' )
105
+ self .filename = filename
106
+ else :
107
+ self .interactions_dict = {}
108
+
97
109
if not build_results and not filename :
98
110
warnings .warn ("Tournament results will not be accessible since build_results=False and no filename was supplied." )
99
111
@@ -105,8 +117,8 @@ def play(self, build_results=True, filename=None,
105
117
if progress_bar :
106
118
self .progress_bar .close ()
107
119
108
- # Make sure that python has finished writing to disk
109
- self .outputfile .flush ()
120
+ if filename :
121
+ self .outputfile .flush ()
110
122
111
123
if build_results :
112
124
return self ._build_result_set (progress_bar = progress_bar ,
@@ -122,14 +134,23 @@ def _build_result_set(self, progress_bar=True, keep_interactions=False):
122
134
-------
123
135
axelrod.BigResultSet
124
136
"""
125
- result_set = ResultSetFromFile (filename = self .filename ,
126
- progress_bar = progress_bar ,
127
- num_interactions = self .num_interactions ,
128
- nrepetitions = self .repetitions ,
129
- players = [str (p ) for p in self .players ],
130
- keep_interactions = keep_interactions ,
131
- game = self .game )
132
- self .outputfile .close ()
137
+ # TODO Change ResultSet -> ResultSetFromDict and then have ResultSet
138
+ # which recognises the inputs (if a filename is provided is brings in
139
+ # ResultSetFromFile, if not it brings in ResultSetFromDict).
140
+ if self .filename is not None :
141
+ result_set = ResultSetFromFile (filename = self .filename ,
142
+ progress_bar = progress_bar ,
143
+ num_interactions = self .num_interactions ,
144
+ nrepetitions = self .repetitions ,
145
+ players = [str (p ) for p in self .players ],
146
+ keep_interactions = keep_interactions ,
147
+ game = self .game )
148
+ self .outputfile .close ()
149
+ else :
150
+ result_set = ResultSet (players = [str (p ) for p in self .players ],
151
+ interactions = self .interactions_dict ,
152
+ progress_bar = progress_bar ,
153
+ game = self .game )
133
154
return result_set
134
155
135
156
def _run_serial (self , progress_bar = False ):
0 commit comments