from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, ) import numpy as np import random author = 'Karthik' doc = """ Cry Wolf Game - Low Quality Chat No Instruction """ class Constants(BaseConstants): name_in_url = 'CryWolfLowQualityChatNoInstruction' players_per_group = None num_rounds = 50 # number of rounds for this game investment_instructions = 'CryWolfLowQualityChatNoInstruction/InvestmentInstructions.html' round_amount = 15.0 # amount provided for each round cost_amount = [3.5,3.75,5.625,7.5,7.75] # amount to deduct if Take the Cost loss_amount = 11.25 # amount to deduct if Take the Risk and they lose take_risk = 'Take the Risk' take_cost = 'Take the Cost' # good outcome and bad outcome payment list based on choice for investment task good_outcome = {1: 0.90, 2: 1.05, 3: 1.20, 4: 1.35, 5: 1.50, 6: 1.65, 7: 1.80, 8: 1.95, 9: 2.10, 10: 2.25} bad_outcome = {1: 0.90, 2: 0.80, 3: 0.70, 4: 0.60, 5: 0.50, 6: 0.40, 7: 0.30, 8: 0.20, 9: 0.10, 10: 0.00} # loss probability list to choose loss probability for each round given in order loss_probabilities_list=[round(random.random(),2) for i in range(50)] #loss_probabilities_list = [0.22, 0.29, 0.95, 0.84, 0.43, #0.81, 0.72, 0.29, 0.73, 0.49, #0.34, 0.48, 0.22, 0.56, 0.46, #0.41, 0.49, 0.52, 0.78, 0.27, #0.13, 0.92, 0.73, 0.74, 0.22, #0.43, 0.53, 0.21, 0.27, 0.49, #0.69, 0.61, 0.77, 0.77, 0.35, #0.57, 0.45, 0.78, 0.55, 0.81, #0.36, 0.27, 0.73, 0.15, 0.57, #0.75, 0.48, 0.34, 0.33, 0.82] # this is the reference list that the bot advice calculation will return based on the loss probability list # and loss tokens and cost tokens specified above for each round. # This is just for reference and not used any where in the application reference = ['Take the Risk', 'Take the Risk', 'Take the Cost', 'Take the Cost', 'Take the Risk', 'Take the Cost', 'Take the Cost', 'Take the Risk', 'Take the Cost', 'Take the Risk', 'Take the Risk', 'Take the Risk', 'Take the Risk', 'Take the Risk', 'Take the Risk', 'Take the Risk', 'Take the Risk', 'Take the Risk', 'Take the Cost', 'Take the Risk', 'Take the Risk', 'Take the Cost', 'Take the Cost', 'Take the Cost', 'Take the Risk', 'Take the Risk', 'Take the Risk', 'Take the Risk', 'Take the Risk', 'Take the Risk', 'Take the Cost', 'Take the Risk', 'Take the Cost', 'Take the Cost', 'Take the Risk', 'Take the Risk', 'Take the Risk', 'Take the Cost', 'Take the Risk', 'Take the Cost', 'Take the Risk', 'Take the Risk', 'Take the Cost', 'Take the Risk', 'Take the Risk', 'Take the Cost', 'Take the Risk', 'Take the Risk', 'Take the Risk', 'Take the Cost'] class Subsession(BaseSubsession): pass class Group(BaseGroup): loss_probability = models.FloatField(min=0, max=1) bot_advice = models.CharField() def calculate_advice(self): #if random.random()<0.5: # #else: advice=Constants.take_risk if (Constants.loss_amount * self.loss_probability) > np.mean(Constants.cost_amount): advice = Constants.take_cost return advice def calculate_random_advice(self): advice = random.choice([Constants.take_risk, Constants.take_cost]) return advice class Player(BasePlayer): decision_taken = models.CharField(widget=widgets.RadioSelect(), choices=['Take the Risk', 'Take the Cost']) random_number_generated = models.FloatField(min=0, max=1) amount_won_in_round = models.FloatField(min=Constants.round_amount-Constants.loss_amount, max=Constants.round_amount) paying_round = models.PositiveIntegerField(min=1, max=Constants.num_rounds) investment_chosen = models.PositiveIntegerField(widget=widgets.RadioSelectHorizontal(), choices=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) investment_outcome = models.CharField() investment_pay = models.FloatField(min=0.0, max=0.9) def set_payoffs(self): self.amount_won_in_round = Constants.round_amount if self.decision_taken == 'Take the Cost': self.amount_won_in_round = Constants.round_amount - random.choice(Constants.cost_amount) else: if self.random_number_generated <= self.group.loss_probability: self.amount_won_in_round = Constants.round_amount - Constants.loss_amount def total_payoffs(self): self.investment_pay = Constants.bad_outcome[self.investment_chosen] if self.investment_outcome == 'Good': self.investment_pay = Constants.good_outcome[self.investment_chosen] self.payoff = round(self.in_round(self.paying_round).amount_won_in_round + self.investment_pay)