from otree.api import * import random import collections import math doc = """ Coin & Dice """ class C(BaseConstants): NAME_IN_URL = 'coinanddice' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 class Subsession(BaseSubsession): total_headsr = models.IntegerField(initial=1) total_tailsr = models.IntegerField(initial=1) total_1r = models.IntegerField(initial=1) total_2r = models.IntegerField(initial=1) total_3r = models.IntegerField(initial=1) total_4r = models.IntegerField(initial=1) total_5r = models.IntegerField(initial=1) total_6r = models.IntegerField(initial=1) total_headsp = models.FloatField(initial = math.log(0.5)) totalrate_1p = models.FloatField(initial = math.log(1/6)) totalrate_2p = models.FloatField(initial = math.log(1/6)) totalrate_3p = models.FloatField(initial = math.log(1/6)) totalrate_4p = models.FloatField(initial = math.log(1/6)) totalrate_5p = models.FloatField(initial = math.log(1/6)) totalrate_6p = models.FloatField(initial = math.log(1/6)) num_people = models.IntegerField(initial=1) class Group(BaseGroup): pass class Player(BasePlayer): treatment = models.IntegerField() coin_1 = models.IntegerField( choices =[[1,'表'],[0,'裏']] ) coin_2 = models.IntegerField( choices =[[1,'表'],[0,'裏']] ) coin_3 = models.IntegerField( choices =[[1,'表'],[0,'裏']] ) coin_4 = models.IntegerField( choices =[[1,'表'],[0,'裏']] ) coin_5 = models.IntegerField( choices =[[1,'表'],[0,'裏']] ) headsrate_p = models.IntegerField(min = 1, max = 100) trial_coin = models.IntegerField(min = 1, max = 100, blank =True) obsc_1 = models.IntegerField(blank =True) obsc_2 = models.IntegerField(blank =True) obsc_3 = models.IntegerField(blank =True) obsc_4 = models.IntegerField(blank =True) obsc_5 = models.IntegerField(blank =True) obsc_6 = models.IntegerField(blank =True) obsc_7 = models.IntegerField(blank =True) dice1 = models.IntegerField( choices = [1,2,3,4,5,6] ) dice2 = models.IntegerField( choices = [1,2,3,4,5,6] ) dice3 = models.IntegerField( choices = [1,2,3,4,5,6] ) dice4 = models.IntegerField( choices = [1,2,3,4,5,6] ) dice5 = models.IntegerField( choices = [1,2,3,4,5,6] ) dice1rate_p = models.IntegerField(min = 1, max = 100) dice2rate_p = models.IntegerField(min = 1, max = 100) dice3rate_p = models.IntegerField(min = 1, max = 100) dice4rate_p = models.IntegerField(min = 1, max = 100) dice5rate_p = models.IntegerField(min = 1, max = 100) dice6rate_p = models.IntegerField(min = 1, max = 100) trial_dice = models.IntegerField(min = 1, max = 100, blank =True) obsd_1 = models.IntegerField(blank =True) obsd_2 = models.IntegerField(blank =True) obsd_3 = models.IntegerField(blank =True) obsd_4 = models.IntegerField(blank =True) obsd_5 = models.IntegerField(blank =True) obsd_6 = models.IntegerField(blank =True) obsd_7 = models.IntegerField(blank =True) sex = models.IntegerField( choices =[[0,'男性'],[1,'女性'],[2,'回答しない']] ) age = models.IntegerField(min = 1, max = 100) #Function def creating_session(subsession): import itertools num_treat = itertools.cycle([1, 2, 3]) for player in subsession.get_players(): player.treatment = next(num_treat) # PAGES class Instruction(Page): pass class Instruction2(Page): pass ### class Instruction3_1(Page): @staticmethod def is_displayed(player): return player.treatment == 1 class Instruction3_2(Page): @staticmethod def is_displayed(player): return player.treatment == 2 class Instruction3_3(Page): @staticmethod def is_displayed(player): return player.treatment == 3 class Instruction4_1(Page): @staticmethod def is_displayed(player): return player.treatment == 1 class Instruction4_2(Page): @staticmethod def is_displayed(player): return player.treatment == 2 class Instruction4_3(Page): @staticmethod def is_displayed(player): return player.treatment == 3 class coin_report_1(Page): form_model = 'player' form_fields = ['coin_1','coin_2','coin_3','coin_4','coin_5','trial_coin','obsc_1','obsc_2','obsc_3','obsc_4','obsc_5','obsc_6','obsc_7'] @staticmethod def is_displayed(player): return player.treatment == 1 class coin_report_2(Page): form_model = 'player' form_fields = ['coin_1','coin_2','coin_3','coin_4','coin_5','trial_coin','obsc_1','obsc_2','obsc_3','obsc_4','obsc_5','obsc_6','obsc_7'] @staticmethod def is_displayed(player): return player.treatment == 2 class coin_report_3(Page): form_model = 'player' form_fields = ['coin_1','coin_2','coin_3','coin_4','coin_5','trial_coin','obsc_1','obsc_2','obsc_3','obsc_4','obsc_5','obsc_6','obsc_7'] @staticmethod def is_displayed(player): return player.treatment == 3 def vars_for_template(player): total_headsr = player.subsession.total_headsr total_tailsr = player.subsession.total_tailsr total_headsp = player.subsession.total_headsp sub_num_people = player.subsession.num_people return dict( total_headsr = total_headsr, total_tailsr = total_tailsr, total_headsp = total_headsp, sub_num_people = sub_num_people, ) ### class coin_prediction(Page): form_model = 'player' form_fields = ['headsrate_p'] ### class dice_report_1(Page): form_model = 'player' form_fields = ['dice1','dice2','dice3','dice4','dice5','trial_dice','obsd_1','obsd_2','obsd_3','obsd_4','obsd_5','obsd_6','obsd_7'] @staticmethod def is_displayed(player): return player.treatment == 1 class dice_report_2(Page): form_model = 'player' form_fields = ['dice1','dice2','dice3','dice4','dice5','trial_dice','obsd_1','obsd_2','obsd_3','obsd_4','obsd_5','obsd_6','obsd_7'] @staticmethod def is_displayed(player): return player.treatment == 2 class dice_report_3(Page): form_model = 'player' form_fields = ['dice1','dice2','dice3','dice4','dice5','trial_dice','obsd_1','obsd_2','obsd_3','obsd_4','obsd_5','obsd_6','obsd_7'] @staticmethod def is_displayed(player): return player.treatment == 3 def vars_for_template(player): total_1r = player.subsession.total_1r total_2r = player.subsession.total_2r total_3r = player.subsession.total_3r total_4r = player.subsession.total_4r total_5r = player.subsession.total_5r total_6r = player.subsession.total_6r totalrate_1p = player.subsession.totalrate_1p totalrate_2p = player.subsession.totalrate_2p totalrate_3p = player.subsession.totalrate_3p totalrate_4p = player.subsession.totalrate_4p totalrate_5p = player.subsession.totalrate_5p totalrate_6p = player.subsession.totalrate_6p sub_num_people = player.subsession.num_people return dict( total_1r = total_1r, total_2r = total_2r, total_3r = total_3r, total_4r = total_4r, total_5r = total_5r, total_6r = total_6r, totalrate_1p = totalrate_1p, totalrate_2p = totalrate_2p, totalrate_3p = totalrate_3p, totalrate_4p = totalrate_4p, totalrate_5p = totalrate_5p, totalrate_6p = totalrate_6p, sub_num_people = sub_num_people, ) ### class dice_prediction(Page): form_model = 'player' form_fields = ['dice1rate_p','dice2rate_p','dice3rate_p','dice4rate_p','dice5rate_p','dice6rate_p'] @staticmethod def error_message(players,values): print('value is', values) if values['dice1rate_p']+values['dice2rate_p']+values['dice3rate_p']+values['dice4rate_p']+values['dice5rate_p']+values['dice6rate_p'] != 100: return '合計で100%になるよう回答してください。' if values['dice1rate_p']*values['dice2rate_p']*values['dice3rate_p']*values['dice4rate_p']*values['dice5rate_p']*values['dice6rate_p'] == 0: return '予測は1%以上で回答してください。' class demogra(Page): form_model = 'player' form_fields = ['age','sex'] class Results_1(Page): @staticmethod def is_displayed(player): return player.treatment == 1 def vars_for_template(player): id_in_group = player.id_in_group treatment = player.treatment return dict( id_in_group = id_in_group, treatment = treatment ) def before_next_page(player: Player,timeout_happened): print("finish") class Results_2(Page): @staticmethod def is_displayed(player): return player.treatment == 2 def vars_for_template(player): id_in_group = player.id_in_group treatment = player.treatment return dict( id_in_group = id_in_group, treatment = treatment ) def before_next_page(player: Player,timeout_happened): print("finish") class Results_3(Page): @staticmethod def is_displayed(player): return player.treatment == 3 def vars_for_template(player): id_in_group = player.id_in_group treatment = player.treatment return dict( id_in_group = id_in_group, treatment = treatment ) def before_next_page(player: Player,timeout_happened): print("finish") #人数を追加 player.subsession.num_people += 1 #報告を追加 player.subsession.total_headsr += (player.coin_1 + player.coin_2 + player.coin_3 + player.coin_4 + player.coin_5) player.subsession.total_tailsr += (5-(player.coin_1 + player.coin_2 + player.coin_3 + player.coin_4 + player.coin_5)) ids = [player.dice1,player.dice2,player.dice3,player.dice4,player.dice5] c = collections.Counter(ids) player.subsession.total_1r += c[1] player.subsession.total_2r += c[2] player.subsession.total_3r += c[3] player.subsession.total_4r += c[4] player.subsession.total_5r += c[5] player.subsession.total_6r += c[6] #予測を追加 player.subsession.total_headsp += math.log(player.headsrate_p/100) player.subsession.totalrate_1p += math.log(player.dice1rate_p/100) player.subsession.totalrate_2p += math.log(player.dice2rate_p/100) player.subsession.totalrate_3p += math.log(player.dice3rate_p/100) player.subsession.totalrate_4p += math.log(player.dice4rate_p/100) player.subsession.totalrate_5p += math.log(player.dice5rate_p/100) player.subsession.totalrate_6p += math.log(player.dice6rate_p/100) page_sequence = [ Instruction, Instruction2, Instruction3_1,Instruction3_2,Instruction3_3, coin_report_1, coin_report_2, coin_report_3, coin_prediction, Instruction4_1,Instruction4_2,Instruction4_3, dice_report_1, dice_report_2, dice_report_3, dice_prediction, demogra, Results_1,Results_2,Results_3]