-
Notifications
You must be signed in to change notification settings - Fork 198
/
models.py
66 lines (51 loc) · 1.58 KB
/
models.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
from otree.api import (
models,
widgets,
BaseConstants,
BaseSubsession,
BaseGroup,
BasePlayer,
Currency as c,
currency_range,
)
doc = """
This is a one-shot "Prisoner's Dilemma". Two players are asked separately
whether they want to cooperate or defect. Their choices directly determine the
payoffs.
"""
class Constants(BaseConstants):
name_in_url = 'prisoner'
players_per_group = 2
num_rounds = 1
instructions_template = 'prisoner/instructions.html'
# payoff if 1 player defects and the other cooperates""",
betray_payoff = c(300)
betrayed_payoff = c(0)
# payoff if both players cooperate or both defect
both_cooperate_payoff = c(200)
both_defect_payoff = c(100)
class Subsession(BaseSubsession):
pass
class Group(BaseGroup):
def set_payoffs(self):
for p in self.get_players():
p.set_payoff()
class Player(BasePlayer):
decision = models.StringField(
choices=[['Cooperate', 'Cooperate'], ['Defect', 'Defect']],
doc="""This player's decision""",
widget=widgets.RadioSelect,
)
def other_player(self):
return self.get_others_in_group()[0]
def set_payoff(self):
payoff_matrix = dict(
Cooperate=dict(
Cooperate=Constants.both_cooperate_payoff,
Defect=Constants.betrayed_payoff,
),
Defect=dict(
Cooperate=Constants.betray_payoff, Defect=Constants.both_defect_payoff
),
)
self.payoff = payoff_matrix[self.decision][self.other_player().decision]