-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathretaliate.py
194 lines (150 loc) · 5.48 KB
/
retaliate.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
from collections import defaultdict
from axelrod.action import Action
from axelrod.player import Player
C, D = Action.C, Action.D
class Retaliate(Player):
"""
A player starts by cooperating but will retaliate once the opponent
has won more than 10 percent times the number of defections the player has.
Names:
- Retaliate: Original name by Owen Campbell
"""
name = "Retaliate"
classifier = {
"memory_depth": float("inf"), # Long memory
"stochastic": False,
"inspects_source": False,
"long_run_time": False,
"manipulates_source": False,
"manipulates_state": False,
}
def __init__(self, retaliation_threshold: float = 0.1) -> None:
"""
Uses the basic init from the Player class, but also set the name to
include the retaliation setting.
"""
super().__init__()
self.retaliation_threshold = retaliation_threshold
self.play_counts = defaultdict(int) # type: defaultdict
def strategy(self, opponent: Player) -> Action:
"""
If the opponent has played D to my C more often than x% of the time
that I've done the same to him, play D. Otherwise, play C.
"""
if len(self.history):
last_round = (self.history[-1], opponent.history[-1])
self.play_counts[last_round] += 1
CD_count = self.play_counts[(C, D)]
DC_count = self.play_counts[(D, C)]
if CD_count > DC_count * self.retaliation_threshold:
return D
return C
class Retaliate2(Retaliate):
"""
Retaliate player with a threshold of 8 percent.
Names:
- Retaliate 2: Original name by Owen Campbell
"""
name = "Retaliate 2"
def __init__(self, retaliation_threshold: float = 0.08) -> None:
super().__init__(retaliation_threshold=retaliation_threshold)
class Retaliate3(Retaliate):
"""
Retaliate player with a threshold of 5 percent.
Names:
- Retaliate 3: Original name by Owen Campbell
"""
name = "Retaliate 3"
def __init__(self, retaliation_threshold: float = 0.05) -> None:
super().__init__(retaliation_threshold=retaliation_threshold)
class LimitedRetaliate(Player):
"""
A player that co-operates unless the opponent defects and wins.
It will then retaliate by defecting. It stops when either, it has beaten
the opponent 10 times more often that it has lost or it reaches the
retaliation limit (20 defections).
Names:
- Limited Retaliate: Original name by Owen Campbell
"""
name = "Limited Retaliate"
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, retaliation_threshold: float = 0.1, retaliation_limit: int = 20
) -> None:
"""
Parameters
----------
retaliation_threshold, float
The threshold of the difference in defections, previous rounds of
(C, D) versus (D, C)
retaliation_limit, int
The maximum number of retaliations until the strategy returns to
cooperation
"""
super().__init__()
self.retaliating = False
self.retaliation_count = 0
self.retaliation_threshold = retaliation_threshold
self.retaliation_limit = retaliation_limit
self.play_counts = defaultdict(int) # type: defaultdict
def strategy(self, opponent: Player) -> Action:
"""
If the opponent has played D to my C more often than x% of the time
that I've done the same to him, retaliate by playing D but stop doing
so once I've hit the retaliation limit.
"""
if len(self.history):
last_round = (self.history[-1], opponent.history[-1])
self.play_counts[last_round] += 1
CD_count = self.play_counts[(C, D)]
DC_count = self.play_counts[(D, C)]
if CD_count > DC_count * self.retaliation_threshold:
self.retaliating = True
else:
self.retaliating = False
self.retaliation_count = 0
if self.retaliating:
if self.retaliation_count < self.retaliation_limit:
self.retaliation_count += 1
return D
else:
self.retaliation_count = 0
self.retaliating = False
return C
class LimitedRetaliate2(LimitedRetaliate):
"""
LimitedRetaliate player with a threshold of 8 percent and a
retaliation limit of 15.
Names:
- Limited Retaliate 2: Original name by Owen Campbell
"""
name = "Limited Retaliate 2"
def __init__(
self, retaliation_threshold: float = 0.08, retaliation_limit: int = 15
) -> None:
super().__init__(
retaliation_threshold=retaliation_threshold,
retaliation_limit=retaliation_limit,
)
class LimitedRetaliate3(LimitedRetaliate):
"""
LimitedRetaliate player with a threshold of 5 percent and a
retaliation limit of 20.
Names:
- Limited Retaliate 3: Original name by Owen Campbell
"""
name = "Limited Retaliate 3"
def __init__(
self, retaliation_threshold: float = 0.05, retaliation_limit: int = 20
) -> None:
super().__init__(
retaliation_threshold=retaliation_threshold,
retaliation_limit=retaliation_limit,
)