from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, Page, WaitPage ) cu = c doc = '' class Constants(BaseConstants): name_in_url = 'my_CPR_AM' players_per_group = 3 num_rounds = 1 group_endowment = c(30) multiplier = 2 a = 23 b = 0.25 p = 5 individual_endowment = c(10) class Subsession(BaseSubsession): pass def set_extractions(group): players = group.get_players() extractions = [p.extraction for p in players] print('extractions:', extractions) def set_payoffs(group): players = group.get_players() extractions = [p.extraction for p in players] votes = [p.vote for p in players] count=votes.count(True) if count<3: group.total_extraction = min(extractions)*3 print('group total extraction if no agreement:', group.total_extraction) group.decision= 'against' elif count ==3: group.total_extraction = sum(extractions) print('group total extraction if agreement:', group.total_extraction) group.decision= 'in favour of' group.individual_share = (Constants.multiplier * (Constants.group_endowment-group.total_extraction)/Constants.players_per_group) for p in players: if count < 3: p.payoff = (min(extractions)/(group.total_extraction+0.0001))*(Constants.a*3*group.total_extraction-(Constants.b*9*group.total_extraction**2))+(Constants.p*(3*Constants.individual_endowment-min(extractions)*3)) print('extractions after desagree', min(extractions)) else: p.payoff = (p.extraction/(group.total_extraction+0.0001))*(Constants.a*3*group.total_extraction-(Constants.b*9*group.total_extraction**2))+(Constants.p*(3*Constants.individual_endowment-p.extraction*3)) print('group extraction puissance 2 is:', group.total_extraction**2) print('opportunity cost is:', (Constants.p*(Constants.individual_endowment-p.extraction))) print('ratio x/X is:', p.extraction/(group.total_extraction+0.0001)) print('a*X is:', (Constants.a*group.total_extraction)) print('x is:', p.extraction) print('X is:', group.total_extraction) payoffs = [p.payoff for p in players] group.total_payoff = sum(payoffs) def set_hypothetical_payoffs(group): players = group.get_players() extractions = [p.extraction for p in players] group.total_extraction = sum(extractions) for p in players: p.hpayoff = (p.extraction/(group.total_extraction+0.0001))*(Constants.a*3*group.total_extraction- (Constants.b*9*group.total_extraction**2))+(Constants.p*(3*Constants.individual_endowment- p.extraction*3)) hpayoffs = [p.hpayoff for p in players] group.total_hpayoff = sum(hpayoffs) def set_real_extractions(group): players = group.get_players() extractions = [p.extraction for p in players] votes = [p.vote for p in players] count=votes.count(True) for p in players: if count<3: p.real_extraction = min(extractions) elif count ==3: p.real_extraction = p.extraction class Group(BaseGroup): individual_share = models.CurrencyField() decision = models.StringField() total_payoff = models.CurrencyField() total_extraction = models.FloatField() total_hpayoff = models.CurrencyField() set_extractions = set_extractions set_payoffs = set_payoffs set_hypothetical_payoffs = set_hypothetical_payoffs set_real_extractions = set_real_extractions class Player(BasePlayer): vote = models.BooleanField(choices=[[True, 'Yes'], [False, 'No']], label='Do you agree with the proposed extractions?') extraction = models.FloatField(label='', max=10, min=0) hpayoff = models.CurrencyField() real_extraction = models.FloatField()