Group matching based on ethnic group

111 views
Skip to first unread message

Joseph Kimani

unread,
Apr 13, 2021, 4:17:48 AM4/13/21
to oTree help & discussion
I would like to implement group matching based on ethnic groups. I have two apps. 

1. In app A
  • The participants chooses their ethnic group from 3 options e.g A, B, C
2. in App B
  • In this app, 2 rounds of groupings should be done
  • In round 1, 2 participants from the same ethnic group should be matched together.
  • In round 2, 2 participants from different ethnic groups should be matched together.
How do I achieve this grouping based on one's ethnic group?

Thanks in advance.

Joseph Kimani

Senior Full Stack Developer, Experiments


joseph...@busaracenter.org   | +254 710778099

Daykio Plaza, 5th Floor | Ngong Lane, Nairobi 

www.busaracenter.org


[CONFIDENTIALITY AND PRIVACY NOTICE]
The information contained in this message is proprietary to the Busara Center for Behavioral Economics (Busara) and is considered confidential and intended for use only by the individual or entity to which it is addressed. If you are not the intended recipient or it appears that this mail has been forwarded to you without proper authority, you are notified that any use or dissemination of this information in any manner is strictly prohibited. In such cases, please delete this mail from your records. In addition, please be aware that any message addressed to Busara's domain is subject to archiving and review by persons other than the intended recipient.

Tommaso Batistoni

unread,
Apr 13, 2021, 7:22:31 AM4/13/21
to Joseph Kimani, oTree help & discussion
This should work (it is in the self format, as probably more familiar, and it assumes ethnicity stored as participant.vars['ethnicity']):

class Subsession(BaseSubsession):

def chunks(self, lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]

def set_groups(self):
ethnicities = Constants.ethnicity
size = 2
ps = self.get_players()

# Uncomment to shuffle players before matching
# from random import shuffle
# shuffle(ps)

# Set macro groups based on ethnicity
ethnic_groups = {k: [p for p in ps if p.participant.vars['ethnicity'] == k] for k in ethnicities}

if self.round_number == 1:
# Set pairs within each ethnic group
group_matrix = list(pair for ethnic_group in ethnic_groups.values() for pair in self.chunks(ethnic_group, size))

elif self.round_number == 2:
from itertools import zip_longest
# Set list of players with alternating ethnic groups (i.e. A, B, C, A, B, C...)
ethnic_mix = list(p for mix in zip_longest(*ethnic_groups.values()) for p in mix if p)
# Set pairs from list with alternating ethnic groups (i.g. [A, B], [C, A], [B, C]
group_matrix = list(self.chunks(ethnic_mix, size))

return self.set_group_matrix(group_matrix)

In pages:

class GroupingPage(WaitPage):
wait_for_all_groups = True
after_all_players_arrive = 'set_groups'

--
You received this message because you are subscribed to the Google Groups "oTree help & discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to otree+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/otree/CAFDbpJ%3DGLr7LUVF%3D-wY2q7FzXVa_F6depR2CDh2SDvHdVoBdDQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages