def experiment(human=False): actr.reset() items = actr.permute_list([ "B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z" ]) text1 = items[0] window = actr.open_exp_window("Letter recognition") actr.add_text_to_exp_window(window, text1, x=125, y=150) actr.add_command("demo2-key-press", respond_to_key_press, "Demo2 task output-key monitor") actr.monitor_command("output-key", "demo2-key-press") global response response = False if human == True: while response == False: actr.process_events() else: actr.install_device(window) actr.run(10, True) actr.remove_command_monitor("output-key", "demo2-key-press") actr.remove_command("demo2-key-press") return response
def person(): global response window = actr.open_exp_window("Choice Experiment", visible=True) actr.add_command("choice-response", respond_to_key_press, "Choice task key response") actr.monitor_command("output-key", "choice-response") actr.add_text_to_exp_window(window, 'choose', x=50, y=100) response = '' while response == '': actr.process_events() actr.clear_exp_window(window) if actr.random(1.0) < .9: answer = 'heads' else: answer = 'tails' actr.add_text_to_exp_window(window, answer, x=50, y=100) start = actr.get_time(False) while (actr.get_time(False) - start) < 1000: actr.process_events() actr.remove_command_monitor("output-key", "choice-response") actr.remove_command("choice-response") return response
def update_current_line(): global current_line, done if current_stick == target: done = True actr.modify_line_for_exp_window(current_line, [75, 330], [target + 75, 330]) actr.add_text_to_exp_window(window, "Done", x=5, y=200) actr.add_text_to_exp_window(window, "对标成功!", x=300, y=200, color='red', height=20, width=75, font_size=24) elif current_stick == 0: if current_line: actr.remove_items_from_exp_window(window, current_line) current_line = None elif current_line: actr.modify_line_for_exp_window(current_line, [75, 330], [current_stick + 75, 330]) else: current_line = actr.add_line_to_exp_window(window, [75, 330], [current_stick + 75, 330], "blue")
def intialize_screen(window, text1, text2, text3, text4, gauge1_startx, gauge1_starty, gauge2_startx, gauge2_starty): global light1_obj, light2_obj, gauge1_obj, gauge2_obj light1_on(window, text1, "intial") light2_on(window, text2, "intial") gauge1_obj = actr.add_text_to_exp_window(window, text3, x=gauge1_startx, y=gauge1_starty, color="black") actr.add_line_to_exp_window(window, [gauge1_startx + 10, 150], [gauge1_startx + 20, 150], color=False) actr.add_line_to_exp_window(window, [gauge1_startx + 10, 250], [gauge1_startx + 20, 250], color=False) gauge2_obj = actr.add_text_to_exp_window(window, text4, x=gauge2_startx, y=gauge2_starty, color="black") actr.add_line_to_exp_window(window, [gauge2_startx + 10, 150], [gauge2_startx + 20, 150], color=False) actr.add_line_to_exp_window(window, [gauge2_startx + 10, 250], [gauge2_startx + 20, 250], color=False)
def task(which=False): global response, response_time actr.reset() alphabet = [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ] letter = actr.permute_list(alphabet)[0] alphabet = ["Z"] + alphabet if which == 'next' or which == 'previous': task = which elif actr.random(2) == 0: task = 'next' else: task = 'previous' time = 1500 + actr.random(1000) window = actr.open_exp_window("Simple task") actr.install_device(window) actr.add_command("pm-issues-response", respond_to_key_press, "Perceptual-motor issues task response") actr.monitor_command("output-key", "pm-issues-response") actr.add_text_to_exp_window(window, letter, x=130, y=150) actr.add_command("pm-issue-display", display_prompt, "Perceptual-motor issues task prompt display") actr.schedule_event_relative(time, "pm-issue-display", params=[window, task], time_in_ms=True) response = [] response_time = False actr.run(10, True) actr.remove_command("pm-issue-display") actr.remove_command_monitor("output-key", "pm-issues-response") actr.remove_command("pm-issues-response") if (len(response) == 2 and response_time > time and response[0] == letter and ((task == 'next' and alphabet.index(response[0]) == (alphabet.index(response[1]) - 1)) or (task == 'previous' and alphabet.index(response[0]) == (alphabet.index(response[1]) + 1)))): result = True else: result = False return [task, result]
def present_next_trial(): global task_state,start_time,val1,val2 task_state = 'trial' actr.clear_exp_window(window) val1 = actr.random(4) val2 = actr.random(4) actr.add_text_to_exp_window(window, str(val1), x=10, y=50) actr.add_text_to_exp_window(window, str(val2), x=110, y=50) start_time = actr.get_time()
def trial(onset_time): actr.reset() letters = actr.permute_list([ "B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z" ]) answers = [] row = actr.random(3) window = actr.open_exp_window("Sperling Experiment", visible=True) for i in range(3): for j in range(4): txt = letters[j + (i * 4)] if i == row: answers.append(txt) actr.add_text_to_exp_window(window, txt, x=(75 + (j * 50)), y=(100 + (i * 50))) actr.install_device(window) if row == 0: freq = 2000 elif row == 1: freq = 1000 else: freq = 500 actr.new_tone_sound(freq, .5, onset_time) actr.schedule_event_relative(900 + actr.random(200), "clear-exp-window", params=[window], time_in_ms=True) global responses responses = [] actr.add_command("sperling-response", respond_to_key_press, "Sperling task key press response monitor") actr.monitor_command("output-key", "sperling-response") actr.run(30, True) actr.remove_command_monitor("output-key", "sperling-response") actr.remove_command("sperling-response") if show_responses: print("answers: %s" % answers) print("responses: %s" % responses) return (compute_score(answers))
def present_trial(trial, new_window=True): if new_window: w = actr.open_exp_window("Alpha-arithmetic Experiment", visible=trial.visible) if run_model: actr.install_device(w) else: actr.clear_exp_window() actr.add_text_to_exp_window(None, trial.text, x=100, y=150) trial.start = actr.get_time(run_model)
def do_experiment(size, trials, human): actr.reset() result = [] model = not (human) window = actr.open_exp_window("Paired-Associate Experiment", visible=human) if model: actr.install_device(window) for i in range(trials): score = 0 time = 0 for prompt, associate in actr.permute_list(pairs[20 - size:]): actr.clear_exp_window(window) actr.add_text_to_exp_window(window, prompt, x=150, y=150) global response response = '' start = actr.get_time(model) if model: actr.run_full_time(5) else: while (actr.get_time(False) - start) < 5000: actr.process_events() if response == associate: score += 1 time += response_time - start actr.clear_exp_window(window) actr.add_text_to_exp_window(window, associate, x=150, y=150) start = actr.get_time(model) if model: actr.run_full_time(5) else: while (actr.get_time(False) - start) < 5000: actr.process_events() if score > 0: average_time = time / score / 1000.0 else: average_time = 0 result.append((score / size, average_time)) return result
def graph_it(data): win = actr.open_exp_window("Irregular Verbs correct",visible=True,width=500,height=475) low = min(data) zoom = min([.9,math.floor(10 * low)/10]) actr.clear_exp_window(win) actr.add_text_to_exp_window(win, "1.0", x=5, y=5, width=22) actr.add_text_to_exp_window(win, "%0.2f" % zoom, x=5, y=400, width=22) actr.add_text_to_exp_window(win, "%0.2f" % (zoom + ((1.0 - zoom) / 2)), x=5, y=200, width=22) actr.add_text_to_exp_window(win, "Trials", x=200, y=420, width=100) actr.add_line_to_exp_window(win,[30,10],[30,410],'black') actr.add_line_to_exp_window(win,[450,410],[25,410],'black') for i in range(10): actr.add_line_to_exp_window(win,[25,10 + i*40],[35,10 + i*40],'black') start = data[0] increment = max([1.0,math.floor( 450 / len(data))]) r = math.floor (400/ (1.0 - zoom)) intercept = r + 10 lastx =30 lasty = intercept - math.floor(r * start) for p in data[1:]: x = lastx + 30 y = intercept - math.floor(r * p) actr.add_line_to_exp_window(win,[lastx,lasty],[x,y],'red') lastx = x lasty = y
def update_current_line(): global current_line,done if current_stick == target: done = True actr.modify_line_for_exp_window(current_line, [75,135], [target + 75,135]) actr.add_text_to_exp_window(window, "Done", x=180, y=200) elif current_stick == 0: if current_line: actr.remove_items_from_exp_window(window,current_line) current_line = None elif current_line: actr.modify_line_for_exp_window(current_line,[75,135],[current_stick + 75,135]) else: current_line = actr.add_line_to_exp_window(window,[75,135],[current_stick + 75,135],"blue")
def test_unit3(): """this test unit examines trace""" assert check_load() trial = ('?', 'Punishment', 'MostlyReward') prompt, feedback, block_type = trial window = actr.open_exp_window("Gambling Experiment", visible=False) actr.install_device(window) actr.clear_exp_window(window) actr.add_text_to_exp_window(window, prompt, x=150, y=150) actr.run_full_time(5) actr.clear_exp_window(window) actr.add_text_to_exp_window(window, feedback, x=150, y=150) actr.run_full_time(5)
def agi (): actr.reset() # open a window and add the text window = actr.open_exp_window("Moving X", visible=True) text = actr.add_text_to_exp_window(window, "x", x=10, y=10) y = 10 actr.install_device(window) # schedule an event to move it but don't have the # periodic-event command available at this point so # just use a relative event which schedules itself actr.add_command("agi-move-it",move_text) actr.schedule_event_relative (1, "agi-move-it",params=[text,y],maintenance=True) # run the model in real time since it's a visible window actr.run(3,True) actr.remove_command("agi-move-it")
def sentence(person, location, target, term): actr.reset() window = actr.open_exp_window("Sentence Experiment", visible=False, width=600, height=300) x = 25 actr.install_device(window) actr.add_command("fan-response", respond_to_key_press, "Fan experiment model response") actr.monitor_command("output-key", "fan-response") if term == 'person': actr.pdisable("retrieve-from-location") else: actr.pdisable("retrieve-from-person") actr.add_text_to_exp_window(window, person, x=50, y=150, width=75) actr.add_text_to_exp_window(window, location, x=250, y=150, width=75) global response, response_time response = '' response_time = 0 actr.run(30) actr.remove_command_monitor("output-key", "fan-response") actr.remove_command("fan-response") if response == '': return (30, False) elif target: if response.lower() == 'k'.lower(): return (response_time / 1000, True) else: return (response_time / 1000, False) else: if response.lower() == 'd'.lower(): return (response_time / 1000, True) else: return (response_time / 1000, False)
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)
def light1_off(window, text1, mode): global light1Color, light1_obj light1Color = "black" if light1_obj != None: actr.remove_items_from_exp_window(window, light1_obj) light1_obj = actr.add_text_to_exp_window(window, text1, x=125, y=100, color=light1Color)
def light2_off(window, text2, mode): global light2Color, light2_obj light2Color = "red" if light2_obj != None: actr.remove_items_from_exp_window(window, light2_obj) light2_obj = actr.add_text_to_exp_window(window, text2, x=175, y=100, color=light2Color)
def example(): actr.reset() # The first window is located at x=0 and y=0 which # is fine for virtual windows, but if it were a real # window that window would be behind the menu bar # at the top of the display under OS X which makes # it difficult to interact with. The second window # is located at x=200, y=200. The default mouse # position is x=0, y=0. w1 = actr.open_exp_window("W1", visible=False, width=100, height=100, x=0, y=0) w2 = actr.open_exp_window("W2", visible=False, width=100, height=100, x=200, y=200) # add text to the same local position in each window. actr.add_text_to_exp_window(w1, "a", x=10, y=10, color='red') actr.add_text_to_exp_window(w2, "a", x=10, y=10, color='blue') # Install both windows and the mouse cursor actr.install_device(w1) actr.install_device(w2) actr.install_device(["vision", "cursor", "mouse"]) # Just run the model to have vision module process # things and print the visicon. # The model doesn't do anything. actr.run(1) actr.print_visicon()
def draw_graph(points): w = actr.open_exp_window('Data', visible=True, width=550, height=460) actr.add_line_to_exp_window(w, [50, 0], [50, 420], 'white') for i in range(11): actr.add_text_to_exp_window(w, "%3.1f" % (1.0 - (i * .1)), x=5, y=(5 + (i * 40)), width=35) actr.add_line_to_exp_window(w, [45, 10 + (i * 40)], [550, 10 + (i * 40)], 'white') x = 50 for (a, b) in zip(points[0:-1], points[1:]): actr.add_line_to_exp_window( w, [x, math.floor(410 - (a * 400))], [x + 25, math.floor(410 - (b * 400))], 'blue') x += 25
def do_guess(prompt, window): """ this function allows model to do first half of the experiment, guessing :param prompt:"?" :param window: :return: response "f" for less, or "j" for more """ # display prompt actr.clear_exp_window(window) actr.add_text_to_exp_window(window, prompt, x=150, y=150) # wait for response global response response = '' start = actr.get_time() actr.run_full_time(5) time = response_time - start return response, time
def present_trial(given_letter = None, train_epochs = 1, new_window = True, open_window = True, delay_time = 10, display = False): global window, window_reading, clickList, chunk_names, chunk_defs if (given_letter == None): letterList = actr.permute_list(list(string.ascii_uppercase)) chosen_letter = letterList[0] else: chosen_letter = given_letter if display: train_n(10, chosen_letter) clickList = [] actr.clear_exp_window(window_reading) actr.clear_exp_window(window) actr.add_text_to_exp_window(window_reading, chosen_letter, 125, 150) actr.add_command('process-click',process_click) actr.monitor_command('click-mouse','process-click') actr.run(1000, open_window) # for actr time or real time correct = correct_drawing(chosen_letter, clickList) if (correct): train_once(chosen_letter) actr.run_full_time(delay_time) else: train_n(train_epochs, chosen_letter) actr.run_full_time(delay_time) actr.remove_command_monitor('click-mouse', 'process-click') actr.remove_command('process-click') return (clickList, correct)
def update_window(self): """Updates the experiment window""" if self.window is not None: # First, clean-up actr.clear_exp_window() # Then, add new elements if self.phase == "fixation": item = actr.add_text_to_exp_window(self.window, "+", x=400, y=300, color="black") elif self.phase == "stimulus": color = self.current_trial.color word = self.current_trial.word item = actr.add_text_to_exp_window(self.window, word, x=395, y=300, color=color) for i, col in enumerate(COLOR_MAPPINGS): item = actr.add_text_to_exp_window(self.window, COLOR_MAPPINGS[col], x=600 + i * 50, y=500, color=col) print(type(COLOR_MAPPINGS)) elif self.phase == "done": color = self.current_trial.color word = self.current_trial.word item = actr.add_text_to_exp_window(self.window, "done", x=395, y=300, color="black")
def schedule_initial_stimuli(self, stim_size, initial=False, time=0): if initial == True: if stim_size == 3: for row in range(3): actr.add_text_to_exp_window( self.window, self.initial_set_stimuli[self.current_block][row][:], x=self.my_x[row], y=self.my_y[row]) else: for row in range(6): actr.add_text_to_exp_window( self.window, self.initial_set_stimuli[self.current_block][row][:], x=self.my_x[row], y=self.my_y[row]) else: if stim_size == 3: for row in range(3): self.event_id_dict[ "initial_stimuli"] = actr.schedule_event_relative( time, "add-text-to-exp-window", [ None, self.initial_set_stimuli[ self.current_block][row][:], { "x": self.my_x[row], "y": self.my_y[row] } ]) else: for row in range(6): self.event_id_dict[ "initial_stimuli"] = actr.schedule_event_relative( time, "add-text-to-exp-window", [ None, self.initial_set_stimuli[ self.current_block][row][:], { "x": self.my_x[row], "y": self.my_y[row] } ])
def play_human(cards, oc1): win = actr.open_exp_window('Human') actr.add_text_to_exp_window(win, 'You', x=50, y=20) actr.add_text_to_exp_window(win, 'Model', x=200, y=20) for i in range(2): for j in range(3): actr.add_text_to_exp_window(win, "C%d" % (j + 1), x=(25 + (j * 30) + (i * 150)), y=40, width=20) if i == 0 and j < 2: actr.add_text_to_exp_window(win, str(cards[j]), x=(25 + (j * 30) + (i * 150)), y=60, width=20) if i == 1 and j == 0: actr.add_text_to_exp_window(win, str(oc1), x=(25 + (j * 30) + (i * 150)), y=60, width=20) global human_action human_action = None start_time = actr.get_time(False) while (actr.get_time(False) - start_time) < 10000: actr.process_events() if human_action: return human_action else: return 's'
def reset_display(): global current_stick if not (done): current_stick = 0 update_current_line() overparking = actr.add_text_to_exp_window(window, "停车超标!", x=300, y=200, color='red', height=20, width=75, font_size=24) time.sleep(1) actr.remove_items_from_exp_window(window, overparking)
def experiment(human=False): actr.reset() items = actr.permute_list([ "B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z" ]) target = items[0] foil = items[1] window = actr.open_exp_window("Letter difference") text1 = foil text2 = foil text3 = foil index = actr.random(3) if index == 0: text1 = target elif index == 1: text2 = target else: text3 = target actr.add_text_to_exp_window(window, text1, x=125, y=75) actr.add_text_to_exp_window(window, text2, x=75, y=175) actr.add_text_to_exp_window(window, text3, x=175, y=175) actr.add_command("unit2-key-press", respond_to_key_press, "Assignment 2 task output-key monitor") actr.monitor_command("output-key", "unit2-key-press") global response response = '' if human == True: while response == '': actr.process_events() else: actr.install_device(window) actr.run(10, True) actr.remove_command_monitor("output-key", "unit2-key-press") actr.remove_command("unit2-key-press") if response.lower() == target.lower(): return True else: return False
def process_moves(model, string): global p1_position, p2_position, current_player, p1_text, p2_text # Let all other models hear this for m in actr.mp_models(): if not (m.lower() == model.lower()): # all other models will hear this actr.set_current_model(m) # Create the originating model's name as a simple chunk if # it isn't one already to avoid a warning of it being # created by default when the sound is generated. if not (actr.chunk_p(model)): actr.define_chunks(model) # Create the sound as a word with location indicating # the speaker. actr.new_word_sound(string, actr.mp_time(), model) if (string.lower() == "one"): move = 1 elif (string.lower() == "two"): move = 2 else: print("Wrong move assumed to be 1: %s" % string) move = 1 if current_player == p1: p1_position += move else: p2_position -= move actr.clear_exp_window(window) if (p2_position <= p1_position): # if there's a winner actr.schedule_event_relative(3, 'set_game_over', params=[current_player]) actr.add_text_to_exp_window(window, current_player, x=60, y=20, color='green', height=30, width=80, font_size=20) else: # not a winner so update the display with the new position 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) if current_player == p1: current_player = p2 else: current_player = p1
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
def build_display(a, b, c, d, e, f, goal): global window, target, current_stick, done, current_line, choice target = goal current_stick = 0 done = False choice = None current_line = None window = actr.open_exp_window("停车对标", visible=visible, width=600, height=400) actr.add_button_to_exp_window(window, text="A", x=5, y=23, action=["bst-button-pressed", a, "under"], height=24, width=40) actr.add_button_to_exp_window(window, text="B", x=5, y=48, action=["bst-button-pressed", b, "over"], height=24, width=40) actr.add_button_to_exp_window(window, text="C", x=5, y=73, action=["bst-button-pressed", c, "under"], height=24, width=40) actr.add_button_to_exp_window(window, text="D", x=5, y=98, action=["bst-button-pressed", d, "under"], height=24, width=40) actr.add_button_to_exp_window(window, text="E", x=5, y=120, action=["bst-button-pressed", e, "under"], height=24, width=40) actr.add_button_to_exp_window(window, text="F", x=5, y=140, action=["bst-button-pressed", f, "under"], height=24, width=40) # actr.add_text_to_exp_window(window, text="实际距离: ", x=5, y=123, height=24, width=65) actr.add_button_to_exp_window(window, text="Reset", x=5, y=340, action="bst-reset-button-pressed", height=24, width=65) actr.add_line_to_exp_window(window, [75, 35], [a + 75, 35], "black") actr.add_line_to_exp_window(window, [75, 60], [b + 75, 60], "black") actr.add_line_to_exp_window(window, [75, 85], [c + 75, 85], "black") actr.add_line_to_exp_window(window, [75, 110], [d + 75, 110], "black") actr.add_line_to_exp_window(window, [75, 135], [e + 75, 135], "black") actr.add_line_to_exp_window(window, [75, 160], [f + 75, 160], "black") actr.add_line_to_exp_window(window, [75, 310], [goal + 75, 310], "green") # actr.add_line_to_exp_window(window, [75, 320], [current_stick + 75, 215], "blue") actr.add_text_to_exp_window( window, "注意!\nB表示目标距离;模型先注视ABCDEF四条线,之后将目标线与C对比,求其差值,依次比较A,D,E,F,直至出现Done,结束对标。\nA表示四级制动,制动距离较长;" "\nC表示一级制动,制动距离最长;", x=400, y=310, color='blue', height=200, width=150, font_size=12)
def present_feedback(result): global task_state task_state = 'feedback' actr.clear_exp_window(window) actr.add_text_to_exp_window(window, result, x=50, y=100)