from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random import math doc = """ 2 firms complete in a market by setting prices for homogenous goods. See "Kruse, J. B., Rassenti, S., Reynolds, S. S., & Smith, V. L. (1994). Bertrand-Edgeworth competition in experimental markets. Econometrica: Journal of the Econometric Society, 343-371." """ class Constants(BaseConstants): players_per_group = 2 name_in_url = 'bertrand' num_rounds = 3 instructions_template = 'bertrand/instructions.html' maximum_price = 52 class Subsession(BaseSubsession): pass class Group(BaseGroup): winning_s_price = models.IntegerField() winning_h_price = models.IntegerField() def set_s_winner(self): players = self.get_players() self.winning_s_price = max([p.s_price for p in players]) s_winners = [p for p in players if p.s_price == self.winning_s_price] for p in players: if p in s_winners and len(s_winners) == 1: p.is_s_winner = True p.s_quantity = p.s_price elif p in s_winners and len(s_winners) == 2: p.is_s_winner = True p.s_quantity = math.ceil(p.s_price/2) else: p.s_quantity = 0 p.is_s_winner = False def set_payoffs(self): players = self.get_players() self.winning_h_price = min([p.h_price for p in players]) self.winning_s_price = max([p.s_price for p in players]) s_winners = [p for p in players if p.s_price == self.winning_s_price] h_winners = [p for p in players if p.h_price == self.winning_h_price] for p in players: if p in h_winners and p in s_winners and len(h_winners) == 1: p.is_h_winner = True p.h_quantity = min(52-p.h_price, p.s_quantity) p.payoff = (p.h_quantity * p.h_price) - (p.s_quantity * p.s_price) elif p in h_winners and p in s_winners and len(s_winners) == 2 and len(h_winners) == 2: p.is_h_winner = True p.h_quantity = min( math.ceil((52-p.h_price)/2), p.s_quantity) p.payoff = (p.h_quantity * p.h_price) - (p.s_quantity * p.s_price) else: p.is_h_winner = False p.payoff = - (p.s_quantity* p.s_price) class Player(BasePlayer): h_price = models.IntegerField( min=1, max=Constants.maximum_price, initial=53, doc="""販売価格""" ) s_price = models.IntegerField( min=1, max=Constants.maximum_price,initial=0, doc="""仕入価格""" ) is_s_winner = models.BooleanField() is_h_winner = models.BooleanField() s_quantity = models.IntegerField() h_quantity = models.IntegerField()