-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathhunter.py
261 lines (210 loc) · 7.39 KB
/
hunter.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
from typing import List, Optional, Tuple
from axelrod._strategy_utils import detect_cycle
from axelrod.action import Action
from axelrod.player import Player
C, D = Action.C, Action.D
class DefectorHunter(Player):
"""A player who hunts for defectors.
Names:
- Defector Hunter: Original name by Karol Langner
"""
name = "Defector Hunter"
classifier = {
"memory_depth": float("inf"), # Long memory
"stochastic": False,
"long_run_time": False,
"inspects_source": False,
"manipulates_source": False,
"manipulates_state": False,
}
def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if (
len(self.history) >= 4
and len(opponent.history) == opponent.defections
):
return D
return C
class CooperatorHunter(Player):
"""A player who hunts for cooperators.
Names:
- Cooperator Hunter: Original name by Karol Langner
"""
name = "Cooperator Hunter"
classifier = {
"memory_depth": float("inf"), # Long memory
"stochastic": False,
"long_run_time": False,
"inspects_source": False,
"manipulates_source": False,
"manipulates_state": False,
}
def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if (
len(self.history) >= 4
and len(opponent.history) == opponent.cooperations
):
return D
return C
def is_alternator(history: List[Action]) -> bool:
for i in range(len(history) - 1):
if history[i] == history[i + 1]:
return False
return True
class AlternatorHunter(Player):
"""A player who hunts for alternators.
Names:
- Alternator Hunter: Original name by Karol Langner
"""
name = "Alternator Hunter"
classifier = {
"memory_depth": float("inf"), # Long memory
"stochastic": False,
"long_run_time": False,
"inspects_source": False,
"manipulates_source": False,
"manipulates_state": False,
}
def __init__(self) -> None:
super().__init__()
self.is_alt = False
def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if len(opponent.history) < 6:
return C
if len(self.history) == 6:
self.is_alt = is_alternator(opponent.history)
if self.is_alt:
return D
return C
class CycleHunter(Player):
"""Hunts strategies that play cyclically, like any of the Cyclers,
Alternator, etc.
Names:
- Cycle Hunter: Original name by Marc Harper
"""
name = "Cycle Hunter"
classifier = {
"memory_depth": float("inf"), # Long memory
"stochastic": False,
"long_run_time": False,
"inspects_source": False,
"manipulates_source": False,
"manipulates_state": False,
}
def __init__(self) -> None:
super().__init__()
self.cycle = None # type: Optional[Tuple[Action]]
def strategy(self, opponent: Player) -> Action:
"""Actual strategy definition that determines player's action."""
if self.cycle:
return D
cycle = detect_cycle(opponent.history, min_size=3)
if cycle:
if len(set(cycle)) > 1:
self.cycle = cycle
return D
return C
class EventualCycleHunter(CycleHunter):
"""Hunts strategies that eventually play cyclically.
Names:
- Eventual Cycle Hunter: Original name by Marc Harper
"""
name = "Eventual Cycle Hunter"
def strategy(self, opponent: Player) -> None:
"""Actual strategy definition that determines player's action."""
if len(opponent.history) < 10:
return C
if len(opponent.history) == opponent.cooperations:
return C
if len(opponent.history) % 10 == 0:
# recheck
self.cycle = detect_cycle(opponent.history, offset=10, min_size=3)
if self.cycle:
return D
else:
return C
class MathConstantHunter(Player):
"""A player who hunts for mathematical constant players.
Names:
Math Constant Hunter: Original name by Karol Langner
"""
name = "Math Constant Hunter"
classifier = {
"memory_depth": float("inf"), # Long memory
"stochastic": False,
"long_run_time": False,
"inspects_source": False,
"manipulates_source": False,
"manipulates_state": False,
}
def strategy(self, opponent: Player) -> Action:
"""
Check whether the number of cooperations in the first and second halves
of the history are close. The variance of the uniform distribution (1/4)
is a reasonable delta but use something lower for certainty and avoiding
false positives. This approach will also detect a lot of random players.
"""
n = len(self.history)
if n >= 8 and opponent.cooperations and opponent.defections:
start1, end1 = 0, n // 2
start2, end2 = n // 4, 3 * n // 4
start3, end3 = n // 2, n
count1 = opponent.history[start1:end1].count(C) + self.history[
start1:end1
].count(C)
count2 = opponent.history[start2:end2].count(C) + self.history[
start2:end2
].count(C)
count3 = opponent.history[start3:end3].count(C) + self.history[
start3:end3
].count(C)
ratio1 = 0.5 * count1 / (end1 - start1)
ratio2 = 0.5 * count2 / (end2 - start2)
ratio3 = 0.5 * count3 / (end3 - start3)
if abs(ratio1 - ratio2) < 0.2 and abs(ratio1 - ratio3) < 0.2:
return D
return C
class RandomHunter(Player):
"""A player who hunts for random players.
Names:
- Random Hunter: Original name by Karol Langner
"""
name = "Random Hunter"
classifier = {
"memory_depth": float("inf"), # Long memory
"stochastic": False,
"long_run_time": False,
"inspects_source": False,
"manipulates_source": False,
"manipulates_state": False,
}
def __init__(self) -> None:
self.countCC = 0
self.countDD = 0
super().__init__()
def strategy(self, opponent: Player) -> Action:
"""
A random player is unpredictable, which means the conditional frequency
of cooperation after cooperation, and defection after defections, should
be close to 50%... although how close is debatable.
"""
# Update counts
if len(self.history) > 1:
if self.history[-2] == C and opponent.history[-1] == C:
self.countCC += 1
if self.history[-2] == D and opponent.history[-1] == D:
self.countDD += 1
n = len(self.history)
if n > 10:
probabilities = []
if self.cooperations > 5:
probabilities.append(self.countCC / self.cooperations)
if self.defections > 5:
probabilities.append(self.countDD / self.defections)
if probabilities and all(
[abs(p - 0.5) < 0.25 for p in probabilities]
):
return D
return C