from pyexpat.errors import XML_ERROR_NO_MEMORY from sqlite3 import PARSE_DECLTYPES from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import numpy as np import json author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'MAIF_ASSO' players_per_group = None num_rounds = 1 associations = [ ("Ligue Contre le Cancer", "cette association caritative vise à soutenir la recherche médicale, l'information et la sensibilisation du public ainsi que la lutte pour le respect de la personne malade et de sa famille."), ("Restos du coeur", "cette association caritative offre une assistance bénévole aux personnes pauvres en favorisant leur accès à des repas gratuits, leur intégration sociale et économique, et en luttant contre la pauvreté."), ("Medecins sans frontières", "cette association caritative fournit une assistance médicale aux populations dont la vie ou la santé sont menacées en cas de conflits armés, d'épidémies, de catastrophes naturelles ou d'exclusion de soins."), ] y = 50 x = 50 pas_y = 2 pas_x = 2 y_values_vec = np.arange(0, y+pas_y , pas_y) y_values = y_values_vec.tolist() max_y_value = max(y_values_vec) min_y_value = min(y_values_vec) x_values_vec = np.arange(0, x+pas_x , pas_x) x_values = x_values_vec.tolist() max_x_value = max(x_values_vec) min_x_value = min(x_values_vec) class Subsession(BaseSubsession): def creating_session(self): # Randomization des pages cibles #---------------------------------------------------------- for p in self.get_players(): ini1 = ['normalization_x', 'normalization_y'] # Mélanger chaque sous-séquence random.shuffle(ini1) # Mélanger les sous-séquences mélangées merged = ini1 # Ajouter les pages non mélangées non_randomized_pages = ['new_step', 'p1_consigne', 'choix_asso', 'new_step2', 'p2_consigne_normalisation'] non_randomized_pages_fin = ['fin_maif_asso', 'consigne_générale_prev'] p.page_sequence = json.dumps(non_randomized_pages + merged + non_randomized_pages_fin) # randomisation de la liste des asso disponibles pour le choix #-------------------------------------------------------------------- for p in self.get_players(): randomised_associations = random.sample(Constants.associations, len(Constants.associations)) p.participant.vars['associations'] = randomised_associations # variables de thunes pour soi p.D_me = p.participant.vars['D_me'] p.y_me = p.participant.vars['y_me'] p.x_me = p.participant.vars['x_me'] p.y_outcome = p.participant.vars['y_outcome'] p.x_outcome = p.participant.vars['x_outcome'] for s in range(1, Constants.num_rounds + 1): p.in_round(s).y_me = p.participant.vars['y_me'] p.in_round(s).x_me = p.participant.vars['x_me'] p.in_round(s).y_outcome = p.participant.vars['y_outcome'] p.in_round(s).x_outcome = p.participant.vars['x_outcome'] p.in_round(s).D_me = p.participant.vars['D_me'] p.in_round(s).max_y_value = Constants.max_y_value p.in_round(s).min_y_value = Constants.min_y_value p.in_round(s).max_x_value = Constants.max_x_value p.in_round(s).min_x_value = Constants.min_x_value # for p in self.get_players(): # p.participant.vars['VEC_OTHER'] = [i for i in range(0, 31, 2)] class Group(BaseGroup): pass class Player(BasePlayer): page_sequence = models.StringField() ENDOWMENT = models.IntegerField() association_choice = models.StringField(blank=False) for i, x in enumerate(Constants.y_values, start=1): locals()[f'choice_y_{i}'] = models.StringField( choices=['A', 'B'], label=f"{x}€" ) del i, x for i, x in enumerate(Constants.x_values, start=1): locals()[f'choice_x_{i}'] = models.StringField( choices=['A', 'B'], label=f"{x}€" ) del i, x y_me = models.IntegerField() x_me = models.IntegerField() y_outcome = models.IntegerField() x_outcome = models.IntegerField() D_me = models.IntegerField() D_norm = models.FloatField() y_norm = models.FloatField() x_norm = models.FloatField() y_norm_computed = models.FloatField() normalisation_last = models.StringField() max_y_value = models.IntegerField() min_y_value = models.IntegerField() max_x_value = models.IntegerField() min_x_value = models.IntegerField() x_norm_asso = models.FloatField() y_norm_asso = models.FloatField() correction = models.IntegerField() def chosen_asso(self): self.participant.vars['association_choice'] = self.association_choice # Y NORMALISATION def get_choice_values_y(self): choice_values = [] for i, x in enumerate(Constants.y_values, start=1): choice_field_name = f'choice_y_{i}' player_choice = getattr(self, choice_field_name) if player_choice == 'A': choice_values.append(1000) elif player_choice == 'B': choice_values.append(x) return choice_values def min_choice_value_y(self): self.participant.vars['value_y_norm'] = min(self.get_choice_values_y()) self.normalisation_last = 'y_norm' if self.participant.vars['value_y_norm'] == 1000: self.y_norm = max(Constants.y_values) self.participant.vars['y_norm'] = max(Constants.y_values) else: self.y_norm = self.participant.vars['value_y_norm'] #- (Constants.pas_y/2) self.participant.vars['y_norm'] = self.y_norm # Méthode pour obtenir les valeurs de choix pour x_me def get_choice_values_x(self): choice_values_x = [] for i, x in enumerate(Constants.x_values, start=1): choice_field_name = f'choice_x_{i}' player_choice = getattr(self, choice_field_name) if player_choice == 'A': choice_values_x.append(1000) elif player_choice == 'B': choice_values_x.append(x) return choice_values_x # Méthode pour trouver la valeur minimale de choix pour x_me def min_choice_value_x(self): self.participant.vars['value_x_norm'] = min(self.get_choice_values_x()) self.normalisation_last = 'x_norm' if self.participant.vars['value_x_norm'] == 1000: self.x_norm = max(Constants.x_values) self.participant.vars['x_norm'] = max(Constants.x_values) else: self.x_norm = self.participant.vars['value_x_norm'] #- (Constants.pas_x/2) # Exley ne prend pas le midpoint (solution conservatrice qui permet de tester la robustesse de ses hypotheses) self.participant.vars['x_norm'] = self.x_norm def corrector_def(self): self.x_norm_asso = self.x_norm self.y_norm_asso = self.y_norm if self.x_norm_asso > self.y_norm_asso: self.x_norm = self.participant.vars['x_me'] self.y_norm = self.participant.vars['y_me'] self.participant.vars['x_norm'] = self.participant.vars['x_me'] self.participant.vars['y_norm'] = self.participant.vars['y_me'] elif self.x_norm_asso - self.y_norm_asso == 0: self.x_norm = self.participant.vars['x_me'] self.y_norm = self.participant.vars['y_me'] self.participant.vars['x_norm'] = self.participant.vars['x_me'] self.participant.vars['y_norm'] = self.participant.vars['y_me'] elif self.x_norm_asso == 0 or self.y_norm_asso == 0: self.x_norm = self.participant.vars['x_me'] self.y_norm = self.participant.vars['y_me'] self.participant.vars['x_norm'] = self.participant.vars['x_me'] self.participant.vars['y_norm'] = self.participant.vars['y_me'] #elif abs(self.x_norm_asso - self.y_norm_asso) < 5 and self.x_norm_asso - self.y_norm_asso !=0: # self.x_norm = self.participant.vars['x_me'] # self.y_norm = self.participant.vars['y_me'] # self.participant.vars['x_norm'] = self.participant.vars['x_me'] # self.participant.vars['y_norm'] = self.participant.vars['y_me'] if self.x_norm_asso != self.x_norm: self.correction = 1 self.participant.vars['correction'] = self.correction else : self.correction = 0 self.participant.vars['correction'] = self.correction def y_norm_computed_def(self): self.y_norm_computed = self.y_norm - self.x_norm self.D_norm = self.x_norm self.participant.vars['y_norm_computed'] = self.y_norm_computed self.participant.vars['D_norm'] = self.D_norm