from otree.api import * import random as r doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'pluralistic' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 class Subsession(BaseSubsession): mode=models.IntegerField() n_ten=models.IntegerField() n_five=models.IntegerField() draw=models.IntegerField() class Group(BaseGroup): p1 =models.IntegerField(initial=0) p2 =models.IntegerField(initial=0) p3 =models.IntegerField(initial=0) class Player(BasePlayer): choice= models.IntegerField(choices=[[10, 'Option A. You get €10 and with a probability of 60% the €2 for the charity will be halved '], [5, 'Option B. You get €5 and the carity will receive €2']]) pay_1=models.IntegerField(initial=0) #player's payoff from first choice pay_2=models.IntegerField(initial=0) #player's payoff from second choice donation_1=models.FloatField(initial=0) #money given to the charity donation_2=models.FloatField(initial=0) #money given to the charity choice2= models.IntegerField(choices=[[10, 'Option A. You get €10 and with a probability of 60% the €2 for the charity will be halved '], [5, 'Option B. You get €5 and the carity will receive €2']]) pers_belief=models.IntegerField(choices=[[10, 'Option A. You get €10 and with a probability of 60% the €2 for the charity will be halved '], [5, 'Option B. You get €5 and the carity will receive €2']]) norm_belief=models.IntegerField(choices=[[10, 'Option A. You get €10 and with a probability of 60% the €2 for the charity will be halved '], [5, 'Option B. You get €5 and the carity will receive €2']]) #personal believes emp_belief=models.IntegerField(choices=[[105, 'One chose Option A and the other chose Option B'], [1010, 'They both chose Option A'], [55, 'They both chose Option B']]) #empirical expectations norm_belief=models.IntegerField(choices=[[105, 'One chose answered Option A and the other answered Option B'], [1010, 'They both answered Option A'], [55, 'They both answered Option B']]) #normative believes pay_belief_q1 =models.FloatField(initial=0) #payment for question 1 pay_belief_q2 =models.FloatField(initial=0) #payment for question 2 pay_2_final=models.FloatField(initial=0) #final payoff after punishment agree =models.FloatField(initial=-1) # = 1 if they chose the same choice2 and 0 otherwise final_payoff=models.FloatField(initial=0) #final payoff total_donation=models.FloatField(initial=0) #final total donatio pun_1=models.IntegerField(choices=[[0, 'Assign 0 points '], [1,'Assign 1 point']]) #first member punished (p2 for p1, p1 for p2, p2 for p3) pun_2=models.IntegerField(choices=[[0, 'Assign 0 points '], [1,'Assign 1 point']])#first member punished (p2 for p1, p1 for p2, p2 for p3) cost_pun=models.IntegerField(initial=0) #cost paid by the punisher red_pun=models.IntegerField(initial=0) #reduction for punished players change=models.IntegerField(initial=0) #=1 if choice2 is changed because of punishment choice2_old=models.IntegerField(initial=0) #old choice2 in case of change final_agree=models.IntegerField(initial=0)#final agreement burn =models.IntegerField(initial=0) # =1 if the first donation is reduced burn_2 =models.IntegerField(initial=0) # =1 if the second donation is reduced #second member punished (p3 for p1, p1 for p2, p2 for p3) #functions def creating_session(subsession: Subsession): session = subsession.session for p in subsession.get_players(): p.participant.vars['goods']=[]# id soggetti che scelgono 10 p.participant.vars['bads']=[]## id soggetti che scelgono 5 p.participant.vars['group_mat']=[]# matrice dei gruppi p.participant.vars['choices']=[] #other's choices in choice 2 print("hello") def store_choice(group: Group): #writes the choices of the members of the group as [p1.choice, p2.choice, p3.choice] p1=group.get_player_by_id(1) p2=group.get_player_by_id(2) p3=group.get_player_by_id(3) for p in group.get_players(): p.participant.vars['choices'].append(p1.choice2) p.participant.vars['choices'].append(p2.choice2) p.participant.vars['choices'].append(p3.choice2) print(p.participant.vars['choices']) def check_beliefs (group: Group): #check beliefs and compute payments p1=group.get_player_by_id(1) p2=group.get_player_by_id(2) p3=group.get_player_by_id(3) for p in group.get_players(): #check agreement on choice2 if (p1.choice2 == p2.choice2) and (p1.choice2 == p3.choice2): p.agree=1 else: p.agree=0 print("agree", p.agree) #check empirical beliefs if p.id_in_group==1: if p2.choice2 + p3.choice2 == 10: #both chose 5 if p.emp_belief == 55: p.pay_belief_q1 = 2 elif p2.choice2 + p3.choice2 == 15: #one chose 5 and the other 10 if p.emp_belief == 105: p.pay_belief_q1 = 2 elif p2.choice2 + p3.choice2 == 20: #both chose 10 if p.emp_belief == 1010: p.pay_belief_q1 = 2 if p.id_in_group==2: if p1.choice2 + p3.choice2 == 10: if p.emp_belief == 55: p.pay_belief_q1 = 2 elif p1.choice2 + p3.choice2 == 15: if p.emp_belief == 105: p.pay_belief_q1 = 2 elif p1.choice2 + p3.choice2 == 20: if p.emp_belief == 1010: p.pay_belief_q1 = 2 if p.id_in_group==3: if p2.choice2 + p3.choice2 == 10: if p.emp_belief == 55: p.pay_belief_q1 = 2 elif p2.choice2 + p3.choice2 == 15: if p.emp_belief == 105: p.pay_belief_q1 = 2 elif p2.choice2 + p3.choice2 == 20: if p.emp_belief == 1010: p.pay_belief_q1 = 2 #check normative beliefs if p.id_in_group==1: if p2.pers_belief + p3.pers_belief == 10: #both chose 5 if p.norm_belief == 55: p.pay_belief_q2 = 2 elif p2.pers_belief + p3.pers_belief == 15: #one chose 5 and the other 10 if p.norm_belief == 105: p.pay_belief_q2 = 2 elif p2.pers_belief + p3.pers_belief == 20: #both chose 10 if p.norm_belief == 1010: p.pay_belief_q2 = 2 if p.id_in_group==2: if p1.pers_belief + p3.pers_belief == 10: if p.norm_belief == 55: p.pay_belief_q2 = 2 elif p1.pers_belief + p3.pers_belief== 15: if p.norm_belief == 105: p.pay_belief_q2 = 2 elif p1.pers_belief + p3.pers_belief== 20: if p.norm_belief == 1010: p.pay_belief_q2 = 2 if p.id_in_group==3: if p2.choice2 + p3.choice2 == 10: if p.norm_belief== 55: p.pay_belief_q2 = 2 elif p2.choice2 + p3.choice2 == 15: if p.norm_belief == 105: p.pay_belief_q2 = 2 elif p2.choice2 + p3.choice2 == 20: if p.norm_belief== 1010: p.pay_belief_q2 = 2 print("pay empirical belief",p1.pay_belief_q1, p2.pay_belief_q1,p3.pay_belief_q1) print("pay normative belief",p1.pay_belief_q2, p2.pay_belief_q2,p3.pay_belief_q2) def compute_payoff (group: Group): #final payoff agreement p1=group.get_player_by_id(1) p2=group.get_player_by_id(2) p3=group.get_player_by_id(3) for p in group.get_players(): p.choice2_old=p.choice2 if p.agree==1: #agreement no punishment p.cost_pun =0; p.red_pun =0; if p.agree == 0: #no agreement punishment p.cost_pun = p.pun_1+p.pun_2 #cost if he punishes if p.id_in_group == 1: #p1 p.red_pun = 3*(p2.pun_1+p3.pun_1) if p.id_in_group == 2: #p2 p.red_pun = 3*(p1.pun_1+p3.pun_2) if p.id_in_group == 3:#p3 p.red_pun = 3*(p1.pun_2+p2.pun_2) if p.red_pun == 6: #change choice2 p.change=1 if p.choice2 == 10: p.choice2 = 5 else: p.choice2 = 10 p.pay_2=p.choice2 if p.choice2 == 5: p.donation_2 = 2 else: if p.subsession.draw <6: p.donation_2 = 2 else: p.donation_2 = 2*0.6 p.burn_2=1 for p in group.get_players(): if (p1.choice2 == p2.choice2) and (p1.choice2 == p3.choice2): #agree print("ID",p.id_in_group,"p1",p1.choice2,"p2",p2.choice2, "p3",p3.choice2, ) p.final_agree=1 p.final_payoff = p.pay_1+p.pay_2+p.pay_belief_q1+p.pay_belief_q2-p.cost_pun-p.red_pun p.total_donation = p.donation_1 + p.donation_2 else: p.final_agree=0 p.final_payoff =p.pay_1+p.pay_belief_q1+p.pay_belief_q2 p.total_donation =p.donation_1 # PAGES class Choice(Page): form_model = 'player' form_fields = ['choice'] def vars_for_template(player: Player): print("ruolo", player.id_in_group ) return dict(label=player.id_in_subsession) #creazione vettore buoni e cattivi def before_next_page (player,timeout_happened): for p in player.subsession.get_players(): if player.choice==10: p.participant.vars['goods'].append(player.id_in_subsession)# else: p.participant.vars['bads'].append(player.id_in_subsession) print(p.participant.vars['goods'],p.participant.vars['bads']) total=player.participant.vars['goods']+player.participant.vars['bads'] print("total",total) group_mat=[] lenght=int(len(total)/3) #creazione matrice dei gruppi da tre giocatori for i in range(0, len(total), 3): group_mat.append(total[i:i + 3]) print("group_mat", group_mat) choices=[] for p in player.subsession.get_players(): p.participant.vars['group_mat']=group_mat class WaitPage1(WaitPage): wait_for_all_groups = True #assegnazione ai gruppi def after_all_players_arrive(subsession: Subsession): subsession.draw=r.randint(1,10) print ("random", subsession.draw) for p in subsession.get_players(): subsession.set_group_matrix(p.participant.vars['group_mat']) print("gruppi",subsession.get_group_matrix()) #calcolo payoff p.pay_1=p.choice if p.choice == 5: p.donation_1 = 2 else: if p.subsession.draw <6: p.donation_1 = 2 else: p.donation_1 = 2*0.6 p.burn = 1 print("payoff", p.pay_1, "donation", p.donation_1) subsession.n_ten=[p.choice for p in subsession.get_players()].count(10) subsession.n_five=[p.choice for p in subsession.get_players()].count(5) print(subsession.n_ten,subsession.n_five) # calcolo della moda if subsession.n_ten>subsession.n_five: subsession.mode=10 elif subsession.n_ten