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_effort' players_per_group = None taskamount = 40 interest_rate = 0.25 max_interest = int(taskamount*(1+interest_rate)) amounts = ['1 week', '1 month', '3 months', '6 months', '1 year', '2 years', '3 years'] num_rounds = 7 units = [ [1, 'week(s)'], [2, 'month(s)'], [3, '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(Constants.amounts, 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() choice_number = models.IntegerField() time = models.StringField() task_now = models.FloatField() 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 25 tasks at any time in the next 2 weeks.'], [1, 'I would complete 10 tasks now and 15 tasks in 2 weeks.'], [0, 'I would complete either 10 tasks now or 15 tasks in 2 weeks.'], ], widget=widgets.RadioSelect, blank=False, label="" ) qn_list = models.IntegerField( choices=[ [0, 'This person indicated that they would prefer to complete all tasks in 6 months.'], [0, 'This person indicated that they would only complete 16 tasks.'], [1, 'This person indicated that they would complete 16 tasks now and 35 tasks in 6 months.'], ], widget=widgets.RadioSelect, blank=False, label="" ) qn_confidence = models.FloatField( blank=False, label="" ) def set_current_task_number(self): setattr(self, 'time', self.participant.vars['list_sequence'][self.round_number-1]) def set_list(self): i=4 task_list = [] while i <= 40: task_list.append(i) i+=2 interest_list= [] i = Constants.max_interest while i >= 4: interest_list.append(i) i -= 2*(1+Constants.interest_rate) for x in range(len(interest_list)): if (float(interest_list[x])).is_integer() == True: interest_list[x]= int(interest_list[x]) count = list(range(0,len(task_list))) x = zip(task_list,interest_list,count) return x def range_bounds(self): if self.switching_point >= 0: bounds = [4, Constants.taskamount] return bounds def table_length1(self): return abs(Constants.taskamount)/2 + 1