oTree Forum >

Creating 3 groups given a certain condition

#1 by Ben (edited )

Hi,

I would like to create 3 groups given the following condition: 
group 1 : only participant with a score above the mean
group 2: only participants with a score below the mean
group 3: both participants with a score above and below the mean

The "score" in question is determined by the answer from a survey on the previous app, and stored in a player.participant.vars

I looked on the different ressources, but I was not able to find a way to do it.

If anyone have a idea on how I should proceed it will help me a lot !

Thanks in advance for your answers

(I attached the code if it might help)


edit : I try that but i only get "Mix"

def creating_group(player: Player, timeout_happened):
    if player.roole == 'A' and (player.group.g1 < 4 or player.group.g3 < 2):
        if player.group.g3 < 2:
            if random.randint(1, 2) == 1:
                player.group.groupe = "Mix"
                player.group.g3 += 1
            else:
                player.group.groupe = "Pro"
                player.group.g1 += 1
        else:
            if player.group.g4 < 2:
                if random.randint(1, 2) == 1:
                    player.group.groupe = "Mix"
                    player.group.g4 += 1
                else:
                    player.group.groupe = "Against"
                    player.group.g2 += 1

#2 by Ben

And I manage to do it that way:

class Results(Page):
    @staticmethod
    def before_next_page(player: Player, timeout_happened):
        if player.rank <= player.group.med:
            if random.randint(1,2) == 1 and player.group.G3 < 2:
                player.roole = "C"
                player.group.G3 += 1
            elif player.group.G1 < 4:
                player.roole = "A"
                player.group.G1 += 1
            else:
                player.roole = "C"
                player.group.G3 += 1
        else:
            if random.randint(1, 2) == 1 and player.group.G4 < 2:
                player.roole = "C"
                player.group.G4 += 1
            elif player.group.G2 < 4:
                player.roole = "B"
            else:
                player.roole = "C"
                player.group.G3 += 1
        player.participant.vars['roole'] = player.roole

#3 by Daniel_Frey

Hi Ben

I'd suggest you set a group matrix after all players arrive at the WaitPage: https://otree.readthedocs.io/en/latest/multiplayer/groups.html#set-group-matrix

There you could do something like this:

def after_all_players_arrive(subsession)
    for p in subsession.get_players()
        *your if-statement*
        
        
Add each player-id to one of three lists: score_above_mean / mix / score_below_mean

and set the group matrix accordingly:

subsession.set_group_matrix([score_above_mean, mix, score_below_mean])

I have not tested this, but that should lead you to the right path :)

Best,
Daniel

#4 by Ben

Thank you Daniel, it works very well!

Write a reply

Set forum username