oTree Forum >

Average of players' actions from the previous round

#1 by macrotl (edited )

I have a game with several rounds where participants have to predict the value of a variable. In the first round they read a constant and submit their guess. In the following round, I want to show them the average of all the guesses from all players in the first round, before they make their guess for round 2. I managed to calculate the average as a group variable, but cannot pass that information on a page where the form_filed is player. I also managed to pass the guess variable between rounds if stored in a participant variable but do not know how to calculate the average if stored in this way. Ideally, I would need to do this over several rounds.

Thanks a lot for your help!

class C(BaseConstants):
    NAME_IN_URL = 'take_one'
    PLAYERS_PER_GROUP = 2
    NUM_ROUNDS = 2
    START_VALUE = float(2.0)

class Subsession(BaseSubsession):
    pass

class Group(BaseGroup):
    average = models.FloatField()

class Player(BasePlayer):
    your_guess = models.FloatField(
        min=float(-100),
        max=float(100),
        label="What is your prediction for tomorrow?"
    )
    earning = models.FloatField()
    average = models.FloatField()


# FUNCTIONS
def set_payoffs(group: Group):
    players = group.get_players()
    group.average = sum([p.your_guess for p in players]) / 2
    for p in players:
        p.earning = round(100 / (1 + abs(p.your_guess - group.average)), 2)
        p.payoff = p.earning * 0.005

# PAGES
class Prediction_start(Page):
    form_model = 'player'
    form_fields = ['your_guess']

    @staticmethod
    def is_displayed(player: Player):
        return player.round_number == 1

    @staticmethod
    def before_next_page(player: Player, timeout_happened):
        player.participant.vars['your_guess'] = player.your_guess


class Prediction_next(Page):
    form_model = 'player'
    form_fields = ['your_guess']

    @staticmethod
    def is_displayed(player: Player):
        return player.round_number > 1

    @staticmethod
    def vars_for_template(player: Player):
        player.your_guess = player.participant.vars['your_guess']


class Wait(WaitPage):
    after_all_players_arrive = set_payoffs


class Results(Page):
    pass
    
page_sequence = [Prediction_start, Prediction_next, Wait, Results]

#2 by Newbie

Hello, I will start working on a similar problem shortly and wanted to ask if you managed to complete your task or if you are still facing any problems.

Write a reply

Set forum username