from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import numpy as np import time author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'EXLEY_ASPC' players_per_group = None num_rounds = 1 num_choices = 21 # variable à modifier en fonction ATTENTION !!!!!!!! class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): TREATMENT = models.StringField() ENDOWMENT = models.IntegerField() num_rounds = models.IntegerField(initial=Constants.num_rounds) num_choices = models.IntegerField(initial=Constants.num_choices) start_time = models.FloatField() temps_decision = models.FloatField() temps_regard_urne = models.LongStringField(blank=True) user_actions = models.LongStringField() association_choice = models.StringField() WTP_VEC = models.StringField() x1_outcome_vec_order = models.StringField() x2_outcome_vec_order = models.StringField() P1_vec_order = models.StringField() x1_me = models.FloatField() # 10€ x2_me = models.FloatField() # 0€ pas de calibration x_filler = models.FloatField() # 5€ x1_norm = models.FloatField() # calbration de 10€ x_filler_norm = models.FloatField() # calibration de 5€ x1_norm_after_correction = models.FloatField() # vraie valeur de x1_norm x2_norm = models.FloatField() # 0€ pas de calibration P1 = models.IntegerField() P2 = models.IntegerField() interval = models.FloatField() is_ascending = models.StringField() for j in range(1, Constants.num_choices + 1): locals()['choix' + str(j)] = models.StringField(choices=['A', 'B']) del j def ascending_or_descending_def(self): self.is_ascending = self.participant.vars['is_ascending'] # Calculer l'intervalle pour avoir exactement 21 nombres interval = self.participant.vars['x1_me'] / (Constants.num_choices - 1) # 20 intervalles pour 21 nombres self.interval = interval self.participant.vars['interval'] = self.interval self.participant.vars['WTP_values'] = [i * interval for i in range(Constants.num_choices)] if self.is_ascending == 'descending': self.participant.vars['WTP_values'] = self.participant.vars['WTP_values'][::-1] else: pass self.WTP_VEC = str(self.participant.vars['WTP_values']) def CREATE_LOTTERIES(self): self.participant.vars['x2_me_vec'] = [self.participant.vars['x2_me'] for _ in range(len(self.participant.vars['P1']))] self.participant.vars['x1_me_vec'] = [self.participant.vars['x1_me'] for _ in range(len(self.participant.vars['P1']))] pairs = list(zip(self.participant.vars['P1'], self.participant.vars['x1_me_vec'], self.participant.vars['x2_me_vec'])) # SI ON VEUT COLLER A EXLEY, IL NE FAUDRA PEUT ETRE PAS RANDOMISER L'APPARITION DES PROBA # Mélangez les paires random.shuffle(pairs) # Divisez les paires mélangées en deux listes séparées P1_order, x1_outcome_vec_order, x2_outcome_vec_order = zip(*pairs) # On record l'ordre des conditions dans la database self.P1_vec_order = str(P1_order) self.x1_outcome_vec_order = str(x1_outcome_vec_order) self.x2_outcome_vec_order = str(x2_outcome_vec_order) for s in range(1, Constants.num_rounds + 1): self.in_round(s).TREATMENT = "ASPC" self.in_round(s).ENDOWMENT = self.participant.vars['ENDOWMENT'] self.in_round(s).association_choice = self.participant.vars['association_choice'] self.in_round(s).x2_me = self.participant.vars['x2_me'] self.in_round(s).x1_me = self.participant.vars['x1_me'] self.in_round(s).x_filler = self.participant.vars['x_filler'] self.in_round(s).x_filler_norm = self.participant.vars['x_filler_norm'] self.in_round(s).x1_norm = self.participant.vars['x1_norm'] self.in_round(s).x1_norm_after_correction = self.participant.vars['x1_norm_after_correction'] self.in_round(s).x2_norm = self.participant.vars['x2_norm'] self.in_round(s).P1 = P1_order[s-1] self.in_round(s).P2 = (100*self.participant.vars['n']) - P1_order[s-1] self.in_round(s).WTP_VEC = str(self.participant.vars['WTP_values']) self.in_round(s).interval = self.participant.vars['interval'] self.in_round(s).is_ascending = self.participant.vars['is_ascending'] ######################################################################## # PAIEMENT ######################################################################## def paiement_def(self): if self.participant.vars['paiement_treatment'] == "ASPC": # Sélection aléatoire du round et de la ligne random_round = self.participant.vars['random_round'] random_row = self.participant.vars['random_row'] player_round = self.in_round(random_round) # Mise à jour des variables pour SELF self.participant.vars.update({ 'random_round': random_round, 'random_row': random_row, 'x1_self_selected': 0, 'x2_self_selected': 0, 'c_self_selected': self.participant.vars['WTP_values'][random_row - 1], 'px1_self_selected': 0, 'px2_self_selected': 0, 'pc_self_selected': 0, 'charity_selected': self.participant.vars['association_choice'], 'x1_charity_selected': player_round.x1_norm_after_correction, 'x2_charity_selected': 0, 'c_charity_selected': 0, 'px1_charity_selected': player_round.P1, 'px2_charity_selected': player_round.P2, 'pc_charity_selected': 100 }) # Traitement des choix pour le MPL sélectionné choix_vec = ['choix' + str(i) for i in range(1, Constants.num_choices + 1)] choix_selected = choix_vec[random_row - 1] choice = getattr(player_round, choix_selected, None) self.participant.vars['choice_selected'] = choice if choice == 'A': outcomes = [self.participant.vars['x1_charity_selected'], self.participant.vars['x2_charity_selected']] weights = [self.participant.vars['px1_charity_selected'], self.participant.vars['px2_charity_selected']] self.participant.vars['paiement_self_exley'] = 0 self.participant.vars['paiement_charity_exley'] = random.choices(outcomes, weights, k=1)[0] elif choice == 'B': outcomes = [self.participant.vars['c_self_selected']] * 2 weights = [self.participant.vars['pc_self_selected']] * 2 self.participant.vars['paiement_self_exley'] = self.participant.vars['c_self_selected'] self.participant.vars['paiement_charity_exley'] = 0 print(self.participant.vars['paiement_self_exley']) print(self.participant.vars['paiement_charity_exley']) print(self.participant.vars['random_round']) print(self.participant.vars['random_row']) print(weights) print(outcomes) if self.participant.vars['paiement_treatment_2'] == "ASPC": # Sélection aléatoire du round et de la ligne random_round_2 = self.participant.vars['random_round_2'] random_row_2 = self.participant.vars['random_row_2'] player_round_2 = self.in_round(random_round_2) # Mise à jour des variables pour SELF self.participant.vars.update({ 'random_round_2': random_round_2, 'random_row_2': random_row_2, 'x1_self_selected_2': 0, 'x2_self_selected_2': 0, 'c_self_selected_2': self.participant.vars['WTP_values'][random_row_2 - 1], 'px1_self_selected_2': 0, 'px2_self_selected_2': 0, 'pc_self_selected_2': 100, 'charity_selected_2': self.participant.vars['association_choice'], 'x1_charity_selected_2': player_round_2.x1_norm_after_correction, 'x2_charity_selected_2': 0, 'c_charity_selected_2': 0, 'px1_charity_selected_2': player_round_2.P1, 'px2_charity_selected_2': player_round_2.P2, 'pc_charity_selected_2': 100 }) # Traitement des choix pour le MPL sélectionné choix_vec_2 = ['choix' + str(i) for i in range(1, Constants.num_choices + 1)] choix_selected_2 = choix_vec_2[random_row_2 - 1] choice_2 = getattr(player_round_2, choix_selected_2, None) self.participant.vars['choice_selected_2'] = choice_2 if choice_2 == 'A': outcomes_2 = [self.participant.vars['x1_charity_selected_2'], self.participant.vars['x2_charity_selected_2']] weights_2 = [self.participant.vars['px1_charity_selected_2'], self.participant.vars['px2_charity_selected_2']] self.participant.vars['paiement_self_exley_2'] = 0 self.participant.vars['paiement_charity_exley_2'] = random.choices(outcomes_2, weights_2, k=1)[0] elif choice_2 == 'B': outcomes_2 = [self.participant.vars['c_self_selected_2']] * 2 weights_2 = [self.participant.vars['pc_self_selected_2']] * 2 self.participant.vars['paiement_self_exley_2'] = self.participant.vars['c_self_selected_2'] self.participant.vars['paiement_charity_exley_2'] = 0 print(self.participant.vars['paiement_self_exley_2']) print(self.participant.vars['paiement_charity_exley_2']) print(self.participant.vars['random_round_2']) print(self.participant.vars['random_row_2']) print(weights_2) print(outcomes_2) print('coucou')