from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) from math import ceil import random import pandas as pd import itertools import numpy as np from sklearn.utils import shuffle doc = """ This is my rendition of the trust game, hopefully it works """ # This is the function I use to determine if offer was accepted or not def response(p): return True if random.random() < p else False class Constants(BaseConstants): name_in_url = 'investment_decisions_responders' players_per_group = None num_rounds = 21 # num_rounds = 2 instructions_template = 'my_trust_RL_responders/Instructions.html' endowment = c(20) payoff_if_rejected = c(0) offer_increment = c(1) offer_choices = currency_range(0, endowment, offer_increment) offer_choices_count = len(offer_choices) keep_give_amounts = [] for offer in offer_choices: keep_give_amounts.append((offer, endowment - offer)) # these probabilities are currently generated from my MATLAB script and are meant to control for crossing point # while changing the relative payout of the different opponents opponent_dict = dict(opp1 = [0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000], opp2 = [0.2200, 0.2400, 0.2600, 0.2800, 0.3000, 0.3200, 0.3400, 0.3600, 0.3800, 0.4000, 0.4200, 0.4400, 0.4600, 0.4800, 0.5000, 0.5200, 0.5400, 0.5600, 0.5800, 0.6000, 0.6200], opp3 = [0.1280, 0.1710, 0.2140, 0.2570, 0.3000, 0.3430, 0.3860, 0.4290, 0.4720, 0.5150, 0.5580, 0.6010, 0.6440, 0.6870, 0.7300, 0.7730, 0.8160, 0.8590, 0.9020, 0.9450, 0.9880]) opponent = "opp1", "opp2", "opp3" opponentb1 = list(opponent * (int((num_rounds / len(opponent)) / 2))) opponentb2 = list(opponent * (int((num_rounds / len(opponent)) / 2))) random.shuffle((opponentb1)) random.shuffle((opponentb2)) opponent_all = opponentb1 + opponentb2 class Subsession(BaseSubsession): pass # slider_starter = np.random.choice(Constants.offer_choices) class Group(BaseGroup): current_opponent = models.CharField() payout = models.IntegerField() soc_or_no = models.CharField() current_offer = models.IntegerField() current_offer_mult_3 = models.IntegerField() amount_returned = models.IntegerField(widget=widgets.RadioSelect()) amount_offered = models.IntegerField() def set_responder_identity(self): # self.current_opponent = self.constants.opponent_all[self.round_number-1] # self.current_opponent = Constants.opponent_all[self.round_number - 1] self.current_offer = (int(Constants.offer_choices[self.round_number - 1])) self.current_offer_mult_3 = (int(Constants.offer_choices[self.round_number - 1]) * 3) def set_payoffs(self): self.payout = ceil(3 * int(self.amount_offered) * Constants.opponent_dict[self.current_opponent][int(self.amount_offered)]) # self.offer_accepted = response(Constants.opponent_dict[self.current_opponent][int(self.amount_offered)]) # # if self.offer_accepted: # # self.player.payoff = Constants.endowment - self.amount_offered # self.payout = int(Constants.endowment) - int(self.amount_offered) # else: # # self.player.payoff = Constants.payoff_if_rejected # self.payout = int(Constants.payoff_if_rejected) class Player(BasePlayer): # slider_starter = random.sample(Constants.offer_choices, 1) final_payout = models.CurrencyField() trial_to_pay = models.IntegerField() amount_earned_on_priors_nonsocial = models.IntegerField() amount_earned_total = models.IntegerField() def set_payoffs(self): self.payoff = ceil(3 * int(self.group.amount_offered) * Constants.opponent_dict[self.group.current_opponent][int(self.group.amount_offered)]) # if self.offer_accepted: # self.payoff = int(Constants.endowment) - int(self.amount_offered) # # self.payout = Constants.endowment - self.amount_offered # else: # self.payoff = int(Constants.payoff_if_rejected) # # self.payout = Constants.payoff_if_rejected def round_to_pay(self): self.round_list = range(1, self.round_number + 1) self.payout_chosen_trial = shuffle(self.round_list) # self.participant.vars['payout_chosen_trial'] = self.payout_chosen_trial[0] self.participant.vars['payout_chosen_trial'] = random.choice(range(1, self.round_number + 1)) self.trial_to_pay = self.participant.vars['payout_chosen_trial'] self.participant.vars['payout_UG'] = self.group.in_round(self.trial_to_pay).payout self.amount_earned_on_priors_nonsocial = self.participant.vars['payout_UG'] # self.amount_earned_total = self.participant.vars['payout_UG'] + self.participant.vars[ # 'payout_social_priors'] def calculate_final_payout(self): self.final_payout = 0.125 * (self.participant.vars['payout_UG'] + self.participant.vars['risk_payout'] + self.participant.vars['payout_dictator'] + self.participant.vars['payout_social_priors']) # this is commented out because I'm not sure if I should pay them for both prior estimation sessions or just 1 # + self.participant.vars['payout_nonsocial_priors'] test_question_1 = models.IntegerField( choices=(5, 20, 35, 40, 45), widget=widgets.RadioSelectHorizontal() ) test_question_2 = models.IntegerField( choices=(5, 20, 35, 40, 45), widget=widgets.RadioSelectHorizontal() ) test_question_3 = models.IntegerField( choices=(5, 20, 35, 40, 45), widget=widgets.RadioSelectHorizontal() )