from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import csv import numpy doc = """ Part 2 (d10) Series of "Infinitely Repeated Prisoner's Dilemma" - Supergame 10. """ SUPERGROUP_NUM_ERR = 'Wrong number of players per supergroup' class Constants(BaseConstants): name_in_url = 'part2_d10_AI' players_per_group = 2 players_per_supergroup = 4 assert players_per_supergroup % players_per_group == 0, \ SUPERGROUP_NUM_ERR num_rounds = 6 # num_rounds = 5 supergroup_threshold1 = 4 supergroup_threshold2 = 8 supergroup_threshold3 = 12 continuation_prob = '35%' delta_limit = 35 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 = int(99) # 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 > 35: self.num_last_round = 1 elif Constants.cont_value_r1 <= 35 and Constants.cont_value_r2 > 35: self.num_last_round = 2 elif Constants.cont_value_r1 <= 35 and Constants.cont_value_r2 <= 35 and Constants.cont_value_r3 > 35: self.num_last_round = 3 elif Constants.cont_value_r1 <= 35 and Constants.cont_value_r2 <= 35 and Constants.cont_value_r3 <= 35 \ and Constants.cont_value_r4 > 35: self.num_last_round = 4 elif Constants.cont_value_r1 <= 35 and Constants.cont_value_r2 <= 35 and Constants.cont_value_r3 <= 35 \ and Constants.cont_value_r4 <= 35 and Constants.cont_value_r5 > 35: self.num_last_round = 5 else: self.num_last_round = 6 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, self.num_last_round) if self.round_number == 1: assert len(self.get_players()) % \ Constants.players_per_supergroup == 0, \ SUPERGROUP_NUM_ERR 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 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() last_round = models.IntegerField() num_last_round = models.IntegerField() decision_type = models.StringField() decision_partner = models.IntegerField() decision_task2_m10_r1 = models.IntegerField() decision_task2_m10_r2 = models.IntegerField() decision_task2_m10_r3 = models.IntegerField() decision_task2_m10_r4 = models.IntegerField() decision_task2_m10_r5 = models.IntegerField() decision_task2_m10_r6 = 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_m10_r1 = models.LongStringField() payoff_int = models.IntegerField() cum_payoff = models.CurrencyField() cum_payoff_int = models.IntegerField() payoff_task2_m10_r1 = models.CurrencyField() payoff_task2_m10_r2 = models.CurrencyField() payoff_task2_m10_r3 = models.CurrencyField() payoff_task2_m10_r4 = models.CurrencyField() payoff_task2_m10_r5 = models.CurrencyField() payoff_task2_m10_r6 = models.CurrencyField() # cooprate_task2_m10_r1 = models.IntegerField() total_coop_task2_m10_r1 = models.IntegerField() total_coop_s1_task2_m10_r1 = models.IntegerField() total_coop_s2_task2_m10_r1 = models.IntegerField() total_coop_s3_task2_m10_r1 = models.IntegerField() total_coop_s4_task2_m10_r1 = models.IntegerField() # coop_rate_s1_task2_m10_r1 = models.IntegerField() # coop_rate_s2_task2_m10_r1 = models.IntegerField() # coop_rate_s3_task2_m10_r1 = models.IntegerField() # coop_rate_s4_task2_m10_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_m10_r1 = self.in_round(self.round_number).payoff if self.round_number == 2: self.payoff_task2_m10_r2 = self.in_round(self.round_number).payoff if self.round_number == 3: self.payoff_task2_m10_r3 = self.in_round(self.round_number).payoff if self.round_number == 4: self.payoff_task2_m10_r4 = self.in_round(self.round_number).payoff if self.round_number == 5: self.payoff_task2_m10_r5 = self.in_round(self.round_number).payoff if self.round_number == 6: self.payoff_task2_m10_r6 = 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'