Assign a random list to each group, and show only one element of the list per round

Skip to first unread message

Niccolò Zaccaria

May 20, 2022, 11:48:58 AM5/20/22
to oTree help & discussion
Hi All,

I am quite new to oTree, and I am having some issues to implement the following design. I have a set of predetermined lists,  and I would like to randomly assign one of these lists to each group, before the experiment starts. Once the list is assigned, it stays. These lists are 1x20, containing integers.

Then I would like to show to the players of the group one element per round of their assigned list. The element shown at each round would correspond to the current round of the experiment. For instance, at round 1 they would see the first element of the list, at round 2 the second element, and so on.

I have tried to load these lists in the constants class, and assign them to the groups via a function at the group level, but it does not work. Below there is the code. These lists are named "subj1, subj2, ..." and collected in "dataset". I also put them in the SESSION_FIELD, but I do not even know if it makes sense. 

I will be really glad for any kind of help or advice. Thank you. 

import random
import pandas as pd
import numpy as np
from otree.api import *

class C(BaseConstants):
    NAME_IN_URL = 'app'
    NUM_ROUNDS = 2
    guess_max = 30
    player0 = pd.read_stata(
    subj1 = player0.iloc[0].tolist()
    subj2 = player0.iloc[1].tolist()
    subj3 = player0.iloc[2].tolist()
    subj4 = player0.iloc[3].tolist()
    subj5 = player0.iloc[4].tolist()
    subj6 = player0.iloc[5].tolist()
    subj7 = player0.iloc[6].tolist()
    subj8 = player0.iloc[7].tolist()
    subj9 = player0.iloc[8].tolist()
    subj10 = player0.iloc[9].tolist()
    subj11 = player0.iloc[10].tolist()
    subj12 = player0.iloc[11].tolist()
    subj13 = player0.iloc[12].tolist()
    subj14 = player0.iloc[13].tolist()
    subj15 = player0.iloc[14].tolist()
    subj16 = player0.iloc[15].tolist()
    subj17 = player0.iloc[16].tolist()
    subj18 = player0.iloc[17].tolist()
    subj19 = player0.iloc[18].tolist()
    subj20 = player0.iloc[19].tolist()
    subj21 = player0.iloc[20].tolist()
    subj22 = player0.iloc[21].tolist()
    subj23 = player0.iloc[22].tolist()
    subj24 = player0.iloc[23].tolist()
    dataset = [subj1, subj2, subj3, subj4, subj5, subj6, subj7, subj8, subj9, subj10, subj11, subj12, subj13, subj14,
               subj15, subj16, subj17, subj18, subj19, subj20]

class Subsession(BaseSubsession):

class Group(BaseGroup):
    id_player0 = models.IntegerField()
    realized_avg_price = models.IntegerField()
    guess_player1 = models.IntegerField(
        min=0, max=C.guess_max, label="Please pick a number from 1 to 30:"
    guess_player2 = models.IntegerField(
        min=0, max=C.guess_max, label="Please pick a number from 1 to 30:"
    indexed = models.IntegerField()
    #index = random.randint(1,20)
    #index = models.IntegerField()
    #actual_player0 = C.dataset[index]

def assign_player0(group):
    group.indexed = random.randint(1, 24) #draws a random number that should #correspond to a list
    group.id_player0 = C.dataset[group.indexed] # retrieves the corresponding list
    group.realized_avg_price = group.id_player0[subsession.round_number]
    # this should just pick the element corresponding to the current round

Luis Avalos Trujillo

May 24, 2022, 5:10:08 PM5/24/22
to oTree help & discussion

I have had a similar problem in the past. Whenever you have data that needs to be accessed at the session level, you need to place it somewhere that can be seen throughout all pages. like in SESSION_CONFIGS.

Once you have the lists in SESSION_CONFIGS you can access it through player.session.config['participation_fee'] in the python files.

Another option, which I haven't used but might work is to initialize the lists in one of the first pages of your study by using the creating_session method.

Hope this can help you get a workaround.


Reply all
Reply to author
0 new messages