55
66from axelrod_fortran import Player , characteristics , all_strategies
77from axelrod import (Alternator , Cooperator , Defector , Match , MoranProcess ,
8- Game , basic_strategies , seed )
8+ Game , RandomGenerator , Tournament , basic_strategies )
99from axelrod .action import Action
1010
1111
@@ -22,7 +22,7 @@ def test_init():
2222 POINTER (c_int ), POINTER (c_int ), POINTER (c_int ), POINTER (c_int ),
2323 POINTER (c_float ))
2424 assert player .original_function .restype == c_int
25- with pytest .raises (ValueError ):
25+ with pytest .raises (AttributeError ):
2626 player = Player ('test' )
2727
2828
@@ -122,16 +122,22 @@ def test_implemented_strategies():
122122 """
123123 for strategy , dictionary in characteristics .items ():
124124 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 :
129127 for opponent_strategy in basic_strategies :
128+ player = Player (strategy )
130129 opponent = opponent_strategy ()
131130 match = Match ((player , opponent ))
132131 interactions = match .play ()
132+
133+ axl_player = axelrod_class ()
134+ opponent = opponent_strategy ()
133135 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
135141
136142
137143def test_champion_v_alternator ():
@@ -140,15 +146,12 @@ def test_champion_v_alternator():
140146 """
141147 player = Player ("k61r" )
142148 opponent = Alternator ()
143-
144149 match = Match ((player , opponent ))
145150
146- seed ( 0 )
147- interactions = match .play ()
151+ seed = 0
152+ interactions = match .play (seed = seed )
148153 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 )
152155
153156
154157def test_warning_for_self_interaction (recwarn ):
@@ -157,9 +160,7 @@ def test_warning_for_self_interaction(recwarn):
157160 """
158161 player = Player ("k42r" )
159162 opponent = player
160-
161163 match = Match ((player , opponent ))
162-
163164 interactions = match .play ()
164165 assert len (recwarn ) == 1
165166
@@ -168,13 +169,10 @@ def test_no_warning_for_normal_interaction(recwarn):
168169 """
169170 Test that a warning is not given for a normal interaction
170171 """
171- player = Player ("k42r" )
172- opponent = Alternator ()
173172 for players in [(Player ("k42r" ), Alternator ()),
174173 (Player ("k42r" ), Player ("k41r" ))]:
175174
176175 match = Match (players )
177-
178176 interactions = match .play ()
179177 assert len (recwarn ) == 0
180178
@@ -185,3 +183,44 @@ def test_multiple_copies(recwarn):
185183 mp = MoranProcess (players )
186184 mp .play ()
187185 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