Beispiel #1
0
def show_model_results(mcards, ocards, mres, ores):

    if len(mcards) == 3:
        mhit = mcards[2]
    else:
        mhit = 'nil'

    if len(ocards) == 3:
        ohit = ocards[2]
    else:
        ohit = 'nil'

    if actr.buffer_read('goal'):
        actr.mod_focus('mc1', mcards[0], 'mc2', mcards[1], 'mc3', mhit, 'mtot',
                       score_cards(mcards), 'mstart', score_cards(mcards[0:2]),
                       'mresult', mres, 'oc1', ocards[0], 'oc2', ocards[1],
                       'oc3', ohit, 'otot', score_cards(ocards), 'ostart',
                       score_cards(ocards[0:1]), 'oresult', ores, 'state',
                       'results')
    else:
        actr.goal_focus(
            actr.define_chunks([
                'isa', 'game-state', 'mc1', mcards[0], 'mc2', mcards[1], 'mc3',
                mhit, 'mtot',
                score_cards(mcards), 'mstart',
                score_cards(mcards[0:2]), 'mresult', mres, 'oc1', ocards[0],
                'oc2', ocards[1], 'oc3', ohit, 'otot',
                score_cards(ocards), 'ostart',
                score_cards(ocards[0:1]), 'oresult', ores, 'state', 'results'
            ])[0])

    actr.run_full_time(10)
Beispiel #2
0
def make_one_goal():

    word = random_word()
  
    actr.set_buffer_chunk('imaginal',actr.define_chunks(['verb',word[0]])[0])
    
    actr.goal_focus('starting-goal')
    
    return(word)
Beispiel #3
0
def play(player1, player2):
    global p1, p2, p1_position, p2_position, game_over, current_player, next_move

    # make sure that both names are valid model names

    if (player1.lower() in (x.lower() for x in actr.mp_models())) and (
            player2.lower() in (x.lower() for x in actr.mp_models())):
        actr.reset()

        actr.add_command('make-move', make_move,
                         "Model action function in simple capture game.")

        # create the goal chunks

        actr.set_current_model(player1)

        actr.define_chunks([
            'goal', 'isa', 'play', 'my-pos', 'p1', 'p1', 0, 'p2', 5, 'state',
            'move'
        ])

        actr.goal_focus('goal')

        actr.set_current_model(player2)

        actr.define_chunks([
            'goal', 'isa', 'play', 'my-pos', 'p1', 'p2', 0, 'p2', 5, 'state',
            'move'
        ])

        actr.goal_focus('goal')

        # initialize the game information

        p1 = player1
        p2 = player2
        p1_position = 0
        p2_position = 5
        game_over = False
        current_player = player1
        next_move = None

        while not (game_over):
            # until a move is made which breaks the run
            # or 1000 simulated seconds pass which is a forfeit
            actr.run(1000)
            process_move(next_move)

        # Give them a chance to process results

        actr.run_full_time(3)

        actr.remove_command('make-move')

    return game_over
Beispiel #4
0
def task1(prime_stimulus):
    """
    This function simulates the prime sentence verification task. The model parses in the prime sentence,
    and attempts to comprehend it.
    :param prime_stimulus: list, for simplification,
                           assumes only syntax and syntax-corr changes based on condition
    :return:
    """
    prime_sentence = actr.define_chunks(prime_stimulus)[0]
    actr.set_buffer_chunk('visual', prime_sentence) # prime sentence
    # set init goal
    # actr.record_history('buffer-trace', 'vocal')
    actr.goal_focus('wait-for-screen')
    actr.run(10)
Beispiel #5
0
def present_choose():

    actr.goal_focus('initial-goal')

    if actr.random(1.0) < .6:
        answer = 'a'
    elif actr.random(1.0) > .5:
        answer = 'b'
    else:
        answer = None

    actr.schedule_event_relative(5,
                                 'utility-learning-issues-show-result',
                                 params=[answer],
                                 output='medium')
Beispiel #6
0
def task2(target_stimulus=None):
    """
    This function simulates the picture description task. The model observes the picture stimuli
    and attempts to describe the picture using one of potential syntactic structure.
    :param target_stimulus: None, for simplification,
                            assume the picture stimuli uses the same verb as prime sentence
    :return:
    """
    target_stimulus = ['isa', 'picture',
                                       'agent', 'n3',
                                       'patient', 'n4',
                                       'action', 'v']
    target_picture = actr.define_chunks(target_stimulus)[0]

    # set second goal
    actr.goal_focus('wait-for-next-screen')
    actr.set_buffer_chunk('visual', target_picture)  # target picture
    actr.run(10)
Beispiel #7
0
def show_model_cards(mcards, ocard):

    if actr.buffer_read('goal'):
        actr.mod_focus('mc1', mcards[0], 'mc2',
                       mcards[1], 'mc3', None, 'mtot', None, 'mstart',
                       score_cards(mcards), 'mresult', None, 'oc1', ocard,
                       'oc2', None, 'oc3', None, 'otot', None, 'ostart',
                       score_cards([ocard]), 'oresult', None, 'state', 'start')
    else:
        actr.goal_focus(
            actr.define_chunks([
                'isa', 'game-state', 'mc1', mcards[0], 'mc2', mcards[1],
                'mstart',
                score_cards(mcards), 'oc1', ocard, 'ostart',
                score_cards([ocard]), 'state', 'start'
            ])[0])

    global model_action
    model_action = 's'
    actr.run_full_time(10)
    return model_action
Beispiel #8
0
def trial(*features):

    actr.reset()
 
    for slot,value in actr.permute_list(list(zip(slots,features))):
        attribute(slot,value)

    goal = actr.define_chunks(["state","categorize"])[0]
    actr.goal_focus(goal)
    actr.run(20)
    
    answer = actr.chunk_slot_value(actr.buffer_read("imaginal"),"category")

    if isinstance(answer,numbers.Number):
        if answer == 1 or answer == 2:
            return((answer,True))
        else:
            actr.model_output("Model responded with invalid category.")
            return((0,False))
    else:
        actr.model_output("Model did not respond or provided a non-numeric category.")
        return((0,False))
Beispiel #9
0
def attribute(name,value):

    goal = actr.define_chunks(["state","add-attribute","name",name,"value", (value + offset)])[0]
    actr.goal_focus(goal)
    actr.run(20)
Beispiel #10
0
def play(player1, player2):
    global window, p1, p2, p1_position, p2_position, current_player, game_over, safety_stop, p1_text, p2_text

    if (player1.lower() in (x.lower() for x in actr.mp_models())) and (
            player2.lower() in (x.lower() for x in actr.mp_models())):

        actr.reset()

        actr.set_current_model(player1)

        # create a goal chunk with the player's color and name
        actr.define_chunks([
            'goal', 'isa', 'play', 'my-color', 'red', 'my-name',
            "'%s'" % player1
        ])
        actr.goal_focus('goal')
        actr.set_parameter_value(':show-focus', 'red')

        actr.set_current_model(player2)

        # create a goal chunk with the player's color and name
        actr.define_chunks([
            'goal', 'isa', 'play', 'my-color', 'blue', 'my-name',
            "'%s'" % player2
        ])
        actr.goal_focus('goal')
        actr.set_parameter_value(':show-focus', 'blue')

        window = actr.open_exp_window("game",
                                      visible=True,
                                      width=200,
                                      height=100)
        safety_window = actr.open_exp_window('Safety',
                                             visible=True,
                                             height=100,
                                             width=100,
                                             x=100,
                                             y=100)

        actr.add_command('stop-a-run', stop_a_run,
                         'Set the flag to terminate the game.')

        actr.add_button_to_exp_window(safety_window,
                                      text="STOP",
                                      x=0,
                                      y=0,
                                      action='stop-a-run',
                                      height=80,
                                      width=80,
                                      color='red')

        p1 = player1
        p2 = player2
        game_over = False
        safety_stop = False
        current_player = player1
        p1_position = 0
        p2_position = 5

        for m in actr.mp_models():
            actr.set_current_model(m)
            actr.install_device(window)

        p1_text = actr.add_text_to_exp_window(window,
                                              str(p1_position),
                                              x=20,
                                              y=10,
                                              color='red',
                                              height=30,
                                              width=30,
                                              font_size=20)
        p2_text = actr.add_text_to_exp_window(window,
                                              str(p2_position),
                                              x=140,
                                              y=10,
                                              color='blue',
                                              height=30,
                                              width=30,
                                              font_size=20)

        actr.add_command("process-moves", process_moves,
                         "Handle player speak actions")
        actr.monitor_command("output-speech", "process-moves")

        # speak to all models telling them the name
        # of the first player.

        for m in actr.mp_models():
            actr.set_current_model(m)
            actr.new_word_sound(p1, 0, 'start')

        actr.add_command('is-game-over', is_game_over,
                         'Test whether game should stop running.')
        actr.add_command('set_game_over', set_game_over,
                         'Set the flag to stop the game.')

        actr.run_until_condition('is-game-over', True)

        actr.remove_command_monitor("output-speech", "process-moves")
        actr.remove_command("process-moves")
        actr.remove_command('stop-a-run')
        actr.remove_command('is-game-over')
        actr.remove_command('set_game_over')

    return game_over
Beispiel #11
0
def process_move(dist):
    global p1_position, p2_position, game_over, current_player

    forfeit = False
    winner = None
    if isinstance(dist, numbers.Number):
        if (current_player == p1):
            p1_position += dist
        else:
            p2_position -= dist
    else:
        forfeit = current_player

    if (forfeit or (p2_position <= p1_position)):  # game over
        if forfeit:
            if (current_player == p1):
                winner = p2
            else:
                winner = p1
        else:
            winner = current_player

        game_over = winner

        # create new chunks with the results

        actr.set_current_model(p1)

        if p1 == winner:
            result = 'win'
        else:
            result = 'lose'

        actr.goal_focus(
            actr.define_chunks([
                'isa', 'play', 'my-pos', 'p1', 'p1', p1_position, 'p2',
                p2_position, 'state', 'game-over', 'result', result
            ])[0])
        # update player 2 goal

        actr.set_current_model(p2)

        if p2 == winner:
            result = 'win'
        else:
            result = 'lose'

        actr.goal_focus(
            actr.define_chunks([
                'isa', 'play', 'my-pos', 'p2', 'p1', p1_position, 'p2',
                p2_position, 'state', 'game-over', 'result', result
            ])[0])
    elif current_player == p1:

        # player 1 moved so update player 2 goal

        actr.set_current_model(p2)

        actr.goal_focus(
            actr.define_chunks([
                'isa', 'play', 'my-pos', 'p2', 'p1', p1_position, 'p2',
                p2_position, 'state', 'move'
            ])[0])
        current_player = p2

    else:

        # player 2 moved so update player 1

        actr.set_current_model(p1)

        actr.goal_focus(
            actr.define_chunks([
                'isa', 'play', 'my-pos', 'p1', 'p1', p1_position, 'p2',
                p2_position, 'state', 'move'
            ])[0])
        current_player = p1
Beispiel #12
0
def do_experiment(subj="depressed", human=False, wlist_amount=2000):
    '''
    Run the experiment
    '''
    global subject
    subject = subj
    assert wlist_amount <= len(
        word_lists_dict
    ), "Chosen too many lists, choose less or create more word lists using function: create_lists()"
    # display_word_lists()

    for idx, (key, value) in enumerate(word_lists_dict.items()):
        actr.reset()
        window = setup_experiment(human)
        global current_list
        current_list = idx  # keep track for which list words are recalled
        setup_dm(value)
        actr.add_command("retrieved-word", record_words_recalled,
                         "Retrieves recalled words.")
        actr.add_command("rehearsed-word", record_words_rehearsed,
                         "Retrieves rehearsed words.")
        for word in value:
            if "neutral" in word:
                color = "black"
            elif "positive" in word:
                color = "green"
            else:
                color = "red"
            actr.add_text_to_exp_window(window,
                                        word,
                                        x=475 - len(word),
                                        y=374,
                                        color=color,
                                        font_size=20)  # change later
            actr.run(
                6, human
            )  # True when choosing Human, False when choosing differently
            actr.clear_exp_window(window)
            actr.run(.5, human)  # 500-ms blank screen
        prepare_for_recall()
        actr.remove_command("rehearsed-word")
        actr.goal_focus("startrecall")  # set goal to start recalling
        actr.run(10, human)
        actr.remove_command("retrieved-word")
        print(f'Experiment {idx+1}/{wlist_amount} completed!', end="\r")
        if idx == wlist_amount - 1:  # run for a chosen amount of word lists
            break
    close_exp_window()  # close window at end of experiment

    avg_recalled, avg_recalled_unique = 0, 0
    for key, val in recalled_words.items():
        avg_recalled += len(val)
        avg_recalled_unique += len(set(val))
    #    print(f'\nList {key} (length={len(val)}, unique={len(set(val))})')

    print("\n\n#############################################")
    print(f'\n[{subj}] Results!\n')

    result = analysis(wlist_amount, False)
    print(f'Avg. Amount of words recalled = {avg_recalled//wlist_amount}')
    print(
        f'Avg. Amount of unique words recalled = {avg_recalled_unique//wlist_amount}'
    )

    for key, val in result.items():
        print(f'{key} = {dict(val)}')
    print()
###################### ACT-R + PYTHON TEMPLATE #######################
#   Author: Cher Yang
#   Date: 09/24/2020
# This template provides a init python code for building an ACT-R model

import actr

actr.load_act_r_model("template-model.lisp")  # load the model
actr.goal_focus('wait-for-screen')  # set init goal

# prompt stimulus info
#prime_sentence = actr.define_chunks(['isa', 'sentence', 'string', 'the nun chases the sailor'])
#actr.set_buffer_chunk('visual', prime_sentence[0])

actr.run(2)