实验组

为将参与人分配至不同的实验组,你可以使用 creating_session。举例来说:

def creating_session(subsession):
    import random
    for player in subsession.get_players():
        player.time_pressure = random.choice([True, False])
        print('set time_pressure to', player.time_pressure)

你也可以将实验组设为小组级别(将 BooleanField 加入 Group 并将上面的代码改为使用 get_groups()group.time_pressure)。

creating_session 会在你点击”create session”按钮后立刻执行,即使应用不在 app_sequence 的首位。

实验组与多轮次

如果你的游戏有很多轮,一位玩家可以在不同轮处于不同实验组,因为 creating_session 会在每一轮独立地执行。为了避免这种情况,将其设置在参与人上,而不是玩家上:

def creating_session(subsession):
    if subsession.round_number == 1:
        for player in subsession.get_players():
            participant = player.participant
            participant.time_pressure = random.choice([True, False])

平衡实验组

上面的代码对每个玩家都进行随机分配,故你最终可能得到一个不平衡的分组。为解决此问题,你可以使用 itertools.cycle:

def creating_session(subsession):
    import itertools
    pressures = itertools.cycle([True, False])
    for player in subsession.get_players():
        player.time_pressure = next(pressures)

选择特定实验组进行游戏

在实际实验中,你一般会随机将玩家分配到实验组。但是当你测试你的游戏时,常常需要显式指定某一实验组进行游戏。假定你在基于上面的例子开发游戏,并想向你的同学展示两个实验组。你可以创建2个除了某一参数外完全相同的session config(在oTree Studio中,添加一个”custom parameter”):

SESSION_CONFIGS = [
    dict(
        name='my_game_primed',
        app_sequence=['my_game'],
        num_demo_participants=1,
        time_pressure=True,
    ),
    dict(
        name='my_game_noprime',
        app_sequence=['my_game'],
        num_demo_participants=1,
        time_pressure=False,
    ),
]

然后在你的代码中,你可以通过下面的方法获当前会话的实验组:

session.config['time_pressure']

你甚至可以将此与随机化方法结合起来。你可以检查 if 'time_pressure' in session.config: ;如果是,那就使用它;如果否,就随机选择一个。

配置会话

你可以使你的会话可配置,这样你就可以在管理员界面调整游戏的参数。

_images/edit-config.png

举例来说,假定你有一个“num_apples”参数。通常的方法是将其定义在 C 中,如 C.NUM_APPLES。但是要使其可配置,你可以将其定义在你的session config中。例如:

dict(
    name='my_session_config',
    display_name='My Session Config',
    num_demo_participants=2,
    app_sequence=['my_app_1', 'my_app_2'],
    num_apples=10
),

当你在管理员界面创建会话的时候,就会有一个文本框可改变这一值。你也可以使用 'doc' 添加帮助信息:

dict(
    name='my_session_config',
    display_name='My Session Config',
    num_demo_participants=2,
    app_sequence=['my_app_1', 'my_app_2'],
    num_apples=10,
    doc="""
    Edit the 'num_apples' parameter to change the factor by which
    contributions to the group are multiplied.
    """
),

在你的代码中,可使用 session.config['num_apples']

注意:

  • 对于一个可配置的参数,它的值必须是数值,布尔类型或字符串。
  • 在管理员界面的“Demo”部分,会话是不可配置的。它仅在 “Sessions” 或 “Rooms” 中创建会话时可用。