Skip to content

Commit a34f7e4

Browse files
committed
Merge pull request #609 from Axelrod-Python/538
Add match_attributes to match class
2 parents 227f399 + c6d32c7 commit a34f7e4

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

axelrod/match.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,51 @@ def is_stochastic(players, noise):
1616
class Match(object):
1717

1818
def __init__(self, players, turns, game=None, deterministic_cache=None,
19-
noise=0):
19+
noise=0, match_attributes=None):
2020
"""
2121
Parameters
2222
----------
2323
players : tuple
2424
A pair of axelrod.Player objects
2525
turns : integer
26-
The number of turns per match
26+
The number of turns per match
2727
game : axelrod.Game
2828
The game object used to score the match
2929
deterministic_cache : dictionary
3030
A cache of resulting actions for deterministic matches
3131
noise : float
3232
The probability that a player's intended action should be flipped
33+
match_attributes : dict
34+
Mapping attribute names to values which should be passed to players.
35+
The default is to use the correct values for turns, game and noise
36+
but these can be overidden if desired.
3337
"""
3438
self.result = []
3539
self.turns = turns
3640
self._cache_key = (players[0].__class__, players[1].__class__, turns)
41+
self.noise = noise
42+
3743
if game is None:
3844
self.game = Game()
3945
else:
4046
self.game = game
41-
self.noise = noise
42-
self.players = list(players)
47+
4348
if deterministic_cache is None:
4449
self._cache = DeterministicCache()
4550
else:
4651
self._cache = deterministic_cache
4752

53+
if match_attributes is None:
54+
self.match_attributes = {
55+
'length': self.turns,
56+
'game': self.game,
57+
'noise': self.noise
58+
}
59+
else:
60+
self.match_attributes = match_attributes
61+
62+
self.players = list(players)
63+
4864
@property
4965
def players(self):
5066
return self._players
@@ -54,10 +70,7 @@ def players(self, players):
5470
"""Ensure that players are passed the match attributes"""
5571
newplayers = []
5672
for player in players:
57-
player.set_match_attributes(
58-
length=self.turns,
59-
game=self.game,
60-
noise=self.noise)
73+
player.set_match_attributes(**self.match_attributes)
6174
newplayers.append(player)
6275
self._players = newplayers
6376

axelrod/match_generator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ def build_single_match_params(self):
122122
"""
123123
Creates a single set of match parameters.
124124
"""
125-
return (self.sample_length(self.prob_end), self.game, None, self.noise)
125+
return (
126+
self.sample_length(self.prob_end), self.game, None, self.noise,
127+
{'length': float('inf'), 'game': self.game, 'noise': self.noise})
126128

127129
def sample_length(self, prob_end):
128130
"""

axelrod/tests/unit/test_match.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ def test_init(self, turns, game):
3232
self.assertEqual(match.noise, 0)
3333
self.assertEqual(match.game.RPST(), game.RPST())
3434

35+
@given(turns=integers(min_value=1, max_value=200), game=games())
36+
@example(turns=5, game=axelrod.DefaultGame)
37+
def test_non_default_attributes(self, turns, game):
38+
p1, p2 = axelrod.Cooperator(), axelrod.Cooperator()
39+
match_attributes = {
40+
'length': 500,
41+
'game': game,
42+
'noise': 0.5
43+
}
44+
match = axelrod.Match((p1, p2), turns, game=game, match_attributes=match_attributes)
45+
self.assertEqual(match.players[0].match_attributes['length'], 500)
46+
self.assertEqual(match.players[0].match_attributes['noise'], 0.5)
47+
3548
@given(turns=integers(min_value=1, max_value=200))
3649
@example(turns=5)
3750
def test_len(self, turns):

axelrod/tests/unit/test_match_generator.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def test_len(self):
115115
self.assertEqual(len(rr), len(list(rr.build_match_chunks())))
116116
self.assertEqual(rr.estimated_size(), len(rr) * turns * repetitions)
117117

118+
118119
class TestProbEndRoundRobin(unittest.TestCase):
119120

120121
@classmethod
@@ -178,6 +179,7 @@ def test_build_single_match_params(self, prob_end, rm):
178179
self.assertIsInstance(match, axelrod.Match)
179180
self.assertLess(len(match), float('inf'))
180181
self.assertGreater(len(match), 0)
182+
self.assertEqual(match.players[0].match_attributes['length'], float('inf'))
181183

182184
# Testing with noise
183185
rr = axelrod.ProbEndRoundRobinMatches(
@@ -200,9 +202,8 @@ def test_build_single_match_params(self, prob_end, rm):
200202

201203
@given(prob_end=floats(min_value=.1, max_value=1), rm=random_module())
202204
def test_len(self, prob_end, rm):
203-
turns = 5
204205
repetitions = 10
205-
rr = axelrod.ProbEndRoundRobinMatches(self.players, prob_end, game=None,
206-
repetitions=repetitions)
206+
rr = axelrod.ProbEndRoundRobinMatches(
207+
self.players, prob_end, game=None, repetitions=repetitions)
207208
self.assertEqual(len(rr), len(list(rr.build_match_chunks())))
208209
self.assertAlmostEqual(rr.estimated_size(), len(rr) * 1. / prob_end * repetitions)

0 commit comments

Comments
 (0)