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_auction2' players_per_group = 3 num_rounds = 1 instructions_template = 'combinatorial_auction2/Instructions.html' endowment = c(100) class Subsession(BaseSubsession): def creating_session(self): for p in self.get_players(): p.private_value = 100 #random.randint(0, Constants.endowment) class Group(BaseGroup): Local_bids = models.CurrencyField() Global_bid = models.CurrencyField() def set_payoffs(self): 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) Local_bids = player2.bid_amount + player3.bid_amount Global_bid = player1.bid_amount # bids = sorted([player1.bid_amount, player2.bid_amount, player3.bid_amount], reverse=True) #self.highest_bid = bids[0] #self.second_highest_bid = bids[1] #efficient_players = [ # p for p in players # if p.bid_amount == self.highest_bid # ] # if tie, winner is chosen at random #winner = random.choice(efficient_players) #for p in players: # if p in efficient_players: # p.is_winner = True if Local_bids < player1.bid_amount: player1.is_winner = True if Local_bids > player1.bid_amount: player2.is_winner = True if Local_bids < player1.bid_amount: player3.is_winner = True for p in players: p.payoff = 0 if p.is_winner: p.payoff += (p.private_value - self.second_highest_bid) class Player(BasePlayer): 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" ) bid_combination = models.CurrencyField( min=0, max=Constants.endowment, doc="Amount bidded by the player" ) is_winner = models.BooleanField( initial=False, doc="""Indicates whether the player is the winner""" )