oTree Forum >

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

#1 by NZaccaria

Hi All,

I already posted my question on the oTree group, but I will give it a shot here too. Thank you again. 

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. These lists correspond to the set of actions of other participants from a past experiment, hence the order of the elements actually corresponds to the rounds. Besides, I do not need only to show these values, but I need them to be stored since I have to use the numbers coming from these lists to compute the payoffs. 

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, which is clearly wrong but at least gives an idea of what I would like to do. 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.

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

Write a reply

Set forum username