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 = 'Nikolas Kirk' doc = """ Intertemporal Choice """ class Constants(BaseConstants): name_in_url = 'intertemporal_real_subadd' players_per_group = None decisions = [ ['In 1 day','Now'], ['In 2 months','In 1.5 months'], ['In 1.5 months','Now'], ['In 2 weeks','Now'], ['In 1 month','In 2 weeks'], ['In 1 week','Now'], ['In 2 months','In 1 month'], ['In 1 month','Now'], ['In 1.5 months','In 1 month'], ['In 2 months','Now'], ] amounts= [60,90,120], num_rounds = 10 units = [ [1, 'day(s)'], [2, 'week(s)'], [3, 'month(s)'], ] list1 = np.arange(1 * 40, - 1, -1) list2 = [] list2.append(1) c = 0 d = 0 for x in range(40): c +=1 if c%2 != 0: list2.append(0) if c % 2 == 0 and c != 0: list2.append(int(d + 5)) d+=5 class Subsession(BaseSubsession): def before_session_starts(self): if self.round_number == 1: for p in self.get_players(): p.participant.vars['failed_comprehension'] = False p.participant.vars['lists_paying_round'] = random.randint(1, Constants.num_rounds) # lol = random.sample([random.sample(range(0, 3), 3), random.sample(range(3, 6), 3)], 2) # p.participant.vars['list_sequence'] = [item for sublist in lol for item in sublist] payoffs = np.repeat(random.sample(Constants.amounts, len(Constants.amounts)), 4) zipped_list = list(zip(range(0, Constants.num_rounds), payoffs)) p.participant.vars['list_sequence'] = random.sample(zipped_list, Constants.num_rounds) # p.participant.vars['pay_sequence'] = np.repeat(Constants.amounts, 3) for p in self.get_players(): p.set_current_task_number() class Group(BaseGroup): pass class Player(BasePlayer): task_identifier = models.StringField() set_identifier = models.IntegerField() task_number = models.IntegerField() time = models.IntegerField() task_amount = models.IntegerField() task_later = models.FloatField() task_later_i = models.IntegerField() task_later_int = models.BooleanField() time_date_lhs = models.StringField() time_date_rhs = models.StringField() type = models.StringField() endowment = models.FloatField() indicator_never_always_switcher = models.IntegerField() switching_point = models.FloatField() choice_identifier = models.StringField() confidence = models.FloatField() confidence_upper_bound = models.FloatField() choice_number = models.IntegerField() confidence_lower_bound = models.FloatField() question_failed = models.IntegerField() delayed_payment_bool = models.StringField() delayed_payment_time = models.StringField() failed_comprehension = models.BooleanField(initial=False) qn_lottery_got_wrong = models.BooleanField(initial=False) qn_list_got_wrong = models.BooleanField(initial=False) qn_confidence_got_wrong = models.BooleanField(initial=False) qn_hypo_got_wrong = models.BooleanField(initial=False) draw_default_num_late = models.IntegerField() draw_default_unit_late = models.IntegerField() draw_default_num_early = models.IntegerField() draw_default_unit_early = models.IntegerField() default_num_early = models.FloatField(min=0, max=100, blank=False, label="") default_unit_early = models.IntegerField( choices=Constants.units, blank=False, label="" ) default_num_late = models.FloatField(min = 0, max = 100, blank = False, label="") default_unit_late = models.IntegerField( choices=Constants.units, blank = False, label="" ) qn_hypo = models.IntegerField( choices=[ [1, 'In making my decisions, I am asked to assume that I would actually complete the tasks assigned, regardless of whether they take place now or in the future.'], [0, 'In making my decisions, I am asked to assume that I would not actually complete the tasks assigned, regardless of whether they take place now or in the future.'], [0, 'In making my decisions, I am asked to assume that I would only actually complete the tasks assigned to be completed now.'], ], widget=widgets.RadioSelect, blank=False, label="" ) qn_lottery = models.IntegerField( choices=[ [0, 'I would complete 7 tasks at any time in the next 5 days.'], [1, 'I would complete either 2 tasks in 2 days or 5 tasks in 5 days.'], [0, 'I would complete 2 tasks in 2 days and 5 tasks in 5 days.'], ], widget=widgets.RadioSelect, blank=False, label="" ) qn_list = models.IntegerField( choices=[ [0, 'This person indicated that they prefer completing 15 tasks in 15 days.'], [0, 'This person indicated that they would only complete 3 tasks now.'], [1, 'This person indicated that completing 15 tasks in 30 days is worth the same to them as completing between 3 and 4 tasks in 15 days.'], ], widget=widgets.RadioSelect, blank=False, label="" ) qn_confidence = models.FloatField( blank=False, label="" ) def set_current_task_number(self): setattr(self, 'time_date_lhs', Constants.decisions[self.participant.vars['list_sequence'][self.round_number-1][0]][0]) setattr(self, 'time_date_rhs', Constants.decisions[self.participant.vars['list_sequence'][self.round_number-1][0]][1]) setattr(self, 'task_amount', self.participant.vars['list_sequence'][self.round_number-1][1]) setattr(self, 'set_identifier', self.participant.vars['list_sequence'][self.round_number-1][0] // 4) setattr(self, 'choice_number', self.participant.vars['list_sequence'][self.round_number-1][0]) def frange(self, start, stop, step): i = start while i < stop: if isinstance(i, int): yield i else: yield round(i, 2) i += step def right_side_amounts1(self): if self.task_amount > 0: lst = np.arange(self.task_amount, -1, -5) return list(enumerate(lst, 1)) def range_bounds(self): if self.switching_point >= 0: bounds = [0, self.task_amount] return bounds def table_length1(self): return abs(self.task_amount) + 1 def current_choice(self): iet_choice = [(self.time_date_lhs, self.time_date_rhs, self.task_amount)] return iet_choice def set_switching_point_and_indicator(self): if self.task_amount > 0: if self.switching_point == 9999: self.switching_point = self.task_amount + 1 self.indicator_never_always_switcher = 2 elif self.switching_point == 0: self.indicator_never_always_switcher = 0 else: self.indicator_never_always_switcher = 1 if self.switching_point > self.task_amount: self.switching_point = 0