1
1
"""Tests for the various Meta strategies."""
2
-
3
- import random
4
-
5
2
import axelrod
6
- from .test_player import TestPlayer , test_responses
3
+ from .test_player import TestPlayer
7
4
8
5
C , D = axelrod .Actions .C , axelrod .Actions .D
9
6
@@ -180,13 +177,16 @@ class TestNiceMetaWinnerEnsemble(TestMetaPlayer):
180
177
def test_strategy (self ):
181
178
self .first_play_test (C )
182
179
183
- P1 = axelrod .NiceMetaWinner (team = [axelrod .Cooperator , axelrod .Defector ])
184
- P2 = axelrod .Cooperator ()
185
- test_responses (self , P1 , P2 , [C ] * 4 , [C ] * 4 , [C ] * 4 )
186
-
187
- P1 = axelrod .NiceMetaWinner (team = [axelrod .Cooperator , axelrod .Defector ])
188
- P2 = axelrod .Defector ()
189
- test_responses (self , P1 , P2 , [D ] * 4 , [C ] * 4 , [D ] * 4 )
180
+ actions = [(C , C )] * 8
181
+ self .versus_test (opponent = axelrod .Cooperator (),
182
+ expected_actions = actions ,
183
+ init_kwargs = {"team" : [axelrod .Cooperator ,
184
+ axelrod .Defector ]})
185
+ actions = [(C , D )] + [(D , D )] * 7
186
+ self .versus_test (opponent = axelrod .Defector (),
187
+ expected_actions = actions ,
188
+ init_kwargs = {"team" : [axelrod .Cooperator ,
189
+ axelrod .Defector ]})
190
190
191
191
192
192
class TestMetaHunter (TestMetaPlayer ):
@@ -208,19 +208,30 @@ def test_strategy(self):
208
208
209
209
# We are not using the Cooperator Hunter here, so this should lead to
210
210
# cooperation.
211
- self .responses_test ([C ], [C , C , C , C ], [C , C , C , C ])
211
+ actions = [(C , C )] * 5
212
+ self .versus_test (opponent = axelrod .Cooperator (),
213
+ expected_actions = actions )
212
214
213
215
# After long histories tit-for-tat should come into play.
214
- self .responses_test ([D ], [C ] * 101 , [C ] * 100 + [D ])
216
+ opponent = axelrod .MockPlayer ([C ] * 100 + [D ])
217
+ actions = [(C , C )] * 100 + [(C , D )] + [(D , C )]
218
+ self .versus_test (opponent = opponent , expected_actions = actions )
219
+
220
+ actions = [(C , C )] * 102
221
+ self .versus_test (opponent = axelrod .Cooperator (),
222
+ expected_actions = actions )
215
223
216
224
# All these others, however, should trigger a defection for the hunter.
217
- self .responses_test ([D ], [C ] * 4 , [D ] * 4 )
218
- self .responses_test ([D ], [C ] * 6 , [C , D ] * 3 )
219
- self .responses_test ([D ], [C ] * 8 , [C , C , C , D , C , C , C , D ])
220
- # Test post 100 rounds responses
221
- self .responses_test ([C ], [C ] * 101 , [C ] * 101 )
222
- self .responses_test ([D ], [C ] * 101 , [C ] * 100 + [D ])
225
+ actions = [(C , D ), (C , D ), (C , D ), (C , D ), (D , D )]
226
+ self .versus_test (opponent = axelrod .Defector (), expected_actions = actions )
227
+
228
+ actions = [(C , C ), (C , D ), (C , C ), (C , D ), (C , C ), (C , D ), (D , C )]
229
+ self .versus_test (opponent = axelrod .Alternator (), expected_actions = actions )
223
230
231
+ actions = [(C , C ), (C , C ), (C , C ), (C , D ),
232
+ (C , C ), (C , C ), (C , C ), (C , D ), (D , C )]
233
+ self .versus_test (opponent = axelrod .CyclerCCCD (),
234
+ expected_actions = actions )
224
235
225
236
class TestMetaHunterAggressive (TestMetaPlayer ):
226
237
@@ -241,15 +252,21 @@ def test_strategy(self):
241
252
242
253
# We are using CooperatorHunter here, so this should lead to
243
254
# defection
244
- self .responses_test ([D ], [C , C , C , C ], [C , C , C , C ])
255
+ actions = [(C , C )] * 4 + [(D , C )]
256
+ self .versus_test (opponent = axelrod .Cooperator (),
257
+ expected_actions = actions )
245
258
246
259
# All these others, however, should trigger a defection for the hunter.
247
- self .responses_test ([D ], [C ] * 4 , [D ] * 4 )
248
- self .responses_test ([D ], [C ] * 6 , [C , D ] * 3 )
249
- self .responses_test ([D ], [C ] * 8 , [C , C , C , D , C , C , C , D ])
250
- # Test post 100 rounds responses
251
- self .responses_test ([D ], [C ] * 101 , [C ] * 101 )
252
- self .responses_test ([D ], [C ] * 101 , [C ] * 100 + [D ])
260
+ actions = [(C , D ), (C , D ), (C , D ), (C , D ), (D , D )]
261
+ self .versus_test (opponent = axelrod .Defector (), expected_actions = actions )
262
+
263
+ actions = [(C , C ), (C , D ), (C , C ), (C , D ), (C , C ), (C , D ), (D , C )]
264
+ self .versus_test (opponent = axelrod .Alternator (), expected_actions = actions )
265
+
266
+ actions = [(C , C ), (C , C ), (C , C ), (C , D ),
267
+ (C , C ), (C , C ), (C , C ), (C , D ), (D , C )]
268
+ self .versus_test (opponent = axelrod .CyclerCCCD (),
269
+ expected_actions = actions )
253
270
254
271
# To test the TFT action of the strategy after 100 turns, we need to
255
272
# remove two of the hunters from its team.
@@ -260,13 +277,13 @@ def test_strategy(self):
260
277
axelrod .DefectorHunter ,
261
278
axelrod .AlternatorHunter ,
262
279
axelrod .RandomHunter ,
263
- # axelrod.MathConstantHunter,
264
280
axelrod .CycleHunter ,
265
- axelrod .EventualCycleHunter ,
266
- # axelrod.CooperatorHunter
281
+ axelrod .EventualCycleHunter
267
282
]
268
- self .responses_test (
269
- [D ], [C ] * 101 , [C ] * 100 + [D ], init_kwargs = {'team' : team })
283
+ opponent = axelrod .MockPlayer ([C ] * 100 + [D ])
284
+ actions = [(C , C )] * 100 + [(C , D ), (D , C )]
285
+ self .versus_test (opponent = opponent , expected_actions = actions ,
286
+ init_kwargs = {'team' : team })
270
287
271
288
272
289
class TestMetaMajorityMemoryOne (TestMetaPlayer ):
@@ -426,36 +443,36 @@ def test_strategy(self):
426
443
427
444
P1 = axelrod .MetaMixer (team = team , distribution = distribution )
428
445
P2 = axelrod .Cooperator ()
446
+ actions = [(C , C )] * 20
447
+ self .versus_test (opponent = axelrod .Cooperator (),
448
+ expected_actions = actions ,
449
+ init_kwargs = {"team" : team ,
450
+ "distribution" : distribution })
451
+
429
452
430
- for k in range (100 ):
431
- P1 .play (P2 )
432
- self .assertEqual (P1 .history [- 1 ], C )
433
453
434
454
team .append (axelrod .Defector )
435
455
distribution = [.2 , .5 , .3 , 0 ] # If add a defector but does not occur
436
-
437
- P1 = axelrod .MetaMixer (team = team , distribution = distribution )
438
-
439
- for k in range (100 ):
440
- P1 .play (P2 )
441
- self .assertEqual (P1 .history [- 1 ], C )
456
+ self .versus_test (opponent = axelrod .Cooperator (),
457
+ expected_actions = actions ,
458
+ init_kwargs = {"team" : team ,
459
+ "distribution" : distribution })
442
460
443
461
distribution = [0 , 0 , 0 , 1 ] # If defector is only one that is played
444
-
445
- P1 = axelrod .MetaMixer (team = team , distribution = distribution )
446
-
447
- for k in range (100 ):
448
- P1 .play (P2 )
449
- self .assertEqual (P1 .history [- 1 ], D )
462
+ actions = [(D , C )] * 20
463
+ self .versus_test (opponent = axelrod .Cooperator (),
464
+ expected_actions = actions ,
465
+ init_kwargs = {"team" : team ,
466
+ "distribution" : distribution })
450
467
451
468
def test_raise_error_in_distribution (self ):
452
469
team = [axelrod .TitForTat , axelrod .Cooperator , axelrod .Grudger ]
453
470
distribution = [.2 , .5 , .5 ] # Not a valid probability distribution
454
471
455
- P1 = axelrod .MetaMixer (team = team , distribution = distribution )
456
- P2 = axelrod .Cooperator ()
472
+ player = axelrod .MetaMixer (team = team , distribution = distribution )
473
+ opponent = axelrod .Cooperator ()
457
474
458
- self .assertRaises (ValueError , P1 .strategy , P2 )
475
+ self .assertRaises (ValueError , player .strategy , opponent )
459
476
460
477
461
478
class TestNMWEDeterministic (TestMetaPlayer ):
0 commit comments