Exemple #1
0
def find_parameters():
    # global param_key
    # get parameters
    if actr.current_model() == "MODEL1":
        param_key = ['ans', 'bll', 'lf']
    elif actr.current_model() == "MODEL2":
        param_key = ['ans', 'bll', 'lf', 'ga', 'mas']
    elif actr.current_model() == "MODEL3":
        param_key = ['alpha', 'egs', 'r1', 'r2', 'ppm']
    elif actr.current_model() == "MODEL4":
        param_key = ['alpha', 'egs', 'r1', 'r2', 'ppm', 'similarities']

    return param_key
Exemple #2
0
def mod_focus(*modifications):
    model = actr.current_model()

    if not (model):
        actr.print_warning('Mod-focus called with no current model.')
    else:
        module = goal_modules[model.lower()]

        if not (module):
            actr.print_warning(
                'No goal module found for model %s when trying to use mod-focus.'
                % model)
        else:
            chunk = actr.buffer_read('goal')
            module.lock.acquire()
            delayed = module.delayed
            module.lock.release()

            if chunk or delayed:
                actr.schedule_mod_buffer_chunk('goal',
                                               modifications,
                                               0,
                                               module='goal',
                                               priority=':max')
                if delayed:
                    return delayed
                else:
                    return chunk
            else:
                actr.print_warning(
                    'No chunk currently in the goal buffer and no pending goal-focus chunk to be modified.'
                )
Exemple #3
0
def grid_search_simulation():
    if actr.current_model()=='MODEL1':
        ans = [0.1, 0.25, 0.5, 0.75, 1.0, 1.5]
        bll = [.1, .3, .5, .7, .9]
        lf = [.1, .3, .5, .7, .9]
        hyper_param = [[i, j, k] for i in ans for j in bll for k in lf]
    elif actr.current_model() == 'MODEL2':
        ans = [0.1, 0.25, 0.5, 0.75, 1.0, 1.5]
        bll = [.1, .3, .5, .7, .9]
        lf = [.5, .7, .9, 1]
        mas = [2.8, 3.2, 3.6]
        ga = [0.5, 1.0, 1.5, 2.0]
        hyper_param = [[i, j, k, l, m] for i in ans for j in bll for k in lf for l in mas for m in ga]
    # global param_key
    param_key=find_parameters()

    for i in tqdm(range(2)):
        param_set = dict(zip(param_key, hyper_param[i]))
        line = simulations(**param_set)
        with open(os.getcwd()+"/simulation_data/"+actr.current_model()+datetime.now().strftime("%Y%m%d")+".txt", "a") as f:
            f.write(json.dumps(line)+'\n')
Exemple #4
0
def simulate(epoch,
             model,
             param_set=None,
             export=True,
             verbose=True,
             file_suffix="",
             HCPID=None):

    # whether to load stimuli order or create random stimuli
    trials = None
    if HCPID:
        trials = load_stimuli(HCPID)

    model_output = []
    for i in range(epoch):
        simulation_start = time.time()
        model_dat = experiment(model=model,
                               param_set=param_set,
                               reload=True,
                               stim_order=trials)  # reset

        model_dat["Epoch"] = i
        param_names = get_parameters_name()
        for param_name in param_names:
            model_dat[param_name] = get_parameter(param_name)

        model_output.append(model_dat)
        simulation_end = time.time()

        if export:
            fpath = './model_output/' + actr.current_model() + pd.to_datetime(
                'now').strftime('%Y%m%d') + file_suffix + ".csv"
            model_dat.to_csv(fpath,
                             mode='a',
                             header=not (os.path.exists(fpath)),
                             index=False)
            print(">> exported")

        if verbose:
            p.pprint(model_dat.head())
            print(">> running time", round(simulation_end - simulation_start,
                                           2))
    return model_output
Exemple #5
0
def do_feedback(feedback, window):
    """
    This  function allows the model to encode feedback
    :param feedback: "win" or "lose"
    :param window:
    :return:
    """

    actr.clear_exp_window(window)
    actr.add_text_to_exp_window(window, feedback, x=150, y=150)

    actr.run_full_time(5)

    # implement reward
    if actr.current_model() == "MODEL2":
        if feedback == "Reward":
            actr.trigger_reward(reward)
        elif feedback == "Punishment":
            actr.trigger_reward(-1.0 * reward)
Exemple #6
0
def set_parameters(**kwargs):
    """
    set parameter to current model
    :param kwargs: dict pair, indicating the parameter name and value (e.g. ans=0.1, r1=1, r2=-1)
    :return:
    """
    for key, value in kwargs.items():
        # set reward parameter
        if key=='r1':
            actr.spp('step3-1', ':reward', value)
        elif key=='r2':
            actr.spp('step3-2', ':reward', value)
        elif key=='similarities' and actr.current_model()=='MODEL3':
            # set-similarities (DO undecided 0.5) (PO undecided 0.5)
            actr.sdp('undecided', ":similarities", [['DO', value], ['PO', value]])
            actr.sdp('DO', ":similarities", [['undecided', value]])
            actr.sdp('PO', ":similarities", [['undecided', value]])
        # normal parameters
        else:
            actr.set_parameter_value(':' + key, value)
Exemple #7
0
def get_parameters_name():
    if actr.current_model() == "MODEL1":
        param_names = ['ans', 'bll', 'lf']
    elif actr.current_model() == "MODEL2":
        param_names = ['alpha', 'egs', 'r']
    return param_names
Exemple #8
0
def check_load(model="model1"):
    has_model = actr.current_model().lower() == model
    has_productions = actr.all_productions() != None
    return has_model & has_productions
Exemple #9
0
def make_move(dist):
    global next_move

    if (actr.current_model().lower() == current_player.lower()):
        next_move = dist
        actr.schedule_break_relative(0, priority=":min")
Exemple #10
0
def pick_button(index):
    global spaces, p1_position, p2_position, current_player

    # Make sure the right player made the action

    if actr.current_model().lower() == current_player.lower():

        if (current_player == p1) and (index > p1_position) and (
            (index - p1_position) <= 2):

            actr.remove_items_from_exp_window(window, spaces[p1_position],
                                              spaces[index])

            # old space is now white and blank

            spaces[p1_position] = actr.add_button_to_exp_window(
                window,
                x=(10 + (p1_position * 40)),
                y=10,
                action=['pick-button', p1_position],
                height=35,
                width=35,
                color='white')

            if index >= p2_position:

                # set the game over after 3 seconds
                actr.schedule_event_relative(3, 'set_game_over', params=[p1])

                # new space is green to indicate a win

                spaces[index] = actr.add_button_to_exp_window(
                    window,
                    text="1",
                    x=(10 + (index * 40)),
                    y=10,
                    action=['pick-button', index],
                    height=35,
                    width=35,
                    color='green')

            else:  # update the p1 position and make p2 the current player

                # p1 position is white

                spaces[index] = actr.add_button_to_exp_window(
                    window,
                    text="1",
                    x=(10 + (index * 40)),
                    y=10,
                    action=['pick-button', index],
                    height=35,
                    width=35,
                    color='white')

                # set p2 position to be blue

                actr.remove_items_from_exp_window(window, spaces[p2_position])

                spaces[p2_position] = actr.add_button_to_exp_window(
                    window,
                    text="2",
                    x=(10 + (p2_position * 40)),
                    y=10,
                    action=['pick-button', p2_position],
                    height=35,
                    width=35,
                    color='blue')

            # update position and player

            p1_position = index
            current_player = p2

        elif (current_player == p2) and (index < p2_position) and (
            (p2_position - index) <= 2):  # if p2 makes a valid move

            actr.remove_items_from_exp_window(window, spaces[p2_position],
                                              spaces[index])

            # old space is now white and blank

            spaces[p2_position] = actr.add_button_to_exp_window(
                window,
                x=(10 + (p2_position * 40)),
                y=10,
                action=['pick-button', p2_position],
                height=35,
                width=35,
                color='white')

            if index <= p1_position:

                # set the game over after 3 seconds
                actr.schedule_event_relative(3, 'set_game_over', params=[p2])

                # new space is green to indicate a win

                spaces[index] = actr.add_button_to_exp_window(
                    window,
                    text="2",
                    x=(10 + (index * 40)),
                    y=10,
                    action=['pick-button', index],
                    height=35,
                    width=35,
                    color='green')

            else:  # update the p2 position and make p2 the current player

                # p2 position is white

                spaces[index] = actr.add_button_to_exp_window(
                    window,
                    text="2",
                    x=(10 + (index * 40)),
                    y=10,
                    action=['pick-button', index],
                    height=35,
                    width=35,
                    color='white')

                # set p1 position to be red

                actr.remove_items_from_exp_window(window, spaces[p1_position])

                spaces[p1_position] = actr.add_button_to_exp_window(
                    window,
                    text="1",
                    x=(10 + (p1_position * 40)),
                    y=10,
                    action=['pick-button', p1_position],
                    height=35,
                    width=35,
                    color='red')

            # update position and player

            p2_position = index
            current_player = p1
Exemple #11
0
def pick_button(model, index):
    global spaces, p1_position, p2_position, current_player, p1_spaces, p2_spaces, game_over

    # Make sure the right player made the action

    if actr.current_model().lower() == current_player.lower():

        if (current_player == p1) and (index > p1_position) and (
            (index - p1_position) <= 2):  # valid move

            if index >= p2_position:

                # end the run after 3 seconds

                actr.schedule_break_relative(3)
                game_over = p1

                # remove the current position

                actr.remove_items_from_exp_window(p1_window,
                                                  p1_spaces[p1_position],
                                                  p1_spaces[index])
                actr.remove_items_from_exp_window(p2_window,
                                                  p2_spaces[5 - p1_position],
                                                  p2_spaces[5 - index])

                # add the blank old one and the new one marked green to indicate a win
                # don't need to store them or provide the action function because game is over

                actr.add_button_to_exp_window(p1_window,
                                              x=(10 + (p1_position * 40)),
                                              y=10,
                                              height=35,
                                              width=35,
                                              color='white')
                actr.add_button_to_exp_window(p1_window,
                                              text="1",
                                              x=(10 + (index * 40)),
                                              y=10,
                                              height=35,
                                              width=35,
                                              color='green')

                # mirror image for p2 window
                actr.add_button_to_exp_window(p2_window,
                                              x=(10 +
                                                 ((5 - p1_position) * 40)),
                                              y=10,
                                              height=35,
                                              width=35,
                                              color='white')
                actr.add_button_to_exp_window(p2_window,
                                              text="2",
                                              x=(10 + ((5 - index) * 40)),
                                              y=10,
                                              height=35,
                                              width=35,
                                              color='green')

            else:  # update the p1 position and make p2 the current player

                actr.remove_items_from_exp_window(p1_window,
                                                  p1_spaces[p1_position],
                                                  p1_spaces[index])
                actr.remove_items_from_exp_window(p2_window,
                                                  p2_spaces[5 - p1_position],
                                                  p2_spaces[5 - index],
                                                  p2_spaces[5 - p2_position])

                # No action needed for spaces that can't be used again

                p1_spaces[p1_position] = actr.add_button_to_exp_window(
                    p1_window,
                    x=(10 + (p1_position * 40)),
                    y=10,
                    height=35,
                    width=35,
                    color='white')
                p1_spaces[index] = actr.add_button_to_exp_window(
                    p1_window,
                    text="1",
                    x=(10 + (index * 40)),
                    y=10,
                    height=35,
                    width=35,
                    color='white')

                p2_spaces[5 - p2_position] = actr.add_button_to_exp_window(
                    p2_window,
                    text="1",
                    x=(10 + ((5 - p2_position) * 40)),
                    y=10,
                    height=35,
                    width=35,
                    color='red')
                p2_spaces[5 - p1_position] = actr.add_button_to_exp_window(
                    p2_window,
                    x=(10 + ((5 - p1_position) * 40)),
                    y=10,
                    action=['pick-button', p2, p1_position],
                    height=35,
                    width=35,
                    color='white')
                p2_spaces[5 - index] = actr.add_button_to_exp_window(
                    p2_window,
                    text="2",
                    x=(10 + ((5 - index) * 40)),
                    y=10,
                    action=['pick-button', p2, index],
                    height=35,
                    width=35,
                    color='white')

                p1_position = index
                current_player = p2

        elif (current_player == p2) and (index < p2_position) and (
            (p2_position - index) <= 2):  # valid move

            if index <= p1_position:

                # end the run after 3 seconds

                actr.schedule_break_relative(3)
                game_over = p2

                # remove the current position

                actr.remove_items_from_exp_window(p2_window,
                                                  p2_spaces[5 - p2_position],
                                                  p2_spaces[5 - index])
                actr.remove_items_from_exp_window(p1_window,
                                                  p1_spaces[p2_position],
                                                  p1_spaces[index])

                # add the blank old one and the new one marked green to indicate a win
                # don't need to store them or provide the action function because game is over

                actr.add_button_to_exp_window(p2_window,
                                              x=(10 +
                                                 ((5 - p2_position) * 40)),
                                              y=10,
                                              height=35,
                                              width=35,
                                              color='white')
                actr.add_button_to_exp_window(p2_window,
                                              text="1",
                                              x=(10 + ((5 - index) * 40)),
                                              y=10,
                                              height=35,
                                              width=35,
                                              color='green')

                actr.add_button_to_exp_window(p1_window,
                                              x=(10 + (p2_position * 40)),
                                              y=10,
                                              height=35,
                                              width=35,
                                              color='white')
                actr.add_button_to_exp_window(p1_window,
                                              text="2",
                                              x=(10 + (index * 40)),
                                              y=10,
                                              height=35,
                                              width=35,
                                              color='green')

            else:  # update the p2 position and make p1 the current player

                actr.remove_items_from_exp_window(p2_window,
                                                  p2_spaces[5 - p2_position],
                                                  p2_spaces[5 - index])
                actr.remove_items_from_exp_window(p1_window,
                                                  p1_spaces[p2_position],
                                                  p1_spaces[index],
                                                  p1_spaces[p1_position])

                # No action needed for spaces that can't be used again

                p2_spaces[5 - p2_position] = actr.add_button_to_exp_window(
                    p2_window,
                    x=(10 + ((5 - p2_position) * 40)),
                    y=10,
                    height=35,
                    width=35,
                    color='white')
                p2_spaces[5 - index] = actr.add_button_to_exp_window(
                    p2_window,
                    text="1",
                    x=(10 + ((5 - index) * 40)),
                    y=10,
                    height=35,
                    width=35,
                    color='white')

                p1_spaces[p2_position] = actr.add_button_to_exp_window(
                    p1_window,
                    x=(10 + (p2_position * 40)),
                    y=10,
                    action=['pick-button', p1, p2_position],
                    height=35,
                    width=35,
                    color='white')
                p1_spaces[p1_position] = actr.add_button_to_exp_window(
                    p1_window,
                    text="1",
                    x=(10 + (p1_position * 40)),
                    y=10,
                    action=['pick-button', p1, p1_position],
                    height=35,
                    width=35,
                    color='red')
                p1_spaces[index] = actr.add_button_to_exp_window(
                    p1_window,
                    text="2",
                    x=(10 + (index * 40)),
                    y=10,
                    action=['pick-button', p1, index],
                    height=35,
                    width=35,
                    color='white')

                p2_position = index
                current_player = p1
Exemple #12
0
def goal_focus(name=None):
    model = actr.current_model()

    if not (model):
        actr.print_warning('Goal-focus called with no current model.')
    else:
        module = goal_modules[model.lower()]

        if not (module):
            actr.print_warning(
                'No goal module found for model %s when trying to use goal-focus.'
                % model)
        elif name:
            if actr.chunk_p(name):

                actr.schedule_set_buffer_chunk('goal',
                                               name,
                                               0,
                                               module='goal',
                                               priority=':max',
                                               requested=False)
                actr.schedule_event_after_module('goal',
                                                 'clear-delayed-goal',
                                                 params=[model],
                                                 module='goal',
                                                 maintenance=True,
                                                 output=False)

                module.lock.acquire()
                module.delayed = name
                module.lock.release()
                return name

            else:
                actr.print_warning(
                    '%S is not the name of a chunk in the current model - goal-focus failed'
                    % name)

        else:
            chunk = actr.buffer_read('goal')
            module.lock.acquire()
            delayed = module.delayed
            module.lock.release()

            if not (chunk) and not (delayed):
                actr.command_output('Goal buffer is empty')
                return None
            elif not (chunk):
                actr.command_output(
                    'Will be a copy of %s when the model runs' % delayed)
                actr.pprint_chunks(delayed)
                return delayed
            elif not (delayed):
                actr.pprint_chunks(chunk)
                return chunk
            else:
                copy = actr.chunk_copied_from(chunk)

                if copy.lower() == delayed.lower():

                    actr.pprint_chunks(chunk)
                    return chunk
                else:
                    actr.command_output(
                        'Will be a copy of %s when the model runs' % delayed)
                    actr.command_output('Currently holds:')
                    actr.pprint_chunks(chunk)
                    return delayed