Skip to content

Commit e1abada

Browse files
authored
Merge pull request #914 from Axelrod-Python/884-axelrod_first
Refactor axelrod_first for #884.
2 parents cca154d + 9dda94a commit e1abada

File tree

1 file changed

+170
-103
lines changed

1 file changed

+170
-103
lines changed

axelrod/tests/strategies/test_axelrod_first.py

Lines changed: 170 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,28 @@ class TestDavis(TestPlayer):
2222
'manipulates_state': False
2323
}
2424

25-
def test_initial_strategy(self):
26-
"""
27-
Starts by cooperating
28-
"""
29-
self.first_play_test(C)
30-
3125
def test_strategy(self):
26+
self.first_play_test(C)
3227
# 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)
3936

4037
# If opponent defects at any point then the player will defect forever
4138
# (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+
4647

4748

4849
class TestRevisedDowning(TestPlayer):
@@ -61,27 +62,35 @@ class TestRevisedDowning(TestPlayer):
6162

6263
def test_strategy(self):
6364
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)
7787

7888
def test_not_revised(self):
7989
# 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)])
8594

8695

8796
class TestFeld(TestPlayer):
@@ -98,11 +107,7 @@ class TestFeld(TestPlayer):
98107
'manipulates_state': False
99108
}
100109

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):
106111
# Test cooperation probabilities
107112
p1 = self.player(start_coop_prob=1.0, end_coop_prob=0.8,
108113
rounds_of_decay=100)
@@ -119,13 +124,31 @@ def test_strategy(self):
119124
self.assertEqual(0.75, p1._cooperation_probability())
120125
p1.history = [C] * 200
121126
self.assertEqual(0.5, p1._cooperation_probability())
127+
128+
def test_decay(self):
122129
# 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)
129152

130153

131154
class TestGrofman(TestPlayer):
@@ -143,13 +166,21 @@ class TestGrofman(TestPlayer):
143166
}
144167

145168
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)
153184

154185

155186
class TestJoss(TestPlayer):
@@ -171,8 +202,19 @@ def test_four_vector(self):
171202
test_four_vector(self, expected_dictionary)
172203

173204
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)
176218

177219

178220
class TestNydegger(TestPlayer):
@@ -211,19 +253,26 @@ def test_score_history(self):
211253
def test_strategy(self):
212254
# Test TFT-type initial play
213255
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])
218258

219259
# 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)
227276

228277

229278
class TestShubik(TestPlayer):
@@ -246,24 +295,28 @@ def test_strategy(self):
246295
# Looks like Tit-For-Tat at first
247296
self.second_play_test(C, D, C, D)
248297

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)
267320

268321

269322
class TestTullock(TestPlayer):
@@ -283,26 +336,33 @@ class TestTullock(TestPlayer):
283336
def test_strategy(self):
284337
"""Cooperates for first ten rounds"""
285338
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+
297350
# 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)
306366

307367

308368
class TestUnnamedStrategy(TestPlayer):
@@ -320,4 +380,11 @@ class TestUnnamedStrategy(TestPlayer):
320380
}
321381

322382
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

Comments
 (0)