oTree Forum >

Creating Manual groups of different size in a session

#1 by Arsil

Hi all,

I have two apps. The first app is individual level real effort task. After that, every body is pushed to second app. In the second app, I want to make groups manually based on the number of people that could be 12, 18, or 24. I want 4,8, and 12 to be role A (Leader) and 2,4, and 6 to be role B (Member) respectively. Later, I want to match 1, 2, and 3 randomly selected role A people respectively, based on total number of participants, each with randomly selected two role B in a group. In other words, there will be 1, 2, and 3 groups respectively with 3 people each. The remaining non-selected role A people will form individual group where they would be alone in the group. 

I do not know how to achieve it in the oTree. I started by removing players_per_group from BaseConstants class. Afterwards, I have the following code for now:

class Subsession(BaseSubsession):

    def creating_session(self):
        num_players = len(self.get_players())
        group_size = Constants.group_counts.get(num_players)

        # number of leaders based on number of players
        leaders_count = num_players // 6 * 4

        # roles to players
        leader_ids = random.sample(range(1, num_players + 1), leaders_count)
        for player in self.get_players():
            if player.id_in_subsession in leader_ids:
                player.role = 'Leader'
            else:
                player.role = 'Member'

        # groups' composition
        leaders = [player for player in self.get_players() if player.role == 'Leader']
        members = [player for player in self.get_players() if player.role == 'Member']

        groups = []

        # remaining leaders
        remaining_leaders = (num_players // 6) * (len(leaders) - 1)
        for i in range(remaining_leaders):
            leader = leaders.pop()
            groups.append([leader])

        # Creating groups for randomly selected leaders and members
        for i in range(group_size - remaining_leaders):
            group_leader = random.choice(leaders)  # Randomly select one leader
            leaders.remove(group_leader)  # Remove selected leader from the list

            # Randomly selected two members
            group_members = random.sample(members, min(len(members), 2))
            for member in group_members:
                members.remove(member)  # Remove selected member from the list

            groups.append([group_leader] + group_members)

        # set group players
        # Assuming groups is a list of Group objects or similar
        for group_players in groups:
            group = self.get_group()
            group.set_players(group_players)
            
          
        However, in the above code I am getting an error regarding get_group() as mentioned above in the second last line of code. Can anyone please suggest some similar code or help me resolving the issue for the desired outcome mentioned above in the setup. 
        
        Regards,
        Arslan

#2 by Chris_oTree

You should use set_group_matrix().

Write a reply

Set forum username