def trials(n,cont=False,v=False): global report,word_list,reward_check actr.add_command("reward-check",verify_reward, "Past tense code check for a reward each trial.") actr.monitor_command("trigger-reward","reward-check") if not(cont) or not(word_list): actr.reset() word_list = make_word_freq_list(verbs) new = [] for x in word_list: for y in x[1:]: if y not in new: new.append(y) for x in new: if not(actr.chunk_p(x)): actr.define_chunks([x]) print_header() report = [] actr.set_parameter_value(":v",v) start = 100 * math.floor(len(report) / 100) count = len(report) % 100 for i in range(n): add_past_tense_to_memory() add_past_tense_to_memory() reward_check = False target = make_one_goal() duration = actr.run(100)[0] add_to_report(target,actr.buffer_read('imaginal')) actr.clear_buffer('imaginal') count += 1 if count == 100: rep_f_i(start, start + 100, 100) count = 0 start += 100 if not(reward_check): actr.print_warning("Model did not receive a reward when given %s."% target[0]) actr.run_full_time(200 - duration) if duration == 100: actr.print_warning("Model spent 100 seconds generating a past tense for %s."% target[0]) rep_f_i(start,start+count,100) actr.remove_command_monitor("trigger-reward","reward-check") actr.remove_command("reward-check")
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
def run_trial(self, trial): """A trial""" sen = trial.sentence pic = trial.picture chunk_s = actr.define_chunks(sen.chunk_definition)[0] actr.set_buffer_chunk('visual', chunk_s) actr.run(time = 10) chunk_p = actr.define_chunks(pic.chunk_definition)[0] actr.schedule_set_buffer_chunk('visual', chunk_p, actr.mp_time() + 0.05) actr.run(time = 10)
def load_decks(): global decka_counter global deckb_counter global deckc_counter global deckd_counter if decka_counter < 40: decka = "yes" else: decka = "no" if deckb_counter < 40: deckb = "yes" else: deckb = "no" if deckc_counter < 40: deckc = "yes" else: deckc = "no" if deckd_counter < 40: deckd = "yes" else: deckd = "no" deck_chunk = actr.define_chunks([ 'isa', 'decks', 'deckA', decka, 'deckB', deckb, 'deckC', deckc, 'deckD', deckd ]) actr.set_buffer_chunk("visual", deck_chunk[0])
def handle_observation(observation): '''observation should have chunk format''' chunk = actr.define_chunks(observation) actr.schedule_simple_event_now("set-buffer-chunk", ['imaginal', chunk[0]]) actr.run(10) d = actr.get_history_data("blending-trace") d = json.loads(d) t = actr.get_history_data("blending-times") MP = actr.get_parameter_value(':mp') # #get t t = access_by_key('TEMPERATURE', d[0][1]) # #the values # vs = [actr.chunk_slot_value(x,'value') for x in chunk_names] # factors = [ 'highest_obstacle_altitude', 'hazard_level', 'closeness_to_hiker', 'heading_to_hiker', 'current_altitude', 'current_heading' ] # factors = ['needsFood', 'needsWater'] result_factors = ['action'] # result_factors = ['food','water'] results = compute_S(d, factors) # ,'f3']) return_dict = {'action': {}} for sums, result_factor in zip(results, result_factors): #print("For", result_factor) for s, factor in zip(sums, factors): return_dict['action'][factor] = MP / t * sum(s) #print(factor, MP / t * sum(s)) return return_dict
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)
def setupACTR(): actr.load_act_r_model( "/Users/paulsomers/ddpg-craft/scripts/ddpg_agent.lisp") actr.wait = True #run ACTR for 10 seconds (simulated) chk = actr.define_chunks(['wait', 'false']) actrThread = threading.Thread(target=actr.run, args=[1000]) actrThread.start() actr.schedule_simple_event_now("set-buffer-chunk", ['imaginal', chk[0]]) #once loaded add some functions that will be called by ACT-R actr.add_command("tic", do_tic) actr.add_command('set_response', set_response) #Setup the imaginal for an initial production chunk = ['isa' 'setup'] #Wait until the first production fires # once act-r is running, the tic will be called to set the wait (below) to false while actr.wait: time.sleep(0.001) print("waiting") return 1
def present_new_situation(self, buffer="imaginal"): """Creates a new situation for the model and presents to the WHERE buffer""" if actr.mp_time() == self.PTET: newdef = self.generate_random_memory(traumatic=True) else: newdef = self.generate_random_memory(traumatic=False) newchunk = actr.define_chunks(newdef[0]) actr.set_buffer_chunk(buffer, newchunk[0])
def add_past_tense_to_memory (): word = random_word() actr.set_buffer_chunk('imaginal', actr.define_chunks(['verb',word[0], 'stem',word[1], 'suffix',word[2]])[0]) actr.clear_buffer('imaginal')
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)
def create_example_memories(): for s in slots: actr.extend_possible_slots(s,False) actr.define_chunks([s,"isa","chunk"]) for c in cat1: chunk = ["isa","example","category",1] for slot,value in list(zip(slots,c)): chunk.append(slot) chunk.append(value) actr.add_dm(chunk) for c in cat2: chunk = ["isa","example","category",2] for slot,value in list(zip(slots,c)): chunk.append(slot) chunk.append(value) actr.add_dm(chunk)
def present_stim(): global chunks global stims global i chunks = actr.define_chunks(['isa', 'stimulus', 'picture', stims[i]]) actr.set_buffer_chunk('visual', chunks[0]) print('Presented: ', stims[i]) print('correct response: ', cor_resps[i])
def relay_speech_output (model, string): 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)
def create_goal_buffer_chunk(buffer, chunk_desc): name = actr.define_chunks(chunk_desc)[0] actr.schedule_set_buffer_chunk(buffer, name, 0, module='goal', priority=-1000) actr.schedule_event_now("purge-chunk", params=[name], module='goal', priority=':min', maintenance=True)
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)
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)
def present_feedback(): global i global current_response global accuracy feedback = 'no' # check if response matches the appropriate key for the current stimulus in cue #need list of correct responses if current_response[i] == cor_resps[i]: feedback = 'yes' accuracy[i] = 1 chunks = actr.define_chunks(['isa', 'feedback', 'feedback', feedback]) actr.set_buffer_chunk('visual', chunks[0]) print("Feedback given: ", feedback) #increase index for next stimulus i = i + 1 actr.schedule_event_relative(1, 'present_stim')
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
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))
def push_observations(obs): '''Converts the observations to symbols and pushes them to ACT-Rs imaginal buffer''' #chunk = ['wait', 'false', 'left', float(obs[2])] # put observations here #chk = actr.define_chunks(chunk) #actr.schedule_simple_event_now("set-buffer-chunk", ['imaginal', chk[0]]) chunk = [ 'isa', 'game_state', 'extreme_left', float(obs[0]), 'far_left', float(obs[1]), 'left', float(obs[2]), 'ahead', float(obs[3]), 'right', float(obs[4]), 'far_right', float(obs[5]), 'extreme_right', float(obs[6]), 'relative_alt', float(obs[7]), 'distance_to_target', float(obs[8]), 'angle_to_target', float(obs[9]), 'wait', 'false' ] chk = actr.define_chunks(chunk) actr.schedule_simple_event_now("set-buffer-chunk", ['imaginal', chk[0]]) return 1
def handle_observation(observation): '''observation should have chunk format''' chunk = actr.define_chunks(observation) actr.schedule_simple_event_now("set-buffer-chunk", ['imaginal', chunk[0]]) actr.run(10) d = actr.get_history_data("blending-trace") d = json.loads(d) t = actr.get_history_data("blending-times") MP = actr.get_parameter_value(':mp') # #get t t = access_by_key('TEMPERATURE', d[0][1]) # #the values # vs = [actr.chunk_slot_value(x,'value') for x in chunk_names] # factors = [ 'current_altitude', 'view_left', 'view_diagonal_left', 'view_center', 'view_diagonal_right', 'view_right' ] # factors = ['needsFood', 'needsWater'] result_factors = [ 'turn', 'altitude_change', 'diagonal_right_turn', 'right_turn', 'ascending', 'drop_action' ] # result_factors = ['food','water'] results = compute_S(d, factors) # ,'f3']) for sums, result_factor in zip(results, result_factors): print("For", result_factor) for s, factor in zip(sums, factors): print(factor, MP / t * sum(s)) print("actual value is", actr.chunk_slot_value('OBSERVATION0', 'ACTUAL')) print("done")
def load_reward(): global reward reward_chunk = actr.define_chunks( ['isa', 'reward-amount', 'amount', reward]) actr.set_buffer_chunk("visual", reward_chunk[0])
def play(player1, player2): global p1, p2, p1_position, p2_position, game_over, current_player, safety_stop, winner, p1p1, p1p2, p2p1, p2p2 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.add_command('stop-a-run', stop_a_run, 'Set the flag to terminate the game.') win = actr.open_exp_window('Safety', visible=True, height=100, width=100, x=100, y=300) actr.add_button_to_exp_window('Safety', text='STOP', x=0, y=0, action='stop-a-run', height=90, width=90, color='red') actr.add_command('move', make_move, 'Handle player key presses') actr.monitor_command('output-key', 'move') actr.reset() # initialize the game information p1 = player1 p2 = player2 p1_position = 0 p2_position = 5 game_over = False current_player = player1 safety_stop = False winner = None actr.set_current_model(player1) actr.define_chunks(player2) feats = actr.add_visicon_features([ 'isa', ['player-loc', 'player'], 'screen-x', 0, 'screen-y', 0, 'name', player1, 'position', [False, p1_position], 'turn', [False, True] ], [ 'isa', ['player-loc', 'player'], 'screen-x', 100, 'screen-y', 0, 'name', player2, 'position', [False, p2_position] ]) p1p1 = feats[0] p1p2 = feats[1] actr.set_current_model(player2) actr.define_chunks(player1) feats = actr.add_visicon_features([ 'isa', ['player-loc', 'player'], 'screen-x', 0, 'screen-y', 0, 'name', player1, 'position', [False, p1_position], 'turn', [False, True] ], [ 'isa', ['player-loc', 'player'], 'screen-x', 100, 'screen-y', 0, 'name', player2, 'position', [False, p2_position] ]) p2p1 = feats[0] p2p2 = feats[1] 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') actr.clear_exp_window(win) actr.remove_command('stop-a-run') actr.remove_command_monitor('output-key', 'move') actr.remove_command('move') actr.remove_command('is-game-over') actr.remove_command('set_game_over') return winner
def attribute(name,value): goal = actr.define_chunks(["state","add-attribute","name",name,"value", (value + offset)])[0] actr.goal_focus(goal) actr.run(20)
def custom_object_chunk(id): if ids[0] == id: return actr.define_chunks([ "isa", "custom-object", "color", "red", "time", actr.mp_time_ms() ])[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
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