oTree Forum >

Issue selecting random choice for groups and setting the payoff matrix

#1 by Vasu (edited )

Hi, 

I am designing a game (akin to risk elicitation with a choice list of 10 scenarios with two options; Option A is a fixed payoff while B is a payoff based on chance with the chance varying across the 10 scenarios). There are 2 players in a group and they make choices sequentially. Player 1 makes their choice for 10 scenarios, it is then displayed to player 2 post which they make their own set of choices. Till this point, the game is working fine. 

Now I need to implement payoffs by randomly selecting one of the choices for each group. Say scenario 2 is selected for the group and then based on the choices of players in the group, their payoff is determined. 

Here is the logic for payoff determination: if both players choose the first option (Option A), then they get a certain payoff while if either of them or both choose the second option (Option B), they get a probabilistic payoff based on a coin flip as the scenario suggests. 

I am facing an issue in my set_payoffs function and somehow the random choice is not getting assigned to the players and the group. I am new to the oTree platform so any help regarding this will be much appreciated. 

The following code snippet is where I suspect my issue is coming up!

class Group(BaseGroup):
    # Initialize payoff variables
    random_choice = models.IntegerField()
    selected_scenario = models.StringField()

    @staticmethod
    def set_payoffs(group):
        # Select a random choice index for the group
        random_choice = random.randint(0, 9) 
        probability = C.COIN_FLIP_PROBABILITIES[random_choice]
        # Now set this random choice in each player's participant vars
        for p in group.get_players():
            p.participant.vars[
                'random_choice'] = random_choice  # This ensures all players in the group get the same random choice

        players = group.get_players()
        choices = [getattr(p, f'c{random_choice}') for p in players]

        if choices == [0, 0]:
            payoff = Currency(50)  # Both chose option 0
        else:
            # Coin flip with the probability specific to the choice
            coin_flip = random.random() < probability
            if coin_flip:
                payoff = Currency(77)  # Higher payoff
            else:
                payoff = Currency(2)  # Lower payoff

        for p in players:
            p.payoff = payoff
            p.participant.vars['random_choice'] = random_choice  # Store for access in templates
            
            
class Results(Page):
    @staticmethod
    def vars_for_template(player):
        return {
            'selected_scenario': player.participant.vars['random_choice'],
            'payoff': player.payoff
        }
        

P.S. this is how I define coin_flip under BaseConstants: 
COIN_FLIP_PROBABILITIES = {
        1: 0.1,  # 10% chance of True for choice 1
        2: 0.2,  # 20% chance of True for choice 2
        3: 0.3,  # And so on...
        4: 0.4,
        5: 0.5,  # Fair coin for choice 5
        6: 0.6,
        7: 0.7,
        8: 0.8,
        9: 0.9,
        10: 1.0  # Always True for choice 10
    }

Write a reply

Set forum username