Heterogenous Groups
In the few previous sections, we learned how the groups work and we build a game where each individual had the identical role and saw identical screens.
In this section, we will focus on the case when the players have different roles. Natuarlly, games with heterogeneous agents have some unique aspects we need to handle:
We should be able to identify each player in group in order to give them role-specific tasks.
We should be able to show each player different screens (or entire pages) based on their role.
Identifying participants in group: id_in_group
oTree
comes with a very hand set of attributes and methods in order to deal with groups.
The first one is the player.id_in_group
. In a group each player gets an id, which is an integer that allows us to identify different participants in a group.
By setting the number of players in each group, each player will have the unique integer starting from 1
to players_per_group
. We can use id_in_group
to define the role of a player. For instance, if the players move sequentially, we can give the first move to the player which have the id_in_group
is equal to 1 in each group.
We can then get players by their id, using the function get_player_by_id(n)
. For instance if we’d like to add 10 points
to the payoff of the player which has the id 2
we can write:
second_player = group.get_player_by_id(2)
second_player.payoff += c(10)
Please note that get_player_by_id
returns the player object which we can use directly, while get_players()
function returns a list of player objects which we usually need to loop over.
Showing specific pages to specific players: is_displayed()
is_displayed
method in a page is a user-defined method to determine if the page is shown to a player or not. If the method returns True
for the player, the page will be shown to the player. For instance, if we would like to show a page to player with the id 2
we should define the page:
class PageForP2(Page):
# ... other things here ...
def is_displayed(self):
return self.player.id_in_group == 1
The method is_displayed
is not necessarily to be defined related with the players. It can be based on a treatment variable or the current round number. For instance:
class IntroTreatmentBaseline(Page):
# ... other things here ...
def is_displayed(self):
return self.session.config['treatment'] == "baseline" & self.round_number == 1
In the next section, we will build a Trust game using id_in_group
and is_displayed()