from otree.api import * c = cu doc = '' class Constants(BaseConstants): name_in_url = 'NewsvendorFI' players_per_group = None num_rounds = 3 trial_rounds = 0 minDemand = 50 maxDemand = 100 pricelow = 95 pricemiddle = 100 pricehigh = 105 costlow = 30 costhigh = 70 salvage = 0 realizedD = (89, 69, 63, 98, 72, 84, 65, 94, 52, 69, 62, 83, 59, 51, 96, 98, 54, 79, 71, 75, 87, 68, 56, 89, 75, 94, 67, 54, 86, 87, 94, 57, 81, 72, 88, 60, 100, 84, 62, 56) realizedP = (100, 100, 95, 90, 100, 100, 95, 95, 90, 95, 95, 90, 90, 95, 100, 100, 100, 90, 95, 90, 100, 90, 100, 100, 95, 90, 95, 95, 90, 100, 100, 90, 100, 100, 95, 95, 100, 100, 95, 90) def DemandInRound(subsession): session = subsession.session if subsession.Demand == -1: subsession.Demand = Constants.realizedD[subsession.round_number-1] def PriceInRound(subsession): session = subsession.session if subsession.Price == -1: subsession.Price = Constants.realizedP[subsession.round_number-1] class Subsession(BaseSubsession): Demand = models.IntegerField(initial=-1) Price = models.IntegerField(initial=-1) class Group(BaseGroup): pass class Player(BasePlayer): name = models.StringField(label='Your ID') OrderQty = models.IntegerField(label='Your order for the round:') roundPrice = models.IntegerField(initial=0) roundDemand = models.IntegerField(initial=0) roundSales = models.IntegerField(initial=0) roundOverOrder = models.IntegerField(initial=0) roundUnderOrder = models.IntegerField(initial=0) roundMaxProfit = models.FloatField(initial=0) roundOverageCost = models.FloatField(initial=0) roundUnderageCost = models.FloatField(initial=0) profit = models.FloatField(initial=0) cumulativeProfit = models.FloatField(initial=0) Treatment = models.StringField() class Preliminary_Instructions(Page): form_model = 'player' form_fields = ['name'] @staticmethod def is_displayed(player): return player.round_number==1 @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.vars['name']=player.name class Newsvendor_Instructions(Page): form_model = 'player' @staticmethod def is_displayed(player): return player.round_number==1 @staticmethod def vars_for_template(player): group = player.group if player.id_in_group % 2 == 1: treatment = 1 else: treatment = 2 cost = Constants.costlow return dict(treatment=treatment,cost=cost) class Newsvendor_Instructions_Continue(Page): form_model = 'player' @staticmethod def is_displayed(player): return player.round_number==1 @staticmethod def vars_for_template(player): group = player.group participant = player.participant if player.id_in_group % 2 == 1: treatment = 1 else: treatment = 2 cost = Constants.costlow participant.vars['Treatment'] = treatment return dict(treatment=treatment,cost=cost) class Ordering(Page): form_model = 'player' form_fields = ['OrderQty'] @staticmethod def vars_for_template(player): session = player.session subsession = player.subsession group = player.group if player.id_in_group % 2 == 1: treatment = 1 else: treatment = 2 cost = Constants.costlow PriceInRound(subsession) if player.id_in_group % 2 == 1: player.roundPrice = subsession.Price # 随机price else: player.roundPrice = Constants.pricemiddle # 确定price unit_underage_cost = player.roundPrice-cost player.Treatment = str(treatment) cumulativeProfits = [p.cumulativeProfit for p in player.in_previous_rounds()] cumulativeProfits.insert(0, 0) return dict(treatment=treatment, cost=cost, unit_underage_cost=unit_underage_cost) @staticmethod def before_next_page(player, timeout_happened): session = player.session subsession = player.subsession group = player.group participant = player.participant player.name = str(participant.vars['name']) PriceInRound(subsession) DemandInRound(subsession) player.roundDemand = subsession.Demand if player.id_in_group % 2 == 1: player.roundPrice = subsession.Price # 随机price else: player.roundPrice = Constants.pricemiddle # 确定price player.roundSales = min(player.subsession.Demand, player.OrderQty) cost = Constants.costlow player.roundMaxProfit = (player.roundPrice-cost) * player.roundDemand if player.roundDemand >= player.OrderQty: player.profit = player.OrderQty*(player.roundPrice-cost) player.roundUnderOrder = player.roundDemand-player.OrderQty player.roundUnderageCost = player.roundUnderOrder*(player.roundPrice-cost) else: player.profit = player.roundDemand*player.roundPrice-player.OrderQty*cost player.roundOverOrder = player.OrderQty - player.roundDemand player.roundOverageCost = player.roundOverOrder*cost if player.round_number <= Constants.trial_rounds: player.cumulativeProfit = 0 elif player.round_number == Constants.trial_rounds + 1: player.cumulativeProfit = player.profit else: player.cumulativeProfit = player.in_round(player.round_number-1).cumulativeProfit + player.profit class Results(Page): form_model = 'player' @staticmethod def vars_for_template(player): group = player.group if player.id_in_group % 2 == 1: treatment = 1 else: treatment = 2 cost = Constants.costlow unit_underage_cost = player.roundPrice-cost return dict(treatment=treatment, cost=cost, unit_underage_cost=unit_underage_cost) class FinalPage(Page): form_model = 'player' @staticmethod def is_displayed(player): return player.round_number == Constants.num_rounds @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.vars['cumulativeProfit'] = player.cumulativeProfit page_sequence = [Preliminary_Instructions, Newsvendor_Instructions, Newsvendor_Instructions_Continue, Ordering, Results, FinalPage]