import json import random from otree.api import * from otree.api import BasePlayer from otree.api import Page, models doc = """ Your app description This experiment studies the incomplete preference and the relationship between incomplete preference and status quo bias """ class Constants(BaseConstants): name_in_url = 'incomplete_preference' players_per_group = None tasks = ['egx_1', 'egy_1', 'egx_2', 'egy_2'] num_rounds = len(tasks) # these are the endowment parameters initial_endowment_m = 10 initial_endowment_c = 3 extra_money_1 = 3 extra_chocolate_1 = 2 extra_money_2 = 5 extra_chocolate_2 = 6 initial_endowment_m_example = 5 initial_endowment_c_example = 1 extra_money_example = 5 extra_chocolate_example = 3 list_m = list(range(1, 11)) list_c = list(range(1, 11)) bundle_a_m = initial_endowment_m + extra_money_1 bundle_a_c = initial_endowment_c bundle_b_m = initial_endowment_m bundle_b_c = initial_endowment_c + extra_chocolate_1 bundle_c_m = 0 bundle_c_c = initial_endowment_c bundle_d_m = 0 bundle_d_c = initial_endowment_c + extra_chocolate_1 bundle_e_m = initial_endowment_m bundle_e_c = 0 bundle_f_m = initial_endowment_m + extra_money_1 bundle_f_c = 0 num_egquestion = 2 num_exchangequestion = num_egquestion/2 num_referencequestion = 4 num_pairwisequestion = num_exchangequestion + num_referencequestion num_totalquestion = num_egquestion + num_pairwisequestion class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): @staticmethod def vars_for_template(self): money_rows = {f'money_row{i}': Constants.list_m[i] for i in range(1, len(Constants.list_m))} # retrieve values from constants and store them in a dictionary return{ 'initial_endowment_c': Constants.initial_endowment_c, 'initial_endowment_m': Constants.initial_endowment_m, 'extra_chocolate_1': Constants.extra_chocolate_1, 'extra_money_1': Constants.extra_money_1, 'extra_chocolate_2': Constants.extra_chocolate_2, 'extra_money_2': Constants.extra_money_2, **money_rows } #models for example and test ExampleMPL = models.StringField(blank=True) test_chocolate_1 = models.IntegerField(blank=True, label='how many pieces of chocolate will X get?') test_money_1 = models.CurrencyField(blank=True, label='how much money will X get?') test_chocolate_2 = models.IntegerField(blank=True, label='how many pieces of chocolate will X get?') test_money_2 = models.CurrencyField(blank=True, label='how much money will X get?') # models needed for the 7 questions egy_1 = models.StringField(blank=True) egx_1 = models.StringField(blank=True) egy_2 = models.StringField(blank=True) egx_2 = models.StringField(blank=True) exchange_ab = models.StringField(blank=True) exchange_abc = models.StringField(blank=True) exchange_abd = models.StringField(blank=True) exchange_abe = models.StringField(blank=True) exchange_abf = models.StringField(blank=True) # models for math question math_q1 = models.IntegerField(blank=True) math_q2 = models.IntegerField(blank=True) math_q3 = models.IntegerField(blank=True) math_q4 = models.IntegerField(blank=True) math_q5 = models.IntegerField(blank=True) random_question = models.IntegerField() random_row_egx_1 = models.IntegerField() random_row_egy_1 = models.IntegerField() randomrow_example = models.IntegerField() example_choice = models.StringField(blank=True) choice = models.StringField() row = models.IntegerField() payment_m = models.IntegerField() payment_c = models.IntegerField() treatment = models.StringField() task_roundscomp = models.StringField() def initialize(self): # Assign a random number to each player self.random_question = random.randint(1, 7) # Adjust the range as needed self.random_row_egx_1 = random.randint(1, len(Constants.list_c)) self.random_row_egy_1 = random.randint(1, len(Constants.list_m)) # function def creating_session(subsession: Subsession): if subsession.round_number == 1: for p in subsession.get_players(): round_numbers = list(range(1, Constants.num_rounds + 1)) random.shuffle(round_numbers) task_rounds = dict(zip(Constants.tasks, round_numbers)) p.task_roundscomp = json.dumps(task_rounds) p.participant.vars['task_roundscomp'] = p.task_roundscomp treatment = ['a', 'b'] p.treatment = random.choice(treatment) p.participant.vars['treatment'] = p.treatment p.treatment = p.participant.vars['treatment'] del treatment if subsession.round_number == 2: for p in subsession.get_players(): p.treatment = p.participant.vars['treatment'] p.task_roundscomp = p.participant.vars['task_roundscomp'] if subsession.round_number == 3: for p in subsession.get_players(): p.treatment = p.participant.vars['treatment'] p.task_roundscomp = p.participant.vars['task_roundscomp'] if subsession.round_number == 4: for p in subsession.get_players(): p.treatment = p.participant.vars['treatment'] p.task_roundscomp = p.participant.vars['task_roundscomp'] # PAGES class Introduction(Page): form_model = 'player' def is_displayed(self): return self.round_number == 1 class section1(Page): form_model = 'player' def is_displayed(self): return self.round_number == 1 class ExampleMPL(Page): form_model = 'player' form_fields = ['ExampleMPL','randomrow_example','example_choice'] @staticmethod def vars_for_template(self): # retrieve values from constants and store them in a dictionary participant = self.participant participant.vars['randomrow_example'] = random.randint(1, len(Constants.list_c)) return{ 'initial_endowment_c_example': Constants.initial_endowment_c_example, 'initial_endowment_m_example': Constants.initial_endowment_m_example, 'extra_chocolate_example': Constants.extra_chocolate_example, 'list_c': Constants.list_c, 'len_list_c': len(Constants.list_c), 'randomrow_example': participant.vars['randomrow_example'] } def before_next_page(self: Player, timeout_happened): participant = self.participant print(f"Before next page: {participant.vars}") participant.vars['ExampleMPL'] = self.ExampleMPL participant.vars['example_choice'] = self.ExampleMPL[participant.vars['randomrow_example']-1] if participant.vars['example_choice'] == "L": participant.vars['Example_payment_c'] = Constants.initial_endowment_c_example participant.vars['Example_payment_m'] = Constants.initial_endowment_m_example + Constants.extra_money_example if participant.vars['example_choice'] == "R": participant.vars['Example_payment_c'] = Constants.initial_endowment_c_example + participant.vars['randomrow_example'] participant.vars['Example_payment_m'] = Constants.initial_endowment_m_example print(f"After setting page_start_time: {participant.vars}") def live_method(self: Player, data): participant = self.participant example_chosenchoice = data[participant.vars['randomrow_example']-1] randomrow_example = participant.vars['randomrow_example'] if example_chosenchoice == "L": participant.vars['Example_payment_c'] = Constants.initial_endowment_c_example participant.vars['Example_payment_m'] = Constants.initial_endowment_m_example + Constants.extra_money_example participant.vars['Example_chosenchoice'] = f'extra {Constants.extra_money_example} dollars' if example_chosenchoice == "R": participant.vars['example_payment_c']= Constants.initial_endowment_c_example + participant.vars['randomrow_example'] participant.vars['example_payment_m'] = Constants.initial_endowment_m_example participant.vars['Example_chosenchoice'] = f'extra {randomrow_example} chocolates' response = {'example_payment_c': participant.vars['example_payment_c'], 'example_payment_m': participant.vars['example_payment_m'], 'example_chosenchoice': participant.vars['Example_chosenchoice'] } return {0: response} def is_displayed(self): return self.round_number == 1 class ExampleElicit(Page): form_model = 'player' @staticmethod def vars_for_template(self): # retrieve values from constants and store them in a dictionary return{ 'randomrow_example': self.participant.vars['randomrow_example'], 'example_choice': self.participant.vars['Example_choice'], 'example_payment_m': self.participant.vars['Example_payment_m'], 'example_payment_c': self.participant.vars['Example_payment_c'] } def is_displayed(self): return self.round_number == 1 class Test(Page): form_model = 'player' form_fields = ['test_chocolate_1','test_money_1','test_chocolate_2','test_money_2'] def is_displayed(self): return self.round_number == 1 class PageEGx_1(Page): # which forms are needed from class player form_model = 'player' form_fields = ['egx_1'] # all 20 options # values that are to be displayed (dictionary) @staticmethod def vars_for_template(self): # retrieve values from constants and store them in a dictionary return{ 'initial_endowment_c': Constants.initial_endowment_c, 'initial_endowment_m': Constants.initial_endowment_m, 'extra_chocolate': Constants.extra_chocolate_1, 'list_c': Constants.list_c, 'len_list_c': len(Constants.list_c) } def before_next_page(self: Player, timeout_happened): participant = self.participant participant.vars['egx_1'] = self.egx_1 @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == json.loads(participant.vars['task_roundscomp'])['egx_1'] class PageEGy_1(Page): # which forms are needed from class player form_model = 'player' form_fields = ['egy_1'] # all 20 options # values that are to be displayed (dictionary) @staticmethod def vars_for_template(self): # retrieve values from constants and store them in a dictionary return{ 'initial_endowment_c': Constants.initial_endowment_c, 'initial_endowment_m': Constants.initial_endowment_m, 'extra_chocolate': Constants.extra_chocolate_1, 'list_m': Constants.list_m, 'len_list_m': len(Constants.list_m) } def before_next_page(self: Player, timeout_happened): participant = self.participant participant.vars['egy_1'] = self.egy_1 @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == json.loads(participant.vars['task_roundscomp'])['egy_1'] class section2(Page): def is_displayed(self: Player): return self.round_number == 4 class PageExchange_ab(Page): # which forms are needed from class player form_model = 'player' form_fields = ['exchange_ab'] # values that are to be displayed (dictionary) @staticmethod def vars_for_template(self): # retrieve values from constants and store them in a dictionary return{ 'bundle_a_m': Constants.bundle_a_m, 'bundle_a_c': Constants.bundle_a_c, 'bundle_b_m': Constants.bundle_b_m, 'bundle_b_c': Constants.bundle_b_c } def before_next_page(self:Player, timeout_happened): participant = self.participant participant.vars['exchange_ab'] = self.exchange_ab def is_displayed(self: Player): return self.participant.vars['treatment'] == 'a' and self.round_number == 4 class PageExchange_ba(Page): # which forms are needed from class player form_model = 'player' form_fields = ['exchange_ab'] # values that are to be displayed (dictionary) @staticmethod def vars_for_template(self): # retrieve values from constants and store them in a dictionary return{ 'bundle_a_m': Constants.bundle_a_m, 'bundle_a_c': Constants.bundle_a_c, 'bundle_b_m': Constants.bundle_b_m, 'bundle_b_c': Constants.bundle_b_c } def before_next_page(self:Player, timeout_happened): participant = self.participant participant.vars['exchange_ab'] = self.exchange_ab def is_displayed(self: Player): return self.participant.vars['treatment'] == 'b' and self.round_number == 4 class PageExchange_abc(Page): # which forms are needed from class player form_model = 'player' form_fields = ['exchange_abc'] # values that are to be displayed (dictionary) @staticmethod def vars_for_template(self): # retrieve values from constants and store them in a dictionary return{ 'bundle_a_m': Constants.bundle_a_m, 'bundle_a_c': Constants.bundle_a_c, 'bundle_b_m': Constants.bundle_b_m, 'bundle_b_c': Constants.bundle_b_c, 'bundle_c_m': Constants.bundle_c_m, 'bundle_c_c': Constants.bundle_c_c, } def before_next_page(self:Player, timeout_happened): participant = self.participant participant.vars['exchange_abc'] = self.exchange_abc def is_displayed(self: Player): return self.round_number == 4 class PageExchange_abd(Page): # which forms are needed from class player form_model = 'player' form_fields = ['exchange_abd'] # values that are to be displayed (dictionary) @staticmethod def vars_for_template(self): # retrieve values from constants and store them in a dictionary return{ 'bundle_a_m': Constants.bundle_a_m, 'bundle_a_c': Constants.bundle_a_c, 'bundle_b_m': Constants.bundle_b_m, 'bundle_b_c': Constants.bundle_b_c, 'bundle_d_m': Constants.bundle_d_m, 'bundle_d_c': Constants.bundle_d_c, } def before_next_page(self:Player, timeout_happened): participant = self.participant participant.vars['exchange_abd'] = self.exchange_abd # Call the initialize method of the player def is_displayed(self: Player): return self.round_number == 4 class PageExchange_abe(Page): # which forms are needed from class player form_model = 'player' form_fields = ['exchange_abe'] # values that are to be displayed (dictionary) @staticmethod def vars_for_template(self): # retrieve values from constants and store them in a dictionary return{ 'bundle_a_m': Constants.bundle_a_m, 'bundle_a_c': Constants.bundle_a_c, 'bundle_b_m': Constants.bundle_b_m, 'bundle_b_c': Constants.bundle_b_c, 'bundle_e_m': Constants.bundle_e_m, 'bundle_e_c': Constants.bundle_e_c, } def before_next_page(self:Player, timeout_happened): participant = self.participant participant.vars['exchange_abe'] = self.exchange_abe # Call the initialize method of the player self.initialize() def is_displayed(self: Player): return self.round_number == 4 class PageExchange_abf(Page): # which forms are needed from class player form_model = 'player' form_fields = ['exchange_abf'] # values that are to be displayed (dictionary) @staticmethod def vars_for_template(self): # retrieve values from constants and store them in a dictionary return{ 'bundle_a_m': Constants.bundle_a_m, 'bundle_a_c': Constants.bundle_a_c, 'bundle_b_m': Constants.bundle_b_m, 'bundle_b_c': Constants.bundle_b_c, 'bundle_f_m': Constants.bundle_f_m, 'bundle_f_c': Constants.bundle_f_c, } def before_next_page(self:Player, timeout_happened): participant = self.participant participant.vars['exchange_abf'] = self.exchange_abf # Call the initialize method of the player self.initialize() def is_displayed(self: Player): return self.round_number == 4 class Math(Page): # which forms are needed from class player form_model = 'player' form_fields = ['math_q1','math_q2','math_q3','math_q4','math_q5'] def is_displayed(self: Player): return self.round_number == 4 class OutcomeEGy(Page): @staticmethod # payment question and rows def vars_for_template(self:Player): # values that are to be displayed (dictionary) if self.random_question == 1: self.choice = self.participant.vars['egx_1'][self.random_row_egx_1 - 1] self.row = self.random_row_egx_1 if self.choice == "L": self.payment_m = Constants.initial_endowment_m + Constants.extra_money_1 self.payment_c = Constants.initial_endowment_c # left, extra money if self.choice == "R": self.payment_m = Constants.initial_endowment_m self.payment_c = Constants.initial_endowment_c + Constants.list_c[self.random_row_egx_1 - 1] if self.random_question == 2: self.choice = self.participant.vars['egy_1'][self.random_row_egy_1 - 1] self.row = self.random_row_egy_1 if self.choice == "L": self.payment_m = Constants.initial_endowment_m self.payment_c = Constants.initial_endowment_c + Constants.extra_chocolate_1 # left, extra money if self.choice == "R": self.payment_m = Constants.initial_endowment_m + Constants.list_m[ self.random_row_egy_1 - 1] self.payment_c = Constants.initial_endowment_c if self.random_question == 3 or self.random_question == 4 or self.random_question == 5 \ or self.random_question == 6 or self.random_question == 7: if self.random_question == 3: self.choice = self.exchange_ab if self.random_question == 4: self.choice = self.exchange_abc if self.random_question == 5: self.choice = self.exchange_abd if self.random_question == 6: self.choice = self.exchange_abe if self.random_question == 7: self.choice = self.exchange_abf self.row = 9999 if self.choice == "a": self.payment_m = Constants.bundle_a_m self.payment_c = Constants.bundle_a_c if self.choice == "b": self.payment_m = Constants.bundle_b_m self.payment_c = Constants.bundle_b_c if self.choice == "c": self.payment_m = Constants.bundle_c_m self.payment_c = Constants.bundle_c_c if self.choice == "d": self.payment_m = Constants.bundle_d_m self.payment_c = Constants.bundle_d_c if self.choice == "e": self.payment_m = Constants.bundle_e_m self.payment_c = Constants.bundle_e_c if self.choice == "f": self.payment_m = Constants.bundle_f_m self.payment_c = Constants.bundle_f_c return{ 'random_question': self.random_question, 'random_row_egx_1': self.random_row_egx_1, 'random_row_egy_1': self.random_row_egy_1, 'payment_m': self.payment_m, 'payment_c': self.payment_c, 'payment_question': self.random_question, 'payment_row': self.row, 'payment_question_choice': self.choice } def is_displayed(self: Player): return self.round_number == 4 class final(Page): def is_displayed(self: Player): return self.round_number == 4 page_sequence = [Introduction, section1, ExampleMPL,ExampleElicit,Test,PageEGx_1, PageEGy_1,PageExchange_ab,PageExchange_ba, PageExchange_abc, PageExchange_abd,PageExchange_abe, PageExchange_abf,section2,Math,OutcomeEGy,final]