Skip to content

Commit 3e2fef1

Browse files
committed
Revert "Remove keep interactions for resultsset from file"
This reverts commit af41e22.
1 parent af41e22 commit 3e2fef1

File tree

5 files changed

+46
-23
lines changed

5 files changed

+46
-23
lines changed

axelrod/result_set.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ class ResultSetFromFile(ResultSet):
753753

754754
def __init__(self, filename, progress_bar=True,
755755
num_interactions=False, players=False, nrepetitions=False,
756-
game=None):
756+
game=None, keep_interactions=False):
757757
"""
758758
Parameters
759759
----------
@@ -773,6 +773,10 @@ def __init__(self, filename, progress_bar=True,
773773
efficiently read from file.
774774
game : axelrod.Game
775775
The particular game that should be used to calculate the scores.
776+
keep_interactions : bool
777+
Whether or not to load the interactions in to memory. WARNING:
778+
for large tournaments this drastically increases the memory
779+
required.
776780
"""
777781
if game is None:
778782
self.game = Game()
@@ -788,8 +792,9 @@ def __init__(self, filename, progress_bar=True,
788792
self.players, self.nrepetitions = players, nrepetitions
789793
self.nplayers = len(self.players)
790794

791-
self._build_empty_metrics()
792-
self._build_score_related_metrics(progress_bar=progress_bar)
795+
self._build_empty_metrics(keep_interactions=keep_interactions)
796+
self._build_score_related_metrics(progress_bar=progress_bar,
797+
keep_interactions=keep_interactions)
793798

794799
def create_progress_bar(self, desc=None):
795800
"""
@@ -938,10 +943,16 @@ def read_match_chunks(self, progress_bar=False):
938943
if progress_bar:
939944
progress_bar.close()
940945

941-
def _build_empty_metrics(self):
946+
def _build_empty_metrics(self, keep_interactions=False):
942947
"""
943948
Creates the various empty metrics ready to be updated as the data is
944949
read.
950+
951+
Parameters
952+
----------
953+
954+
keep_interactions : bool
955+
Whether or not to load the interactions in to memory
945956
"""
946957
plist = range(self.nplayers)
947958
replist = range(self.nrepetitions)
@@ -962,6 +973,8 @@ def _build_empty_metrics(self):
962973
self.total_interactions = [0 for player in plist]
963974
self.good_partner_rating = [0 for player in plist]
964975

976+
if keep_interactions:
977+
self.interactions = {}
965978

966979
def _update_match_lengths(self, repetition, p1, p2, interaction):
967980
self.match_lengths[repetition][p1][p2] = len(interaction)
@@ -1041,14 +1054,17 @@ def build_good_partner_rating(self):
10411054
max(1, float(self.total_interactions[player]))
10421055
for player in range(self.nplayers)]
10431056

1044-
def _build_score_related_metrics(self, progress_bar=False):
1057+
def _build_score_related_metrics(self, progress_bar=False,
1058+
keep_interactions=False):
10451059
"""
10461060
Read the data and carry out all relevant calculations.
10471061
10481062
Parameters
10491063
----------
10501064
progress_bar : bool
10511065
Whether or not to display a progress bar
1066+
keep_interactions : bool
1067+
Whether or not to lad the interactions in to memory
10521068
"""
10531069
match_chunks = self.read_match_chunks(progress_bar)
10541070

@@ -1058,6 +1074,12 @@ def _build_score_related_metrics(self, progress_bar=False):
10581074
for repetition, record in enumerate(match):
10591075
interaction = list(zip(record[4], record[5]))
10601076

1077+
if keep_interactions:
1078+
try:
1079+
self.interactions[(p1, p2)].append(interaction)
1080+
except KeyError:
1081+
self.interactions[(p1, p2)] = [interaction]
1082+
10611083
scores_per_turn = iu.compute_final_score_per_turn(interaction,
10621084
game=self.game)
10631085
cooperations = iu.compute_cooperations(interaction)

docs/tutorials/advanced/making_tournaments.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Using this class we can create a tournament with little effort::
4141
>>> players = [axl.Cooperator(), axl.Defector(), axl.TitForTat(),
4242
... axl.Grudger(), axl.Alternator()]
4343
>>> tournament = axl.Tournament(players, match_generator=StochasticMatchups, turns=2, repetitions=2)
44-
>>> results = tournament.play()
44+
>>> results = tournament.play(keep_interactions=True)
4545

4646
We can then look at the interactions (results may differ based on random seed)
4747
for the first repetition::
@@ -88,6 +88,6 @@ We can take a look at the match lengths when using this generator::
8888
... axl.Grudger(), axl.Alternator()]
8989
>>> tournament = axl.Tournament(players, match_generator=OneShotOrRepetitionMatchups,
9090
... turns=float("inf"), repetitions=1)
91-
>>> results = tournament.play()
91+
>>> results = tournament.play(keep_interactions=True)
9292
>>> sorted(list(set([len(matches[0]) for matches in results.interactions.values()])))
9393
[1, 200]

docs/tutorials/further_topics/spatial_tournaments.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ indices::
2929
To create a spatial tournament you call the :code:`SpatialTournamnent` class::
3030

3131
>>> spatial_tournament = axl.SpatialTournament(players, edges=edges)
32-
>>> results = spatial_tournament.play()
32+
>>> results = spatial_tournament.play(keep_interactions=True)
3333

3434
We can plot the results::
3535

@@ -50,7 +50,7 @@ Let's run a small tournament of 2 :code:`turns` and 5 :code:`repetitions`
5050
and obtain the interactions::
5151

5252
>>> spatial_tournament = axl.SpatialTournament(players ,turns=2, repetitions=2, edges=edges)
53-
>>> results = spatial_tournament.play()
53+
>>> results = spatial_tournament.play(keep_interactions=True)
5454
>>> for index_pair, interaction in results.interactions.items():
5555
... player1 = spatial_tournament.players[index_pair[0]]
5656
... player2 = spatial_tournament.players[index_pair[1]]
@@ -67,10 +67,11 @@ It is also possible to create a probabilistic ending spatial tournament with the
6767
:code:`ProbEndSpatialTournament` class::
6868

6969
>>> prob_end_spatial_tournament = axl.ProbEndSpatialTournament(players, edges=edges, prob_end=.1, repetitions=1)
70-
>>> prob_end_results = prob_end_spatial_tournament.play()
70+
>>> prob_end_results = prob_end_spatial_tournament.play(keep_interactions=True)
7171

7272
We see that the match lengths are no longer all equal::
7373

74+
>>> axl.seed(0)
7475
>>> lengths = []
7576
>>> for interaction in prob_end_results.interactions.values():
7677
... lengths.append(len(interaction[0]))

docs/tutorials/getting_started/interactions.rst

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interactions::
1313
... axl.Cooperator(), axl.Defector(),
1414
... axl.TitForTat(), axl.Grudger()]
1515
>>> tournament = axl.Tournament(players, turns=3, repetitions=1)
16-
>>> results = tournament.play()
16+
>>> results = tournament.play(keep_interactions=True)
1717

1818
If the play method is called with :code:`keep_interactions=True`, the result set
1919
object will have an :code:`interactions` attribute which contains all the
@@ -23,17 +23,17 @@ view the history of the interactions::
2323
>>> for index_pair, interaction in results.interactions.items():
2424
... player1 = tournament.players[index_pair[0]]
2525
... player2 = tournament.players[index_pair[1]]
26-
... print('%s vs %s: %s' % (player1, player2, interaction))
27-
Cooperator vs Defector: [[('C', 'D'), ('C', 'D'), ('C', 'D')]]
28-
Defector vs Tit For Tat: [[('D', 'C'), ('D', 'D'), ('D', 'D')]]
29-
Cooperator vs Cooperator: [[('C', 'C'), ('C', 'C'), ('C', 'C')]]
30-
Tit For Tat vs Grudger: [[('C', 'C'), ('C', 'C'), ('C', 'C')]]
31-
Grudger vs Grudger: [[('C', 'C'), ('C', 'C'), ('C', 'C')]]
32-
Tit For Tat vs Tit For Tat: [[('C', 'C'), ('C', 'C'), ('C', 'C')]]
33-
Defector vs Grudger: [[('D', 'C'), ('D', 'D'), ('D', 'D')]]
34-
Cooperator vs Grudger: [[('C', 'C'), ('C', 'C'), ('C', 'C')]]
35-
Cooperator vs Tit For Tat: [[('C', 'C'), ('C', 'C'), ('C', 'C')]]
36-
Defector vs Defector: [[('D', 'D'), ('D', 'D'), ('D', 'D')]]
26+
... print('%s vs %s: %s' % (player1, player2, interaction)) # doctest: +SKIP
27+
Cooperator vs Defector: [('C', 'D'), ('C', 'D'), ('C', 'D')]
28+
Defector vs Tit For Tat: [('D', 'C'), ('D', 'D'), ('D', 'D')]
29+
Cooperator vs Cooperator: [('C', 'C'), ('C', 'C'), ('C', 'C')]
30+
Tit For Tat vs Grudger: [('C', 'C'), ('C', 'C'), ('C', 'C')]
31+
Grudger vs Grudger: [('C', 'C'), ('C', 'C'), ('C', 'C')]
32+
Tit For Tat vs Tit For Tat: [('C', 'C'), ('C', 'C'), ('C', 'C')]
33+
Defector vs Grudger: [('D', 'C'), ('D', 'D'), ('D', 'D')]
34+
Cooperator vs Grudger: [('C', 'C'), ('C', 'C'), ('C', 'C')]
35+
Cooperator vs Tit For Tat: [('C', 'C'), ('C', 'C'), ('C', 'C')]
36+
Defector vs Defector: [('D', 'D'), ('D', 'D'), ('D', 'D')]
3737

3838
We can use these interactions to reconstruct :code:`axelrod.Match` objects which
3939
have a variety of available methods for analysis (more information can be found

docs/tutorials/getting_started/tournament_results.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ We see that :code:`Cooperator` has no difference in score with all players
166166
except against the :code:`Defector`::
167167

168168
>>> results.score_diffs[0]
169-
[[0.0, 0.0, 0.0], [-5.0, -5.0, -5.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
169+
[[-0.0, -0.0, -0.0], [-5.0, -5.0, -5.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
170170

171171
Payoff difference means
172172
-----------------------

0 commit comments

Comments
 (0)