@@ -22,27 +22,28 @@ class TestDavis(TestPlayer):
22
22
'manipulates_state' : False
23
23
}
24
24
25
- def test_initial_strategy (self ):
26
- """
27
- Starts by cooperating
28
- """
29
- self .first_play_test (C )
30
-
31
25
def test_strategy (self ):
26
+ self .first_play_test (C )
32
27
# Cooperates for the first ten rounds
33
- player_history = []
34
- opponent_history = []
35
- for i in range (9 ):
36
- opponent_history .append (random .choice ([C , D ]))
37
- player_history .append (C )
38
- self .responses_test ([C ], player_history , opponent_history )
28
+ actions = [(C , C )] * 10
29
+ self .versus_test (axelrod .Cooperator (), expected_actions = actions )
30
+
31
+ actions = [(C , D )] * 10
32
+ self .versus_test (axelrod .Defector (), expected_actions = actions )
33
+
34
+ actions = [(C , C ), (C , D )] * 5
35
+ self .versus_test (axelrod .Alternator (), expected_actions = actions )
39
36
40
37
# If opponent defects at any point then the player will defect forever
41
38
# (after 10 rounds)
42
- self .responses_test ([C ], [C , D , D , D ], [C , C , C , C ])
43
- self .responses_test ([C ], [C , C , D , D , D ], [C , D , C , C , C ])
44
- self .responses_test ([D ], [C ] * 10 + [C , C , D , D , D ],
45
- [C ] * 10 + [C , D , C , C , C ])
39
+ opponent = axelrod .MockPlayer ([C ] * 10 + [D ])
40
+ actions = [(C , C )] * 10 + [(C , D ), (D , C )]
41
+ self .versus_test (opponent , expected_actions = actions )
42
+
43
+ opponent = axelrod .MockPlayer ([C ] * 15 + [D ])
44
+ actions = [(C , C )] * 15 + [(C , D ), (D , C )]
45
+ self .versus_test (opponent , expected_actions = actions )
46
+
46
47
47
48
48
49
class TestRevisedDowning (TestPlayer ):
@@ -61,27 +62,35 @@ class TestRevisedDowning(TestPlayer):
61
62
62
63
def test_strategy (self ):
63
64
self .first_play_test (C )
64
- self .responses_test ([C ], [C ], [C ])
65
- self .responses_test ([C ], [C ], [D ])
66
- self .responses_test ([C ], [C , C ], [C , C ])
67
- self .responses_test ([D ], [C , C ], [C , D ])
68
- self .responses_test ([C ], [C , C ], [D , C ])
69
- self .responses_test ([D ], [C , C ], [D , D ])
70
- self .responses_test ([D ], [C , C , D ], [C , D , C ])
71
- self .responses_test ([C ], [C , C , C ], [D , C , C ])
72
- self .responses_test ([D ], [C , C , D ], [C , D , D ])
73
- self .responses_test ([C ], [C , C , C ], [D , C , D ])
74
- self .responses_test ([D ], [C , C , D , D ], [C , D , D , D ])
75
- self .responses_test ([C ], [C , C , C , C ], [D , C , D , C ])
76
- self .responses_test ([C ], [C , D , C , C , D , D ], [C , C , C , C , D , D ])
65
+
66
+ actions = [(C , C ), (C , C ), (C , C )]
67
+ self .versus_test (axelrod .Cooperator (), expected_actions = actions )
68
+
69
+ actions = [(C , D ), (C , D ), (D , D )]
70
+ self .versus_test (axelrod .Defector (), expected_actions = actions )
71
+
72
+ opponent = axelrod .MockPlayer ([D , C , C ])
73
+ actions = [(C , D ), (C , C ), (C , C ), (C , D )]
74
+ self .versus_test (opponent , expected_actions = actions )
75
+
76
+ opponent = axelrod .MockPlayer ([D , D , C ])
77
+ actions = [(C , D ), (C , D ), (D , C ), (D , D )]
78
+ self .versus_test (opponent , expected_actions = actions )
79
+
80
+ opponent = axelrod .MockPlayer ([C , C , D , D , C , C ])
81
+ actions = [(C , C ), (C , C ), (C , D ), (C , D ), (D , C ), (D , C ), (D , C )]
82
+ self .versus_test (opponent , expected_actions = actions )
83
+
84
+ opponent = axelrod .MockPlayer ([C , C , C , C , D , D ])
85
+ actions = [(C , C ), (C , C ), (C , C ), (C , C ), (C , D ), (C , D ), (C , C )]
86
+ self .versus_test (opponent , expected_actions = actions )
77
87
78
88
def test_not_revised (self ):
79
89
# Test not revised
80
- p1 = self .player (revised = False )
81
- p2 = axelrod .Cooperator ()
82
- p1 .play (p2 )
83
- p1 .play (p2 )
84
- self .assertEqual (p1 .history , [D , D ])
90
+ player = self .player (revised = False )
91
+ opponent = axelrod .Cooperator ()
92
+ match = axelrod .Match ((player , opponent ), turns = 2 )
93
+ self .assertEqual (match .play (), [(D , C ), (D , C )])
85
94
86
95
87
96
class TestFeld (TestPlayer ):
@@ -98,11 +107,7 @@ class TestFeld(TestPlayer):
98
107
'manipulates_state' : False
99
108
}
100
109
101
- def test_strategy (self ):
102
- self .first_play_test (C )
103
- # Test retaliate
104
- self .responses_test ([D ], [C ], [D ])
105
- self .responses_test ([D ], [D ], [D ])
110
+ def test_cooperation_probability (self ):
106
111
# Test cooperation probabilities
107
112
p1 = self .player (start_coop_prob = 1.0 , end_coop_prob = 0.8 ,
108
113
rounds_of_decay = 100 )
@@ -119,13 +124,31 @@ def test_strategy(self):
119
124
self .assertEqual (0.75 , p1 ._cooperation_probability ())
120
125
p1 .history = [C ] * 200
121
126
self .assertEqual (0.5 , p1 ._cooperation_probability ())
127
+
128
+ def test_decay (self ):
122
129
# Test beyond 200 rounds
123
- player_history = [C ] * 200
124
- opponent_history = [C ] * 200
125
- self .responses_test ([C , C , D , D ], player_history , opponent_history ,
126
- seed = 1 )
127
- self .responses_test ([D , D , D , D ], player_history , opponent_history ,
128
- seed = 50 )
130
+ for opponent in [axelrod .Cooperator (), axelrod .Defector ()]:
131
+ player = self .player ()
132
+ self .assertEqual (player ._cooperation_probability (),
133
+ player ._start_coop_prob )
134
+ match = axelrod .Match ((player , opponent ), turns = 201 )
135
+ match .play ()
136
+ self .assertEqual (player ._cooperation_probability (),
137
+ player ._end_coop_prob )
138
+
139
+ def test_strategy (self ):
140
+ self .first_play_test (C )
141
+
142
+ actions = [(C , C )] * 41 + [(D , C )]
143
+ self .versus_test (axelrod .Cooperator (), expected_actions = actions ,
144
+ seed = 1 )
145
+
146
+ actions = [(C , C )] * 16 + [(D , C )]
147
+ self .versus_test (axelrod .Cooperator (), expected_actions = actions ,
148
+ seed = 2 )
149
+
150
+ actions = [(C , D )] + [(D , D )] * 20
151
+ self .versus_test (axelrod .Defector (), expected_actions = actions )
129
152
130
153
131
154
class TestGrofman (TestPlayer ):
@@ -143,13 +166,21 @@ class TestGrofman(TestPlayer):
143
166
}
144
167
145
168
def test_strategy (self ):
146
- self .responses_test ([C , C , C ])
147
- self .responses_test ([D ], [C , C ], [C , D ])
148
- self .responses_test ([C ], [C ] * 6 , [C ] * 6 )
149
- self .responses_test ([D ], [C ] * 6 , [D ] * 6 )
150
- self .responses_test ([C ], [C ] * 7 , [C ] * 7 )
151
- self .responses_test ([C ], [C ] * 7 , [D ] * 7 , seed = 1 )
152
- self .responses_test ([D ], [C ] * 7 , [D ] * 7 , seed = 2 )
169
+ self .first_play_test (C )
170
+
171
+ actions = [(C , C )] * 7
172
+ self .versus_test (axelrod .Cooperator (), expected_actions = actions )
173
+
174
+ actions = [(C , C ), (C , D ), (D , C )]
175
+ self .versus_test (axelrod .Alternator (), expected_actions = actions )
176
+
177
+ opponent = axelrod .MockPlayer ([D ] * 8 )
178
+ actions = [(C , D )] * 2 + [(D , D )] * 5 + [(C , D )] + [(C , D )]
179
+ self .versus_test (opponent , expected_actions = actions , seed = 1 )
180
+
181
+ opponent = axelrod .MockPlayer ([D ] * 8 )
182
+ actions = [(C , D )] * 2 + [(D , D )] * 5 + [(C , D )] + [(D , D )]
183
+ self .versus_test (opponent , expected_actions = actions , seed = 2 )
153
184
154
185
155
186
class TestJoss (TestPlayer ):
@@ -171,8 +202,19 @@ def test_four_vector(self):
171
202
test_four_vector (self , expected_dictionary )
172
203
173
204
def test_strategy (self ):
174
- self .responses_test ([D ], [C ], [C ], seed = 2 )
175
- self .responses_test ([D ], [C ], [D ], seed = 4 )
205
+ self .first_play_test (C )
206
+
207
+ actions = [(C , C ), (C , C ), (C , C ), (C , C )]
208
+ self .versus_test (axelrod .Cooperator (), expected_actions = actions , seed = 1 )
209
+
210
+ actions = [(C , C ), (D , C ), (D , C ), (C , C )]
211
+ self .versus_test (axelrod .Cooperator (), expected_actions = actions , seed = 2 )
212
+
213
+ actions = [(C , D ), (D , D ), (D , D ), (D , D )]
214
+ self .versus_test (axelrod .Defector (), expected_actions = actions , seed = 1 )
215
+
216
+ actions = [(C , D ), (D , D ), (D , D ), (D , D )]
217
+ self .versus_test (axelrod .Defector (), expected_actions = actions , seed = 2 )
176
218
177
219
178
220
class TestNydegger (TestPlayer ):
@@ -211,19 +253,26 @@ def test_score_history(self):
211
253
def test_strategy (self ):
212
254
# Test TFT-type initial play
213
255
self .first_play_test (C )
214
- self .responses_test ([C , C ], [C ], [C ])
215
- self .responses_test ([D ], [C ], [D ])
216
- self .responses_test ([D ], [C , D ], [D , C ])
217
- self .responses_test ([D ], [C , D ], [D , D ])
256
+
257
+ # self.responses_test([D], [C, D], [D, C])
218
258
219
259
# Test trailing post-round 3 play
220
- for i in range (4 , 9 ):
221
- self .responses_test ([C ], [C ] * i , [C ] * i )
222
- self .responses_test ([C ], [D ] * i , [D ] * i )
223
- self .responses_test ([C ], [C ] * i + [C , D , C ], [C ] * i + [C , D , C ])
224
- self .responses_test ([D ], [C ] * i + [D , C , D ], [C ] * i + [C , C , C ])
225
- self .responses_test ([D ], [C ] * i + [D , C , C ], [C ] * i + [C , C , C ])
226
- self .responses_test ([C ], [C ] * i + [C , C , C ], [C ] * i + [D , C , C ])
260
+
261
+ actions = [(C , C )] * 9
262
+ self .versus_test (axelrod .Cooperator (), expected_actions = actions )
263
+
264
+ actions = [(C , D ), (D , D ), (D , D ), (C , D ),
265
+ (C , D ), (C , D ), (C , D ), (C , D )]
266
+ self .versus_test (axelrod .Defector (), expected_actions = actions )
267
+
268
+ actions = [(C , C ), (C , D ), (D , C ), (C , D ),
269
+ (D , C ), (C , D ), (D , C ), (C , D )]
270
+ self .versus_test (axelrod .Alternator (), expected_actions = actions )
271
+
272
+ opponent = axelrod .MockPlayer ([D , C ])
273
+ actions = [(C , D ), (D , C ), (D , D ), (D , C ),
274
+ (D , D ), (D , C ), (D , D ), (D , C )]
275
+ self .versus_test (opponent , expected_actions = actions )
227
276
228
277
229
278
class TestShubik (TestPlayer ):
@@ -246,24 +295,28 @@ def test_strategy(self):
246
295
# Looks like Tit-For-Tat at first
247
296
self .second_play_test (C , D , C , D )
248
297
249
- # Plays a modified TFT.
250
- self .responses_test ([C , C , C ], [C , C , C ], [C , C , C ])
251
- # Make sure that the retaliations are increasing
252
- # Retaliate once and forgive
253
- self .responses_test ([D ], [C ], [D ])
254
- self .responses_test ([C ], [C , D ], [D , C ])
255
- self .responses_test ([C ], [C , D , C ], [D , C , C ])
256
- # Retaliate twice and forgive
257
- self .responses_test ([D , D ], [C , D , C ], [D , C , D ])
258
- self .responses_test ([C ], [C , D , C , D , D ], [D , C , D , C , C ])
259
- # Opponent defection during retaliation doesn't increase retaliation
260
- # period.
261
- self .responses_test ([C ], [C , D , C , D , D ], [D , C , D , D , C ])
262
- # Retaliate thrice and forgive
263
- self .responses_test ([D , D , D ], [C , D , C , D , D , C ], [D , C , D , C , C , D ])
264
- player_history = [C , D , C , D , D , C , D , D , D ]
265
- opponent_history = [D , C , D , C , C , D , C , C , C ]
266
- self .responses_test ([C ], player_history , opponent_history )
298
+ actions = [(C , C ), (C , C ), (C , C )]
299
+ self .versus_test (axelrod .Cooperator (), expected_actions = actions )
300
+
301
+ actions = [(C , C ), (C , D ), (D , C )]
302
+ self .versus_test (axelrod .Alternator (), expected_actions = actions )
303
+
304
+ opponent = axelrod .MockPlayer ([D , C , C ])
305
+ actions = [(C , D ), (D , C ), (C , C ), (C , D )]
306
+ self .versus_test (opponent , expected_actions = actions )
307
+
308
+ opponent = axelrod .MockPlayer ([D , C , D , C , C ])
309
+ actions = [(C , D ), (D , C ), (C , D ), (D , C ), (D , C ), (C , D ), (D , C )]
310
+ self .versus_test (opponent , expected_actions = actions )
311
+
312
+ opponent = axelrod .MockPlayer ([D , C , D , D , C ])
313
+ actions = [(C , D ), (D , C ), (C , D ), (D , D ), (D , C ), (C , D ), (D , C )]
314
+ self .versus_test (opponent , expected_actions = actions )
315
+
316
+ opponent = axelrod .MockPlayer ([D , C , D , C , C , D ])
317
+ actions = [(C , D ), (D , C ), (C , D ), (D , C ), (D , C ),
318
+ (C , D ), (D , D ), (D , C ), (D , D ), (D , C )]
319
+ self .versus_test (opponent , expected_actions = actions )
267
320
268
321
269
322
class TestTullock (TestPlayer ):
@@ -283,26 +336,33 @@ class TestTullock(TestPlayer):
283
336
def test_strategy (self ):
284
337
"""Cooperates for first ten rounds"""
285
338
self .first_play_test (C )
286
- for i in range ( 10 ):
287
- player_history = [C ] * i
288
- opponent_history = [ C ] * i
289
- self . responses_test ([ C ], player_history , opponent_history )
290
- # Now cooperate 10% less than opponent
291
- player_history = [ C ] * 11
292
- opponent_history = [ D ] * 11
293
- self . responses_test ([D ], player_history , opponent_history , seed = 10 )
294
- player_history = [C ] * 11
295
- opponent_history = [ D ] * 10 + [ C ]
296
- self . responses_test ([ D ], player_history , opponent_history , seed = 10 )
339
+
340
+ actions = [( C , C ), ( C , D ) ] * 5
341
+ self . versus_test ( axelrod . Alternator (), expected_actions = actions )
342
+
343
+ actions = [( C , D )] * 11 + [( D , D )] * 2
344
+ self . versus_test ( axelrod . Defector (), expected_actions = actions )
345
+
346
+ opponent = axelrod . MockPlayer ([D ] * 10 + [ C ] )
347
+ actions = [( C , D ) ] * 10 + [( C , C ), ( D , D )]
348
+ self . versus_test ( opponent , expected_actions = actions )
349
+
297
350
# Test beyond 10 rounds
298
- player_history = [C ] * 11
299
- opponent_history = [D ] * 5 + [C ] * 6
300
- self .responses_test ([D , D , D , D ], player_history , opponent_history ,
301
- seed = 20 )
302
- player_history = [C ] * 11
303
- opponent_history = [C ] * 9 + [D ] * 2
304
- self .responses_test ([C , D , D , C ], player_history , opponent_history ,
305
- seed = 25 )
351
+ opponent = axelrod .MockPlayer ([D ] * 5 + [C ] * 6 )
352
+ actions = [(C , D )] * 5 + [(C , C )] * 6 + [(D , D )] * 4
353
+ self .versus_test (opponent , expected_actions = actions , seed = 20 )
354
+
355
+ opponent = axelrod .MockPlayer ([D ] * 5 + [C ] * 6 )
356
+ actions = [(C , D )] * 5 + [(C , C )] * 6 + [(C , D ), (D , D ), (D , D ), (C , D )]
357
+ self .versus_test (opponent , expected_actions = actions , seed = 1 )
358
+
359
+ opponent = axelrod .MockPlayer ([C ] * 9 + [D ] * 2 )
360
+ actions = [(C , C )] * 9 + [(C , D )] * 2 + [(C , C ), (D , C ), (D , C ), (C , C )]
361
+ self .versus_test (opponent , expected_actions = actions , seed = 1 )
362
+
363
+ opponent = axelrod .MockPlayer ([C ] * 9 + [D ] * 2 )
364
+ actions = [(C , C )] * 9 + [(C , D )] * 2 + [(D , C ), (D , C ), (C , C ), (C , C )]
365
+ self .versus_test (opponent , expected_actions = actions , seed = 2 )
306
366
307
367
308
368
class TestUnnamedStrategy (TestPlayer ):
@@ -320,4 +380,11 @@ class TestUnnamedStrategy(TestPlayer):
320
380
}
321
381
322
382
def test_strategy (self ):
323
- self .responses_test ([C , C , D , C , C , D ], seed = 10 )
383
+ self .first_play_test (C )
384
+
385
+ actions = [(D , C ), (C , C ), (C , C ), (D , C ), (C , C ), (C , C )]
386
+ self .versus_test (axelrod .Cooperator (), expected_actions = actions , seed = 1 )
387
+
388
+ actions = [(C , C ), (C , C ), (D , C ), (C , C ), (C , C ), (D , C )]
389
+ self .versus_test (axelrod .Cooperator (), expected_actions = actions ,
390
+ seed = 10 )
0 commit comments