-
Notifications
You must be signed in to change notification settings - Fork 0
/
bots.py
498 lines (399 loc) · 13.8 KB
/
bots.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import random
import torch
import numpy as np
actions_dict = {1: 'cooperate🤝', -1: 'defect👊', 0: 'wait'}
class Bot:
def __init__(self, name, emoji=':robot:', rounds=10):
self.name = name
self.emoji = emoji
self.rounds = rounds
self.record = [[0 for _ in range(rounds)], [0 for _ in range(rounds)]]
self.coins = rounds
def __str__(self):
return self.name
@property
def turns(self) -> int:
"""
Get the number of turns that have passed
获取已经过去的回合数
"""
return self.record[0].index(0)
@property
def player_coins(self) -> int:
"""
Get the number of coins the player has
获取玩家拥有的金币数
"""
return 10 - self.record[0].count(1) + self.record[1].count(1) * 3
@property
def opponent_coins(self) -> int:
"""
Get the number of coins the opponent has
获取对手拥有的金币数
"""
return self.rounds - self.record[1].count(1) + self.record[0].count(1) * 3
def reset(self):
"""
Reset the record of the game
重置游戏记录
"""
if 0 not in self.record[0]:
self.record = [[0 for _ in range(self.rounds)], [0 for _ in range(self.rounds)]]
self.coins = self.rounds
class Imitator(Bot):
"""
Imitates the opponent's last move
模仿对手的最后一次行动
"""
def __init__(self, name="Imitator", number='', rounds=10):
super().__init__(name + number, rounds=rounds)
def action(self) -> int:
if self.turns == 9:
return -1
if self.turns == 0:
return 1
else:
return self.record[1][self.turns - 1]
class Randomizer(Bot):
"""
Randomly chooses to cooperate or defect
随机选择合作或欺骗
"""
def __init__(self, name="Randomizer", number='', rounds=10):
super().__init__(name + number, rounds=rounds)
def action(self) -> int:
r = random.random()
if r < 0.5:
return 1
else:
return -1
class Selfish(Bot):
"""
Always defects
总是欺骗
"""
def __init__(self, name="Selfish", number='', rounds=10):
super().__init__(name + number, rounds=rounds)
def action(self) -> int:
return -1
class Nice(Bot):
"""
Always cooperates
总是合作
"""
def __init__(self, name="Nice", number='', rounds=10):
super().__init__(name + number, rounds=rounds)
def action(self) -> int:
return 1
class Speculator(Bot):
"""
Cooperates until the opponent defects, then defects
选择合作直到对手选择欺骗,然后欺骗
"""
def __init__(self, name="Speculator", number='', rounds=10):
super().__init__(name + number, rounds=rounds)
def action(self) -> int:
if self.turns == 9:
return -1
if self.turns == 0:
return 1
elif -1 in self.record[1]:
return -1
else:
return 1
class Perception(Bot):
"""
Choose by the opponent's first move
根据对手的第一次行动选择
"""
def __init__(self, name="Perception", number='', rounds=10):
super().__init__(name + number, rounds=rounds)
def action(self) -> int:
if self.turns == 9:
return -1
if self.turns == 0:
return 1
else:
return self.record[1][0]
class Reverser(Bot):
"""
Choose the opposite of the opponent's last move
选择与对手的最后一次行动相反的行动
"""
def __init__(self, name="Reverser", number='', rounds=10):
super().__init__(name + number, rounds=rounds)
def action(self) -> int:
if self.turns == 9:
return -1
if self.turns == 0:
return 1
else:
return -self.record[1][self.turns - 1]
class Merlin(Bot):
"""
Cooperates if coins are more than 10, defects otherwise
金币数大于等于10时合作,否则欺骗
"""
def __init__(self, name="Merlin", number='', emoji=":grinning_face:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
def action(self) -> int:
if self.turns == 9:
return -1
if self.player_coins >= 10:
return 1
else:
return -1
class Why(Bot):
"""
Choose by trust value
根据信任值选择
"""
def __init__(self, name="JB114514CM", number='', emoji=":face_without_mouth:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
def action(self) -> int:
if self.turns == 9:
return -1
if self.turns == 0:
return 1
else:
return self.record[1][0]
def get_trust(self) -> int:
"""
Get the trust value of the opponent
获取对手的信任值
"""
trust = 1
turns = self.turns
if turns == 0:
return 1
else:
if self.record[turns - 1][1] == -1:
trust -= 1
else:
if self.record[turns - 1][0] == -1:
trust += 2
else:
trust += 1
return 1 if trust > 0 else -1
class Holmes(Bot):
"""
Imitates the opponent's last move if opponent defects less than 2 times, otherwise defects
如果对手欺骗次数小于2次,模仿对手的最后一次行动,否则欺骗
"""
def __init__(self, name="Holmes", number='', emoji=":detective:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
def action(self) -> int:
if self.turns == 9:
return -1
if self.turns == 0:
return 1
else:
if self.record[1].count(-1) < 2:
return self.record[1][self.turns - 1]
else:
return -1
class White(Bot):
"""
If opponent cooperates, choose to cooperate 2 times, otherwise choose to defect
如果对手合作,选择合作2次,否则选择欺骗
"""
def __init__(self, name="White", number='', emoji=":face_blowing_a_kiss:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
def action(self) -> int:
turns = self.turns
if turns == 9:
return -1
if turns == 0:
return 1
elif turns == 1:
return self.record[1][0]
else:
if 1 in self.record[1][turns - 2:turns]:
return 1
else:
return -1
class Planner(Bot):
"""
Cooperates two times and defects one times;if opponent defects more than 2 times, defects
选择合作1次,欺骗1次;如果对手欺骗次数大于等于2次,选择欺骗
"""
def __init__(self, name="Planner", number='', emoji=":face_with_monocle:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
def action(self) -> int:
turns = self.turns
if turns == 9:
return -1
if self.record[1].count(-1) >= 2:
return -1
else:
return 1 if turns % 2 == 0 else -1
class Luck(Bot):
"""
If opponent detects odd number of times, defects, otherwise cooperates
如果对手欺骗奇数次,选择欺骗,否则选择合作
"""
def __init__(self, name="Dululu", number='', emoji=":zany_face:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
def action(self) -> int:
return 1 if self.record[1].count(-1) % 2 == 0 else -1
class Revenge(Bot):
"""
If opponent defects, choose to defect 2 times, otherwise choose to cooperate
如果对手欺骗,选择欺骗2次,否则选择合作
"""
def __init__(self, name="M0nesy", number='', emoji=":smiling_face_with_smiling_eyes:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
def action(self) -> int:
turns = self.turns
if turns == 9:
return -1
if turns == 0:
return 1
elif turns == 1:
return self.record[1][0]
else:
if -1 in self.record[1][turns - 2:turns]:
return -1
else:
return 1
class Compare(Bot):
"""
Defect if opponent's coins are more than mine, otherwise cooperate
如果对手金币数大于我的金币数,选择欺骗,否则选择合作
"""
def __init__(self, name="SDDL", number='', emoji=":bear:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
def action(self) -> int:
if self.turns == 9:
return -1
if self.player_coins < self.opponent_coins:
return -1
else:
return 1
class Snobbish(Bot):
"""
Defect if opponent's coins are less than mine, otherwise cooperate
如果对手金币数小于我的金币数,选择欺骗,否则选择合作
"""
def __init__(self, name="LDDS", number='', emoji=":teddy_bear:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
def action(self) -> int:
if self.turns == 9:
return -1
if self.player_coins > self.opponent_coins:
return -1
else:
return 1
class Nico(Bot):
"""
Cooperate in the first 4 rounds, if coins more than 10 in the first 5 rounds, cooperate in the next 5 rounds;otherwise defect
前五轮合作;如果第四轮后硬币大于10,则之后一直合作,否则一直欺骗
"""
def __init__(self, name="Nico", number='', emoji=":smiling_face:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
self.flag = 0
def action(self) -> int:
turns = self.turns
if turns == 9:
return -1
if turns < 4:
self.flag = 0
return 1
elif turns == 4:
self.flag = self.player_coins > 10
return 1 if self.flag else -1
else:
return 1 if self.flag else -1
class Analyst(Bot):
"""
If opponent's cooperation ratio is greater than 0.5, cooperate, otherwise defect
如果对方合作的比例大于0.5,选择合作,否则选择欺骗
"""
def __init__(self, name="Analyst", number='', emoji=":face_with_hand_over_mouth:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
def action(self) -> int:
if self.turns == 9:
return -1
if self.turns == 0:
return 1
else:
return 1 if self.record[1].count(1) / self.turns > 0.6 else -1
class Rocket(Bot):
def __init__(self, name="Rocket", emoji=":rocket:", number='', rounds=10):
super().__init__(name + number, rounds=rounds, emoji=emoji)
self.model = torch.load('model.pth')
def action(self):
actions = [1, -1]
record = torch.from_numpy(np.array(self.record)).float()
if torch.cuda.is_available():
record = record.cuda()
index = self.model(record).argmax()
return actions[index]
class Rice(Bot):
"""
Defect if opponent's coins are odd, otherwise cooperate
如果对方硬币为奇数就欺骗,否则合作
"""
def __init__(self, name="RiceSmell", number='', emoji=":folded_hands:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
def action(self) -> int:
if self.turns == 9:
return -1
return 1 if self.opponent_coins % 2 == 0 else -1
class Sword(Bot):
"""
Cooperate in the first round; if opponent did the same as me in the last round, do the same as last round, otherwise do the opposite
第一轮合作,如果上一轮双方相同,则采取上一轮的策略,否则采取相反的策略
"""
def __init__(self, name="Swordmeme", number='', emoji=":grinning_squinting_face:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
def action(self) -> int:
turns = self.turns
if turns == 9:
return -1
if turns == 0:
return 1
else:
if self.record[0][turns - 1] == self.record[1][turns - 1]:
return -self.record[0][turns - 1]
else:
return self.record[0][turns - 1]
class AV4k(Bot):
"""
Cooperate in the first round; imitate the opponent's action in the last round in even rounds, otherwise cooperate if the coin is greater than 10, otherwise defect
第一轮合作;偶数轮模仿上一轮对方行动;奇数轮硬币大于10合作,否则欺骗。
"""
def __init__(self, name="AV4k", number='', emoji=":hamburger:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
def action(self) -> int:
turns = self.turns
if turns == 9:
return -1
if turns == 0:
return 1
else:
if turns % 2 == 0:
return self.record[1][turns - 1]
else:
if self.player_coins > 10:
return 1
else:
return -1
class Hypocrite(Bot):
"""
Cooperate until opponent defects, then defect forever;always defect in the last two rounds
合作,直到对方欺骗,然后一直欺骗;最后两轮必定欺骗
"""
def __init__(self, name="Hypocrite", number='', emoji=":smiling_face_with_horns:", rounds=10):
super().__init__(name + number, emoji=emoji, rounds=rounds)
def action(self) -> int:
turns = self.turns
if turns >= 8:
return -1
if turns == 0:
return 1
else:
if -1 in self.record[1]:
return -1
else:
return 1