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_effortv3' players_per_group = None decisions = { 'time': [[1], [5], [10], [15], [20], [30]], 'amounts': [15, 20, 25, 30, 35, 40], } num_rounds = 7 combinations = len(decisions['time']) * len(decisions['amounts']) units = [ [1, 'day(s)'], [2, 'week(s)'], [3, 'month(s)'], [4, 'year(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) p.participant.vars['list_sequence'] = random.sample(range(0, Constants.combinations), Constants.num_rounds) 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_now = models.IntegerField() task_later = models.FloatField() task_later_i = models.IntegerField() task_later_int = models.BooleanField() 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() 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() 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 17 tasks at any time in the next 5 days.'], [1, 'I would complete either 7 tasks now or 10 tasks in 5 days.'], [0, 'I would complete 7 tasks now and 10 tasks in 5 days.'], ], widget=widgets.RadioSelect, blank=False, label="" ) qn_list = models.IntegerField( choices=[ [0, 'This person indicated that they prefer completing 30 tasks now.'], [0, 'This person indicated that they would only complete 17 tasks.'], [1, 'This person indicated that completing 30 tasks in 20 days is worth the same to them as completing between 17 and 18 tasks now.'], ], widget=widgets.RadioSelect, blank=False, label="" ) qn_confidence = models.FloatField( blank=False, label="" ) def set_current_task_number(self): setattr(self, 'task_number', self.participant.vars['list_sequence'][self.round_number-1]) task_list = [] l = itertools.product( Constants.decisions['time'], Constants.decisions['amounts'], ) task_list.extend(l) tup = task_list[self.task_number] setattr(self, 'time', random.choice(tup[0])) setattr(self, 'task_now', tup[1]) 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_now > 0: lst = np.arange(self.task_now, -1, -1) return list(enumerate(lst, 1)) def range_bounds(self): if self.switching_point >= 0: bounds = [0, self.task_now] return bounds def table_length1(self): return abs(self.task_now) + 1 def current_choice(self): iet_choice = [(self.time, self.task_now)] return iet_choice def set_switching_point_and_indicator(self): if self.task_now > 0: if self.switching_point == 9999: self.switching_point = self.task_now + 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 elif self.task_now < 0: if self.switching_point == 9999: self.switching_point = 1 self.indicator_never_always_switcher = 2 elif self.switching_point == self.task_now: self.indicator_never_always_switcher = 0 else: self.indicator_never_always_switcher = 1