-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathgobymajority.py
247 lines (176 loc) · 6.35 KB
/
gobymajority.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import copy
from typing import Any, Dict, Union
from axelrod.action import Action
from axelrod.player import Player
C, D = Action.C, Action.D
class GoByMajority(Player):
"""Submitted to Axelrod's second tournament by Gail Grisell. It came 23rd
and was written in 10 lines of BASIC.
A player examines the history of the opponent: if the opponent has more
defections than cooperations then the player defects.
In case of equal
number of defections and cooperations this player will Cooperate. Passing
the `soft=False` keyword argument when initialising will create a
HardGoByMajority which Defects in case of equality.
An optional memory attribute will limit the number of turns remembered (by
default this is 0)
Names:
- Go By Majority: [Axelrod1984]_
- Grisell: [Axelrod1980b]_
- Soft Majority: [Mittal2009]_
"""
name = "Go By Majority"
classifier = {
"stochastic": False,
"inspects_source": False,
"long_run_time": False,
"manipulates_source": False,
"manipulates_state": False,
"memory_depth": float("inf"),
} # type: Dict[str, Any]
def __init__(
self, memory_depth: Union[int, float] = float("inf"), soft: bool = True
) -> None:
"""
Parameters
----------
memory_depth: int >= 0
The number of rounds to use for the calculation of the cooperation
and defection probabilities of the opponent.
soft: bool
Indicates whether to cooperate or not in the case that the
cooperation and defection probabilities are equal.
"""
super().__init__()
self.soft = soft
self.classifier["memory_depth"] = memory_depth
if self.classifier["memory_depth"] < float("inf"):
self.memory = self.classifier["memory_depth"]
else:
self.memory = 0
self.name = "Go By Majority" + (self.memory > 0) * (
": %i" % self.memory
)
if self.soft:
self.name = "Soft " + self.name
else:
self.name = "Hard " + self.name
def __repr__(self):
return self.name
def strategy(self, opponent: Player) -> Action:
"""This is affected by the history of the opponent.
As long as the opponent cooperates at least as often as they defect then
the player will cooperate. If at any point the opponent has more
defections than cooperations in memory the player defects.
"""
history = opponent.history[-self.memory :]
defections = sum([s == D for s in history])
cooperations = sum([s == C for s in history])
if defections > cooperations:
return D
if defections == cooperations:
if self.soft:
return C
else:
return D
return C
class GoByMajority40(GoByMajority):
"""
GoByMajority player with a memory of 40.
Names:
- Go By Majority 40: Original name by Karol Langner
"""
name = "Go By Majority 40"
classifier = copy.copy(GoByMajority.classifier)
classifier["memory_depth"] = 40
def __init__(self) -> None:
super().__init__(memory_depth=40)
class GoByMajority20(GoByMajority):
"""
GoByMajority player with a memory of 20.
Names:
- Go By Majority 20: Original name by Karol Langner
"""
name = "Go By Majority 20"
classifier = copy.copy(GoByMajority.classifier)
classifier["memory_depth"] = 20
def __init__(self) -> None:
super().__init__(memory_depth=20)
class GoByMajority10(GoByMajority):
"""
GoByMajority player with a memory of 10.
Names:
- Go By Majority 10: Original name by Karol Langner
"""
name = "Go By Majority 10"
classifier = copy.copy(GoByMajority.classifier)
classifier["memory_depth"] = 10
def __init__(self) -> None:
super().__init__(memory_depth=10)
class GoByMajority5(GoByMajority):
"""
GoByMajority player with a memory of 5.
Names:
- Go By Majority 5: Original name by Karol Langner
"""
name = "Go By Majority 5"
classifier = copy.copy(GoByMajority.classifier)
classifier["memory_depth"] = 5
def __init__(self) -> None:
super().__init__(memory_depth=5)
class HardGoByMajority(GoByMajority):
"""A player examines the history of the opponent: if the opponent has more
defections than cooperations then the player defects. In case of equal
number of defections and cooperations this player will Defect.
An optional memory attribute will limit the number of turns remembered (by
default this is 0)
Names:
- Hard Majority: [Mittal2009]_
"""
name = "Hard Go By Majority"
def __init__(self, memory_depth: Union[int, float] = float("inf")) -> None:
super().__init__(memory_depth=memory_depth, soft=False)
class HardGoByMajority40(HardGoByMajority):
"""
HardGoByMajority player with a memory of 40.
Names:
- Hard Go By Majority 40: Original name by Karol Langner
"""
name = "Hard Go By Majority 40"
classifier = copy.copy(GoByMajority.classifier)
classifier["memory_depth"] = 40
def __init__(self) -> None:
super().__init__(memory_depth=40)
class HardGoByMajority20(HardGoByMajority):
"""
HardGoByMajority player with a memory of 20.
Names:
- Hard Go By Majority 20: Original name by Karol Langner
"""
name = "Hard Go By Majority 20"
classifier = copy.copy(GoByMajority.classifier)
classifier["memory_depth"] = 20
def __init__(self) -> None:
super().__init__(memory_depth=20)
class HardGoByMajority10(HardGoByMajority):
"""
HardGoByMajority player with a memory of 10.
Names:
- Hard Go By Majority 10: Original name by Karol Langner
"""
name = "Hard Go By Majority 10"
classifier = copy.copy(GoByMajority.classifier)
classifier["memory_depth"] = 10
def __init__(self) -> None:
super().__init__(memory_depth=10)
class HardGoByMajority5(HardGoByMajority):
"""
HardGoByMajority player with a memory of 5.
Names:
- Hard Go By Majority 5: Original name by Karol Langner
"""
name = "Hard Go By Majority 5"
classifier = copy.copy(GoByMajority.classifier)
classifier["memory_depth"] = 5
def __init__(self) -> None:
super().__init__(memory_depth=5)