from otree.api import * from statistics import mean author = 'Yushi Nagao' doc = """ 5人で行う共通価値オークション """ class C(BaseConstants): NAME_IN_URL = 'common_value_auction_5' PLAYERS_PER_GROUP = 5 NUM_ROUNDS = 2 min_allowable_bid = 0 max_allowable_bid = 100 class Subsession(BaseSubsession): pass # def set_estimated_value(self): # 各グループの事前調査の価格を設定 # import random # for p in self.get_players(): # p.estimated_value = random.randrange(C.min_allowable_bid, C.max_allowable_bid, 1) class Group(BaseGroup): true_value = models.IntegerField(initial=0) # 油田の本当の価格の初期設定。0にしておく highest_value = models.IntegerField() # 最も高い指値の保存場所 # def set_winner(self): # import random # self.highest_value = max([p.bid for p in self.get_players()]) # self.true_value = round(mean([p.estimated_value for p in self.get_players()])) # 油田の本当の価格の計算 # highest_player_box = [ # p for p in self.get_players() if p.bid == self.highest_value # ] # # print(self.true_value) # # highest_player = random.choice( # 最高価格を入札したグループが複数いた場合はランダムに勝者を決定 # highest_player_box # ) # # highest_player.is_winner = True # for p in self.get_players(): # 各プレイヤーの利得を計算 # p.set_payoff() # def set_estimated_value(self): # 各グループの事前調査の価格を設定 # import random # for p in self.get_players(): # p.estimated_value = random.randrange(C.min_allowable_bid, C.max_allowable_bid, 1) class Player(BasePlayer): estimated_value = models.IntegerField() # 事前調査の価格の保存場所 bid = models.IntegerField( # 入札価格 min=C.min_allowable_bid, max=C.max_allowable_bid, label = "入札額", ) bid_reason = models.LongStringField( label = "入札額の理由" ) forecast_value = models.IntegerField( # 予測価格 min=C.min_allowable_bid, label = "予測値", ) forecast_reason = models.LongStringField( label="予測値の理由" ) is_winner = models.BooleanField( initial=False, doc="""Indicates whether the player is the winner""" ) utility = models.IntegerField() def set_payoff(self): if self.is_winner: self.utility = self.group.true_value - self.bid else: self.utility = 0 # PAGES class Instruction(Page): def is_displayed(self): # インストラクションを表示させるのは最初のラウンドだけ return self.subsession.round_number == 1 class FirstWaitPage(WaitPage): @staticmethod def set_estimated_value(group): # 各グループの事前調査の価格を設定 import random for p in group.get_players(): p.estimated_value = random.randrange(C.min_allowable_bid, C.max_allowable_bid, 1) after_all_players_arrive = set_estimated_value class Game(Page): form_model = 'player' form_fields = ['bid','bid_reason','forecast_value','forecast_reason'] class ResultWaitPage(WaitPage): @staticmethod def set_winner(group): import random group.highest_value = max([p.bid for p in group.get_players()]) group.true_value = round(mean([p.estimated_value for p in group.get_players()])) # 油田の本当の価格の計算 highest_player_box = [ p for p in group.get_players() if p.bid == group.highest_value ] print(group.true_value) highest_player = random.choice( # 最高価格を入札したグループが複数いた場合はランダムに勝者を決定 highest_player_box ) highest_player.is_winner = True for p in group.get_players(): # 各プレイヤーの利得を計算 p.set_payoff() after_all_players_arrive = set_winner class Result(Page): timeout_seconds = 90 def vars_for_template(self): return dict(is_greedy=self.group.true_value - self.bid < 0) class End(Page): def is_displayed(self): return self.subsession.round_number == C.NUM_ROUNDS def vars_for_template(self): bids={} estimated_values={} forecast_values={} highest_values={} true_values={} utilities={} for i in range(2): bids[i+1]=(self.in_round(i+1).bid) estimated_values[i+1]=(self.in_round(i+1).estimated_value) forecast_values[i+1]=(self.in_round(i+1).forecast_value) highest_values[i+1]=(self.group.in_round(i+1).highest_value) true_values[i+1]=(self.group.in_round(i+1).true_value) utilities[i+1]=(self.in_round(i+1).utility) return dict( bids_1=bids[1], bids_2=bids[2], estimated_values_1=estimated_values[1], estimated_values_2=estimated_values[2], forecast_values_1 = forecast_values[1], forecast_values_2 = forecast_values[2], highest_values_1=highest_values[1], highest_values_2=highest_values[2], true_values_1=true_values[1], true_values_2=true_values[2], utilities_1=utilities[1], utilities_2=utilities[2], ) page_sequence = [Instruction, FirstWaitPage, Game, ResultWaitPage, Result, End]