from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) author = 'YT' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'pd_coop' players_per_group = None num_rounds = 20 instructions_template = 'pd_coop/instructions.html' # profit if 1 player defects and the other cooperates""", betray_profit = 5 betrayed_profit = 0 # profit if both players cooperate or both defect both_cooperate_profit = 3 both_defect_profit = 1 class Subsession(BaseSubsession): def creating_session(self): group_matrix = [] players = self.get_players() ppg = self.session.config['players_per_group'] for i in range(0, len(players), ppg): group_matrix.append(players[i:i + ppg]) self.set_group_matrix(group_matrix) class Group(BaseGroup): def set_profits(self): # 利得行列の定義 profit_matrix = dict( パー = dict( パー = Constants.both_cooperate_profit, チョキ = Constants.betrayed_profit, ), チョキ = dict( パー = Constants.betray_profit, チョキ = Constants.both_defect_profit ), ) print('profit_matrix is', profit_matrix) # デバッグ用 # AI 行動の決定(協力) for p in self.get_players(): p.decision_AI = 'パー' print('decision is', p.decision) # デバッグ用 print('decision_AI is', p.decision_AI) # デバッグ用 p.profit = profit_matrix[p.decision][p.decision_AI] p.AI_profit = profit_matrix[p.decision_AI][p.decision] print('profit is', p.profit) # デバッグ用 print('AI_profit is', p.AI_profit) # デバッグ用 if self.round_number == 1: p.profit_accumulation = p.profit p.AI_profit_accumulation = p.AI_profit else: p.profit_accumulation = p.in_round(self.round_number - 1).profit_accumulation + p.profit p.AI_profit_accumulation = p.in_round(self.round_number - 1).AI_profit_accumulation + p.AI_profit print('profit_accumulation is', p.profit_accumulation) # デバッグ用 print('AI_profit_accumulation is', p.AI_profit_accumulation) # デバッグ用 # 0: 協力 # 1: 非協力 class Player(BasePlayer): decision = models.StringField( initial=0, choices=['パー', 'チョキ'], widget=widgets.RadioSelect, ) decision_AI = models.StringField( initial=0, choices=['パー', 'チョキ'], ) profit = models.IntegerField( initial=0, ) AI_profit = models.IntegerField( initial=0, ) profit_accumulation = models.IntegerField( initial=0, ) AI_profit_accumulation = models.IntegerField( initial=0, )