-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGame.py
105 lines (85 loc) · 3.14 KB
/
Game.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
import abc
import random
import numpy as np
from enum import Enum
"""
The range of strategies which a player can perform in a game
"""
class Strategy(Enum):
cooperate = 0
defect = 1
"""
An individual agent involved in a game
"""
class Player:
def __init__(self, strategy):
self.strategy = strategy
self.cell = None
self.payoff = 0
class PrisonersDilemmaPlayer(Player):
def __init__(self, strategy):
super().__init__(strategy)
class AsymmetricPrisonersDilemmaPlayer(Player):
def __init__(self, strategy, p):
super().__init__(strategy)
self.physio = np.random.choice(np.arange(0, 2), p=[p, 1 - p])
self.safety = self.physio * \
np.random.choice(np.arange(0, 2), p=[p, 1 - p])
self.love = self.safety * \
np.random.choice(np.arange(0, 2), p=[p, 1 - p])
self.esteem = self.love * \
np.random.choice(np.arange(0, 2), p=[p, 1 - p])
self.fulfill = self.esteem * \
np.random.choice(np.arange(0, 2), p=[p, 1 - p])
"""
A set of rules which defines the interaction between players and determines
their payoffs
"""
class Game(abc.ABC):
@abc.abstractmethod
def play(self, p1: Player, p2: Player):
pass
class PrisonersDilemma(Game):
def __init__(self, T, R, P, S):
"""initialise the prisoners dilemma with:
T: temptacion
R: reward
P: punishment
S: sucker's payoff
"""
self.T = T
self.R = R
self.P = P
self.S = S
def play(self, p1: Player, p2: Player):
if p1.strategy == Strategy.cooperate:
if p2.strategy == Strategy.cooperate:
return (self.R, self.R)
elif p2.strategy == Strategy.defect:
return (self.S, self.T)
elif p1.strategy == Strategy.defect:
if p2.strategy == Strategy.cooperate:
return (self.T, self.S)
elif p2.strategy == Strategy.defect:
return (self.P, self.P)
else:
print("Player strategies not well defined!")
quit()
class AsymmetricPrisonersDilemma(PrisonersDilemma):
def play(self, p1: Player, p2: Player):
if p1.strategy == Strategy.cooperate:
if p2.strategy == Strategy.cooperate:
return (self.R*(1+(p1.physio + p1.safety + p1.love + p1.esteem + p1.fulfill)/5),
self.R*(1+(p2.physio + p2.safety + p2.love + p2.esteem + p2.fulfill)/5))
elif p2.strategy == Strategy.defect:
return (self.S*(1+(p1.love + p1.esteem)/2),
self.T*(2/(1+(p2.physio + p2.safety + p2.love + p2.esteem + p2.fulfill)/5)))
elif p1.strategy == Strategy.defect:
if p2.strategy == Strategy.cooperate:
return (self.T*(2/(1+(p1.physio + p1.safety + p1.love + p1.esteem + p1.fulfill)/5)),
self.S*(1+(p2.love + p2.esteem)/2))
elif p2.strategy == Strategy.defect:
return (self.P * (1 + p1.fulfill), self.P * (1 + p2.fulfill))
else:
print("Player strategies not well defined!")
exit()