from otree.api import * import csv import random as r doc = """ Entrainement """ class C(BaseConstants): NAME_IN_URL = 'training_triCV' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 WIN_HIGH = 45 WIN_LOW = 30 LOSE_LOW = -20 LOSE_HIGH = -60 CRITERIA = 0.5 WIN_ESTIMATION = 60 # Nature of the evidences and probabilities associates # INFO A = 80/20 # INFO B = 70/30 # INFO C = 60/40 # INFO D = 50/50 INFOS = [ ('Genre', ['Femme', 'Homme']), ('Diplome', ['Pertinent', 'Non pertinent']), ('Experience', ['Plus de deux ans', 'Moins de deux ans']), ('Age', ['Plus de cinquante ans', 'Moins de cinquante ans']) ] PATH = '_static/trainingTriCV/' #Dictionnaires contenant les informations sur les candidats et les probabilités associées def gain_estimations(self, proba_sub, proba_obj): gain = cu(0) float_proba_obj = float(proba_obj) pourcentage_proba_sub = proba_sub * 100 source = 'estimation' score = 0 if pourcentage_proba_sub < 50: loto = r.randint(0, 49) if loto < pourcentage_proba_sub: source = 'loterie' tirage = r.randint(0, 49) if tirage >= loto: gain = cu(C.WIN_ESTIMATION) elif float_proba_obj < 0.50: gain = cu(C.WIN_ESTIMATION) else: loto = r.randint(50, 100) if loto > pourcentage_proba_sub: source = 'loterie' tirage = r.randint(50, 100) if tirage <= loto: gain = cu(C.WIN_ESTIMATION) elif float_proba_obj >= 0.50: gain = cu(C.WIN_ESTIMATION) if (pourcentage_proba_sub >= 50 and float_proba_obj >= 0.50) or ( pourcentage_proba_sub < 50 and float_proba_obj < 0.50): score = 1 return gain, source, score def gain_choix(self, choix_sub, choix_obj): gain = 0 score = 0 float_choix_obj = float(choix_obj) if float_choix_obj == 3 or float_choix_obj == 4: if choix_sub == 1: gain = C.LOSE_HIGH elif choix_sub == 2: gain = C.LOSE_LOW elif choix_sub == 3: gain = C.WIN_LOW score = 1 elif choix_sub == 4: gain = C.WIN_HIGH score = 1 else: if choix_sub == 1: gain = C.WIN_HIGH score = 1 elif choix_sub == 2: gain = C.WIN_LOW score = 1 elif choix_sub == 3: gain = C.LOSE_LOW elif choix_sub == 4: gain = C.LOSE_HIGH return gain, score def dict_data(self): dict_data = dict() with open(C.PATH+'E_Candidats.csv', newline='') as csv_infos: DATA = csv.DictReader(csv_infos) for row in DATA: tuple_indices = (row['Indice 1'], row['Indice 2'], row['Indice 3'], row['Indice 4'], row['Occurrences']) dict_data_indices = {'p_indices' : row['P(indices)'], 'nb_indices' : row['Nb_indices'], 'p_bon_indices' : row['P(bon|indices)'], 'eg_rejet_high' : row['EG Rejeter High'], 'eg_rejet_low' : row['EG Rejeter Low'], 'eg_accept_low' : row['EG Accepter Low'], 'eg_accept_high' : row['EG Accepter High'], 'eg_max' : row['EG max'], 'p_indices*eg_max' : row['P(indices)*EG max']} dict_data[tuple_indices] = dict_data_indices.copy() return dict_data def dict_data4indices(self): dic_data_4indices = dict() with open(C.PATH+'E_Candidats4indices.csv', newline='') as csv_infos: DATA = csv.DictReader(csv_infos) for row in DATA: tuple_indices = (row['Indice 1'], row['Indice 2'], row['Indice 3'], row['Indice 4'], row['Occurrences']) dict_data_indices = {'p_indices' : row['P(indices)'], 'nb_indices' : row['Nb_indices'], 'p_bon_indices' : row['P(bon|indices)'], 'eg_rejet_high' : row['EG Rejeter High'], 'eg_rejet_low' : row['EG Rejeter Low'], 'eg_accept_low' : row['EG Accepter Low'], 'eg_accept_high' : row['EG Accepter High'], 'eg_max' : row['EG max'], 'p_indices*eg_max' : row['P(indices)*EG max']} dic_data_4indices[tuple_indices] = dict_data_indices.copy() return dic_data_4indices def get_population(self): dict4indices = self.dict_data4indices(C) dict = self.dict_data(C) nb_candidats = len(dict4indices) liste_candidats = r.sample(population=list(dict4indices.keys()), k=nb_candidats) pile_cv = [] pile_infos = [] infos = C.INFOS.copy() for candidat in liste_candidats: r.shuffle(infos) orga_infos = [] for info in infos: l = info[1].copy() r.shuffle(l) info_shuffled = [info[0], l] orga_infos.append(info_shuffled) pile_infos.append(orga_infos) pile_cv.append(self.get_candidat(C, candidat, dict, orga_infos)) return pile_cv, pile_infos #Prend en paramètres un candidat (liste) et un pool de candidats (dictionnaire) et renvoie une liste contenant le parcours de révélation d'un candidat indice par indice def get_candidat(self, candidat, dict, orga_infos): revelation_candidat = ['0', '0', '0', '0', '1'] ordre_indices = [1, 2, 3, 4] r.shuffle(ordre_indices) indice1 = self.get_indice(C, 1, ordre_indices, orga_infos, revelation_candidat, candidat, dict) indice2 = self.get_indice(C, 2, ordre_indices, orga_infos, indice1[2], candidat, dict) indice3 = self.get_indice(C, 3, ordre_indices, orga_infos, indice2[2], candidat, dict) indice4 = self.get_indice(C, 4, ordre_indices, orga_infos, indice3[2], candidat, dict) parcours_candidat = [indice1, indice2, indice3, indice4] return parcours_candidat # Prend en argument un indice (le numéro de l'indice souhaité) et rend l'indice associé et le candidat sachant l'indice donné def get_indice(self, num, ordre_indices, orga_infos, revelation_candidat_prev, candidat, dict): # Permet de définir quelle info sera donnée (Âge, Expérience, Bac, etc). # Si 1 alors Genre, Si 2 = Bac, Si 3 = Expérience, Si 4 = Age EDIT : Faux avec ordre aléatoire type_info = ordre_indices.index(num) # On met à jour l'état de connaissance sur le candidat selon l'info qui va être donnée revelation_candidat = revelation_candidat_prev.copy() revelation_candidat[type_info] = candidat[type_info] # On récupère le dictionnaire contenant les informations sur le candidat sachant les infos donnés candidat_ap_indice = dict[tuple(revelation_candidat)] # Indice à présenter #indice = '' # On définit l'indice selon la valeur (1 ou 2) et selon le type de l'info déterminé par l'emplacement sur le tuple key pour un candidat indice = orga_infos[type_info][0] + ' : ' + orga_infos[type_info][1][int(revelation_candidat[type_info])-1] # On renvoie : # - l'indice à afficher (string), # - les infos sur le candidat mises à jour (dictionnaire) # - l'état de dévoilement du candidat (liste) reponse = [indice, candidat_ap_indice, revelation_candidat, candidat] return reponse class Subsession(BaseSubsession): pass class Group(BaseGroup): pass def creating_session(subsession:Subsession): if subsession.round_number == 1: for p in subsession.get_players(): tup = C.get_population(C) p.participant.vars['pile_CV'] = tup[0] p.participant.vars['pile_infos'] = tup[1] class Player(BasePlayer): id_candidat = models.StringField() genre = models.StringField() diplome = models.StringField() experience = models.StringField() age = models.StringField() indice_1 = models.StringField() indice_2 = models.StringField() indice_3 = models.StringField() indice_4 = models.StringField() proba_sub_candidat_1 = models.FloatField( choices=[i for i in range(101)] ) proba_sub_candidat_2 = models.FloatField( choices=[i for i in range(101)] ) proba_sub_candidat_3 = models.FloatField( choices=[i for i in range(101)] ) proba_sub_candidat_4 = models.FloatField( choices=[i for i in range(101)] ) choix_sub = models.IntegerField( choices=[ [1, 'Rejeter -'], [2, 'Rejeter'], [3, 'Accepter'], [4, 'Accepter +'] ], widget=widgets.RadioSelectHorizontal ) proba_obj_candidat_1 = models.StringField() proba_obj_candidat_2 = models.StringField() proba_obj_candidat_3 = models.StringField() proba_obj_candidat_4 = models.StringField() choix_obj = models.IntegerField() score_1 = models.IntegerField() score_2 = models.IntegerField() score_3 = models.IntegerField() score_4 = models.IntegerField() score_choix = models.IntegerField() class Transition(Page): timeout_seconds = 1 class Indice1(Page): @staticmethod def vars_for_template(player): k = player.round_number - 1 candidat = player.participant.vars['pile_CV'][k] indice1 = candidat[0][0] orga_infos = player.participant.vars['pile_infos'][k] info_a = orga_infos[0][0] label_a1 = orga_infos[0][1][0] label_a2 = orga_infos[0][1][1] info_b = orga_infos[1][0] label_b1 = orga_infos[1][1][0] label_b2 = orga_infos[1][1][1] info_c = orga_infos[2][0] label_c1 = orga_infos[2][1][0] label_c2 = orga_infos[2][1][1] info_d = orga_infos[3][0] label_d1 = orga_infos[3][1][0] label_d2 = orga_infos[3][1][1] return dict( indice1=indice1, info_a=info_a, label_a1=label_a1, label_a2=label_a2, info_b=info_b, label_b1=label_b1, label_b2=label_b2, info_c=info_c, label_c1=label_c1, label_c2=label_c2, info_d=info_d, label_d1=label_d1, label_d2=label_d2, ) @staticmethod def js_vars(player): k = player.round_number - 1 candidat = player.participant.vars['pile_CV'][k] indice1 = candidat[0][0] candidat_ap_indice = candidat[0][1] proba_obj_candidat1 = candidat_ap_indice['p_bon_indices'] return dict( indice1=indice1, proba_obj_candidat1=proba_obj_candidat1, ) form_model = 'player' form_fields = ['proba_sub_candidat_1', 'indice_1', 'proba_obj_candidat_1'] @staticmethod def get_form_fields(player): return Indice1.form_fields @staticmethod def before_next_page(player: Player, timeout_happened): player.proba_sub_candidat_1 = player.proba_sub_candidat_1 / 100 gain = C.gain_estimations(C, player.proba_sub_candidat_1, player.proba_obj_candidat_1) player.score_1 = gain[2] class Indice2(Page): @staticmethod def vars_for_template(player): k = player.round_number - 1 derniere_proba = int(player.proba_sub_candidat_1 * 100) derniere_non_proba = 100 - derniere_proba candidat = player.participant.vars['pile_CV'][k] orga_infos = player.participant.vars['pile_infos'][k] info_a = orga_infos[0][0] label_a1 = orga_infos[0][1][0] label_a2 = orga_infos[0][1][1] info_b = orga_infos[1][0] label_b1 = orga_infos[1][1][0] label_b2 = orga_infos[1][1][1] info_c = orga_infos[2][0] label_c1 = orga_infos[2][1][0] label_c2 = orga_infos[2][1][1] info_d = orga_infos[3][0] label_d1 = orga_infos[3][1][0] label_d2 = orga_infos[3][1][1] indice1 = candidat[0][0] indice2 = candidat[1][0] return dict( indice1=indice1, indice2=indice2, derniere_proba=derniere_proba, derniere_non_proba=derniere_non_proba, info_a=info_a, label_a1=label_a1, label_a2=label_a2, info_b=info_b, label_b1=label_b1, label_b2=label_b2, info_c=info_c, label_c1=label_c1, label_c2=label_c2, info_d=info_d, label_d1=label_d1, label_d2=label_d2, ) @staticmethod def js_vars(player): k = player.round_number - 1 candidat = player.participant.vars['pile_CV'][k] indice2 = candidat[1][0] candidat_ap_indice = candidat[1][1] proba_obj_candidat2 = candidat_ap_indice['p_bon_indices'] return dict( indice2=indice2, proba_obj_candidat2=proba_obj_candidat2, ) form_model = 'player' form_fields = ['proba_sub_candidat_2', 'indice_2', 'proba_obj_candidat_2'] @staticmethod def get_form_fields(player): return Indice2.form_fields @staticmethod def before_next_page(player: Player, timeout_happened): player.proba_sub_candidat_2 = player.proba_sub_candidat_2 / 100 gain = C.gain_estimations(C, player.proba_sub_candidat_2, player.proba_obj_candidat_2) player.score_2 = gain[2] class Indice3(Page): @staticmethod def vars_for_template(player): k = player.round_number - 1 derniere_proba = int(player.proba_sub_candidat_2 * 100) derniere_non_proba = 100 - derniere_proba candidat = player.participant.vars['pile_CV'][k] orga_infos = player.participant.vars['pile_infos'][k] info_a = orga_infos[0][0] label_a1 = orga_infos[0][1][0] label_a2 = orga_infos[0][1][1] info_b = orga_infos[1][0] label_b1 = orga_infos[1][1][0] label_b2 = orga_infos[1][1][1] info_c = orga_infos[2][0] label_c1 = orga_infos[2][1][0] label_c2 = orga_infos[2][1][1] info_d = orga_infos[3][0] label_d1 = orga_infos[3][1][0] label_d2 = orga_infos[3][1][1] indice1 = candidat[0][0] indice2 = candidat[1][0] indice3 = candidat[2][0] return dict( indice1=indice1, indice2=indice2, indice3=indice3, derniere_proba=derniere_proba, derniere_non_proba=derniere_non_proba, info_a=info_a, label_a1=label_a1, label_a2=label_a2, info_b=info_b, label_b1=label_b1, label_b2=label_b2, info_c=info_c, label_c1=label_c1, label_c2=label_c2, info_d=info_d, label_d1=label_d1, label_d2=label_d2, ) @staticmethod def js_vars(player): k = player.round_number - 1 candidat = player.participant.vars['pile_CV'][k] indice3 = candidat[2][0] candidat_ap_indice = candidat[2][1] proba_obj_candidat3 = candidat_ap_indice['p_bon_indices'] return dict( indice3=indice3, proba_obj_candidat3=proba_obj_candidat3, ) form_model = 'player' form_fields = ['proba_sub_candidat_3', 'indice_3', 'proba_obj_candidat_3'] @staticmethod def get_form_fields(player): return Indice3.form_fields @staticmethod def before_next_page(player: Player, timeout_happened): player.proba_sub_candidat_3 = player.proba_sub_candidat_3 / 100 gain = C.gain_estimations(C, player.proba_sub_candidat_3, player.proba_obj_candidat_3) player.score_3 = gain[2] class Indice4(Page): @staticmethod def vars_for_template(player): k = player.round_number - 1 derniere_proba = int(player.proba_sub_candidat_3 * 100) derniere_non_proba = 100 - derniere_proba candidat = player.participant.vars['pile_CV'][k] orga_infos = player.participant.vars['pile_infos'][k] info_a = orga_infos[0][0] label_a1 = orga_infos[0][1][0] label_a2 = orga_infos[0][1][1] info_b = orga_infos[1][0] label_b1 = orga_infos[1][1][0] label_b2 = orga_infos[1][1][1] info_c = orga_infos[2][0] label_c1 = orga_infos[2][1][0] label_c2 = orga_infos[2][1][1] info_d = orga_infos[3][0] label_d1 = orga_infos[3][1][0] label_d2 = orga_infos[3][1][1] indice1 = candidat[0][0] indice2 = candidat[1][0] indice3 = candidat[2][0] indice4 = candidat[3][0] return dict( indice1=indice1, indice2=indice2, indice3=indice3, indice4=indice4, derniere_proba=derniere_proba, derniere_non_proba=derniere_non_proba, info_a=info_a, label_a1=label_a1, label_a2=label_a2, info_b=info_b, label_b1=label_b1, label_b2=label_b2, info_c=info_c, label_c1=label_c1, label_c2=label_c2, info_d=info_d, label_d1=label_d1, label_d2=label_d2, ) @staticmethod def js_vars(player): k = player.round_number - 1 candidat = player.participant.vars['pile_CV'][k] indice4 = candidat[3][0] candidat_ap_indice = candidat[3][1] proba_obj_candidat4 = candidat_ap_indice['p_bon_indices'] return dict( indice4=indice4, proba_obj_candidat4=proba_obj_candidat4, ) form_model = 'player' form_fields = ['proba_sub_candidat_4', 'indice_4', 'proba_obj_candidat_4'] @staticmethod def get_form_fields(player): return Indice4.form_fields @staticmethod def before_next_page(player: Player, timeout_happened): player.proba_sub_candidat_4 = player.proba_sub_candidat_4 / 100 gain = C.gain_estimations(C, player.proba_sub_candidat_4, player.proba_obj_candidat_4) player.score_4 = gain[2] class Choix(Page): form_model = 'player' form_fields = ['choix_sub', 'choix_obj'] @staticmethod def vars_for_template(player): derniere_proba = int(player.proba_sub_candidat_4 * 100) derniere_non_proba = 100 - derniere_proba k = player.round_number - 1 candidat = player.participant.vars['pile_CV'][k] orga_infos = player.participant.vars['pile_infos'][k] info_a = orga_infos[0][0] label_a1 = orga_infos[0][1][0] label_a2 = orga_infos[0][1][1] info_b = orga_infos[1][0] label_b1 = orga_infos[1][1][0] label_b2 = orga_infos[1][1][1] info_c = orga_infos[2][0] label_c1 = orga_infos[2][1][0] label_c2 = orga_infos[2][1][1] info_d = orga_infos[3][0] label_d1 = orga_infos[3][1][0] label_d2 = orga_infos[3][1][1] indice1 = candidat[0][0] indice2 = candidat[1][0] indice3 = candidat[2][0] indice4 = candidat[3][0] return dict( indice1=indice1, indice2=indice2, indice3=indice3, indice4=indice4, derniere_proba=derniere_proba, derniere_non_proba=derniere_non_proba, info_a=info_a, label_a1=label_a1, label_a2=label_a2, info_b=info_b, label_b1=label_b1, label_b2=label_b2, info_c=info_c, label_c1=label_c1, label_c2=label_c2, info_d=info_d, label_d1=label_d1, label_d2=label_d2, ) @staticmethod def js_vars(player): k = player.round_number - 1 candidat = player.participant.vars['pile_CV'][k] candidat_ap_indice = candidat[3][1] l = [float(candidat_ap_indice['eg_rejet_high']), float(candidat_ap_indice['eg_rejet_low']), float(candidat_ap_indice['eg_accept_low']), float(candidat_ap_indice['eg_accept_high']), ] choix_objectif = l.index(max(l)) + 1 # 'eg_rejet_high' : row['EG Rejeter High'], 'eg_rejet_low' : row['EG Rejeter Low'], 'eg_accept_low' : row['EG Accepter Low'], 'eg_accept_high' : row['EG Accepter High'], 'eg_max' : row['EG max'] return dict( choix_objectif=choix_objectif, ) @staticmethod def get_form_fields(player): return Choix.form_fields @staticmethod def before_next_page(player: Player, timeout_happened): gain = C.gain_choix(C, player.choix_sub, player.choix_obj) player.score_choix = gain[1] k = player.round_number - 1 candidat = player.participant.vars['pile_CV'][k] id_candidat = candidat[0][3] player.id_candidat = str(id_candidat[0:3]) player.genre = id_candidat[0] player.diplome = id_candidat[1] player.experience = id_candidat[2] player.age = id_candidat[3] player.id_candidat = player.genre + '-' + player.diplome + '-' + player.experience + '-' + player.age class Fin(Page): timeout_seconds = 10 timer_text = 'L\'expérience commence dans ' @staticmethod def is_displayed(player): return player.round_number == C.NUM_ROUNDS page_sequence = [ Transition, Indice1, Indice2, Indice3, Indice4, Choix, Fin ]