from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) # from otree_tools.forms.fields import OtherModelField import numpy as np # from mpl.config import * from random import randrange import random import os author = 'Mohsen Foroughifar' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'competition_I_D' players_per_group = None num_rounds = 1 participation_fee = 6 lottery_a_hi = 12 lottery_a_lo = 0 lottery_b_hi = 12 lottery_b_lo = 0 num_choices = 1 certain_choice = True random_order = False enforce_consistency = False percentage = False small_pies = False large_pies = False progress_bar = True instructions = True results = False competition_award = 40 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): Guess1 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess2 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess3 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess4 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess5 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess6 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess7 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess8 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess9 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess10 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess11 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess12 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess13 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess14 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess15 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess16 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess17 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess18 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess19 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess20 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess21 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Confidence1 = models.IntegerField(min=0, max=100) Confidence2 = models.IntegerField(min=0, max=100) Confidence3 = models.IntegerField(min=0, max=100) Confidence4 = models.IntegerField(min=0, max=100) Confidence5 = models.IntegerField(min=0, max=100) Confidence6 = models.IntegerField(min=0, max=100) Confidence7 = models.IntegerField(min=0, max=100) Confidence8 = models.IntegerField(min=0, max=100) Confidence9 = models.IntegerField(min=0, max=100) Confidence10 = models.IntegerField(min=0, max=100) Confidence11 = models.IntegerField(min=0, max=100) Confidence12 = models.IntegerField(min=0, max=100) Confidence13 = models.IntegerField(min=0, max=100) Confidence14 = models.IntegerField(min=0, max=100) Confidence15 = models.IntegerField(min=0, max=100) Confidence16 = models.IntegerField(min=0, max=100) Confidence17 = models.IntegerField(min=0, max=100) Confidence18 = models.IntegerField(min=0, max=100) Confidence19 = models.IntegerField(min=0, max=100) Confidence20 = models.IntegerField(min=0, max=100) Confidence21 = models.IntegerField(min=0, max=100) Guess_About_Neighbor_1 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_2 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_3 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_4 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_5 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_6 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_7 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_8 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_9 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_10 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_11 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_12 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_13 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_14 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_15 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_16 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_17 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_18 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_19 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_20 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Guess_About_Neighbor_21 = models.IntegerField(choices=[[1, 'Box H (Head)'],[2, 'Box T (Tail)'],],widget=widgets.RadioSelect) Confidence_for_neighbor_1 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_2 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_3 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_4 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_5 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_6 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_7 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_8 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_9 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_10 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_11 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_12 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_13 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_14 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_15 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_16 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_17 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_18 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_19 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_20 = models.IntegerField(min=0, max=100) Confidence_for_neighbor_21 = models.IntegerField(min=0, max=100) Switching_point = models.IntegerField(min=0, max=100) Major = models.StringField() Age = models.IntegerField(min=0, max=100) Prob_course = models.BooleanField() Education = models.IntegerField() Gender = models.IntegerField(choices=[[1, 'Male '],[2, 'Female '],[3, 'Other '],],widget=widgets.RadioSelectHorizontal) Comment_ball = models.LongStringField() Comment_neighbor = models.LongStringField() Feedback = models.LongStringField() participant_vars_dump = models.LongStringField() # set player's payoff # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: def set_payoffs(self): # random draw to pick one round from each part # ------------------------------------------------------------------------------------------------------------ random_part1 = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]) # random_part2 = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]) random_part3 = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]) # set player's payoff part 1 # ------------------------------------------------------------------------------------------------------------ guess_or_post = 1 self.participant.vars['pay_off_guess_or_post'] = [guess_or_post] if guess_or_post == 1: if ((self.participant.vars['Guesses'][random_part1 - 1] == 'Box T (Tail)') + 1 == self.participant.vars['state_realized'][random_part1 - 1]): payoff = [Constants.lottery_b_hi] else: payoff = [Constants.lottery_b_lo] else: X = random.choice(range(1, 101)) if self.participant.vars['switching_rows'][random_part1 - 1] <= X: if ((self.participant.vars['Guesses'][random_part1 - 1] == 'Box T (Tail)') + 1 == self.participant.vars['state_realized'][random_part1 - 1]): payoff = [Constants.lottery_b_hi] else: payoff = [Constants.lottery_b_lo] else: draw_for_posterior_lottery = np.random.binomial(1, X / 100, 1) if draw_for_posterior_lottery == 1: payoff = [Constants.lottery_b_hi] else: payoff = [Constants.lottery_b_lo] guess_or_post = 1 self.participant.vars['pay_off_guess_or_post'].append(guess_or_post) if guess_or_post == 1: if ((self.participant.vars['Guesses_About_Neighbor'][random_part3 - 1] == 'Box T (Tail)') + 1 == self.participant.vars['neighbor_state' + str(random_part3)]): payoff.append(Constants.lottery_b_hi) else: payoff.append(Constants.lottery_b_lo) else: X = random.choice(range(1, 101)) if self.participant.vars['switching_rows_neighbor'][random_part3 - 1] <= X: if ((self.participant.vars['Guesses_About_Neighbor'][random_part3 - 1] == 'Box T (Tail)') + 1 == self.participant.vars['neighbor_state' + str(random_part3)]): payoff.append(Constants.lottery_b_hi) else: payoff.append(Constants.lottery_b_lo) else: draw_for_posterior_lottery = np.random.binomial(1, X / 100, 1) if draw_for_posterior_lottery == 1: payoff.append(Constants.lottery_b_hi) else: payoff.append(Constants.lottery_b_lo) # set payoff as global variable # ------------------------------------------------------------------------------------------------------------ self.participant.vars['payoff'] = payoff self.participant.vars['pay_off_randrounds'] = [random_part1, random_part3] self.participant.vars['Check_Guesses'] = self.sorted_check(self.participant.vars['Guesses'], self.participant.vars['state_realized'], self.participant.vars['seq']) self.participant.vars['state_realized_sorted'] = self.sort(self.participant.vars['state_realized'], self.participant.vars['seq']) # self.participant.vars['switching_rows_sorted'] = self.sort(self.participant.vars['switching_rows'], # self.participant.vars['seq']) self.participant.vars['Guesses_sorted'] = self.sort(self.participant.vars['Guesses'], self.participant.vars['seq']) self.participant.vars['Guesses_conf_sorted'] = self.sort(self.participant.vars['Guesses_confidence'], self.participant.vars['seq']) self.participant.vars['draw_sorted'] = self.sort(self.participant.vars['draw'], self.participant.vars['seq']) neighbors = [self.participant.vars['neighbor_id' + str(kk)] for kk in range(1, len(self.participant.vars['Guesses_sorted']) + 1)] neighbors_states = [self.participant.vars['neighbor_state' + str(kk)] for kk in range(1, len(self.participant.vars['Guesses_sorted']) + 1)] neighbors_round = [self.participant.vars['neighbor_round' + str(kk)] for kk in range(1, len(self.participant.vars['Guesses_sorted']) + 1)] neighbors_draw = [self.participant.vars['neighbor_draw' + str(kk)] for kk in range(1, len(self.participant.vars['Guesses_sorted']) + 1)] neighbors_guess = [self.participant.vars['neighbor_guess' + str(kk)] for kk in range(1, len(self.participant.vars['Guesses_sorted']) + 1)] neighbors_round_seq = [self.participant.vars['neighbor_seq' + str(kk)] for kk in range(1, len(self.participant.vars['Guesses_sorted']) + 1)] self.participant.vars['Check_Guess_neighbor'] = self.sorted_check(self.participant.vars['Guesses_About_Neighbor'], neighbors_states, neighbors_round_seq) self.participant.vars['neighbor_state_sorted'] = self.sort(neighbors_states, neighbors_round_seq) # self.participant.vars['switching_rows_I_sorted'] = self.sort(self.participant.vars['switching_rows_I'], # neighbors_round_seq) self.participant.vars['Guesses_neighbor_sorted'] = self.sort(self.participant.vars['Guesses_About_Neighbor'], neighbors_round_seq) self.participant.vars['Guesses_conf_neighbor_sorted'] = self.sort( self.participant.vars['Guesses_condifence_About_Neighbor'], neighbors_round_seq) self.participant.vars['neighbor_draw_sorted'] = self.sort(neighbors_draw, neighbors_round_seq) self.participant.vars['neighbor_guess_sorted'] = self.sort(neighbors_guess, neighbors_round_seq) self.participant.vars['Guesses_sorted_competition'] = self.sort(self.participant.vars['Guesses_competition'], self.participant.vars['seq']) self.participant.vars['Guesses_conf_sorted_competition'] = self.sort(self.participant.vars['Guesses_confidence_competition'], self.participant.vars['seq']) self.participant.vars['Guesses_neighbor_sorted_competition'] = self.sort(self.participant.vars['Guesses_About_Neighbor_competition'], neighbors_round_seq) self.participant.vars['Guesses_conf_neighbor_sorted_competition'] = self.sort( self.participant.vars['Guesses_About_Neighbor_confidence_competition'], neighbors_round_seq) competition_score = 0; for i in range(len(self.participant.vars['Guesses_conf_neighbor_sorted_competition'])): competition_score -= (self.participant.vars['Guesses_conf_sorted_competition'][i])^2 if ((self.participant.vars['Guesses_sorted_competition'][i] == 'Box T (Tail)') + 1 == self.participant.vars['state_realized_sorted'][i]): competition_score += 200*self.participant.vars['Guesses_conf_sorted_competition'][i] competition_score -= (self.participant.vars['Guesses_conf_neighbor_sorted_competition'][i])^2 if ((self.participant.vars['Guesses_neighbor_sorted_competition'][i] == 'Box T (Tail)') + 1 == self.participant.vars['neighbor_state_sorted'][i]): competition_score += 200*self.participant.vars['Guesses_conf_neighbor_sorted_competition'][i] self.participant.vars['competition_score'] = competition_score def sorted_check(self, guesses, states, seq): output = [] for i in range(1, len(states) + 1): Ind = seq.index(i) output.append(((guesses[Ind] == 'Box T (Tail)') + 1 == states[Ind]) * 1) return output def sort(self, input, orders): temp = [] for i in range(1, len(orders) + 1): Ind = orders.index(i) temp.append(input[Ind]) return temp