2
2
3
3
import axelrod
4
4
from .test_player import TestPlayer
5
+ from axelrod import MockPlayer
5
6
6
7
C , D = axelrod .Actions .C , axelrod .Actions .D
7
8
@@ -10,8 +11,8 @@ class TestHardGoByMajority(TestPlayer):
10
11
11
12
name = "Hard Go By Majority"
12
13
player = axelrod .HardGoByMajority
13
- default_soft = False
14
14
eq_play = D
15
+ soft = False
15
16
16
17
expected_classifier = {
17
18
'stochastic' : False ,
@@ -24,40 +25,55 @@ class TestHardGoByMajority(TestPlayer):
24
25
}
25
26
26
27
def test_strategy (self ):
27
- # Starts by defecting.
28
28
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 ):
40
56
player = self .player ()
41
- self .assertEqual (player .soft , self . default_soft )
57
+ self .assertFalse (player .soft )
42
58
43
59
44
60
class TestGoByMajority (TestHardGoByMajority ):
45
61
46
62
name = "Soft Go By Majority"
47
63
player = axelrod .GoByMajority
48
- default_soft = True
49
64
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 )
61
77
player = self .player (soft = True )
62
78
self .assertTrue (player .soft )
63
79
player = self .player (soft = False )
@@ -68,17 +84,22 @@ def test_name(self):
68
84
self .assertEqual (player .name , "Soft Go By Majority" )
69
85
player = self .player (soft = False )
70
86
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" )
71
89
72
- def test_repr (self ):
90
+ def test_str (self ):
73
91
player = self .player (soft = True )
74
92
name = str (player )
75
93
self .assertEqual (name , "Soft Go By Majority" )
76
94
player = self .player (soft = False )
77
95
name = str (player )
78
96
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" )
79
100
80
101
81
- def factory_TestGoByRecentMajority (L , soft = True ):
102
+ def factory_TestGoByRecentMajority (memory_depth , soft = True ):
82
103
83
104
prefix = "Hard"
84
105
prefix2 = "Hard"
@@ -88,12 +109,13 @@ def factory_TestGoByRecentMajority(L, soft=True):
88
109
89
110
class TestGoByRecentMajority (TestPlayer ):
90
111
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 ))
93
115
94
116
expected_classifier = {
95
117
'stochastic' : False ,
96
- 'memory_depth' : L ,
118
+ 'memory_depth' : memory_depth ,
97
119
'makes_use_of' : set (),
98
120
'long_run_time' : False ,
99
121
'inspects_source' : False ,
@@ -102,27 +124,38 @@ class TestGoByRecentMajority(TestPlayer):
102
124
}
103
125
104
126
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
+
106
136
if soft :
107
137
self .first_play_test (C )
108
138
else :
109
139
self .first_play_test (D )
110
140
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
115
148
116
- # Test 50:50 play difference with soft
117
- k = L
118
- if L % 2 == 1 :
119
- k -= 1
120
149
if soft :
121
- self .responses_test ([C ], [C ] * k ,
122
- [C ] * (k // 2 ) + [D ] * (k // 2 ))
150
+ first_move = [C ]
123
151
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 )
126
159
127
160
return TestGoByRecentMajority
128
161
0 commit comments