oTree Forum >

BaseConstant matrix

#1 by HJK

Hi I am a newbie to the otree hub. I am trying to make a set of constants A = [a1, a2, a3, a4, a5] of which value corresponds to B = [b1, b2, b3, b4, b5]. All elements are integer or float. 

Constant = {
    a1:b1
    a2:b2
    a3:b3
    a4:b4
    a5:b5
}

How can I input this type of matrix in the constant section of my app? For Constants, I could only manipulate integer, float, currency, boolean, string, and list. I think the only option is to do with the list feature but it seems not working. 

I saw this code from public goods game with punishment. I am not sure how to input it on oTree studio.
         
punishment_schedule = {
        0: 0,
        1: 1,
        2: 2,
        3: 4,
        4: 6,
        5: 9,
        6: 12,
        7: 16,
        8: 20,
        9: 25,
        10: 30,
}

#2 by HJK (edited )

Here is a screenshoot of code and oTree studio interface.

#3 by HJK

Related to this question, I wonder how we can define such {form fields} like under Player class

punish_p1 = make_punishment_field(1)
punish_p2 = make_punishment_field(2)
punish_p3 = make_punishment_field(3)
punish_p4 = make_punishment_field(4)

All I can choose from the options are in the format of 

models.IntegerField
models.FloatField
models.CurrencyField
models.StringField

I am playing around with the experiment code that are similar to what I have in mind to implement. 
I have very limited understanding. Thanks in advance.

#4 by HJK (edited )

I attach screenshots for the second question as well. I am basically trying to implement the code below on oTree Studio(Online):  https://s3.amazonaws.com/otreehub/browsable_source/d7188187-cdba-4c61-ae3a-d7cc3d6fcde9/punishment/__init__.py

#5 by Chris_oTree

Instead of declaring a Constant, you can define a subsession function that returns that dict. Whenever you need that dict, call the function.

As for the second question, that is not available with otree studio. Just declare the fields explicitly.

#6 by HJK

Thanks Chris, I got the first issue resolved. But I am still trying to understand how to declare the fields explicitly on otree studio.

<Original source code (attached)>
def make_punishment_field(id_in_group):
    return models.IntegerField(
        min=0, max=C.MAX_PUNISHMENT, label="Punishment to player {}".format(id_in_group)
    )

class Player(BasePlayer):
    punish_p1 = make_punishment_field(1)
    punish_p2 = make_punishment_field(2)
    punish_p3 = make_punishment_field(3)
    punish_p4 = make_punishment_field(4)
    
<Code for otree?>
def make_punishment_field(group):
    return models.IntegerField(
        min=0, max=C.MAX_PUNISHMENT, label = "Punishment to player {}".format(id_in_group)
    )
    
if id_in_group == 1
return punish_p1
if id_in_group == 2
return punish_p2
if id_in_group == 3
return punish_p3
else
return punish_p4

I think if I can get this solved, I can get running the PG game with punishmnet. I appreciate help.

#7 by HJK

At this point, I am trying to replicate your PG game with Fehr and Gachter punishment code from featured public project on otree studio.

#8 by Chris_oTree

You don’t need the function. Instead define 4 integer fields.

Write a reply

Set forum username