from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import pandas as pd author = 'Cecilia Chen' doc = """ Choose whether to work or stay at home for a number of different scenarios """ class Constants(BaseConstants): name_in_url = 'decision_task' players_per_group = None num_rounds = 13 # parameters in the game num_of_other_members = [i for i in range(10)] work_payoff_infected_demo = [-82 + 3 * i for i in range(10)] work_payoff_not_infected_demo = [43 + 3 * i for i in range(10)] home_payoff_demo = [10 + 3 * i for i in range(10)] prob_demo = [18 + 2 * i for i in range(10)] # import the csv file as a dataframe df = pd.read_csv('_static/game_parameters.csv') # extract the relevant parameter information from the csv file using pandas payoff_infected = [] payoff_not_infected = [] payoff_home = [] probability = [] social_optimal = 1 import random for i in range(num_rounds): payoff_infected.append(df[df['round number'] == i + 1]['payoff infected'].values.tolist()) payoff_not_infected.append(df[df['round number'] == i + 1]['payoff not infected'].values.tolist()) payoff_home.append(df[df['round number'] == i + 1]['payoff home'].values.tolist()) probability.append(df[df['round number'] == i + 1]['prob'].values.tolist()) class Subsession(BaseSubsession): def creating_session(self): # print(Constants.payoff_not_infected) for p in self.get_players(): if self.round_number == 1: randomizer = [i for i in range(Constants.num_rounds)] random.shuffle(randomizer) p.participant.vars['randomizer'] = randomizer p.round_to_play = p.participant.vars['randomizer'][p.round_number - 1] for p in self.get_players(): p.suggestion = 1 p.suggestiondoc = 1 p.suggestiongov = 1 p.suggestioncdc = 0 if p.round_to_play == 0 : p.social_optimal=52.4 p.personal_optimal=49.4 elif p.round_to_play == 1: p.social_optimal=83.6 p.personal_optimal = 57.5 elif p.round_to_play == 2: p.social_optimal = 82 p.personal_optimal = 42.7 elif p.round_to_play == 3: p.social_optimal= 54 p.personal_optimal = 26 elif p.round_to_play == 4: p.social_optimal= 22 p.personal_optimal = 8.2 elif p.round_to_play == 5: p.social_optimal= 57.7 p.personal_optimal = 100 elif p.round_to_play == 6: p.social_optimal= 35.2 p.personal_optimal = 73.9 elif p.round_to_play == 7: p.social_optimal= 21.6 p.personal_optimal = 44.4 elif p.round_to_play == 8: p.social_optimal= 9.6 p.personal_optimal = 19.8 elif p.round_to_play == 9: p.social_optimal= 83 p.personal_optimal = 0 elif p.round_to_play == 10: p.social_optimal= 81 p.personal_optimal = 0 elif p.round_to_play == 11: p.social_optimal= 52 p.personal_optimal = 0 elif p.round_to_play == 12: p.social_optimal= 21.6 p.personal_optimal = 0 if p.social_optimal <= 25: p.warningcolor = 1 if p.social_optimal >= 25: if p.social_optimal < 50: p.warningcolor=2 if p.social_optimal >=50: if p.social_optimal < 75: p.warningcolor=3 if p.social_optimal > 75: p.warningcolor=4 for p in self.get_players(): p.probsug = random.random() if p.probsug>p.social_optimal/100: p.suggestionsoc=0 elif p.probsug<=p.social_optimal/100: p.suggestionsoc=1 if p.probsug>p.personal_optimal/100: p.suggestionper=0 elif p.probsug<=p.personal_optimal/100: p.suggestionper=1 class Group(BaseGroup): pass class Player(BasePlayer): pid= models.StringField( label='please enter your Prolific ID here,which you can find it in your Prolific account. Please make sure it is correct, otherwise you will not get paid') work = models.BooleanField(widget=widgets.RadioSelect, choices=[[True, 'Work'], [False, 'Stay at Home']], label='I choose to ') round_to_play = models.IntegerField() suggestionsoc = models.IntegerField() warningcolor = models.IntegerField() suggestionper = models.IntegerField() social_optimal = models.FloatField() suggestion = models.IntegerField() suggestiondoc = models.IntegerField() suggestiongov = models.IntegerField() suggestioncdc = models.IntegerField() personal_optimal = models.FloatField() probsug = models.FloatField() q1 = models.BooleanField(widget=widgets.RadioSelect, choices=[[True, 'True'], [False, 'False']], label='True or false: suppose that you choose to work, your probability of infection ' 'depends the decisions of the other members in your group.') q2 = models.BooleanField(widget=widgets.RadioSelect, choices=[[True, 'True'], [False, 'False']], label='True of false: if you choose to stay at home, your payoff is determined by the ' 'work payoff table.') q3 = models.IntegerField(choices=[[1, 52], [2, -73], [3, 58], [4, 100], [5, 46]], label='Supposed that 3 others choose to work. What is the payoff in points if you choose ' 'to work and are NOT infected? ', widget=widgets.RadioSelect) q4 = models.IntegerField( choices=[[1, -82], [2, 52], [3, -73], [4, -64], [5, -100]], label='Suppose that 3 others choose to work, what is the payoff in points if you choose ' 'to work and are infected? ', widget=widgets.RadioSelect ) q5 = models.IntegerField( choices=[[1, '1%'], [2, '18%'], [3, '20%'], [4, '24%'], [5, '37%']], label='If 3 others choose to work, what is your probability of getting infected if you choose to work? ', widget=widgets.RadioSelect ) q6 = models.IntegerField( choices=[[1, 0], [2, 46], [3, 19], [4, 28], [5, 37]], label='If 3 others choose to work, what is the payoff if you choose to stay at home? ', widget=widgets.RadioSelect ) q1_correct = models.BooleanField() q2_correct = models.BooleanField() q3_correct = models.BooleanField() q4_correct = models.BooleanField() q5_correct = models.BooleanField() q6_correct = models.BooleanField() def q1_error_message(self, value): if value is not True: self.q1_correct = False return 'The correct answer is True. According to the work payoff table Probability of infection row, the ' \ 'more people in your group choose to work, the higher the probability of infection for everyone ' \ 'choose to work. ' def q2_error_message(self, value): if value is not False: self.q2_correct = False return 'The correct answer is False. In this study, once you choose to stay at home, your payoff ' \ 'is determined by the home payoff table. ' def q3_error_message(self, value): if value is not 1: self.q3_correct = False return 'The correct answer is 52. Since you choose to work and are Not infected, you need to look at ' \ 'the Payoff if NOT infected row in the Work payoff table. Given that there are 3 others in your ' \ 'group who choose to work, you will look at the cell in the Payoff if NOT infected row where 3 others ' \ 'choose ' \ 'to work. ' def q4_error_message(self, value): if value is not 3: self.q4_correct = False return 'The correct answer is -73. Since you choose to work and are infected, you need to look at ' \ 'the Payoff in infected row in the Work payoff table. Given that there are 3 others in your group ' \ 'who choose to work, you will look at the cell in the Payoff if infected row where 3 others choose ' \ 'to work. ' def q5_error_message(self, value): if value is not 4: self.q5_correct = False return 'The correct answer is 24%. Since you choose to work, you need to look at ' \ 'the Probability of infection row in the Work payoff table. Given that there are 3 others in your ' \ 'group who choose to work, you will look at the cell in the Probability of infection row where 3 others ' \ 'choose ' \ 'to work. ' def q6_error_message(self, value): if value is not 3: self.q6_correct = False return 'The correct answer is 19. Since you choose to stay at home, you need to look at ' \ 'the Payoff row in the Stay at home payoff table. Given that there are 3 others in your ' \ 'group who choose to work, you will look at the cell in the Payoff row where 3 others ' \ 'choose ' \ 'to work. ' def record_work_home_choice(self): work_choices = [] for r in self.in_all_rounds(): work_choices.append([r.round_to_play + 1, r.work]) # print('My choices are', work_choices) return work_choices