from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import csv import numpy doc = """ Task 2 (deltaM)- S8: Supergame 8 - Series of "Infinitely Repeated Prisoner's Dilemma". """ SUPERGROUP_NUM_ERR = 'Wrong number of players per supergroup' class Constants(BaseConstants): name_in_url = 'code_task2_M8' players_per_group = 2 players_per_supergroup = 4 assert players_per_supergroup % players_per_group == 0, \ SUPERGROUP_NUM_ERR num_rounds = 12 # num_rounds = 5 supergroup_threshold1 = 4 supergroup_threshold2 = 8 supergroup_threshold3 = 12 # cont_value_r1 = 100 cont_value_r1 = random.randint(1, 100) cont_value_r2 = random.randint(1, 100) cont_value_r3 = random.randint(1, 100) cont_value_r4 = random.randint(1, 100) cont_value_r5 = random.randint(1, 100) cont_value_r6 = random.randint(1, 100) cont_value_r7 = random.randint(1, 100) cont_value_r8 = random.randint(1, 100) cont_value_r9 = random.randint(1, 100) cont_value_r10 = random.randint(1, 100) cont_value_r11 = random.randint(1, 100) cont_value_r12 = int(97) round_numbers = [cont_value_r1, cont_value_r2, cont_value_r3, cont_value_r4, cont_value_r5, cont_value_r6, cont_value_r7, cont_value_r8, cont_value_r9, cont_value_r10, cont_value_r11, cont_value_r12] 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() last_round = models.IntegerField() num_last_round = 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.num_supergames = self.session.config['num_supergames'] p.continuation_prob = self.session.config['continuation_prob'] p.delta_limit = self.session.config['delta_limit'] 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'] p.delta_limit = self.session.config['delta_limit'] if Constants.cont_value_r1 > 60: self.num_last_round = 1 elif Constants.cont_value_r1 <= 60 and Constants.cont_value_r2 > 60: self.num_last_round = 2 elif Constants.cont_value_r1 <= 60 and Constants.cont_value_r2 <= 60 and Constants.cont_value_r3 > 60: self.num_last_round = 3 elif Constants.cont_value_r1 <= 60 and Constants.cont_value_r2 <= 60 and Constants.cont_value_r3 <= 60\ and Constants.cont_value_r4 > 60: self.num_last_round = 4 elif Constants.cont_value_r1 <= 60 and Constants.cont_value_r2 <= 60 and Constants.cont_value_r3 <= 60\ and Constants.cont_value_r4 <= 60 and Constants.cont_value_r5 > 60: self.num_last_round = 5 elif Constants.cont_value_r1 <= 60 and Constants.cont_value_r2 <= 60 and Constants.cont_value_r3 <= 60\ and Constants.cont_value_r4 <= 60 and Constants.cont_value_r5 <= 60 and Constants.cont_value_r6 > 60: self.num_last_round = 6 elif Constants.cont_value_r1 <= 60 and Constants.cont_value_r2 <= 60 and Constants.cont_value_r3 <= 60\ and Constants.cont_value_r4 <= 60 and Constants.cont_value_r5 <= 60 and Constants.cont_value_r6 <= 60\ and Constants.cont_value_r7 > 60: self.num_last_round = 7 elif Constants.cont_value_r1 <= 60 and Constants.cont_value_r2 <= 60 and Constants.cont_value_r3 <= 60 \ and Constants.cont_value_r4 <= 60 and Constants.cont_value_r5 <= 60 and Constants.cont_value_r6 <= 60 \ and Constants.cont_value_r7 <= 60 and Constants.cont_value_r8 > 60: self.num_last_round = 8 elif Constants.cont_value_r1 <= 60 and Constants.cont_value_r2 <= 60 and Constants.cont_value_r3 <= 60 \ and Constants.cont_value_r4 <= 60 and Constants.cont_value_r5 <= 60 and Constants.cont_value_r6 <= 60 \ and Constants.cont_value_r7 <= 60 and Constants.cont_value_r8 <= 60 and Constants.cont_value_r9 > 60: self.num_last_round = 9 elif Constants.cont_value_r1 <= 60 and Constants.cont_value_r2 <= 60 and Constants.cont_value_r3 <= 60 \ and Constants.cont_value_r4 <= 60 and Constants.cont_value_r5 <= 60 and Constants.cont_value_r6 <= 60 \ and Constants.cont_value_r7 <= 60 and Constants.cont_value_r8 <= 60 and Constants.cont_value_r9 <= 60 \ and Constants.cont_value_r10 > 60: self.num_last_round = 10 elif Constants.cont_value_r1 <= 60 and Constants.cont_value_r2 <= 60 and Constants.cont_value_r3 <= 60 \ and Constants.cont_value_r4 <= 60 and Constants.cont_value_r5 <= 60 and Constants.cont_value_r6 <= 60 \ and Constants.cont_value_r7 <= 60 and Constants.cont_value_r8 <= 60 and Constants.cont_value_r9 <= 60 \ and Constants.cont_value_r10 <= 60 and Constants.cont_value_r11 > 60: self.num_last_round = 11 else: self.num_last_round = 12 print(Constants.cont_value_r1, Constants.cont_value_r2, Constants.cont_value_r3, Constants.cont_value_r4, Constants.cont_value_r5, Constants.cont_value_r6, Constants.cont_value_r7, Constants.cont_value_r8, Constants.cont_value_r9, Constants.cont_value_r10, Constants.cont_value_r11, Constants.cont_value_r12, self.num_last_round) 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) # p.participant.vars['id_random'] = p.random_id for g in self.get_groups(): for p in g.get_players(): if p.participant.vars['id_random'] == 1 or p.participant.vars['id_random'] == 5 \ or p.participant.vars['id_random'] == 9 or p.participant.vars['id_random'] == 13: p.participant.vars['task2_supergroup'] = int(1) elif p.participant.vars['id_random'] == 2 or p.participant.vars['id_random'] == 6 \ or p.participant.vars['id_random'] == 10 or p.participant.vars['id_random'] == 14: p.participant.vars['task2_supergroup'] = int(2) elif p.participant.vars['id_random'] == 3 or p.participant.vars['id_random'] == 7 \ or p.participant.vars['id_random'] == 11 or p.participant.vars['id_random'] == 15: p.participant.vars['task2_supergroup'] = int(3) else: p.participant.vars['task2_supergroup'] = int(4) p.task2_supergroup = str(p.participant.vars['task2_supergroup']) if self.round_number == self.num_last_round: self.last_round = 1 else: self.last_round = 0 for g in self.get_groups(): for p in g.get_players(): p.num_last_round = self.num_last_round p.last_round = self.last_round p.participant.vars['last_round'] = p.num_last_round p.participant.vars['num_last_round'] = p.num_last_round p.cont_value_r1 = Constants.cont_value_r1 p.cont_value_r2 = Constants.cont_value_r2 p.cont_value_r3 = Constants.cont_value_r3 p.cont_value_r4 = Constants.cont_value_r4 p.cont_value_r5 = Constants.cont_value_r5 p.cont_value_r6 = Constants.cont_value_r6 p.cont_value_r7 = Constants.cont_value_r7 p.cont_value_r8 = Constants.cont_value_r8 p.cont_value_r9 = Constants.cont_value_r9 p.cont_value_r10 = Constants.cont_value_r10 p.cont_value_r11 = Constants.cont_value_r11 p.cont_value_r12 = Constants.cont_value_r12 if self.round_number == 1: subgroups_task1 = set([p.vars['task2_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['task2_supergroup'] == s] random.shuffle(A) sliced_supergroup = slice_list(A) new_matrix.extend(sliced_supergroup) print(new_matrix) self.set_group_matrix(new_matrix) # with open('strategic_draft0/matching_matrix_task2.csv', 'w') as csvfile: # writer = csv.writer(csvfile) # writer.writerows(new_matrix) else: self.group_like_round(self.round_number - 1) class Group(BaseGroup): def set_payoffs(self): for p in self.get_players(): p.set_payoff() class Player(BasePlayer): num_supergames = models.IntegerField() continuation_prob = models.CharField() delta_limit = models.IntegerField() conversion_rate = models.StringField() payoff_R = models.IntegerField() payoff_P = models.IntegerField() payoff_S = models.IntegerField() payoff_T = models.IntegerField() decision = models.IntegerField( choices=[[1, 'X'], [0, 'Y']], doc="""This player's decision""", widget=widgets.RadioSelect, ) # 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, #) round_number = models.IntegerField() task1_supergroup = models.LongStringField() task2_supergroup = models.LongStringField() random_id = models.IntegerField() random_max_rounds = models.IntegerField() cont_value_r1 = models.IntegerField() cont_value_r2 = models.IntegerField() cont_value_r3 = models.IntegerField() cont_value_r4 = models.IntegerField() cont_value_r5 = models.IntegerField() cont_value_r6 = models.IntegerField() cont_value_r7 = models.IntegerField() cont_value_r8 = models.IntegerField() cont_value_r9 = models.IntegerField() cont_value_r10 = models.IntegerField() cont_value_r11 = models.IntegerField() cont_value_r12 = models.IntegerField() last_round = models.IntegerField() num_last_round = models.IntegerField() randnums_m8 = models.LongStringField() decision_type = models.StringField() decision_partner = models.IntegerField() task2_decision_m8_r1 = models.IntegerField() task2_decision_m8_r2 = models.IntegerField() task2_decision_m8_r3 = models.IntegerField() task2_decision_m8_r4 = models.IntegerField() task2_decision_m8_r5 = models.IntegerField() task2_decision_m8_r6 = models.IntegerField() task2_decision_m8_r7 = models.IntegerField() task2_decision_m8_r8 = models.IntegerField() task2_decision_m8_r9 = models.IntegerField() task2_decision_m8_r10 = models.IntegerField() task2_decision_m8_r11 = models.IntegerField() task2_decision_m8_r12 = models.IntegerField() # task2_decision_type_m8_r1 = models.StringField() # task2_decision_type_m8_r2 = models.StringField() # task2_decision_type_m8_r3 = models.StringField() # task2_decision_type_m8_r4 = models.StringField() # task2_decision_type_m8_r5 = models.StringField() # task2_decision_type_m8_r6 = models.StringField() # decision_guess_task2_m8_r1 = models.LongStringField() payoff_int = models.IntegerField() cum_payoff = models.CurrencyField() cum_payoff_int = models.IntegerField() task2_payoff_m8_r1 = models.CurrencyField() task2_payoff_m8_r2 = models.CurrencyField() task2_payoff_m8_r3 = models.CurrencyField() task2_payoff_m8_r4 = models.CurrencyField() task2_payoff_m8_r5 = models.CurrencyField() task2_payoff_m8_r6 = models.CurrencyField() task2_payoff_m8_r7 = models.CurrencyField() task2_payoff_m8_r8 = models.CurrencyField() task2_payoff_m8_r9 = models.CurrencyField() task2_payoff_m8_r10 = models.CurrencyField() task2_payoff_m8_r11 = models.CurrencyField() task2_payoff_m8_r12 = models.CurrencyField() # total_coop_task2_m8_r1 = models.IntegerField() # total_coop_s1_task2_m8_r1 = models.IntegerField() # total_coop_s2_task2_m8_r1 = models.IntegerField() # total_coop_s3_task2_m8_r1 = models.IntegerField() # total_coop_s4_task2_m8_r1 = models.IntegerField() # coop_rate_s1_task2_m8_r1 = models.IntegerField() # coop_rate_s2_task2_m8_r1 = models.IntegerField() # coop_rate_s3_task2_m8_r1 = models.IntegerField() # coop_rate_s4_task2_m8_r1 = models.IntegerField() def other_player(self): return self.get_others_in_group()[0] 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.payoff = payoff_matrix[self.decision_type][self.other_player().decision_type] self.decision_partner = self.other_player().decision self.task1_supergroup = str(self.participant.vars['task1_supergroup']) self.random_id = int(self.participant.vars['id_random']) if self.decision == 1 and self.other_player().decision == 1: self.payoff_int = int(self.payoff_R) elif self.decision == 0 and self.other_player().decision == 0: self.payoff_int = int(self.payoff_P) elif self.decision == 1 and self.other_player().decision == 0: self.payoff_int = int(self.payoff_S) else: self.payoff_int = int(self.payoff_T) if self.round_number == 1: self.task2_payoff_m8_r1 = self.in_round(self.round_number).payoff if self.round_number == 2: self.task2_payoff_m8_r2 = self.in_round(self.round_number).payoff if self.round_number == 3: self.task2_payoff_m8_r3 = self.in_round(self.round_number).payoff if self.round_number == 4: self.task2_payoff_m8_r4 = self.in_round(self.round_number).payoff if self.round_number == 5: self.task2_payoff_m8_r5 = self.in_round(self.round_number).payoff if self.round_number == 6: self.task2_payoff_m8_r6 = self.in_round(self.round_number).payoff if self.round_number == 7: self.task2_payoff_m8_r7 = self.in_round(self.round_number).payoff if self.round_number == 8: self.task2_payoff_m8_r8 = self.in_round(self.round_number).payoff if self.round_number == 9: self.task2_payoff_m8_r9 = self.in_round(self.round_number).payoff if self.round_number == 10: self.task2_payoff_m8_r10 = self.in_round(self.round_number).payoff if self.round_number == 11: self.task2_payoff_m8_r11 = self.in_round(self.round_number).payoff if self.round_number == 12: self.task2_payoff_m8_r12 = self.in_round(self.round_number).payoff def role(self): if self.participant.vars['task2_supergroup'] == int(1): return '1' elif self.participant.vars['task2_supergroup'] == int(2): return '2' elif self.participant.vars['task2_supergroup'] == int(3): return '3' else: return '4'