Skip to content

Commit 86d856c

Browse files
committed
re-did gobymajority test
1 parent 96d9880 commit 86d856c

File tree

1 file changed

+76
-67
lines changed

1 file changed

+76
-67
lines changed

axelrod/tests/strategies/test_gobymajority.py

Lines changed: 76 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ class TestHardGoByMajority(TestPlayer):
1111

1212
name = "Hard Go By Majority"
1313
player = axelrod.HardGoByMajority
14-
eq_play = D
15-
soft = False
14+
default_soft = False
1615

1716
expected_classifier = {
1817
'stochastic': False,
@@ -24,60 +23,77 @@ class TestHardGoByMajority(TestPlayer):
2423
'manipulates_state': False
2524
}
2625

27-
def test_strategy(self):
28-
self.first_play_test(self.eq_play)
26+
def test_first_play(self):
27+
self.first_play_test(D)
2928

30-
expected, opponent_actions = self.get_infinite_memory_depth_actions()
31-
self.versus_test(MockPlayer(actions=opponent_actions),
32-
expected_actions=expected)
29+
def test_memory_depth_infinite_soft_is_false(self):
30+
init_kwargs = {}
31+
if self.default_soft:
32+
init_kwargs['soft'] = False
3333

34-
def get_infinite_memory_depth_actions(self):
35-
opponent_actions = [C, D, D]
36-
first_three = [(self.eq_play, C), (C, D), (self.eq_play, D)]
37-
second_three = [(D, C), (self.eq_play, D), (D, D)]
38-
subsequent = [(D, C), (D, D), (D, D)]
39-
expected = first_three + second_three + subsequent * 10
40-
return expected, opponent_actions
34+
opponent_actions = [C] * 50 + [D] * 100 + [C] * 52
35+
actions = ([(D, C)] + [(C, C)] * 49 + [(C, D)] * 50 + [(D, D)] * 50 +
36+
[(D, C)] * 51 + [(C, C)])
37+
opponent = MockPlayer(actions=opponent_actions)
38+
self.versus_test(opponent, expected_actions=actions,
39+
init_kwargs=init_kwargs)
4140

42-
def test_memory_depth(self):
41+
def test_memory_depth_even_soft_is_false(self):
4342
memory_depth = 4
44-
opponent_actions = [C, C, C, D, D, D]
45-
first_six = [(self.eq_play, C), (C, C), (C, C),
46-
(C, D), (C, D), (self.eq_play, D)]
47-
subsequent = [(D, C), (D, C), (self.eq_play, C),
48-
(C, D), (C, D), (self.eq_play, D)]
49-
50-
expected = first_six + subsequent * 10
51-
self.versus_test(MockPlayer(actions=opponent_actions),
52-
expected_actions=expected,
53-
init_kwargs={'memory_depth': memory_depth})
54-
55-
def test_soft_value(self):
43+
init_kwargs = {'memory_depth': memory_depth}
44+
if self.default_soft:
45+
init_kwargs['soft'] = False
46+
47+
opponent = MockPlayer(actions=[C] * memory_depth + [D] * memory_depth)
48+
actions = ([(D, C)] + [(C, C)] * 3 + [(C, D)] * 2 + [(D, D)] * 2 +
49+
[(D, C)] * 3 + [(C, C)])
50+
self.versus_test(opponent, expected_actions=actions,
51+
init_kwargs=init_kwargs)
52+
53+
def test_memory_depth_odd(self):
54+
memory_depth = 5
55+
init_kwargs = {'memory_depth': memory_depth}
56+
if self.default_soft:
57+
first_action = [(C, C)]
58+
else:
59+
first_action = [(D, C)]
60+
opponent = MockPlayer(actions=[C] * memory_depth + [D] * memory_depth)
61+
actions = (first_action + [(C, C)] * 4 + [(C, D)] * 3 + [(D, D)] * 2 +
62+
[(D, C)] * 3 + [(C, C)] * 2)
63+
self.versus_test(opponent, expected_actions=actions,
64+
init_kwargs=init_kwargs)
65+
66+
def test_default_values(self):
5667
player = self.player()
57-
self.assertFalse(player.soft)
68+
self.assertEqual(player.soft, self.default_soft)
69+
self.assertEqual(player.memory, 0)
5870

5971

6072
class TestGoByMajority(TestHardGoByMajority):
6173

6274
name = "Soft Go By Majority"
6375
player = axelrod.GoByMajority
64-
eq_play = C
65-
soft = True
66-
67-
def test_set_soft_to_false(self):
68-
self.eq_play = D
69-
expected, opponent_actions = self.get_infinite_memory_depth_actions()
70-
self.versus_test(MockPlayer(actions=opponent_actions),
71-
expected_actions=expected, init_kwargs={'soft': False})
72-
self.eq_play = C
73-
74-
def test_soft_value(self):
75-
default = self.player()
76-
self.assertTrue(default.soft)
77-
player = self.player(soft=True)
78-
self.assertTrue(player.soft)
79-
player = self.player(soft=False)
80-
self.assertFalse(player.soft)
76+
default_soft = True
77+
78+
def test_first_play(self):
79+
self.first_play_test(C)
80+
81+
def test_memory_depth_infinite_soft_is_true(self):
82+
opponent_actions = [C] * 50 + [D] * 100 + [C] * 52
83+
actions = ([(C, C)] * 50 + [(C, D)] * 51 + [(D, D)] * 49 +
84+
[(D, C)] * 50 + [(C, C)] * 2)
85+
opponent = MockPlayer(actions=opponent_actions)
86+
self.versus_test(opponent, expected_actions=actions)
87+
88+
def test_memory_depth_even_soft_is_true(self):
89+
memory_depth = 4
90+
init_kwargs = {'memory_depth': memory_depth}
91+
92+
opponent = MockPlayer([C] * memory_depth + [D] * memory_depth)
93+
actions = ([(C, C)] * 4 + [(C, D)] * 3 + [(D, D)] +
94+
[(D, C)] * 2 + [(C, C)] * 2)
95+
self.versus_test(opponent, expected_actions=actions,
96+
init_kwargs=init_kwargs)
8197

8298
def test_name(self):
8399
player = self.player(soft=True)
@@ -124,38 +140,31 @@ class TestGoByRecentMajority(TestPlayer):
124140
}
125141

126142
def test_strategy(self):
127-
"""
128-
with memory_depth=3 always switches after
129-
opponent_history=[C, C, C, D, D] (int(3*1.5) + 1 = 5)
130-
with memory_depth=4 soft switches after
131-
op_history=[C, C, C, C, D, D, D] (int(4*1.5) + 1 = 7)
132-
and hard switches after
133-
op_history=[C, C, C, C, D, D] (int(4 * 1.5) = 6)
134-
"""
135143

136144
if soft:
137145
self.first_play_test(C)
138146
else:
139147
self.first_play_test(D)
140148

149+
# for example memory_depth=2 plays against [C, C, D, D]
150+
# soft actions = [(C, C), (C, C), (C, D), (C, D)]
151+
# hard actions = [(D, C), (C, C), (C, D), (D, D)]
141152
opponent_actions = [C] * memory_depth + [D] * memory_depth
142-
143-
if memory_depth % 2 == 1 or soft:
144-
cooperation_len = int(memory_depth * 1.5) + 1
145-
else:
146-
cooperation_len = int(memory_depth * 1.5)
147-
defect_len = 2 * memory_depth - cooperation_len
148-
153+
opponent = MockPlayer(actions=opponent_actions)
149154
if soft:
150-
first_move = [C]
155+
first_player_action = [C]
156+
else:
157+
first_player_action = [D]
158+
if memory_depth % 2 == 1 or soft:
159+
cooperations = int(memory_depth * 1.5)
151160
else:
152-
first_move = [D]
161+
cooperations = int(memory_depth * 1.5) - 1
162+
defections = len(opponent_actions) - cooperations - 1
163+
player_actions = (first_player_action + [C] * cooperations +
164+
[D] * defections)
153165

154-
player_actions = (first_move + [C] * (cooperation_len - 1) +
155-
[D] * defect_len)
156-
expected = list(zip(player_actions, opponent_actions))
157-
self.versus_test(MockPlayer(actions=opponent_actions),
158-
expected_actions=expected)
166+
actions = list(zip(player_actions, opponent_actions))
167+
self.versus_test(opponent, expected_actions=actions)
159168

160169
return TestGoByRecentMajority
161170

0 commit comments

Comments
 (0)