-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategies.py
340 lines (219 loc) · 7.86 KB
/
strategies.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import random
class Player:
'''General template for each strategy'''
name = 'Player'
classifier = {
'niceness' : 0,
'forgiveness' : 0,
'memory_depth' : 0
}
def __init__(self):
self.history = [] # tracks its own moves
self.score = 0
def strategy(self, opponent_history):
'''
Defines behaviour for a move. Override in subclasses
Function returns one of two values for each move:
"C" to cooperate,
"D" to defect.
'''
raise NotImplementedError()
def reset(self):
'''Resets the player's state for a new tournament'''
self.history = []
self.score = 0
class TitForTat(Player):
name = 'Tit_for_tat'
'''Cooperates on the first move, otherwise returns opponent's last move'''
classifier = {
'niceness' : 1,
'forgiveness' : 1,
'memory_depth' : 1
}
def strategy(self, opponent_history):
if not opponent_history:
return 'C'
return opponent_history[-1]
class AlwaysCooperate(Player):
name = 'Always_cooperate'
'''Always cooperates'''
classifier = {
'niceness' : 1,
'forgiveness' : 1,
'memory_depth' : 0
}
def strategy(self, opponent_history):
return 'C'
class AlwaysDefect(Player):
name = 'Always_defect'
'''Always defects'''
classifier = {
'niceness' : 0,
'forgiveness' : 0,
'memory_depth' : 0
}
def strategy(self, opponent_history):
return 'D'
class TitForTwoTats(Player):
name = 'Tit_for_two_tats'
'''Cooperate on first move, otherwise only defects if opponent defects twice in a row'''
classifier = {
'niceness' : 1,
'forgiveness' : .5,
'memory_depth' : 2
}
def strategy(self, opponent_history):
'''Counts how many defections in last 2 moves'''
count_opponent_defections = 0
for move in opponent_history[-2:]:
if move == 'D':
count_opponent_defections += 1
if not opponent_history or count_opponent_defections < 2:
return 'C'
else:
return 'D'
class Random(Player):
name = 'Random'
'''Cooperates or defects randomly'''
classifier = {
'niceness' : None,
'forgiveness' : None,
'memory_depth' : 0
}
def strategy(self, opponent_history):
return random.choice(['C', 'D'])
class Alternator(Player):
name = 'Alternator'
'''Alternates between cooperating and defecting'''
classifier = {
'niceness' : None,
'forgiveness' : None,
'memory_depth' : 0
}
def strategy(self, opponent_history):
if len(opponent_history) % 2 == 0:
return 'C'
else:
return 'D'
class NotNiceTitForTat(Player):
name = 'Not_nice_tit_for_tat'
'''TitForTat but defects on first move'''
classifer = {
'niceness' : 0,
'forgiveness' : .5,
'memory_depth' : 1
}
def strategy(self, opponent_history):
if not opponent_history:
return 'D'
else:
return opponent_history[-1]
class Friedman(Player):
name = 'Grim_Trigger'
'''Cooperates until the opponent defects; once defected against, it defects forever.'''
classifier = {
'niceness': 1,
'forgiveness': 0,
'memory_depth': float('inf')
}
def strategy(self, opponent_history):
return 'D' if 'D' in opponent_history else 'C'
class Pavlov(Player):
name = 'Pavlov'
'''Cooperates if the previous round resulted in a payoff of 3 (mutual cooperation) or 5 (exploiting the opponent), otherwise defects.'''
classifier = {
'niceness': 0.75,
'forgiveness': 0.75,
'memory_depth': 1
}
def strategy(self, opponent_history):
if not opponent_history:
return 'C'
return 'C' if opponent_history[-1] == self.history[-1] else 'D'
class Prober(Player):
name = 'Prober'
'''Defects initially to test the opponent, then behaves like Tit for Tat or Always Defect based on the opponent's response.'''
classifier = {
'niceness': 0.5,
'forgiveness': 0.5,
'memory_depth': float('inf')
}
def strategy(self, opponent_history):
if len(opponent_history) < 3:
return 'D' if len(opponent_history) in [0, 2] else 'C'
return 'D' if opponent_history[1:3] == ['D', 'D'] else TitForTat().strategy(opponent_history)
class Tester(Player):
name = 'Tester'
'''Starts with "DCC", then cooperates if the opponent defects in response to defection; otherwise, defects forever.'''
def strategy(self, opponent_history):
if len(opponent_history) < 3:
return 'D' if len(opponent_history) == 0 else 'C'
return 'C' if opponent_history[1] == 'D' else 'D'
class Joss(Player):
name = 'Joss'
'''A variant of Tit For Tat that randomly defects with a small probability (e.g., 10%).'''
def strategy(self, opponent_history):
if not opponent_history:
return 'C'
return 'D' if random.random() < 0.1 else opponent_history[-1]
class SoftMajority(Player):
name = 'Soft_Majority'
'''Cooperates if the opponent has cooperated more than they have defected; otherwise, defects.'''
def strategy(self, opponent_history):
if not opponent_history:
return 'C'
return 'C' if opponent_history.count('C') > opponent_history.count('D') else 'D'
class AdaptiveTitForTat(Player):
name = 'Adaptive_Tit_For_Tat'
'''Starts with cooperation but adjusts its behavior by being more forgiving if the opponent cooperates frequently.'''
def strategy(self, opponent_history):
if not opponent_history:
return 'C'
if opponent_history.count('C') > opponent_history.count('D'):
return 'C'
return opponent_history[-1]
class Punisher(Player):
name = 'Punisher'
'''Starts with cooperation but defects for a set number of rounds when the opponent defects.'''
def __init__(self):
self.punishment_rounds = 0
def strategy(self, opponent_history):
if self.punishment_rounds > 0:
self.punishment_rounds -= 1
return 'D'
if opponent_history and opponent_history[-1] == 'D':
self.punishment_rounds = 2 # Punish for 2 rounds
return 'D'
return 'C'
class Extortioner(Player):
name = 'Extortioner'
'''Attempts to exploit the opponent by cooperating only enough to keep them from always defecting.'''
def strategy(self, opponent_history):
if not opponent_history or len(opponent_history) % 3 == 0:
return 'C'
return 'D'
class Retaliator(Player): # needs work with init method (inheritance bug - use super.()__init__ and change reset function if necessary)
name = 'Retaliator'
'''Cooperates until the opponent defects, then retaliates for the same number of rounds'''
def __init__(self):
self.retaliation_rounds = 0
def strategy(self, opponent_history):
if self.retaliation_rounds > 0:
self.retaliation_rounds -= 1
return 'D'
if opponent_history and opponent_history[-1] == 'D':
self.retaliation_rounds = 1 # Retaliate for 1 round
return 'D'
return 'C'
class Spiteful(Player):
name = 'Spiteful'
'''Starts with cooperation but switches to Always Defect after the opponent defects a certain number of times.'''
def __init__(self):
self.spited = False
def strategy(self, opponent_history):
if self.spited:
return 'D'
if opponent_history.count('D') >= 3:
self.spited = True
return 'D'
return 'C'