from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import csv import numpy doc = """ Task 1: The one-shot "Prisoner's Dilemma" (unconditional and conditional choices) play. """ SUPERGROUP_NUM_ERR = 'Wrong number of players per supergroup' class Constants(BaseConstants): name_in_url = 'code_task1_Dorder' players_per_group = 2 players_per_supergroup = 4 assert players_per_supergroup % players_per_group == 0, \ SUPERGROUP_NUM_ERR # To test: num_rounds = 1 num_rounds = 1 supergroup_threshold1 = 4 supergroup_threshold2 = 8 supergroup_threshold3 = 12 def slice_list(input): ppg = Constants.players_per_group output = [input[i:i+ppg] for i in range(0, len(input), ppg)] for o in output: assert len(o) == ppg, SUPERGROUP_NUM_ERR return output class Subsession(BaseSubsession): task1_order = models.IntegerField() task2_order = models.IntegerField() def creating_session(self): self.task1_order = self.session.config['task1_order'] self.task2_order = self.session.config['task2_order'] for p in self.get_players(): p.conversion_rate = self.session.config['points100_value_eur'] # Payoff from mutual cooperation (Reward Payoff) p.payoff_R = self.session.config['payoff_R'] # Payoff from mutual cooperation (Punishment Payoff) p.payoff_P = self.session.config['payoff_P'] # Payoff from unilateral cooperation (Sucker Payoff) p.payoff_S = self.session.config['payoff_S'] # Payoff from unilateral defection (Temptation Payoff) p.payoff_T = self.session.config['payoff_T'] if self.round_number == 1: assert len(self.get_players()) % \ Constants.players_per_supergroup == 0, \ SUPERGROUP_NUM_ERR # import itertools # random_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] # random.shuffle(random_list) # random_ids = itertools.cycle(random_list) # for p in self.get_players(): # p.random_id = next(random_ids) for g in self.get_groups(): for p in g.get_players(): if p.participant.vars['id_random'] <= Constants.supergroup_threshold1: p.participant.vars['task1_supergroup'] = int(1) elif p.participant.vars['id_random'] > Constants.supergroup_threshold1 and p.participant.vars['id_random'] <= Constants.supergroup_threshold2: p.participant.vars['task1_supergroup'] = int(2) elif p.participant.vars['id_random'] > Constants.supergroup_threshold2 and p.participant.vars['id_random'] <= Constants.supergroup_threshold3: p.participant.vars['task1_supergroup'] = int(3) else: p.participant.vars['task1_supergroup'] = int(4) p.random_id = p.participant.vars['id_random'] p.task1_supergroup = str(p.participant.vars['task1_supergroup']) subgroups_task1 = set([p.vars['task1_supergroup'] for p in self.session.get_participants()]) new_matrix = [] for s in subgroups_task1: A = [p for p in self.get_players() if p.participant.vars['task1_supergroup'] == s] random.shuffle(A) sliced_supergroup = slice_list(A) new_matrix.extend(sliced_supergroup) print(new_matrix) self.set_group_matrix(new_matrix) class Group(BaseGroup): def set_relevant_choices(self): for p in self.get_players(): p.relevant_choice() def set_decisive_choices(self): for p in self.get_players(): p.decisive_choice() def set_payoffs(self): for p in self.get_players(): p.set_payoff() class Player(BasePlayer): conversion_rate = models.StringField() payoff_R = models.IntegerField() payoff_P = models.IntegerField() payoff_S = models.IntegerField() payoff_T = models.IntegerField() task1_supergroup = models.LongStringField() random_id = models.IntegerField() decision_1 = models.IntegerField( choices=[[1, 'X'], [0, 'Y']], doc="""This player's decision""", widget=widgets.RadioSelect, ) decision_2 = models.IntegerField( choices=[[1, 'X'], [0, 'Y']], doc="""This player's decision""", widget=widgets.RadioSelect, ) decision_3 = models.IntegerField( choices=[[1, 'X'], [0, 'Y']], doc="""This player's decision""", widget=widgets.RadioSelect, ) task1_decision_uncond = models.IntegerField() task1_decision_type_uncond = models.StringField() task1_decision_condX = models.IntegerField() task1_decision_type_condX = models.StringField() task1_decision_condY = models.IntegerField() task1_decision_type_condY = models.StringField() task1_relevant_choice = models.IntegerField() task1_relevant_choice_type = models.StringField() task1_decisive_choice = models.IntegerField() task1_decisive_choice_type = models.StringField() task1_decisive_choice_partner = models.IntegerField() task1_decisive_choice_type_partner = models.StringField() temp_task1_decisive_choice = models.IntegerField() temp_task1_decisive_choice_partner = models.IntegerField() task1_group_id = models.IntegerField() task1_role_in_group = models.IntegerField() task1_payoff_int = models.IntegerField() task1_payoff = models.CurrencyField() # DECISION GUESS # decision_guess = models.IntegerField(choices=[[0, '0'], [1, '1'], [2, '2'], # [3, '3'], [4, '4']], # min=0, max=4, # widget=widgets.RadioSelectHorizontal, # label="" # ) # If group-size=6players switch to: # decision_guess = models.IntegerField(choices=[[1, '0%-10%'], [2, '11%-20%'], [3, '21%-30%'], # [4, '31%-40%'], [5, '41%-50%'], [5, '41%-50%'], # [6, '51%-60%'],[7, '61%-70%'], [8, '71%-80%'], [9, '81%-90%'], [10, '91%-100%']], # min=1, max=10, # widget=widgets.RadioSelectHorizontal, # ) # decision_guess_task1_r1 = models.LongStringField() # cooprate_task1_r1 = models.IntegerField() # total_coop_task1_r1 = models.IntegerField() # total_coop_s1 = models.IntegerField() # total_coop_s2 = models.IntegerField() # total_coop_s3 = models.IntegerField() # total_coop_s4 = models.IntegerField() def other_player(self): return self.get_others_in_group()[0] def relevant_choice(self): if self.id_in_group == 1: self.task1_relevant_choice = 1 self.task1_relevant_choice_type = "INCONDIZIONATA" self.temp_task1_decisive_choice = self.task1_decision_uncond self.temp_task1_decisive_choice_partner = 999 else: self.task1_relevant_choice = 0 self.task1_relevant_choice_type = "CONDIZIONATA" self.temp_task1_decisive_choice = 999 self.temp_task1_decisive_choice_partner = self.other_player().temp_task1_decisive_choice def decisive_choice(self): if self.id_in_group == 2 and self.temp_task1_decisive_choice_partner == 1: self.task1_decisive_choice = self.task1_decision_condX self.task1_decisive_choice_type = self.task1_decision_type_condX elif self.id_in_group == 2 and self.temp_task1_decisive_choice_partner == 0: self.task1_decisive_choice = self.task1_decision_condY self.task1_decisive_choice_type = self.task1_decision_type_condY else: self.task1_decisive_choice = self.task1_decision_uncond self.task1_decisive_choice_type = self.task1_decision_type_uncond def set_payoff(self): payoff_matrix = dict( X=dict( X=c(self.payoff_R), Y=c(self.payoff_S), ), Y=dict( X=c(self.payoff_T), Y=c(self.payoff_P) ), ) self.task1_payoff = payoff_matrix[self.task1_decisive_choice_type][self.other_player().task1_decisive_choice_type] self.task1_decisive_choice_partner = self.other_player().task1_decisive_choice self.task1_decisive_choice_type_partner = self.other_player().task1_decisive_choice_type self.task1_group_id = int(self.participant.vars['task1_group_id']) self.task1_supergroup = str(self.participant.vars['task1_supergroup']) self.task1_role_in_group = self.id_in_group self.participant.vars['task1_role_in_group'] = self.task1_role_in_group if self.task1_decisive_choice == 1 and self.other_player().task1_decisive_choice == 1: self.task1_payoff_int = int(self.payoff_R) elif self.task1_decisive_choice == 0 and self.other_player().task1_decisive_choice == 0: self.task1_payoff_int = int(self.payoff_P) elif self.task1_decisive_choice == 1 and self.other_player().task1_decisive_choice == 0: self.task1_payoff_int = int(self.payoff_S) else: self.task1_payoff_int = int(self.payoff_T) self.task1_payoff = self.task1_payoff def role(self): if self.participant.vars['task1_supergroup'] == int(1): return '1' elif self.participant.vars['task1_supergroup'] == int(2): return '2' elif self.participant.vars['task1_supergroup'] == int(3): return '3' else: return '4'