from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random doc = """ In this Vickrey auction, 3 players bid for an object with private values. Each player can only submit one bid. See: Vickrey, William. "Counterspeculation, auctions, and competitive ' sealed tenders." The Journal of finance 16.1 (1961): 8-37. """ class Constants(BaseConstants): name_in_url = 'combinatorial_auction_vickrey' players_per_group = 3 num_rounds = 5 instructions_template = 'combinatorial_auction_vickrey/Instructions.html' endowment = c(200) class Subsession(BaseSubsession): def creating_session(self): for p in self.get_players(): p.private_value = 0 p.value_item = random.randint(0, 200) class Group(BaseGroup): # local_bids = models.CurrencyField() #global_bid = models.CurrencyField() def set_payoffs(self): value_1 = 200 value_2 = 80 value_3 = 90 players = self.get_players() player1 = self.get_player_by_id(1) player2 = self.get_player_by_id(2) player3 = self.get_player_by_id(3) # player1.value_item = random.randint(0, 200) # player2.value_item = random.randint(0, 100) # player3.value_item = random.randint(0, 100) local_bids = player2.bid_amount + player3.bid_amount global_bid = player1.bid_amount if local_bids < player1.bid_amount: player1.is_winner = True player1.payment = local_bids player2.payment = 0 player3.payment = 0 if local_bids >= player1.bid_amount: player2.is_winner = True player3.is_winner = True player1.payment = 0 player2.payment = max(0, player1.bid_amount - player3.bid_amount) player3.payment = max(0, player1.bid_amount - player2.bid_amount) for p in players: if p.is_winner: p.payoff = p.value_item - p.payment else: p.payoff = 0 for p in players: if player1.value_item > player2.value_item + player3.value_item and local_bids < player1.bid_amount: p. efficiency_rate = 100 elif player1.value_item >= player2.value_item + player3.value_item and local_bids > player1.bid_amount: p. efficiency_rate = 100 * (player2.value_item + player3.value_item) / player1.value_item elif player1.value_item <= player2.value_item + player3.value_item and local_bids < player1.bid_amount: p. efficiency_rate = 100 * player1.value_item / (player2.value_item + player3.value_item) else: p. efficiency_rate = 100 # if local_bids > player1.bid_amount: # player3.is_winner = True # for p in players: # p.payoff = Constants.endowment # if p.is_winner: # p.payoff += (p.private_value - 10) class Player(BasePlayer): def role(self): if self.id_in_group == 1: return 'global bidder' elif self.id_in_group == 2: return 'local bidder 1' else: return 'local bidder 2' value_item = models.CurrencyField() payment = models.CurrencyField() private_value = models.CurrencyField( doc="How much the player values the item, generated randomly" ) bid_amount = models.CurrencyField( min=0, max=Constants.endowment, doc="Amount bidded by the player" ) efficiency_rate = models.CurrencyField() is_winner = models.BooleanField( initial=False, doc="""Indicates whether the player is the winner""" )