Skip to content

Commit 96d9880

Browse files
committed
original PR state. will fix according to suggestions on #989
1 parent b2953c8 commit 96d9880

File tree

2 files changed

+85
-51
lines changed

2 files changed

+85
-51
lines changed

axelrod/strategies/gobymajority.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from axelrod.actions import Actions, Action
22
from axelrod.player import Player
33

4-
from typing import Dict, Any
4+
from typing import Dict, Any, Union
55

66
import copy
77

@@ -30,23 +30,24 @@ class GoByMajority(Player):
3030
'manipulates_source': False,
3131
'manipulates_state': False,
3232
'memory_depth': float('inf')
33-
} # type: Dict[str, Any]
33+
} # type: Dict[str, Any]
3434

35-
def __init__(self, memory_depth: int = float('inf'), soft: bool = True) -> None:
35+
def __init__(self, memory_depth: Union[int, float] = float('inf'),
36+
soft: bool = True) -> None:
3637
"""
3738
Parameters
3839
----------
39-
memory_depth, int >= 0
40+
memory_depth: int >= 0
4041
The number of rounds to use for the calculation of the cooperation
4142
and defection probabilities of the opponent.
42-
soft, bool
43+
soft: bool
4344
Indicates whether to cooperate or not in the case that the
4445
cooperation and defection probabilities are equal.
4546
"""
4647

4748
super().__init__()
4849
self.soft = soft
49-
self.classifier['memory_depth'] = memory_depth
50+
self.classifier['memory_depth'] = memory_depth
5051
if self.classifier['memory_depth'] < float('inf'):
5152
self.memory = self.classifier['memory_depth']
5253
else:
@@ -140,7 +141,7 @@ class HardGoByMajority(GoByMajority):
140141
"""
141142
name = 'Hard Go By Majority'
142143

143-
def __init__(self, memory_depth: int = float('inf')) -> None:
144+
def __init__(self, memory_depth: Union[int, float] = float('inf')) -> None:
144145
super().__init__(memory_depth=memory_depth, soft=False)
145146

146147

axelrod/tests/strategies/test_gobymajority.py

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import axelrod
44
from .test_player import TestPlayer
5+
from axelrod import MockPlayer
56

67
C, D = axelrod.Actions.C, axelrod.Actions.D
78

@@ -10,8 +11,8 @@ class TestHardGoByMajority(TestPlayer):
1011

1112
name = "Hard Go By Majority"
1213
player = axelrod.HardGoByMajority
13-
default_soft = False
1414
eq_play = D
15+
soft = False
1516

1617
expected_classifier = {
1718
'stochastic': False,
@@ -24,40 +25,55 @@ class TestHardGoByMajority(TestPlayer):
2425
}
2526

2627
def test_strategy(self):
27-
# Starts by defecting.
2828
self.first_play_test(self.eq_play)
29-
# If opponent cooperates at least as often as they defect then the
30-
# player defects.
31-
self.responses_test([self.eq_play], [C, D, D, D], [D, D, C, C])
32-
# If opponent defects strictly more often than they defect then the
33-
# player defects.
34-
self.responses_test([D], [C, C, D, D, C], [D, D, C, C, D])
35-
# If opponent cooperates strictly more often than they defect then the
36-
# player cooperates.
37-
self.responses_test([C], [C, C, D, D, C], [D, C, C, C, D])
38-
39-
def test_default_soft(self):
29+
30+
expected, opponent_actions = self.get_infinite_memory_depth_actions()
31+
self.versus_test(MockPlayer(actions=opponent_actions),
32+
expected_actions=expected)
33+
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
41+
42+
def test_memory_depth(self):
43+
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):
4056
player = self.player()
41-
self.assertEqual(player.soft, self.default_soft)
57+
self.assertFalse(player.soft)
4258

4359

4460
class TestGoByMajority(TestHardGoByMajority):
4561

4662
name = "Soft Go By Majority"
4763
player = axelrod.GoByMajority
48-
default_soft = True
4964
eq_play = C
50-
51-
def test_strategy(self):
52-
# In case of equality (including first play), cooperates.
53-
super().test_strategy()
54-
55-
# Test tie break rule for soft=False
56-
player = self.player(soft=False)
57-
opponent = axelrod.Cooperator()
58-
self.assertEqual('D', player.strategy(opponent))
59-
60-
def test_soft(self):
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)
6177
player = self.player(soft=True)
6278
self.assertTrue(player.soft)
6379
player = self.player(soft=False)
@@ -68,17 +84,22 @@ def test_name(self):
6884
self.assertEqual(player.name, "Soft Go By Majority")
6985
player = self.player(soft=False)
7086
self.assertEqual(player.name, "Hard Go By Majority")
87+
player = self.player(memory_depth=5)
88+
self.assertEqual(player.name, "Soft Go By Majority: 5")
7189

72-
def test_repr(self):
90+
def test_str(self):
7391
player = self.player(soft=True)
7492
name = str(player)
7593
self.assertEqual(name, "Soft Go By Majority")
7694
player = self.player(soft=False)
7795
name = str(player)
7896
self.assertEqual(name, "Hard Go By Majority")
97+
player = self.player(memory_depth=5)
98+
name = str(player)
99+
self.assertEqual(name, "Soft Go By Majority: 5")
79100

80101

81-
def factory_TestGoByRecentMajority(L, soft=True):
102+
def factory_TestGoByRecentMajority(memory_depth, soft=True):
82103

83104
prefix = "Hard"
84105
prefix2 = "Hard"
@@ -88,12 +109,13 @@ def factory_TestGoByRecentMajority(L, soft=True):
88109

89110
class TestGoByRecentMajority(TestPlayer):
90111

91-
name = "{} Go By Majority: {}".format(prefix, L)
92-
player = getattr(axelrod, "{}GoByMajority{}".format(prefix2, L))
112+
name = "{} Go By Majority: {}".format(prefix, memory_depth)
113+
player = getattr(axelrod, "{}GoByMajority{}".format(prefix2,
114+
memory_depth))
93115

94116
expected_classifier = {
95117
'stochastic': False,
96-
'memory_depth': L,
118+
'memory_depth': memory_depth,
97119
'makes_use_of': set(),
98120
'long_run_time': False,
99121
'inspects_source': False,
@@ -102,27 +124,38 @@ class TestGoByRecentMajority(TestPlayer):
102124
}
103125

104126
def test_strategy(self):
105-
# Test initial play.
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+
"""
135+
106136
if soft:
107137
self.first_play_test(C)
108138
else:
109139
self.first_play_test(D)
110140

111-
self.responses_test([C], [C] * L,
112-
[C] * (L // 2 + 1) + [D] * (L // 2 - 1))
113-
self.responses_test([D], [C] * L,
114-
[D] * (L // 2 + 1) + [C] * (L // 2 - 1))
141+
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
115148

116-
# Test 50:50 play difference with soft
117-
k = L
118-
if L % 2 == 1:
119-
k -= 1
120149
if soft:
121-
self.responses_test([C], [C] * k,
122-
[C] * (k // 2) + [D] * (k // 2))
150+
first_move = [C]
123151
else:
124-
self.responses_test([D], [C] * k,
125-
[C] * (k // 2) + [D] * (k // 2))
152+
first_move = [D]
153+
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)
126159

127160
return TestGoByRecentMajority
128161

0 commit comments

Comments
 (0)