-
Notifications
You must be signed in to change notification settings - Fork 1
/
response.py
63 lines (52 loc) · 1.79 KB
/
response.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
import random
payoff = { ("loyal", "loyal"): 3,
("loyal", "defect"): -5,
("defect", "defect"): 1,
("defect", "loyal"): 9 }
class Person:
def __init__(self, defectresp, loyalresp):
self.dr = max(.01, min(.99, defectresp))
self.lr = max(.01, min(.99, loyalresp))
self.wealth = 0.0
def play(self, opplastmove):
choice = random.random()
if opplastmove == "defect":
if self.dr > choice:
return "loyal"
else:
return "defect"
else:
if self.lr > choice:
return "loyal"
else:
return "defect"
def mutate(self):
return Person(self.dr + random.uniform(0.01, -.01),
self.lr + random.uniform(0.01, -.01))
def clone(self):
return Person(self.dr, self.lr)
def __str__(self):
return "Person(%g, %g, %g)" % (self.dr, self.lr, self.wealth)
# create a 50/50 person
p = Person(.5, .5)
populations = [ p.mutate() for _ in range(100) ]
# repeatedly:
# mutate them into many populations
# have those populations play each other
# find the top 2 populations
for i in range(1000):
for p in populations:
one = p.clone()
other = p.clone()
lmone = "loyal"
lmother = "loyal"
for _ in range(1000):
lmone, lmother = one.play(lmother), other.play(lmone)
one.wealth += payoff[lmone, lmother]
other.wealth += payoff[lmother, lmone]
p.wealth = one.wealth + other.wealth
populations.sort(key=lambda x: x.wealth)
winner = populations[-1]
print(winner)
populations = [ winner.mutate() for _ in range(100) ]
# print out the ending defect responses and loyalty response probabilities