-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacters.py
More file actions
134 lines (112 loc) · 3.73 KB
/
characters.py
File metadata and controls
134 lines (112 loc) · 3.73 KB
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
from trust_basics import trust_result
import random
DET_PLAY_BOOK = [1, 0, 1, 1]
class Character():
changing_strategies = [
'copycat',
'grudger',
'detective',
'copykitten',
'simpleton',
'random'
]
def __init__(self, strategy, action=None, start_score=0, payoff=2):
self.strategy = strategy
self.score = start_score
self.play_tally = []
self.action = action
self.player_id = None
if self.strategy == 'detective':
self.det_play_book = iter(DET_PLAY_BOOK)
self.payoff = payoff
def play(self):
if self.strategy in self.changing_strategies:
self.action = self.get_action()
return self.action
def round(self, other, mistake):
self_action = self.play()
other_action = other.play()
r1 = random.random()
r2 = random.random()
if r1 <= mistake:
self_action = not self_action
if r2 <= mistake:
other_action = not other_action
results = trust_result(self_action, other_action, self.payoff)
self.play_tally.append(other_action)
other.play_tally.append(self_action)
self.score += results[0]
other.score += results[1]
return self_action, other_action
def get_action(self):
if self.strategy == 'copycat':
if self.play_tally == []:
# print(self.play_tally)
return 1
else:
return self.play_tally[-1]
if self.strategy == 'grudger':
if 0 not in self.play_tally:
return 1
else:
return 0
if self.strategy == 'detective':
try:
return next(self.det_play_book)
except StopIteration:
if 0 not in self.play_tally:
return 0
else:
return self.play_tally[-1]
if self.strategy == 'copykitten':
if self.play_tally == []:
return 1
elif len(self.play_tally) >= 2 and not any(self.play_tally[-2:]):
return 0
else:
return 1
if self.strategy == 'simpleton':
if self.play_tally == []:
return 1
elif self.play_tally[-1]:
return self.action
elif not self.play_tally[-1]:
return not self.action
if self.strategy == 'random':
return random.random() >= 0.5
def reset(self):
self.play_tally = []
if self.strategy == 'detective':
self.det_play_book = iter(DET_PLAY_BOOK)
def born_again(self):
self.score = 0
def to_dict(self):
return {
'id': self.player_id,
'strategy': self.strategy,
'score': self.score
}
if __name__ == '__main__':
type_A = Character('cheat', action=0)
type_B = Character('cooperate', action=1)
type_C = Character('copycat')
type_D = Character('grudger')
type_E = Character('detective')
print(type_A.strategy, type_B.strategy)
for i in range(10):
type_A.round(type_B)
print(type_A.score, type_B.score)
type_A.reset()
type_B.reset()
print(type_A.strategy, type_C.strategy)
for i in range(10):
type_A.round(type_C)
print(type_A.score, type_C.score)
print(type_B.strategy, type_C.strategy)
type_B.reset()
type_C.reset()
for i in range(10):
type_B.round(type_C)
print(type_B.score, type_C.score)
type_C.reset()
type_B.reset()