Skip to content

Commit af3c387

Browse files
committed
Update many tests with new random seeds and values, up to axelrod_first.py
1 parent 423b59f commit af3c387

13 files changed

+122
-139
lines changed

axelrod/player.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import itertools
44
import types
55
from typing import Any, Dict
6+
import warnings
67

78
import numpy as np
89

@@ -121,7 +122,9 @@ def set_match_attributes(self, length=-1, game=None, noise=0):
121122
def set_seed(self, seed=None):
122123
"""Set a random seed for the player's random number generator."""
123124
if seed is None:
124-
# Warning: using global seed
125+
warnings.warn(
126+
"Initializing player with seed from Axelrod module random number generator."
127+
" Results may not be seed reproducible.")
125128
self._seed = _module_random.random_seed_int()
126129
else:
127130
self._seed = seed

axelrod/strategies/axelrod_first.py

-7
Original file line numberDiff line numberDiff line change
@@ -745,15 +745,8 @@ class FirstByTullock(Player):
745745
}
746746

747747
def __init__(self) -> None:
748-
"""
749-
Parameters
750-
----------
751-
rounds_to_cooperate: int
752-
The number of rounds to cooperate initially
753-
"""
754748
super().__init__()
755749
self._rounds_to_cooperate = 11
756-
self.memory_depth = self._rounds_to_cooperate
757750

758751
def strategy(self, opponent: Player) -> Action:
759752
if len(self.history) < self._rounds_to_cooperate:

axelrod/tests/integration/test_filtering.py

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def setUp(self) -> None:
2121
def tearDown(self) -> None:
2222
warnings.simplefilter("default", category=UserWarning)
2323

24+
@settings(deadline=None)
2425
@given(strategies=strategy_lists(min_size=20, max_size=20))
2526
def test_boolean_filtering(self, strategies):
2627

axelrod/tests/integration/test_matches.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_outcome_repeats_stochastic(self, strategies, turns, seed):
4747
results = []
4848
for _ in range(3):
4949
players = [s() for s in strategies]
50-
results.append(axelrod.Match(players, turns=turns, seed=seed).play())
50+
results.append(axl.Match(players, turns=turns, seed=seed).play())
5151

5252
self.assertEqual(results[0], results[1])
5353
self.assertEqual(results[1], results[2])

axelrod/tests/integration/test_tournament.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_matches_have_different_length(self):
164164
p2 = axl.Cooperator()
165165
p3 = axl.Cooperator()
166166
players = [p1, p2, p3]
167-
tournament = axelrod.Tournament(players, prob_end=0.5, repetitions=2, seed=3)
167+
tournament = axl.Tournament(players, prob_end=0.5, repetitions=2, seed=3)
168168
results = tournament.play(progress_bar=False)
169169
# Check that match length are different across the repetitions
170170
self.assertNotEqual(results.match_lengths[0], results.match_lengths[1])

axelrod/tests/strategies/test_adaptor.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ def test_strategy(self):
2929
# Error corrected.
3030
actions = [(C, C), (C, D), (D, C), (C, C)]
3131
self.versus_test(
32-
opponent=axl.AdaptorBrief(), expected_actions=actions, seed=22
32+
opponent=axl.AdaptorBrief(), expected_actions=actions, seed=245
3333
)
3434

3535
# Error corrected, example 2
3636
actions = [(D, C), (C, D), (D, C), (C, D), (C, C)]
3737
self.versus_test(
38-
opponent=axl.AdaptorBrief(), expected_actions=actions, seed=925
38+
opponent=axl.AdaptorBrief(), expected_actions=actions, seed=7935
3939
)
4040

4141
# Versus Cooperator
@@ -74,7 +74,7 @@ def test_strategy(self):
7474
# Error corrected.
7575
actions = [(C, C), (C, D), (D, D), (C, C), (C, C)]
7676
self.versus_test(
77-
opponent=axl.AdaptorLong(), expected_actions=actions, seed=22
77+
opponent=axl.AdaptorLong(), expected_actions=actions, seed=305
7878
)
7979

8080
# Versus Cooperator

axelrod/tests/strategies/test_averagecopier.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ def test_strategy(self):
4545
(C, D),
4646
(C, C),
4747
(D, D),
48-
(D, C),
48+
(C, C),
4949
(C, D),
5050
]
5151
self.versus_test(axl.Alternator(), expected_actions=actions, seed=1)
5252

5353
actions = [
5454
(D, C),
5555
(C, D),
56-
(D, C),
56+
(C, C),
5757
(C, D),
5858
(C, C),
5959
(D, D),
60-
(D, C),
60+
(C, C),
6161
(D, D),
6262
(C, C),
6363
(D, D),
@@ -73,9 +73,9 @@ def test_strategy(self):
7373
(D, D),
7474
(C, D),
7575
(D, C),
76-
(D, C),
77-
(D, D),
76+
(C, C),
7877
(D, D),
78+
(C, D),
7979
]
8080
self.versus_test(opponent, expected_actions=actions, seed=1)
8181

@@ -85,12 +85,12 @@ def test_strategy(self):
8585
(C, C),
8686
(C, C),
8787
(C, D),
88-
(D, D),
88+
(C, D),
8989
(C, D),
9090
(C, C),
9191
(D, C),
92-
(D, C),
93-
(D, D),
92+
(C, C),
93+
(C, D),
9494
]
9595
self.versus_test(opponent, expected_actions=actions, seed=2)
9696

@@ -126,22 +126,22 @@ def test_strategy(self):
126126
(D, D),
127127
(D, C),
128128
(C, D),
129-
(C, C),
129+
(D, C),
130130
(C, D),
131131
(D, C),
132-
(D, D),
132+
(C, D),
133133
]
134134
self.versus_test(axl.Alternator(), expected_actions=actions, seed=1)
135135

136136
actions = [
137137
(C, C),
138138
(C, D),
139139
(D, C),
140-
(D, D),
141-
(C, C),
142140
(C, D),
143141
(D, C),
144-
(D, D),
142+
(C, D),
143+
(D, C),
144+
(C, D),
145145
(D, C),
146146
(C, D),
147147
]
@@ -155,7 +155,7 @@ def test_strategy(self):
155155
(C, D),
156156
(D, D),
157157
(D, D),
158-
(C, C),
158+
(D, C),
159159
(D, C),
160160
(C, D),
161161
(D, D),
@@ -168,11 +168,11 @@ def test_strategy(self):
168168
(C, C),
169169
(C, C),
170170
(C, D),
171-
(D, D),
172-
(D, D),
173-
(C, C),
171+
(C, D),
172+
(C, D),
173+
(D, C),
174174
(C, C),
175175
(D, C),
176-
(D, D),
176+
(C, D),
177177
]
178178
self.versus_test(opponent, expected_actions=actions, seed=2)

axelrod/tests/strategies/test_axelrod_first.py

+25-37
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ def test_decay(self):
127127
self.assertEqual(player._cooperation_probability(), player._end_coop_prob)
128128

129129
def test_strategy(self):
130-
actions = [(C, C)] * 41 + [(D, C)]
130+
actions = [(C, C)] * 13 + [(D, C)]
131131
self.versus_test(axl.Cooperator(), expected_actions=actions, seed=1)
132132

133-
actions = [(C, C)] * 16 + [(D, C)]
133+
actions = [(C, C)] * 11 + [(D, C)]
134134
self.versus_test(axl.Cooperator(), expected_actions=actions, seed=2)
135135

136136
actions = [(C, D)] + [(D, D)] * 20
@@ -163,19 +163,19 @@ def test_strategy(self):
163163
actions += [(D, C)] # 51 turns
164164
actions += [(C, D), (D, C)] * 2 + [(C, D)] # 56 turns
165165
self.versus_test(
166-
axl.Alternator(), expected_actions=actions, attrs=expected_attrs
166+
axl.Alternator(), expected_actions=actions, attrs=expected_attrs, seed=0
167167
)
168168

169169
# Against defector
170170
actions = [(C, D)] + [(D, D)] * 55 # 56 turns
171171
self.versus_test(
172-
axl.Defector(), expected_actions=actions, attrs=expected_attrs
172+
axl.Defector(), expected_actions=actions, attrs=expected_attrs, seed=0
173173
)
174174

175175
# Against cooperator
176176
actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 5
177177
self.versus_test(
178-
axl.Cooperator(), expected_actions=actions, attrs=expected_attrs
178+
axl.Cooperator(), expected_actions=actions, attrs=expected_attrs, seed=0
179179
)
180180

181181
# Test recognition of random player
@@ -185,12 +185,12 @@ def test_strategy(self):
185185
}
186186
actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 5 # 56 turns
187187
self.versus_test(
188-
axl.Cooperator(), expected_actions=actions, attrs=expected_attrs
188+
axl.Cooperator(), expected_actions=actions, attrs=expected_attrs, seed=1
189189
)
190-
expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 68}
190+
expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 63}
191191
actions += [(C, C)] # 57 turns
192192
self.versus_test(
193-
axl.Cooperator(), expected_actions=actions, attrs=expected_attrs
193+
axl.Cooperator(), expected_actions=actions, attrs=expected_attrs, seed=8
194194
)
195195

196196
expected_attrs = {
@@ -202,11 +202,11 @@ def test_strategy(self):
202202
actions += [(C, D), (D, C)] * 3 # 57 turns
203203
actions += [(D, D)]
204204
self.versus_test(
205-
axl.Alternator(), expected_actions=actions, attrs=expected_attrs
205+
axl.Alternator(), expected_actions=actions, attrs=expected_attrs, seed=3
206206
)
207207
actions += [(D, C), (D, D)] * 5
208208
self.versus_test(
209-
axl.Alternator(), expected_actions=actions, attrs=expected_attrs
209+
axl.Alternator(), expected_actions=actions, attrs=expected_attrs, seed=4
210210
)
211211

212212
# Test versus TfT
@@ -222,16 +222,16 @@ def test_strategy(self):
222222
)
223223

224224
# Test random defections
225-
expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 78}
226-
actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 16 + [(D, C)] + [(C, C)]
225+
expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 76}
226+
actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 15 + [(D, C)] + [(C, C)]
227227
self.versus_test(
228228
axl.Cooperator(), expected_actions=actions, seed=0, attrs=expected_attrs
229229
)
230230

231-
expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 77}
232-
actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 12 + [(D, C)] + [(C, C)]
231+
expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 79}
232+
actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 14 + [(D, C)] + [(C, C)]
233233
self.versus_test(
234-
axl.Cooperator(), expected_actions=actions, seed=1, attrs=expected_attrs
234+
axl.Cooperator(), expected_actions=actions, seed=5, attrs=expected_attrs
235235
)
236236

237237

@@ -251,17 +251,17 @@ class TestFirstByGrofman(TestPlayer):
251251

252252
def test_strategy(self):
253253
actions = [(C, C)] * 7
254-
self.versus_test(axl.Cooperator(), expected_actions=actions)
254+
self.versus_test(axl.Cooperator(), expected_actions=actions, seed=0)
255255

256256
actions = [(C, C), (C, D), (D, C)]
257-
self.versus_test(axl.Alternator(), expected_actions=actions)
257+
self.versus_test(axl.Alternator(), expected_actions=actions, seed=0)
258258

259259
opponent = axl.MockPlayer(actions=[D] * 8)
260-
actions = [(C, D), (C, D), (D, D), (C, D), (D, D), (C, D), (C, D), (D, D)]
260+
actions = [(C, D), (C, D), (D, D), (C, D), (D, D), (C, D), (D, D), (C, D)]
261261
self.versus_test(opponent, expected_actions=actions, seed=1)
262262

263263
opponent = axl.MockPlayer(actions=[D] * 8)
264-
actions = [(C, D), (D, D), (C, D), (D, D), (C, D), (C, D), (C, D), (D, D)]
264+
actions = [(C, D), (D, D), (C, D), (D, D), (C, D), (D, D), (C, D), (C, D)]
265265
self.versus_test(opponent, expected_actions=actions, seed=2)
266266

267267

@@ -284,10 +284,10 @@ def test_four_vector(self):
284284
test_four_vector(self, expected_dictionary)
285285

286286
def test_strategy(self):
287-
actions = [(C, C), (C, C), (C, C), (C, C)]
287+
actions = [(C, C), (C, C), (C, C), (D, C)]
288288
self.versus_test(axl.Cooperator(), expected_actions=actions, seed=1)
289289

290-
actions = [(C, C), (D, C), (D, C), (C, C)]
290+
actions = [(C, C), (C, C), (C, C), (C, C)]
291291
self.versus_test(axl.Cooperator(), expected_actions=actions, seed=2)
292292

293293
actions = [(C, D), (D, D), (D, D), (D, D)]
@@ -424,22 +424,10 @@ def test_strategy(self):
424424
self.versus_test(opponent, expected_actions=actions)
425425

426426
# Test beyond 10 rounds
427-
opponent = axl.MockPlayer(actions=[D] * 5 + [C] * 6)
428-
actions = [(C, D)] * 5 + [(C, C)] * 6 + [(D, D)] * 4
427+
opponent = axl.MockPlayer(actions=[D] * 5 + [C] * 5 + [C, D] * 5)
428+
actions = [(C, D)] * 5 + [(C, C)] * 6 + [(D, D)] + [(D, C), (C, D), (C, C)]
429429
self.versus_test(opponent, expected_actions=actions, seed=20)
430430

431-
opponent = axl.MockPlayer(actions=[D] * 5 + [C] * 6)
432-
actions = [(C, D)] * 5 + [(C, C)] * 6 + [(C, D), (D, D), (D, D), (C, D)]
433-
self.versus_test(opponent, expected_actions=actions, seed=1)
434-
435-
opponent = axl.MockPlayer(actions=[C] * 9 + [D] * 2)
436-
actions = [(C, C)] * 9 + [(C, D)] * 2 + [(C, C), (D, C), (D, C), (C, C)]
437-
self.versus_test(opponent, expected_actions=actions, seed=1)
438-
439-
opponent = axl.MockPlayer(actions=[C] * 9 + [D] * 2)
440-
actions = [(C, C)] * 9 + [(C, D)] * 2 + [(D, C), (D, C), (C, C), (C, C)]
441-
self.versus_test(opponent, expected_actions=actions, seed=2)
442-
443431

444432
class TestFirstByAnonymous(TestPlayer):
445433

@@ -456,10 +444,10 @@ class TestFirstByAnonymous(TestPlayer):
456444
}
457445

458446
def test_strategy(self):
459-
actions = [(D, C), (C, C), (C, C), (D, C), (C, C), (C, C)]
447+
actions = [(D, C), (C, C), (C, C), (C, C), (D, C), (C, C)]
460448
self.versus_test(axl.Cooperator(), expected_actions=actions, seed=1)
461449

462-
actions = [(C, C), (C, C), (D, C), (C, C), (C, C), (D, C)]
450+
actions = [(D, C), (D, C), (D, C), (C, C), (D, C), (D, C)]
463451
self.versus_test(axl.Cooperator(), expected_actions=actions, seed=10)
464452

465453

axelrod/tests/strategies/test_player.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010
import axelrod as axl
1111
from axelrod.tests.property import strategy_lists
1212

13-
1413
C, D = axl.Action.C, axl.Action.D
15-
1614
random = axl.RandomGenerator()
17-
1815
short_run_time_short_mem = [
1916
s
2017
for s in axl.short_run_time_strategies
@@ -487,7 +484,7 @@ def test_clone(self, seed):
487484
axl.Cooperator(),
488485
axl.Defector(),
489486
axl.TitForTat(),
490-
axl.Random(p=r),
487+
axl.Random(p=0.5),
491488
]:
492489
for p in [player1, player2]:
493490
m = axl.Match((p, op), turns=turns, reset=True, seed=seed)
@@ -687,7 +684,6 @@ def test_memory(player, opponent, memory_length, seed=0, turns=10):
687684
opponent.reset()
688685
player._history = axl.LimitedHistory(memory_length)
689686
opponent._history = axl.LimitedHistory(memory_length)
690-
axl.seed(seed)
691687
match = axl.Match((player, opponent), turns=turns, reset=False, seed=seed)
692688
limited_plays = [p[0] for p in match.play()]
693689

0 commit comments

Comments
 (0)