from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants class Introduction(Page): form_model = 'player' form_fields = ['name', 'treatment'] def is_displayed(self): return self.round_number == 1 def vars_for_template(self): self.subsession.CalculateOptimal() #from scipy.stats import truncnorm #self.player.roundDemand = round(truncnorm.rvs((Constants.truncLower - Constants.mean) / Constants.sd, (Constants.truncHigher - Constants.mean) / Constants.sd, loc=Constants.mean, scale=Constants.sd, size=1)[0]) #if(self.player.roundDemand >= self.subsession.optimalOrder): # self.optimalProfit = self.optimalOrder*(Constants.price-Constants.costhigh) # else: # self.optimalProfit = (self.Demand*(Constants.price-Constants.costhigh))-((self.optimalOrder-self.Demand)*Constants.costhigh) # if(self.round_number>1): # self.optimalCumulativeProfit = self.in_round(self.round_number-1).optimalCumulativeProfit + self.optimalProfit # else: # self.optimalCumulativeProfit = self.optimalProfit; return dict() class Ordering(Page): form_model = 'player' form_fields = ['OrderQty'] def vars_for_template(self): from scipy.stats import truncnorm if self.round_number > 1: self.player.treatment = self.player.in_round(1).treatment self.subsession.optimalOrder = self.subsession.in_round(1).optimalOrder # calculate optimal profit and cumulative profit after generating demand for the round for the player self.player.roundDemand = round(truncnorm.rvs((Constants.truncLower - Constants.mean) / Constants.sd, (Constants.truncHigher - Constants.mean) / Constants.sd, loc=Constants.mean, scale=Constants.sd, size=1)[0]) if(self.player.roundDemand >= self.subsession.optimalOrder): self.player.optimalProfit = self.subsession.optimalOrder*(Constants.price-Constants.costhigh) else: self.player.optimalProfit = (self.player.roundDemand*(Constants.price-Constants.costhigh))-((self.subsession.optimalOrder-self.player.roundDemand)*Constants.costhigh) if(self.round_number>1): self.player.optimalCumulativeProfit = self.player.in_round(self.round_number-1).optimalCumulativeProfit + self.player.optimalProfit else: self.player.optimalCumulativeProfit = self.player.optimalProfit; # calculate cumulative profit for the previous rounds cumulativeProfits = [p.cumulativeProfit for p in self.player.in_previous_rounds()] cumulativeProfits.insert(0,0); optimalCumulativeProfits = [p.optimalCumulativeProfit for p in self.player.in_previous_rounds()] optimalCumulativeProfits.insert(0,0); for p in self.player.in_previous_rounds(): p.optimalCumulativeProfitInRound = optimalCumulativeProfits[p.round_number]; showCumulative = 0 if self.player.treatment >1 and self.subsession.round_number >= Constants.breakRound: showCumulative = 1 return dict(optimalCumulativeProfits = optimalCumulativeProfits, cumulativeProfits = cumulativeProfits, showCumulative = showCumulative) def before_next_page(self): if(self.player.roundDemand >= self.player.OrderQty): self.player.profit = self.player.OrderQty*(Constants.price-Constants.costhigh) else: self.player.profit = (self.player.roundDemand*(Constants.price-Constants.costhigh))-((self.player.OrderQty-self.player.roundDemand)*Constants.costhigh) if(self.round_number>1): self.player.cumulativeProfit = self.player.in_round(self.round_number-1).cumulativeProfit + self.player.profit; else: self.player.cumulativeProfit = self.player.profit; class Results(Page): form_model = 'player' class Feedback(Page): form_model = 'player' def is_displayed(self): showFeedback = False if self.player.treatment!=1 and self.round_number == Constants.breakRound: showFeedback = True return showFeedback ==True def vars_for_template(self): orders = [p.OrderQty for p in self.player.in_previous_rounds()] demands = [p.roundDemand for p in self.player.in_previous_rounds()] cumulativeProfits = [p.cumulativeProfit for p in self.player.in_previous_rounds()] cumulativeProfits.insert(0,0); optimalCumulativeProfits = [p.optimalCumulativeProfit for p in self.player.in_previous_rounds()] optimalCumulativeProfits.insert(0,0); return dict(demands = demands, orders = orders, optimalCumulativeProfits = optimalCumulativeProfits, cumulativeProfits = cumulativeProfits) class Reflection(Page): form_model = 'player' form_fields = ['rStrategy', 'rForecast', 'rChasingMean', 'rImprove', 'rFutureStrategy'] def is_displayed(self): showReflection = False if self.player.treatment == 3 and self.round_number == Constants.breakRound: showReflection = True return showReflection ==True class FinalPage(Page): form_model = 'player' def is_displayed(self): return self.round_number == Constants.num_rounds def vars_for_template(self): optimalCumulativeProfit = self.player.optimalCumulativeProfit highestProfit = 0; for p in self.subsession.get_players(): if p.cumulativeProfit >= highestProfit and p.treatment == self.player.treatment: highestProfit = p.cumulativeProfit return dict(highestProfit = highestProfit, optimalCumulativeProfit = optimalCumulativeProfit) page_sequence = [Introduction, Ordering, Results, Feedback, Reflection, FinalPage]