Skip to content

Commit 53999c9

Browse files
committed
Have initial concept working.
1 parent 012e801 commit 53999c9

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

axelrod/tournament.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
from .game import Game
1313
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
1617

1718

1819
class Tournament(object):
@@ -55,6 +56,11 @@ def __init__(self, players, match_generator=RoundRobinMatches,
5556
self._with_morality = with_morality
5657
self._logger = logging.getLogger(__name__)
5758

59+
self.interactions_dict = None
60+
self.outputfile = None
61+
self.writer = None
62+
self.filename = None
63+
5864
def setup_output_file(self, filename=None):
5965
"""Open a CSV writer for tournament output."""
6066
if filename:
@@ -93,7 +99,13 @@ def play(self, build_results=True, filename=None,
9399
self.progress_bar = tqdm.tqdm(total=len(self.match_generator),
94100
desc="Playing matches")
95101

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+
97109
if not build_results and not filename:
98110
warnings.warn("Tournament results will not be accessible since build_results=False and no filename was supplied.")
99111

@@ -105,8 +117,8 @@ def play(self, build_results=True, filename=None,
105117
if progress_bar:
106118
self.progress_bar.close()
107119

108-
# Make sure that python has finished writing to disk
109-
self.outputfile.flush()
120+
if filename:
121+
self.outputfile.flush()
110122

111123
if build_results:
112124
return self._build_result_set(progress_bar=progress_bar,
@@ -122,14 +134,23 @@ def _build_result_set(self, progress_bar=True, keep_interactions=False):
122134
-------
123135
axelrod.BigResultSet
124136
"""
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)
133154
return result_set
134155

135156
def _run_serial(self, progress_bar=False):

0 commit comments

Comments
 (0)