from otree.api import * c = cu doc = '\nThis is a modified one-shot "Prisoner\'s Dilemma". Two players are asked separately\nwhether they want to cooperate or defect. Their choices directly determine the\npayoffs.\nHowever every player goes through three rounds. One where he knows that he plays against another human, one where he knows that he plays against a computer and one where he does not know whether the oponent is human.\nbefore the decison is made the player can trasmit one sentence as a prime to his opponent. He than receives this prime from his oponent and only after this exchange both plyers decide simultanously their strategy.\n' class C(BaseConstants): NAME_IN_URL = 'prisoner' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 1 CHAT_GPT_PRIME_LIST = ('Let me surprise you.', 'I will produce a lot and hope you will allow me the higher profit.', 'To maximize our individual yields, it may be wise for one of us to grow a lot of wheat while the other grows little', 'I will plant a lot because I have a large farmland at my availability.', 'Consider the potential advantage of securing a high yield by growing a lot of wheat this year.', 'We should both produce little to get the fairest profit for both.') PAYOFF_HIGH = '10' PAYOFF_MEDIUM = '5' PAYOFF_LOW = '3' PAYOFF_ZERO = '1' G = () GPT_PRIMES = ('Consider the potential advantage of securing a high yield by growing a lot of wheat this year.', 'To maximize our individual yields, it may be wise for one of us to grow a lot of wheat while the other grows little') class Subsession(BaseSubsession): pass class Group(BaseGroup): has_played_human = models.BooleanField(initial=False) cuurrently_playing_human = models.BooleanField(initial=False) round_against_human = models.IntegerField(initial=2) def set_game_mode(group: Group): import random group.cuurrently_playing_human = None if (group.has_played_human): group.cuurrently_playing_human = False group.round_against_human = 1 return if not (group.has_played_human): group.cuurrently_playing_human = random.choice([True,False]) if (group.cuurrently_playing_human): group.has_played_human = True def set_results1(group: Group): for p in group.get_players(): if group.cuurrently_playing_human: opponent_cooperated = p.get_others_in_group()[0].has_cooperated1 else: if p.prime1 == C.CHAT_GPT_PRIME_LIST[0]: opponent_cooperated = False if p.prime1 == C.CHAT_GPT_PRIME_LIST[1]: opponent_cooperated = True if p.prime1 == C.CHAT_GPT_PRIME_LIST[2]: opponent_cooperated = False if p.prime1 == C.CHAT_GPT_PRIME_LIST[3]: opponent_cooperated = True if p.prime1 == C.CHAT_GPT_PRIME_LIST[4]: opponent_cooperated = True if p.prime1 == C.CHAT_GPT_PRIME_LIST[5]: opponent_cooperated = True if p.has_cooperated1 == opponent_cooperated and p.has_cooperated1 == True: p.result1 = 'You both cooperated' p.payoff1 = 3 if p.has_cooperated1 == opponent_cooperated and p.has_cooperated1 == False: p.result1 = 'You both defected' p.payoff1 = 5 if p.has_cooperated1 != opponent_cooperated and p.has_cooperated1 == False: p.result1 = 'You defected while your opponent cooperated' p.payoff1 = 10 if p.has_cooperated1 != opponent_cooperated and p.has_cooperated1 == True: p.result1 = 'You cooperated while your opponent defected' p.payoff1 = 1 def set_results2(group: Group): for p in group.get_players(): if group.cuurrently_playing_human: opponent_cooperated = p.get_others_in_group()[0].has_cooperated2 else: if p.prime2 == C.CHAT_GPT_PRIME_LIST[0]: opponent_cooperated = False if p.prime2 == C.CHAT_GPT_PRIME_LIST[1]: opponent_cooperated = True if p.prime2 == C.CHAT_GPT_PRIME_LIST[2]: opponent_cooperated = False if p.prime2 == C.CHAT_GPT_PRIME_LIST[3]: opponent_cooperated = True if p.prime2 == C.CHAT_GPT_PRIME_LIST[4]: opponent_cooperated = True if p.prime2 == C.CHAT_GPT_PRIME_LIST[5]: opponent_cooperated = True if p.has_cooperated1 == opponent_cooperated and p.has_cooperated2 == True: p.result2 = 'You both cooperated' p.payoff2 = 3 if p.has_cooperated1 == opponent_cooperated and p.has_cooperated2 == False: p.result2 = 'You both defected' p.payoff2 = 5 if p.has_cooperated1 != opponent_cooperated and p.has_cooperated2 == False: p.result2 = 'You defected while your opponent cooperated' p.payoff2 = 10 if p.has_cooperated1 != opponent_cooperated and p.has_cooperated2 == True: p.result2 = 'You cooperated while your opponent defected' p.payoff2 = 1 p.payoff_overall = p.payoff1 + p.payoff2 def set_opponent_prime1(group: Group): import random if group.cuurrently_playing_human: for p in group.get_players(): p.opponent_prime1 = other_player(p).prime1 else: for p in group.get_players(): selected_option = random.choice(C.GPT_PRIMES) p.opponent_prime1 = selected_option def set_opponent_prime2(group: Group): session = group.session subsession = group.subsession import random for group in subsession.get_groups(): if group.cuurrently_playing_human: for p in group.get_players(): p.opponent_prime2 = other_player(p).prime2 else: for p in group.get_players(): selected_option = random.choice(C.GPT_PRIMES) p.opponent_prime2 = selected_option class Player(BasePlayer): has_cooperated1 = models.BooleanField(choices=[[True, 'Cooperate'], [False, 'Defect']], doc='This player s decision', widget=widgets.RadioSelect) prime1 = models.StringField(choices=C.CHAT_GPT_PRIME_LIST, initial='No prime was set.', label='Please select your prime that gets communicated to your opponent ', widget=widgets.RadioSelect) achieved_payoff1 = models.IntegerField() is_ai_expert = models.BooleanField(label='Would you say you understand how artificial intelligence like ChatGPT works?', widget=widgets.RadioSelectHorizontal) opponent_prime1 = models.StringField() result1 = models.StringField() ai_expert = models.IntegerField(blank=True, choices=[[1, '1'], [2, '2'], [3, '3'], [4, '4'], [5, '5'], [6, '6'], [7, '7'], [0, 'None']], label='Would you say you understand how artificial intelligence like ChatGPT works? (None = Prefer not to say, 7 = Excellent)', widget=widgets.RadioSelectHorizontal) age = models.IntegerField(blank=True, label='Your age:', max=110, min=16) gender = models.StringField(blank=True, choices=[['m', 'm'], ['f', 'f'], ['d', 'd']], label='Your gender:') result2 = models.StringField() opponent_prime2 = models.StringField() achieved_payoff2 = models.IntegerField() control_answers_1 = models.StringField(label='If both farmers grow little wheat they both achive:', choices=['medium yield of 5', 'high yield of 10', 'low yield of 3'], widget=widgets.RadioSelect) control_answers_2 = models.StringField(label='If both farmers grow much wheat they both achive:',choices=['medium yield of 5', 'high yield of 10', 'low yield of 3'], widget=widgets.RadioSelect) control_answers_3 = models.StringField(label='If farmer A grows a lot of wheat and farmer B grows little wheat:',choices=['farmer A achieves a very low yield of 1 and farmer B achieves a high yield of 10', 'farmer A achieves a medium yield of 5 and farmer B achieves a low yield of 3', 'farmer A achieves a high yield of 10 and farmer B achieves a very low yield of 1'], widget=widgets.RadioSelect) prime2 = models.StringField(choices=C.CHAT_GPT_PRIME_LIST, initial='No prime was set.', label='Please select your prime that gets communicated to your opponent ',widget=widgets.RadioSelect) has_cooperated2 = models.BooleanField(choices=[[True, 'Cooperate'], [False, 'Defect']], widget=widgets.RadioSelect) belief_opponent_round = models.IntegerField(choices=[[1, '1'], [2, '2']], label='In which round did you play against a human?', widget=widgets.RadioSelect) payoff1 = models.IntegerField(initial=0) payoff2 = models.IntegerField(initial=0) payoff_overall = models.IntegerField(initial=0) my_belief1 = models.StringField(choices=[['a lot', 'a lot'], ['little', 'little']], label='I expect my opponent to grow') my_belief2 = models.StringField(choices=[['a lot', 'a lot'], ['little', 'little']], label='I expect my opponent to grow') def set_payoff(player: Player): payoff_matrix = { (False, True): C.PAYOFF_A, (True, True): C.PAYOFF_B, (False, False): C.PAYOFF_C, (True, False): C.PAYOFF_D, } other = other_player(player) player.payoff = payoff_matrix[(player.cooperate, other.cooperate)] def other_player(player: Player): group = player.group return player.get_others_in_group()[0] class Introduction(Page): form_model = 'player' class Explanation(Page): form_model = 'player' class ControlQuestions(Page): form_model = 'player' form_fields = ['control_answers_1', 'control_answers_2', 'control_answers_3'] @staticmethod def error_message(player: Player, values): solutions = dict(control_answers_1='medium yield of 5',control_answers_2='low yield of 3',control_answers_3='farmer A achieves a high yield of 10 and farmer B achieves a very low yield of 1') errors = {name: 'Wrong' for name in solutions if values[name] != solutions[name]} if errors: return "One or more answers were incorrect." class SetGameModeWaitPage(WaitPage): after_all_players_arrive = set_game_mode class InputPrime(Page): form_model = 'player' form_fields = ['prime1'] class InputWaitPage(WaitPage): after_all_players_arrive = set_opponent_prime1 class Decision(Page): form_model = 'player' form_fields = ['my_belief1', 'has_cooperated1'] class DecisionWaitPage(WaitPage): after_all_players_arrive = set_results1 class SetGameModeWaitPage2(WaitPage): after_all_players_arrive = set_game_mode class InputPrime2(Page): form_model = 'player' form_fields = ['prime2'] class InputWaitPage2(WaitPage): after_all_players_arrive = set_opponent_prime2 class Decision2(Page): form_model = 'player' form_fields = ['my_belief2', 'has_cooperated2'] class DecisionWaitPage2(WaitPage): after_all_players_arrive = set_results2 class Beliefs(Page): form_model = 'player' form_fields = ['belief_opponent_round'] class Demographic(Page): form_model = 'player' form_fields = ['age', 'gender', 'ai_expert'] class ResultWaitPage(WaitPage): wait_for_all_groups = True class Results(Page): form_model = 'player' class Goodby(Page): form_model = 'player' page_sequence = [Introduction, Explanation, ControlQuestions, SetGameModeWaitPage, InputPrime, InputWaitPage, Decision, DecisionWaitPage, SetGameModeWaitPage2, InputPrime2, InputWaitPage2, Decision2, DecisionWaitPage2, Beliefs, Demographic, ResultWaitPage, Results, Goodby]