from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random doc = """ This is a one-period public goods game with 3 players. """ class Constants(BaseConstants): name_in_url = 'public_goods' players_per_group = 4 num_rounds = 25 instructions_template = 'public_goods/Instructions.html' # """Amount allocated to each player""" STORAGE_COST_PER_UNIT = 0.5 BACKORDER_PENALTY_COST_PER_UNIT = 1 # We can play the full game since no actor is programmed to dump stock near end of game ##WEEKS_TO_PLAY = 36 QUEUE_DELAY_WEEKS = 2 INITIAL_STOCK = 12 INITIAL_COST = 0 INITIAL_CURRENT_ORDERS = 0 CUSTOMER_INITIAL_ORDERS = 4 CUSTOMER_SUBSEQUENT_ORDERS = 8 Demand=[4,4,4,4,8,8,8,8,8,8,8,8] TARGET_STOCK = 12 MAX_PLACED_ORDER=1000 class Subsession(BaseSubsession): def vars_for_admin_report(self): contributions = [p.contribution for p in self.get_players() if p.contribution != None] if contributions: return { 'avg_contribution': sum(contributions)/len(contributions), 'min_contribution': min(contributions), 'max_contribution': max(contributions), } else: return { 'avg_contribution': '(no data)', 'min_contribution': '(no data)', 'max_contribution': '(no data)', } class Group(BaseGroup): #SupplyChainActors = {"armin": 1, "ben": 2, "jan": 1} def calcPlayersStateVariables (self,p): player_id=p.id_in_group if player_id == 1: p.incoming_order = Constants.Demand[self.round_number - 3] + p.in_round(self.round_number - 1).backlog else: p.incoming_order = self.get_player_by_id(player_id-1).in_round(self.round_number - 3).placed_order + p.in_round(self.round_number - 1).backlog if player_id <4: p.received_shipment = self.get_player_by_id(player_id+1).in_round(self.round_number - 3).outgoing_shipment else: p.received_shipment = self.get_player_by_id(player_id).in_round( self.round_number - 2).placed_order inventory_level_begin = p.in_round(self.round_number - 1).inventory_level + p.received_shipment p.outgoing_shipment = min(p.incoming_order, inventory_level_begin) p.backlog = p.incoming_order - p.outgoing_shipment p.inventory_level = inventory_level_begin - p.outgoing_shipment if player_id<4: p.on_order_inventory = self.get_player_by_id(player_id).in_round( self.round_number - 1).on_order_inventory + self.get_player_by_id(player_id).in_round( self.round_number - 1).placed_order - p.received_shipment else: p.on_order_inventory = self.get_player_by_id(player_id).in_round( self.round_number - 2).placed_order p.currentCost=p.in_round(self.round_number-1).currentCost + p.inventory_level+ 2*p.backlog ##to show the real inventory to players on the screen p.effectiveInventory=p.inventory_level-p.backlog def calcStateVariables(self): ##nur player aus einer Grpoup berücksichtigen wichtig!!!! for p in self.get_players(): if self.round_number<=3: p.incoming_order = 4 p.inventory_level = 12 p.outgoing_shipment = 4 p.received_shipment = 4 p.on_order_inventory = 8 p.backlog=0 p.effectiveInventory=p.inventory_level-p.backlog if self.round_number==1: p.currentCost = 0 elif self.round_number==2: p.currentCost = 12 else: p.currentCost = 24 else: self.calcPlayersStateVariables(p) class Player(BasePlayer): placed_order = models.IntegerField(min=0, max = Constants.MAX_PLACED_ORDER, label ="How much will you order?") incoming_order = models.IntegerField() received_shipment = models.IntegerField() outgoing_shipment = models.IntegerField() inventory_level = models.IntegerField() on_order_inventory = models.IntegerField() currentCost = models.IntegerField() backlog=models.IntegerField() received_shipment_cumulated=models.IntegerField() placed_order_cumulated=models.IntegerField() effectiveInventory=models.IntegerField() # def calcStateVariables(self): # self.incoming_order = 5 # self.received_shipment = 12 # self.inventory_level = 3 # self.on_order_inventory = 0 # self.currentCost = 34 def role(self): if self.id_in_group == 1: return 'Retailer' if self.id_in_group == 2: return 'Wholesaler' if self.id_in_group == 3: return 'Distributor' if self.id_in_group == 4: return 'Factory'