forked from Axelrod-Python/Axelrod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extortionExamples.py
143 lines (111 loc) · 3.26 KB
/
extortionExamples.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
import axelrod as axl
from axelrod.player import Player
C = axl.action.Action.C
D = axl.action.Action.D
class Extortable(Player):
""" A player who can be extorted """
def __init__(self, remaining: int = 10) -> None:
self.remaining = remaining
super().__init__()
classifier = {
"stochastic" : True,
}
rates = {C:[0,0], D:[0,0]}
def strategy(self, opponent):
if len(self.history) < 1:
return C
# borrowed from adaptive
game = self.match_attributes["game"]
last_round = (self.history[-1], opponent.history[-1])
scores = game.score(last_round)
self.rates[last_round[0]][0] += scores[0]
self.rates[last_round[0]][1] += 1
if len(self.history) < 10:
return C
elif len(self.history) < 20:
return D
if self.remaining > 0:
self.remaining -= 1
return self.history[-1]
else:
self.remaining = 10
ratio = (self.rates[C][0]*self.rates[D][1])/(self.rates[C][1]*self.rates[D][0])
return self._random.random_choice(ratio)
def __repr__(self):
""" The string method for the strategy: """
return 'Extortable'
class Extortionist(Player):
""" An extortionist. Very similar to BBE """
def __init__(self, pun_length: int = 3, honesty: float = 0.9) -> None:
self.pun_length = pun_length
self.honesty = honesty
self.retaliated = 0
self.triggered = False
super().__init__()
classifier = {
"stochastic" : True,
}
def strategy(self, opponent):
if len(self.history) < 1:
return C
if opponent.history[-1] == D:
self.triggered = True
if self.triggered:
if self.retaliated < self.pun_length:
self.retaliated += 1
else:
self.triggered = False
self.retaliated
return D
return self._random.random_choice(self.honesty)
def __repr__(self):
""" The string method for the strategy: """
return 'Extortionist'
print("and now for the matches")
def print_match(c1, c2):
s1 = c1()
s2 = c2()
match = axl.Match([s1, s2], turns=2000, prob_end=0.00001)
match.play()
print()
print(s1, " vs ", s2)
print(match.normalised_state_distribution())
print(match.final_score_per_turn())
print()
print_match(axl.ZDExtort3, axl.TitForTat)
print_match(Extortionist, axl.TitForTat)
print_match(Extortable, axl.TitForTat)
print_match(axl.TitForTat, axl.TitForTat)
print_match(Extortionist, axl.Adaptive)
print_match(axl.ZDExtort2, axl.Adaptive)
print_match(axl.ZDExtort2, axl.Adaptive)
print_match(Extortionist, axl.DBS)
print_match(axl.ZDExtort2, axl.DBS)
print_match(axl.ZDExtort3, axl.DBS)
print_match(Extortionist, Extortable)
print_match(axl.ZDExtort2, Extortable)
print_match(axl.ZDExtort3, Extortable)
print_match(Extortable, Extortable)
print_match(Extortionist, Extortionist)
print_match(axl.ZDExtort2, axl.ZDExtort2)
print_match(axl.ZDExtort2, axl.ZDExtort3)
print_match(axl.ZDExtort3, axl.ZDExtort3)
# players = [
# Extortionist(),
# Extortable(),
# axl.DBS(),
# axl.Adaptive(),
# axl.ZDExtort2(),
# axl.ZDExtort3(),
# axl.TitForTat()
# ]
# tournament = axl.Tournament(
# players,
# seed=1,
# turns=2000,
# prob_end=0.00001,
# repetitions=3
# )
# results = tournament.play()
# results.ranked_names
# print(results.ranked_names)