from typing import List import itertools from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import otree.forms from otree.db.models import Model from random import (randrange, uniform, sample) import json import time import datetime author = 'Obukwelu Ebuka' from .countries import CountryField doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'problemsolvingexperiment' players_per_group = None num_rounds = 20 treatments = [2] highlighted_entries = [[0], [11, 12, 13, 14, 15], [6, 7, 8, 9, 10, 16, 17, 18, 19, 20], [6, 7, 8, 9, 10, 16, 17, 18, 19, 20], [6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [0], [1, 2, 3, 4, 5, 16, 17, 18, 19, 20], [1, 2, 3, 4, 5, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 21, 22, 23, 24, 25], [6, 7, 8, 9, 10, 16, 17, 18, 19, 20], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 17, 18, 19, 20], [6, 7, 8, 9, 10, 21, 22, 23, 24, 25], [0], [1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [6, 7, 8, 9, 10, 16, 17, 18, 19, 20], [6, 7, 8, 9, 10, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]] # add hardcoded highlighted cells error_positions1 = [[0], [13], [19], [6], [9, 17], [4, 7], [0], [4, 7, 18], [3, 12, 17], [1], [25], [11, 20], [3, 6, 20, 22], [6], [5, 20], [21], [13], [1], [7, 14, 18], [17], [10, 17, 22]] # positions of wrong encryptions (with 25% FP) error_positions2 = [[0], [8, 13], [6, 19], [6], [9, 17], [4, 7, 13], [0], [4, 7, 18], [3, 12, 17], [1, 10], [25], [10, 11, 20], [3, 6, 20, 22], [6, 19], [5, 20], [10, 21], [13], [1], [7, 14, 18], [10, 17], [10, 17, 22]] # positions of wrong encryptions (with 5% FP) class Subsession(BaseSubsession): #def creating_session(self): #print("create session") def creating_session(self): # before_session_starts in older otree treatments = itertools.cycle(Constants.treatments) if self.round_number == 1: for p in self.get_players(): #p.participant.vars['treatment'] = next(treatments) p.participant.vars['treatment'] = 2 p.participant.vars['tutorial_points'] = 0 p.participant.vars["start_time"] = 0 p.seed() for p in self.get_players(): q = p.current_question(self.round_number) print(q) encryption_table = q[0].encryption matrix = q[0].matrix p.set_encryption(encryption_table) p.set_matrix(matrix) p.errors = q[0].errors #p.suggested_solution = str(randrange(500, 9000, 23)).zfill(4) p.treatment = p.participant.vars["treatment"] class Group(BaseGroup): pass class Question_Encryption(Model): value = models.IntegerField() matrix = models.CharField() encryption = models.CharField() errors = models.CharField() class Player(BasePlayer): treatment = models.IntegerField() matrix = models.CharField() encryption = models.CharField() errors = models.CharField() error_pos = models.IntegerField() col = models.IntegerField() your_answer = models.CharField(blank=True) solution = models.CharField() prolific_id = models.StringField(label="Please enter your prolific participant id", blank=False) suggested_solution = models.StringField() task_duration = models.FloatField() is_correct = models.BooleanField() experiment_end = models.StringField() experiment_start = models.StringField() estimation = models.IntegerField() tutorial_points = models.IntegerField() tut_box_1 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) tut_box_2 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) tut_box_3 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) tut_box_4 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) tut_box_5 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_1 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_2 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_3 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_4 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_5 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_6 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_7 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_8 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_9 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_10 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_11 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_12 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_13 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_14 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_15 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_16 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_17 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_18 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_19 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_20 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_21 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_22 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_23 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_24 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) box_25 = models.BooleanField(choices=[[False, 'NoError'], [True, 'Error']], widget=widgets.CheckboxInput) Employment_Choice =( ("Pupil/Student", "Pupil/Student"), ("employed", "Employed"), ("non-employed", "Non-employed"), ("self-employed", "Self-employed"), ("retiree", "Retiree") ) Education_Choice = ( ("without any schooling degree", "Without any schooling degree"), ("lower secondary schooling degree", "Lower secondary schooling degree"), ("secondary schooling degree", "Secondary schooling degree"), ("university entrance level degree", "University entrance level degree"), ("Apprenticeship", "Apprenticeship"), ("Academic degree", "Academic degree"), ("Others", "Others"), ) choice_scale4 = ( (1, ""), (2, ""), (3, ""), (4, ""), ) choice_scale5 = ( (1, ""), (2, ""), (3, ""), (4, ""), (5, ""), ) choice_scale7 = ( (1, ""), (2, ""), (3, ""), (4, ""), (5, ""), (6, ""), (7, ""), ) def vars_for_template(self): if self.participant.vars["treatment"] == 1: errors = Constants.error_positions1[self.round_number] else: errors = Constants.error_positions2[self.round_number] return { "treatment": self.participant.vars["treatment"], "task_count": self.round_number, "highlighted_entries": Constants.highlighted_entries[self.round_number], "error_positions": errors, "tutorial_points": self.participant.vars["tutorial_points"], "box_1": self.box_1, "box_2": self.box_2, "box_3": self.box_3, "box_4": self.box_4, "box_5": self.box_5, "box_6": self.box_6, "box_7": self.box_7, "box_8": self.box_8, "box_9": self.box_9, "box_10": self.box_10, "box_11": self.box_11, "box_12": self.box_12, "box_13": self.box_13, "box_14": self.box_14, "box_15": self.box_15, "box_16": self.box_16, "box_17": self.box_17, "box_18": self.box_18, "box_19": self.box_19, "box_20": self.box_20, "box_21": self.box_21, "box_22": self.box_22, "box_23": self.box_23, "box_24": self.box_24, "box_25": self.box_25 } # generation of matrices def seed(self): questions = Question_Encryption.objects.filter(value=1) question_len = len(questions) if question_len < 1000000: for _ in range(Constants.num_rounds): ranger = sample(range(0, 100), 26) # generates encryptions of the alphabet encryption_table = [[chr(i), str(ranger[j]).zfill(2)] for i, j in zip(range(ord('A'), ord('Z') + 1), range(len(ranger)))] q = Question_Encryption.objects.create(value=1) q.value = _+1 print("Matrix generated") q.encryption = json.dumps(encryption_table) q.matrix = json.dumps([[encryption_table[randrange(0, 26)], encryption_table[randrange(0, 26)]] for i in range(25)]) q.errors = json.dumps([[row, randrange(1,6)] for row in sample(range(1, 6, randrange(1, 2)), 4)]) # make this as random as possible. I am generating a list of error positions q.save() def current_question(self, x: int): question = Question_Encryption.objects.filter(value=x) return list(question) # question fields q1 = models.CharField(label="What is your year of birth?", choices=[str(i) for i in range(1930, 2015)], initial="1930") q2 = CountryField(label="What is your country of residence?", initial="AD") q3 = models.CharField(label="What is your employment status?", widget=widgets.RadioSelect, choices= Employment_Choice, initial="" ) q4 = models.StringField(label="What is your highest educational level?", widget=widgets.RadioSelect, choices= Education_Choice, initial="Pupil/Student") q4_other = models.CharField(blank=True, label="") q5 = models.IntegerField(label="I understand what the system should do.", choices=choice_scale7, widget=widgets.RadioSelect) q6 = models.IntegerField(label="I understand the limitations of the system.", choices=choice_scale7, widget=widgets.RadioSelect) q7 = models.IntegerField(label="I understand the capabilities of the system.", choices=choice_scale7, widget=widgets.RadioSelect) q8 = models.IntegerField(label="I understand how the system executes tasks.", choices=choice_scale7, widget=widgets.RadioSelect) q9 = models.IntegerField(label="The system helps me achieve my goals.", choices=choice_scale7, widget=widgets.RadioSelect) q10 = models.IntegerField(label="The system performs consistently.", choices=choice_scale7, widget=widgets.RadioSelect) q11 = models.IntegerField(label="The system performs the way it should.", choices=choice_scale7, widget=widgets.RadioSelect) q12 = models.IntegerField(label="I am rarely surprised by how the system responds.", choices=choice_scale7, widget=widgets.RadioSelect) q13 = models.IntegerField(label="I feel comfortable relying on the information provided by the system", choices=choice_scale7, widget=widgets.RadioSelect) def set_matrix(self, x): self.matrix = x def get_matrix(self): return json.loads(self.matrix) def set_encryption(self, x): self.encryption = x def get_encryption(self): return json.loads(self.encryption) def set_timing(self): self.task_duration = time.time() def set_end(self): self.experiment_end = str(datetime.datetime.now()) def set_start(self): self.experiment_start = str(datetime.datetime.now()) def check_correct(self): if self.participant.vars["treatment"] == 1: error = Constants.error_positions1[self.round_number] else: error = Constants.error_positions2[self.round_number] selections = [self.box_1, self.box_2, self.box_3, self.box_4, self.box_5, self.box_6, self.box_7, self.box_8, self.box_9, self.box_10, self.box_11, self.box_12, self.box_13, self.box_14, self.box_15, self.box_16, self.box_17, self.box_18, self.box_19, self.box_20, self.box_21, self.box_22, self.box_23, self.box_24, self.box_25] print("print correct error locations:") print(error) print("print participant's selection in error locations:") for x in range(1, 26): if x in error: print(selections[x-1]) if selections[x-1]: self.payoff += c(0.1) def check_estimation(self): if 4 <= self.estimation <= 6: self.payoff += c(1)