from itertools import count, takewhile 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 = 'my_gift' players_per_group = 2 num_rounds = 3 instructions_template = 'my_gift/instructions.html' # payoff if 1 player defects and the other cooperates""", betray_payoff = c(100) betrayed_payoff = c(0) - c(50) # payoff if both players cooperate or both defect both_cooperate_payoff = c(50) both_defect_payoff = c(0) class Subsession(BaseSubsession): def creating_session(self): # assign treatment import itertools treatment_assignment = itertools.cycle(['FF','UF','FP','FN']) all_paticipant_id = itertools.count(1) if self.round_number == 1: for p in self.get_players(): if 'treatment' in self.session.config: # for demo use p.treatment = self.session.config['treatment'] else: # live experiment p.treatment = next(treatment_assignment) print('set treatment to', p.treatment) for p in self.get_players(): p.participant_number = next(all_paticipant_id) print('assign', p.participant_number, 'as ID') for p in self.get_players(): p.participant.vars['treatment'] = p.treatment for p in self.get_players(): p.participant.vars['participant_id'] = p.participant_number print('participant variables are stored as', p.participant.vars) # make sure each player plays PD with each of other players once new_structure_1 = [[1,5],[9,13],[2,6],[10,14],[3,7],[11,15],[4,8],[12,16]] new_structure_2 = [[1,9],[5,13],[2,10],[6,14],[3,11],[7,15],[4,12],[8,16]] new_structure_3 = [[1,13],[5,9],[2,14],[6,10],[3,15],[7,11],[4,16],[8,12]] if self.round_number == 1: self.set_group_matrix(new_structure_1) else : if self.round_number == 2: self.set_group_matrix(new_structure_2) else : self.set_group_matrix(new_structure_3) self.session.vars['endowment_for_UF']=[50,100,150,200] class Group(BaseGroup): def set_payoffs(self): for p in self.get_players(): p.set_payoff() def set_endowments(self): for p in self.get_players(): p.set_endowment() p.set_opponent_endowment() class Player(BasePlayer): decision = models.StringField( choices=[['P', 'P'], ['Q', 'Q']], doc="""This player's decision""", widget=widgets.RadioSelect, ) treatment = models.StringField() participant_number = models.IntegerField() endowment = models.CurrencyField() opponent_endowment = models.CurrencyField() total_payoff = models.CurrencyField() total_wealth= models.CurrencyField() def other_player(self): return self.get_others_in_group()[0] def set_endowment(self): self.endowment = c(self.participant.vars['endowment_in_stage2']) def set_opponent_endowment(self): self.opponent_endowment = c(self.other_player().participant.vars['endowment_in_stage2']) 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] self.total_payoff = sum([p.payoff for p in self.in_all_rounds()]) self.total_wealth = self.total_payoff + self.endowment