from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import csv import numpy 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. """ SUPERGROUP_NUM_ERR = 'Wrong number of players per supergroup' class Constants(BaseConstants): name_in_url = 'prisoner' players_per_group = 2 players_per_supergroup = 4 assert players_per_supergroup % players_per_group == 0, \ SUPERGROUP_NUM_ERR num_rounds = 15 supergroup_threshold1=4 instructions_template = 'strategic_draft0/instructions.html' # payoff if 1 player defects and the other cooperates""", betray_payoff = c(300) betrayed_payoff = c(0) # payoff if both players cooperate or both defect both_cooperate_payoff = c(200) both_defect_payoff = c(100) 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): def creating_session(self): 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] 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.random_id <= Constants.supergroup_threshold1: p.participant.vars['supergroup'] = int(1) else: p.participant.vars['supergroup'] = int(2) # p.participant.vars['random_id'] = p.random_id subgroups = set([p.vars['supergroup'] for p in self.session.get_participants()]) new_matrix = [] for s in subgroups: A = [p for p in self.get_players() if p.participant.vars['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.csv', 'w') as csvfile: writer = csv.writer(csvfile) writer.writerows(new_matrix) with open('strategic_draft0/matching_matrix2.csv', 'w+') as my_csv: csvWriter = csv.writer(my_csv, delimiter=',') csvWriter.writerows(new_matrix) #numpy.savetxt('strategic_draft0/matching_matrix3.csv', new_matrix, delimiter=",") for g in self.get_groups(): for p in g.get_players(): if p.decision == 1: p.decision_type = str("Cooperate") else: p.decision_type = str("Defect") #if self.round_number == 1: # assign a random number to participants [1,N] #for p in self.get_players(): #p.participant.vars['id_random'] = random.choice([1,2,3,4,5,6,7,8]) class Group(BaseGroup): def set_payoffs(self): for p in self.get_players(): p.set_payoff() class Player(BasePlayer): decision = models.IntegerField( choices=[[1 , 'Cooperate'], [0 , 'Defect']], doc="""This player's decision""", widget=widgets.RadioSelect, ) decision_guess= models.IntegerField(choices=[[1 , '0%-10%'], [2 , '11%-20%'], [3 , '21%-30%'], [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.RadioSelect, ) supergroup = models.LongStringField() random_id = models.IntegerField() decision_type = models.StringField() def other_player(self): return self.get_others_in_group()[0] 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_type][self.other_player().decision_type] self.supergroup = str(self.participant.vars['supergroup']) #.random_id = str(self.participant.vars['random_id'])