from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import csv import numpy doc = """ Part 2 (d1) Series of "Infinitely Repeated Prisoner's Dilemma" - Supergame 1. """ SUPERGROUP_NUM_ERR = 'Wrong number of players per supergroup' class Constants(BaseConstants): name_in_url = 'part2_d1_CI' players_per_group = 2 players_per_supergroup = 4 assert players_per_supergroup % players_per_group == 0, \ SUPERGROUP_NUM_ERR num_rounds = 20 # num_rounds = 5 supergroup_threshold1 = 4 supergroup_threshold2 = 8 supergroup_threshold3 = 12 continuation_prob = '75%' delta_limit = 75 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 = random.randint(1, 100) cont_value_r13 = random.randint(1, 100) cont_value_r14 = random.randint(1, 100) cont_value_r15 = random.randint(1, 100) cont_value_r16 = random.randint(1, 100) cont_value_r17 = random.randint(1, 100) cont_value_r18 = random.randint(1, 100) cont_value_r19 = random.randint(1, 100) cont_value_r20 = int(90) # payoff if 1 player defects and the other cooperates""", betray_payoff = c(100) betrayed_payoff = c(10) # payoff if both players cooperate or both defect both_cooperate_payoff = c(73) both_defect_payoff = c(43) 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): last_round = models.IntegerField() num_last_round = models.IntegerField() def creating_session(self): if Constants.cont_value_r1 > 75: self.num_last_round = 1 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 > 75: self.num_last_round = 2 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 > 75: self.num_last_round = 3 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75\ and Constants.cont_value_r4 > 75: self.num_last_round = 4 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75\ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 > 75: self.num_last_round = 5 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75\ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 <= 75 and Constants.cont_value_r6 > 75: self.num_last_round = 6 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75\ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 <= 75 and Constants.cont_value_r6 <= 75\ and Constants.cont_value_r7 > 75: self.num_last_round = 7 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75 \ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 <= 75 and Constants.cont_value_r6 <= 75 \ and Constants.cont_value_r7 <= 75 and Constants.cont_value_r8 > 75: self.num_last_round = 8 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75 \ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 <= 75 and Constants.cont_value_r6 <= 75 \ and Constants.cont_value_r7 <= 75 and Constants.cont_value_r8 <= 75 and Constants.cont_value_r9 > 75: self.num_last_round = 9 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75 \ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 <= 75 and Constants.cont_value_r6 <= 75 \ and Constants.cont_value_r7 <= 75 and Constants.cont_value_r8 <= 75 and Constants.cont_value_r9 <= 75 \ and Constants.cont_value_r10 > 75: self.num_last_round = 10 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75 \ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 <= 75 and Constants.cont_value_r6 <= 75 \ and Constants.cont_value_r7 <= 75 and Constants.cont_value_r8 <= 75 and Constants.cont_value_r9 <= 75 \ and Constants.cont_value_r10 <= 75 and Constants.cont_value_r11 > 75: self.num_last_round = 11 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75 \ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 <= 75 and Constants.cont_value_r6 <= 75 \ and Constants.cont_value_r7 <= 75 and Constants.cont_value_r8 <= 75 and Constants.cont_value_r9 <= 75 \ and Constants.cont_value_r10 <= 75 and Constants.cont_value_r11 <= 75 and Constants.cont_value_r12 > 75: self.num_last_round = 12 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75 \ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 <= 75 and Constants.cont_value_r6 <= 75 \ and Constants.cont_value_r7 <= 75 and Constants.cont_value_r8 <= 75 and Constants.cont_value_r9 <= 75 \ and Constants.cont_value_r10 <= 75 and Constants.cont_value_r11 <= 75 \ and Constants.cont_value_r12 <= 75 and Constants.cont_value_r13 > 75: self.num_last_round = 13 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75 \ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 <= 75 and Constants.cont_value_r6 <= 75 \ and Constants.cont_value_r7 <= 75 and Constants.cont_value_r8 <= 75 and Constants.cont_value_r9 <= 75 \ and Constants.cont_value_r10 <= 75 and Constants.cont_value_r11 <= 75 \ and Constants.cont_value_r12 <= 75 and Constants.cont_value_r13 <= 75 \ and Constants.cont_value_r14 > 75: self.num_last_round = 14 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75 \ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 <= 75 and Constants.cont_value_r6 <= 75 \ and Constants.cont_value_r7 <= 75 and Constants.cont_value_r8 <= 75 and Constants.cont_value_r9 <= 75 \ and Constants.cont_value_r10 <= 75 and Constants.cont_value_r11 <= 75 \ and Constants.cont_value_r12 <= 75 and Constants.cont_value_r13 <= 75 \ and Constants.cont_value_r14 <= 75 and Constants.cont_value_r15 > 75: self.num_last_round = 15 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75 \ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 <= 75 and Constants.cont_value_r6 <= 75 \ and Constants.cont_value_r7 <= 75 and Constants.cont_value_r8 <= 75 and Constants.cont_value_r9 <= 75 \ and Constants.cont_value_r10 <= 75 and Constants.cont_value_r11 <= 75 \ and Constants.cont_value_r12 <= 75 and Constants.cont_value_r13 <= 75 \ and Constants.cont_value_r14 <= 75 and Constants.cont_value_r15 <= 75 \ and Constants.cont_value_r16 > 75: self.num_last_round = 16 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75 \ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 <= 75 and Constants.cont_value_r6 <= 75 \ and Constants.cont_value_r7 <= 75 and Constants.cont_value_r8 <= 75 and Constants.cont_value_r9 <= 75 \ and Constants.cont_value_r10 <= 75 and Constants.cont_value_r11 <= 75 \ and Constants.cont_value_r12 <= 75 and Constants.cont_value_r13 <= 75 \ and Constants.cont_value_r14 <= 75 and Constants.cont_value_r15 <= 75 \ and Constants.cont_value_r16 <= 75 and Constants.cont_value_r17 > 75: self.num_last_round = 17 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75 \ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 <= 75 and Constants.cont_value_r6 <= 75 \ and Constants.cont_value_r7 <= 75 and Constants.cont_value_r8 <= 75 and Constants.cont_value_r9 <= 75 \ and Constants.cont_value_r10 <= 75 and Constants.cont_value_r11 <= 75 \ and Constants.cont_value_r12 <= 75 and Constants.cont_value_r13 <= 75 \ and Constants.cont_value_r14 <= 75 and Constants.cont_value_r15 <= 75 \ and Constants.cont_value_r16 <= 75 and Constants.cont_value_r17 <= 75 \ and Constants.cont_value_r18 > 75: self.num_last_round = 18 elif Constants.cont_value_r1 <= 75 and Constants.cont_value_r2 <= 75 and Constants.cont_value_r3 <= 75 \ and Constants.cont_value_r4 <= 75 and Constants.cont_value_r5 <= 75 and Constants.cont_value_r6 <= 75 \ and Constants.cont_value_r7 <= 75 and Constants.cont_value_r8 <= 75 and Constants.cont_value_r9 <= 75 \ and Constants.cont_value_r10 <= 75 and Constants.cont_value_r11 <= 75 \ and Constants.cont_value_r12 <= 75 and Constants.cont_value_r13 <= 75 \ and Constants.cont_value_r14 <= 75 and Constants.cont_value_r15 <= 75 \ and Constants.cont_value_r16 <= 75 and Constants.cont_value_r17 <= 75 \ and Constants.cont_value_r18 <= 75 and Constants.cont_value_r19 > 75: self.num_last_round = 19 else: self.num_last_round = 20 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, Constants.cont_value_r13, Constants.cont_value_r14, Constants.cont_value_r15, Constants.cont_value_r16, Constants.cont_value_r17, Constants.cont_value_r18, Constants.cont_value_r19, Constants.cont_value_r20, 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['supergroup_task2'] = 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['supergroup_task2'] = 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['supergroup_task2'] = int(3) else: p.participant.vars['supergroup_task2'] = int(4) p.supergroup_task2 = str(p.participant.vars['supergroup_task2']) 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 p.cont_value_r13 = Constants.cont_value_r13 p.cont_value_r14 = Constants.cont_value_r14 p.cont_value_r15 = Constants.cont_value_r15 p.cont_value_r16 = Constants.cont_value_r16 p.cont_value_r17 = Constants.cont_value_r17 p.cont_value_r18 = Constants.cont_value_r18 p.cont_value_r19 = Constants.cont_value_r19 p.cont_value_r20 = Constants.cont_value_r20 if self.round_number == 1: subgroups_task1 = set([p.vars['supergroup_task2'] 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['supergroup_task2'] == 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): 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() supergroup_task1 = models.LongStringField() supergroup_task2 = 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() cont_value_r13 = models.IntegerField() cont_value_r14 = models.IntegerField() cont_value_r15 = models.IntegerField() cont_value_r16 = models.IntegerField() cont_value_r17 = models.IntegerField() cont_value_r18 = models.IntegerField() cont_value_r19 = models.IntegerField() cont_value_r20 = models.IntegerField() last_round = models.IntegerField() num_last_round = models.IntegerField() decision_type = models.StringField() decision_partner = models.IntegerField() decision_task2_m1_r1 = models.IntegerField() decision_task2_m1_r2 = models.IntegerField() decision_task2_m1_r3 = models.IntegerField() decision_task2_m1_r4 = models.IntegerField() decision_task2_m1_r5 = models.IntegerField() decision_task2_m1_r6 = models.IntegerField() decision_task2_m1_r7 = models.IntegerField() decision_task2_m1_r8 = models.IntegerField() decision_task2_m1_r9 = models.IntegerField() decision_task2_m1_r10 = models.IntegerField() decision_task2_m1_r11 = models.IntegerField() decision_task2_m1_r12 = models.IntegerField() decision_task2_m1_r13 = models.IntegerField() decision_task2_m1_r14 = models.IntegerField() decision_task2_m1_r15 = models.IntegerField() decision_task2_m1_r16 = models.IntegerField() decision_task2_m1_r17 = models.IntegerField() decision_task2_m1_r18 = models.IntegerField() decision_task2_m1_r19 = models.IntegerField() decision_task2_m1_r20 = models.IntegerField() # decision_task2_type_m1_r1 = models.StringField() # decision_task2_type_m1_r2 = models.StringField() # decision_task2_type_m1_r3 = models.StringField() # decision_task2_type_m1_r4 = models.StringField() # decision_task2_type_m1_r5 = models.StringField() # decision_task2_type_m1_r6 = models.StringField() decision_guess_task2_m1_r1 = models.LongStringField() payoff_int = models.IntegerField() cum_payoff = models.CurrencyField() cum_payoff_int = models.IntegerField() payoff_task2_m1_r1 = models.CurrencyField() payoff_task2_m1_r2 = models.CurrencyField() payoff_task2_m1_r3 = models.CurrencyField() payoff_task2_m1_r4 = models.CurrencyField() payoff_task2_m1_r5 = models.CurrencyField() payoff_task2_m1_r6 = models.CurrencyField() payoff_task2_m1_r7 = models.CurrencyField() payoff_task2_m1_r8 = models.CurrencyField() payoff_task2_m1_r9 = models.CurrencyField() payoff_task2_m1_r10 = models.CurrencyField() payoff_task2_m1_r11 = models.CurrencyField() payoff_task2_m1_r12 = models.CurrencyField() payoff_task2_m1_r13 = models.CurrencyField() payoff_task2_m1_r14 = models.CurrencyField() payoff_task2_m1_r15 = models.CurrencyField() payoff_task2_m1_r16 = models.CurrencyField() payoff_task2_m1_r17 = models.CurrencyField() payoff_task2_m1_r18 = models.CurrencyField() payoff_task2_m1_r19 = models.CurrencyField() payoff_task2_m1_r20 = models.CurrencyField() cooprate_task2_m1_r1 = models.IntegerField() total_coop_task2_m1_r1 = models.IntegerField() total_coop_s1_task2_m1_r1 = models.IntegerField() total_coop_s2_task2_m1_r1 = models.IntegerField() total_coop_s3_task2_m1_r1 = models.IntegerField() total_coop_s4_task2_m1_r1 = models.IntegerField() # coop_rate_s1_task2_m1_r1 = models.IntegerField() # coop_rate_s2_task2_m1_r1 = models.IntegerField() # coop_rate_s3_task2_m1_r1 = models.IntegerField() # coop_rate_s4_task2_m1_r1 = models.IntegerField() def other_player(self): return self.get_others_in_group()[0] def set_payoff(self): payoff_matrix = dict( X=dict( X=Constants.both_cooperate_payoff, Y=Constants.betrayed_payoff, ), Y=dict( X=Constants.betray_payoff, Y=Constants.both_defect_payoff ), ) self.payoff = payoff_matrix[self.decision_type][self.other_player().decision_type] self.decision_partner = self.other_player().decision self.supergroup_task1 = str(self.participant.vars['supergroup_task1']) self.random_id = int(self.participant.vars['id_random']) if self.decision == 1 and self.other_player().decision == 1: self.payoff_int = int(Constants.both_cooperate_payoff) elif self.decision == 0 and self.other_player().decision == 0: self.payoff_int = int(Constants.both_defect_payoff) elif self.decision == 1 and self.other_player().decision == 0: self.payoff_int = int(Constants.betrayed_payoff) else: self.payoff_int = int(Constants.betray_payoff) if self.round_number == 1: self.payoff_task2_m1_r1 = self.in_round(self.round_number).payoff if self.round_number == 2: self.payoff_task2_m1_r2 = self.in_round(self.round_number).payoff if self.round_number == 3: self.payoff_task2_m1_r3 = self.in_round(self.round_number).payoff if self.round_number == 4: self.payoff_task2_m1_r4 = self.in_round(self.round_number).payoff if self.round_number == 5: self.payoff_task2_m1_r5 = self.in_round(self.round_number).payoff if self.round_number == 6: self.payoff_task2_m1_r6 = self.in_round(self.round_number).payoff if self.round_number == 7: self.payoff_task2_m1_r7 = self.in_round(self.round_number).payoff if self.round_number == 8: self.payoff_task2_m1_r8 = self.in_round(self.round_number).payoff if self.round_number == 9: self.payoff_task2_m1_r9 = self.in_round(self.round_number).payoff if self.round_number == 10: self.payoff_task2_m1_r10 = self.in_round(self.round_number).payoff if self.round_number == 11: self.payoff_task2_m1_r11 = self.in_round(self.round_number).payoff if self.round_number == 12: self.payoff_task2_m1_r12 = self.in_round(self.round_number).payoff if self.round_number == 13: self.payoff_task2_m1_r13 = self.in_round(self.round_number).payoff if self.round_number == 14: self.payoff_task2_m1_r14 = self.in_round(self.round_number).payoff if self.round_number == 15: self.payoff_task2_m1_r15 = self.in_round(self.round_number).payoff if self.round_number == 16: self.payoff_task2_m1_r16 = self.in_round(self.round_number).payoff if self.round_number == 17: self.payoff_task2_m1_r17 = self.in_round(self.round_number).payoff if self.round_number == 18: self.payoff_task2_m1_r18 = self.in_round(self.round_number).payoff if self.round_number == 19: self.payoff_task2_m1_r19 = self.in_round(self.round_number).payoff if self.round_number == 20: self.payoff_task2_m1_r20 = self.in_round(self.round_number).payoff def role(self): if self.participant.vars['supergroup_task2'] == int(1): return '1' elif self.participant.vars['supergroup_task2'] == int(2): return '2' elif self.participant.vars['supergroup_task2'] == int(3): return '3' else: return '4'