5
5
6
6
from axelrod_fortran import Player , characteristics , all_strategies
7
7
from axelrod import (Alternator , Cooperator , Defector , Match , MoranProcess ,
8
- Game , basic_strategies , seed )
8
+ Game , RandomGenerator , Tournament , basic_strategies )
9
9
from axelrod .action import Action
10
10
11
11
@@ -22,7 +22,7 @@ def test_init():
22
22
POINTER (c_int ), POINTER (c_int ), POINTER (c_int ), POINTER (c_int ),
23
23
POINTER (c_float ))
24
24
assert player .original_function .restype == c_int
25
- with pytest .raises (ValueError ):
25
+ with pytest .raises (AttributeError ):
26
26
player = Player ('test' )
27
27
28
28
@@ -122,16 +122,22 @@ def test_implemented_strategies():
122
122
"""
123
123
for strategy , dictionary in characteristics .items ():
124
124
axelrod_class = dictionary ["axelrod-python_class" ]
125
- player = Player (strategy )
126
- if (axelrod_class is not None and
127
- player .classifier ["stochastic" ] is False ):
128
- axl_player = axelrod_class ()
125
+ stochastic = Player (strategy ).classifier ["stochastic" ]
126
+ if axelrod_class is not None and not stochastic :
129
127
for opponent_strategy in basic_strategies :
128
+ player = Player (strategy )
130
129
opponent = opponent_strategy ()
131
130
match = Match ((player , opponent ))
132
131
interactions = match .play ()
132
+
133
+ axl_player = axelrod_class ()
134
+ opponent = opponent_strategy ()
133
135
axl_match = Match ((axl_player , opponent ))
134
- assert interactions == axl_match .play (), (player , opponent )
136
+ axl_interactions = axl_match .play ()
137
+ print (player , axl_player , opponent )
138
+ print (interactions )
139
+ print (axl_interactions )
140
+ assert interactions == axl_interactions
135
141
136
142
137
143
def test_champion_v_alternator ():
@@ -140,15 +146,12 @@ def test_champion_v_alternator():
140
146
"""
141
147
player = Player ("k61r" )
142
148
opponent = Alternator ()
143
-
144
149
match = Match ((player , opponent ))
145
150
146
- seed ( 0 )
147
- interactions = match .play ()
151
+ seed = 0
152
+ interactions = match .play (seed = seed )
148
153
assert interactions [25 :30 ] == [(C , D ), (C , C ), (C , D ), (D , C ), (C , D )]
149
-
150
- seed (0 )
151
- assert interactions == match .play ()
154
+ assert interactions == match .play (seed = seed )
152
155
153
156
154
157
def test_warning_for_self_interaction (recwarn ):
@@ -157,9 +160,7 @@ def test_warning_for_self_interaction(recwarn):
157
160
"""
158
161
player = Player ("k42r" )
159
162
opponent = player
160
-
161
163
match = Match ((player , opponent ))
162
-
163
164
interactions = match .play ()
164
165
assert len (recwarn ) == 1
165
166
@@ -168,13 +169,10 @@ def test_no_warning_for_normal_interaction(recwarn):
168
169
"""
169
170
Test that a warning is not given for a normal interaction
170
171
"""
171
- player = Player ("k42r" )
172
- opponent = Alternator ()
173
172
for players in [(Player ("k42r" ), Alternator ()),
174
173
(Player ("k42r" ), Player ("k41r" ))]:
175
174
176
175
match = Match (players )
177
-
178
176
interactions = match .play ()
179
177
assert len (recwarn ) == 0
180
178
@@ -185,3 +183,44 @@ def test_multiple_copies(recwarn):
185
183
mp = MoranProcess (players )
186
184
mp .play ()
187
185
mp .populations_plot ()
186
+
187
+
188
+ def test_match_reproducibility ():
189
+ for _ in range (10 ):
190
+ rng = RandomGenerator ()
191
+ seed = rng .random_seed_int ()
192
+ strategies = rng .choice (all_strategies , size = 2 )
193
+ print (strategies )
194
+ players1 = [Player (strategy ) for strategy in strategies ]
195
+ # players1 = (p() for p in strategies)
196
+ match1 = Match (players1 , turns = 200 , noise = 0.1 , seed = seed )
197
+ results1 = match1 .play ()
198
+ players2 = [Player (strategy ) for strategy in strategies ]
199
+ # players2 = (p() for p in strategies)
200
+ match2 = Match (players2 , turns = 200 , noise = 0.1 , seed = seed )
201
+ results2 = match2 .play ()
202
+ assert (results1 == results2 )
203
+
204
+
205
+ def test_tournament_reproducibility ():
206
+ rng = RandomGenerator ()
207
+ seed = rng .random_seed_int ()
208
+ strategies = rng .choice (all_strategies , size = 2 )
209
+ players1 = [Player (strategy ) for strategy in strategies ]
210
+ tournament1 = Tournament (players1 , seed = seed , repetitions = 2 )
211
+ results1 = tournament1 .play (processes = 2 )
212
+
213
+ players2 = [Player (strategy ) for strategy in strategies ]
214
+ tournament2 = Tournament (players2 , seed = seed , repetitions = 2 )
215
+ results2 = tournament2 .play (processes = 2 )
216
+
217
+ assert (results1 .ranked_names == results2 .ranked_names )
218
+
219
+
220
+ if __name__ == "__main__" :
221
+ test_init ()
222
+ test_matches ()
223
+ test_noisy_matches ()
224
+ test_implemented_strategies ()
225
+ test_match_reproducibility ()
226
+ test_tournament_reproducibility ()
0 commit comments