from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import itertools import numpy as np from django import forms from django.forms import widgets as django_widgets import math author = 'Zheng Li' doc = """ Direct Elicitation for money experiment """ class Constants(BaseConstants): name_in_url = 'direct_elicitation' players_per_group = None # pairs of tau2 and tau1# decisions = [ # [0:6] choose 2 and 1 from the chosen 2 ['In 1 week', 'Today'], ['In 1 month', 'Today'], ['In 3 months', 'Today'], ['In 3 years', 'Today'], ['In 4 years', 'Today'], ['In 7 years', 'Today'], # [6:12] choose 2 ['In 2 months', 'In 1 month'], ['In 6 months', 'In 1 month'], ['In 1 year', 'In 1 month'], ['In 2 years', 'In 1 month'], ['In 4 years', 'In 1 month'], ['In 7 years', 'In 1 month'], # [12:15] sub-additivity 1 ['In 6 months', 'Today'], ['In 1 year', 'In 6 months'], ['In 1 year', 'Today'], # [15:18] sub-additivity 2 ['In 2 years', 'In 1 year'], ['In 3 years', 'In 2 years'], ['In 3 years', 'In 1 year'], # [18:21] sub-additivity 3 ['In 1 year', 'Today'], ['In 2 years', 'In 1 year'], ['In 2 years', 'Today'], ] amounts = range(36, 58, 2) steps = 2 num_rounds = 6 # to generate the certainty-uncertainty spectrum bar list1 = np.arange(1 * 20, - 1, -1) list2 = [] list2.append(0) for x in range(20): list2.append((x+1)*5) class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: for p in self.get_players(): p.participant.vars['sub_'] = random.choice([1, 2, 3]) datessub1 = Constants.decisions[12:15] dates_sub1 = list() amount_tmp = random.choice(Constants.amounts) for date in datessub1: date = (date, amount_tmp, 1) dates_sub1.append(date) datessub2 = Constants.decisions[15:18] dates_sub2 = list() amount_tmp = random.choice(Constants.amounts) for date in datessub2: date = (date, amount_tmp, 2) dates_sub2.append(date) datessub3 = Constants.decisions[18:21] dates_sub3 = list() amount_tmp = random.choice(Constants.amounts) for date in datessub3: date = (date, amount_tmp, 3) dates_sub3.append(date) if p.participant.vars['sub_'] == 1: dates_sub = dates_sub1 elif p.participant.vars['sub_'] == 2: dates_sub = dates_sub2 elif p.participant.vars['sub_'] == 3: dates_sub = dates_sub3 dates_1 = list() dates1 = random.sample(Constants.decisions[0:6], 2) for date in dates1: amount_tmp = random.choice(Constants.amounts) date = (date, amount_tmp, 0) dates_1.append(date) dates_1_extra = random.choice(dates_1) dates_1.append(dates_1_extra) p.participant.vars['dates'] = random.sample(dates_sub + dates_1, 6) for p in self.get_players(): p.set_current_task_number() class Group(BaseGroup): pass class Player(BasePlayer): # whether the participant has been passing all tests so far tests_passed = models.BooleanField(initial=True) # current set sub_identifier = models.IntegerField() # parameters in the optimization problem pay_amount = models.IntegerField() # benchmark payment at tau2 time_date_lhs = models.StringField() # tau2 time_date_rhs = models.StringField() # tau1 # indicator_never_always_switcher = models.IntegerField() # indifferent payment task: switching_point = models.FloatField(min=0) def switching_point_max(self): return self.pay_amount confidence = models.FloatField() # higher -> more confident; from 0 to 20 respectively corresponding to from 0% to 100%; step = $5% def set_current_task_number(self): setattr(self, 'time_date_lhs', self.participant.vars['dates'][self.round_number - 1][0][0]) setattr(self, 'time_date_rhs', self.participant.vars['dates'][self.round_number - 1][0][1]) setattr(self, 'pay_amount', self.participant.vars['dates'][self.round_number - 1][1]) setattr(self, 'sub_identifier', self.participant.vars['dates'][self.round_number - 1][2]) def current_choice(self): iet_choice = [(self.time_date_lhs, self.time_date_rhs, self.pay_amount)] return iet_choice