from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, Page, WaitPage ) cu = c doc = "CPR avec MA, règle d'approbation majorité, et allocation décidée par un dictateur choisi random parmi les membres du groupe" class Constants(BaseConstants): name_in_url = 'CPR_AM_Dictator_Majority' players_per_group = 3 num_rounds = 4 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<2: group.total_extraction = min(extractions)*3 print('group total extraction if no agreement:', group.total_extraction) group.decision= 'against' elif count >=2: group.total_extraction = sum(extractions) print('group total extraction if agreement:', group.total_extraction) group.decision= 'in favour of' for p in players: if count <2: 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<2: p.real_extraction = min(extractions) elif count >=2: p.real_extraction = p.extraction def set_dictator(group): import random number_list = [1, 2, 3] dictator = random.choice(number_list) group.dictator = dictator print('dictator is:', dictator) def set_payoff_dictator_extractions(group): total_dict_extraction = group.Dict_extraction_P1+group.Dict_extraction_P2+group.Dict_extraction_P3 print('Total Dict Extraction is:', total_dict_extraction) Dict_payoff_P1 = (group.Dict_extraction_P1/(total_dict_extraction+0.0001))*(Constants.a*3*total_dict_extraction- (Constants.b*9*total_dict_extraction**2))+(Constants.p*(3*Constants.individual_endowment- group.Dict_extraction_P1*3)) group.Dict_payoff_P1 = Dict_payoff_P1 print('Dict_payoff_P1:', Dict_payoff_P1) Dict_payoff_P2 = (group.Dict_extraction_P2/(total_dict_extraction+0.0001))*(Constants.a*3*total_dict_extraction- (Constants.b*9*total_dict_extraction**2))+(Constants.p*(3*Constants.individual_endowment- group.Dict_extraction_P2*3)) group.Dict_payoff_P2 = Dict_payoff_P2 print('Dict_payoff_P2:', Dict_payoff_P2) Dict_payoff_P3 = (group.Dict_extraction_P3/(total_dict_extraction+0.0001))*(Constants.a*3*total_dict_extraction- (Constants.b*9*total_dict_extraction**2))+(Constants.p*(3*Constants.individual_endowment- group.Dict_extraction_P3*3)) group.Dict_payoff_P3 = Dict_payoff_P3 print('Dict_payoff_P3:', Dict_payoff_P3) Total_Dict_Payoff = Dict_payoff_P1 + Dict_payoff_P2 + Dict_payoff_P3 group.Total_Dict_Payoff = Total_Dict_Payoff def Dict_extraction_P1_max(group): players = group.get_players() extractions = [p.extraction for p in players] return max(extractions) def Dict_extraction_P1_min(group): players = group.get_players() extractions = [p.extraction for p in players] return min(extractions) def Dict_extraction_P2_max(group): players = group.get_players() extractions = [p.extraction for p in players] return max(extractions) def Dict_extraction_P2_min(group): players = group.get_players() extractions = [p.extraction for p in players] return min(extractions) def Dict_extraction_P3_max(group): players = group.get_players() extractions = [p.extraction for p in players] return max(extractions) def Dict_extraction_P3_min(group): players = group.get_players() extractions = [p.extraction for p in players] return min(extractions) class Group(BaseGroup): decision = models.StringField() total_payoff = models.CurrencyField() total_extraction = models.FloatField() total_hpayoff = models.CurrencyField() dictator = models.IntegerField() Dict_extraction_P1 = models.IntegerField() Dict_extraction_P2 = models.IntegerField(max=10, min=0) Dict_extraction_P3 = models.IntegerField(max=10, min=0) Dict_payoff_P1 = models.CurrencyField() Dict_payoff_P2 = models.CurrencyField() Dict_payoff_P3 = models.CurrencyField() Total_Dict_Payoff = models.CurrencyField() set_extractions = set_extractions set_payoffs = set_payoffs set_hypothetical_payoffs = set_hypothetical_payoffs set_real_extractions = set_real_extractions set_dictator = set_dictator set_payoff_dictator_extractions = set_payoff_dictator_extractions Dict_extraction_P1_max = Dict_extraction_P1_max Dict_extraction_P1_min = Dict_extraction_P1_min Dict_extraction_P2_max = Dict_extraction_P2_max Dict_extraction_P2_min = Dict_extraction_P2_min Dict_extraction_P3_max = Dict_extraction_P3_max Dict_extraction_P3_min = Dict_extraction_P3_min class Player(BasePlayer): vote = models.BooleanField(choices=[[True, 'Yes'], [False, 'No']], label='Do you agree with the proposed extractions?') hpayoff = models.CurrencyField() real_extraction = models.FloatField() extraction = models.IntegerField(max=10, min=0)