oTree Forum >

Randomize app sequence

#1 by emily255

Hi there,

I am a student and just started learning oTree. I want to design an experiment in which each player goes through all apps in the app sequence, but in a different and randomized order. Specifically:
SESSION_CONFIGS = [
    dict(
        name='Treatment 1',
        display_name='Treatment 1',
        app_sequence=['A', 'B', 'C', 'D', 'E'],
        num_demo_participants=6,
        show_up=5,
        dollar_convert=20,
        treatment=1,
    ),
I want to shuffle the order of apps 'C' and 'D' so that within the same session, some players would do 'C' first and then 'D', while others would do 'D' first and then 'C'.

Do you have any suggestions for me? Many thanks in advance!

Emily

#2 by Chris_oTree

This is not supported. I recommend to create 2 session configs and 2 sessions.

#3 by AndyFM

You can do it by duplicating app C and using the app_after_this_page function on the last page of app B conditional on a background variable X you set randomly to 0 or 1. 
Example:
On last page of B you put

 def app_after_this_page():
     if player.X == 0:
         return "C"
     else:
         return "D"

Your app sequence in the settings would be app_sequence=['A', 'B', 'C', 'D', 'C2', 'E']
Then respondents with X=0 will go from the last page of app B to C, while those with X=1 will be propelled to D.
Then you do the same thing on the last page of app D, sending those who already participated in C (X=0) directly to E.

It's a hassle but it's doable. I'm currently randomising the order of 4 apps..

#4 by emily255

Thanks for your reply. Is there any way I can randomize these two "sessions" to have both configs in one session?

Emily

#5 by Chris_oTree

If you need it to be 1 session, then the solution suggested by AndyFM works fine.

#6 by emily255

I see. Thank you both!

#7 by nicolas

Hello AndyFM, Did you manage to randomise the sequence of 4 apps? If so, how?

#8 by AndyFM

By doing exactly what I suggest above but expanded to four apps instead of two.
In synthesis:
- Compute the number of permutations of your apps
- Set a variable to a random number between 0 and than number -1
- Each app sequence corresponds to one of those numbers, so it's a matter of making sure that at the end of each app the right people are redirected towards the right app.

You will have to surely 1) duplicate some of those apps, 2) check whether you can reduce the number of branches by nesting some of those sequences into each other.

If someone can come up with something better than this, please share!

#9 by nicolas

Ok, thanks for the quick reply!
I have just created a new post to show my setup. 

If the configuration allows (e.g. number of questions or pages per application), a single application could be created. Python functions would then be used to control the questions displayed and the order in which they are displayed per participant, effectively packing all the applications into a single application.

#10 by chonghaopeng

hi everyone, I am a beginner in otree and trying to do the same setting about random app sequences (ACDE or ABCE).
now I have written the following code on the last page of the first app, and using participant.vars to store the random x because I want to use this value on the last page of app C too.

but it didn't work, the code neither print the x value nor leading the participants into different app, all of them just went into the app C


class EndOfA(WaitPage):
    def is_displayed(self):
        # only display in the final round
        return self.round_number == Constants.num_rounds
   
    @staticmethod
    def before_next_page(player, timeout_happened):
        # random number between 0 - 1
        x = random.random()
        player.participant.vars['random_x'] = x
        print("Random x value: {x}")
    
    @staticmethod
    def app_after_this_page(player, upcoming_apps):
        x = player.participant.vars.get('random_x', 0)
        if x > 0.5:
            return upcoming_apps[0]  # B
        else:
            return upcoming_apps[1]  # C
            
Do you have any suggestions? Many thanks in advance!

#11 by Daniel_Frey (edited )

I'm not sure if the functions 'before_next_page' and 'app_after_this_page' work on a WaitPage (if someone knows that's wrong, please correct me!).

Do you need this Page to be a WaitPage, or can it be a regular page?

If you need it, you can instead show another page after the WaitPage with a short timeout, so that the functions get triggered.

#12 by AndyFM

I'm not sure about the app_after_this_page in a wait page (might have time to test it later today), but one way to get around the problem at least for the before_next_page command is to use after_all_players_arrive function, which should in principle yield the same outcome: the function is executed after all participants reached the wait page rather than when they move out of it. You need to check whether this usage is feasible with your setup.

Write a reply

Set forum username