-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategies.py
142 lines (132 loc) · 5.57 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
__author__ = 'Areg'
import random
class Strategies:
def __init__(self):
self.allstrategies = {}
self.allstrategies["titfortat"] = self.TitForTat
self.allstrategies["titfor2tats"] = self.TitFor2Tats
self.allstrategies["random"] = self.Random
self.allstrategies["alwaysdefect"] = self.AlwaysDefect
self.allstrategies["alwayscooperate"] = self.AlwaysCooperate
self.allstrategies["maximin"] = self.Maximin
self.allstrategies["winstayloseshift"] = self.WinStayLoseShift
self.allstrategies["siri"] = self.Siri
self.allstrategies_names = {}
self.allstrategies_names["titfortat"] = "Tit-For-Tat"
self.allstrategies_names["titfor2tats"] = "Tit-For-2-Tats"
self.allstrategies_names["random"] = "Random"
self.allstrategies_names["alwaysdefect"] = "Always Defect"
self.allstrategies_names["alwayscooperate"] = "Always Cooperate"
self.allstrategies_names["maximin"] = "Maximin"
self.allstrategies_names["winstayloseshift"] = "Win-Stay-Lose-Shift"
self.allstrategies_names["siri"] = "Siri"
def TitForTat(self, turn, previousrounds, place, game, lastscore):
if turn == 0:
step = "c"
else:
step = previousrounds[turn-1][1-place]
return step
def TitFor2Tats(self, turn, previousrounds, place, game, lastscore):
if turn < 2:
step = "c"
elif previousrounds[turn-1][1-place] == 'd' and previousrounds[turn-2][1-place] == 'd':
step = 'd'
else:
step = 'c'
return step
def Random(self, turn, previousrounds, place, game, lastscore):
step = random.choice(["c", "d", "d", "c", "c", "c", "d", "d"])
return step
def AlwaysDefect(self, turn, previousrounds, place, game, lastscore):
return "d"
def AlwaysCooperate(self, turn, previousrounds, place, game, lastscore):
return "c"
def Maximin(self, turn, previousrounds, place, game, lastscore):
if game == 'prisonersdillema':
step = 'd'
elif game == 'chicken':
if turn < 5:
step = 'c'
elif turn % 5 == 0:
step = 'd'
else:
step = 'c'
elif game == 'staghunt':
step = 'd'
return step
def WinStayLoseShift(self, turn, previousrounds, place, game, lastscore):
if turn == 0:
step = 'c'
elif game == 'prisonersdillema':
if lastscore >= 2.25:
step = previousrounds[turn-1][place]
else:
if previousrounds[turn-1][place] == 'c':
step = 'd'
else:
step = 'c'
elif game == 'chicken':
if lastscore >= 3.5:
step = previousrounds[turn-1][place]
else:
if previousrounds[turn-1][place] == 'c':
step = 'd'
else:
step = 'c'
elif game == 'staghunt':
if lastscore >= 1:
step = previousrounds[turn-1][place]
else:
if previousrounds[turn-1][place] == 'c':
step = 'd'
else:
step = 'c'
return step
def Siri(self, turn, previousrounds, place, game, lastscore):
#I am observing other participants my Master
initialsteps = ['c', 'd', 'd', 'c', 'c', 'c', 'd', 'd', 'c', 'd']
QtyDefectionsOpponent = 0
for i in range(0, len(previousrounds), 1):
if previousrounds[i][1-place] == 'd':
QtyDefectionsOpponent += 1
if game == 'staghunt':
if turn == 0:
step = 'c'
else:
if QtyDefectionsOpponent == turn:
#My Master, this oppponent is a defecter
step = 'd'
elif QtyDefectionsOpponent == 0:
#My Master, this opponent is a cooperator
step = 'c'
else:
step = 'd'
else:
if turn < 10:
step = initialsteps[turn]
else:
if QtyDefectionsOpponent == turn:
#My Master, this oppponent is a defecter
if game == 'prisonersdillema':
step = 'd'
elif game == 'chicken':
step = 'c'
elif QtyDefectionsOpponent == 0:
#My Master, this opponent is a cooperator
step = 'd'
elif float(QtyDefectionsOpponent)/float(turn + 1) <= float(1)/float(5):
#My Master, this is a kind opponent
step = 'd'
elif float(QtyDefectionsOpponent)/float(turn + 1) <= float(3)/float(5):
#My Master, this is opponent most likely is similar to us
if game == 'chicken':
step = 'd'
else:
step = 'c'
elif float(QtyDefectionsOpponent)/float(turn + 1) > float(3)/float(5):
#My Master, This is opponent most likely is a defecter
if game == 'prisonersdillema':
step = 'd'
elif game == 'chicken':
step = 'c'
return step