def create(self, otherchunk, actrvariables=None): """ Create (aka set) a chunk for manual control. The chunk is returned (and could be used by device or external environment). """ if actrvariables == None: actrvariables = {} try: mod_attr_val = { x[0]: utilities.check_bound_vars(actrvariables, x[1]) for x in otherchunk.removeunused() } #creates dict of attr-val pairs according to otherchunk except ACTRError as arg: raise ACTRError( "Setting the chunk '%s' in the manual buffer is impossible; %s" % (otherchunk, arg)) new_chunk = chunks.Chunk(self._MANUAL, **mod_attr_val) #creates new chunk if new_chunk.cmd.values not in utilities.CMDMANUAL: raise ACTRError( "Motor module received an invalid command: '%s'. The valid commands are: '%s'" % (new_chunk.cmd.values, utilities.CMDMANUAL)) if new_chunk.cmd.values == utilities.CMDPRESSKEY: pressed_key = new_chunk.key.values.upper( ) #change key into upper case mod_attr_val["key"] = pressed_key new_chunk = chunks.Chunk(self._MANUAL, **mod_attr_val) #creates new chunk if pressed_key not in self.LEFT_HAND and new_chunk.key.values not in self.RIGHT_HAND: raise ACTRError("Motor module received an invalid key: %s" % pressed_key) return new_chunk
def create(self, otherchunk, harvest=None, actrvariables=None): """ Create (aka set) a chunk in goal buffer. """ try: mod_attr_val = {x[0]: utilities.check_bound_vars(actrvariables, x[1]) for x in otherchunk.removeunused()} #creates dict of attr-val pairs according to otherchunk except utilities.ACTRError as arg: raise utilities.ACTRError("Setting the buffer using the chunk '%s' is impossible; %s" % (otherchunk, arg)) new_chunk = chunks.Chunk(otherchunk.typename, **mod_attr_val) #creates new chunk self.add(new_chunk, 0, harvest) #put chunk using add
def modify(self, otherchunk, actrvariables=None): """ Modify the chunk in Buffer according to the info in otherchunk. """ if actrvariables == None: actrvariables = {} elem = self._data.pop() try: mod_attr_val = {x[0]: utilities.check_bound_vars(actrvariables, x[1]) for x in otherchunk.removeunused()} #creates dict of attr-val pairs according to otherchunk except utilities.ACTRError as arg: raise utilities.ACTRError("The modification by the chunk '%s is impossible; %s" % (otherchunk, arg)) elem_attr_val = {x[0]: x[1] for x in elem} elem_attr_val.update(mod_attr_val) #updates original chunk with attr-val from otherchunk mod_chunk = chunks.Chunk(otherchunk.typename, **elem_attr_val) #creates new chunk self._data.add(mod_chunk) #put chunk directly into buffer
def modify(self, otherchunk, actrvariables=None): """ Modify the chunk in Buffer according to the info in otherchunk. """ if actrvariables == None: actrvariables = {} elem = self._data.pop() try: mod_attr_val = {x[0]: utilities.check_bound_vars(actrvariables, x[1]) for x in otherchunk.removeunused()} #creates dict of attr-val pairs according to otherchunk except utilities.ACTRError as arg: raise utilities.ACTRError("The chunk '%s' is not defined correctly; %s" % (otherchunk, arg)) elem_attr_val = {x[0]: x[1] for x in elem} elem_attr_val.update(mod_attr_val) #updates original chunk with attr-val from otherchunk mod_chunk = chunks.Chunk(otherchunk.typename, **elem_attr_val) #creates new chunk self._data.add(mod_chunk) #put chunk directly into buffer
def retrieveorset(self, name, updated, otherchunk, temp_actrvariables, time): """ Find out whether a buffer should be set (for buffers that are not attached to any dm, i.e., Goal or Motor or Vision) or should trigger retrieval. """ updated.state = updated._BUSY if isinstance(updated, goals.Goal): yield from self.clear(name, updated, otherchunk, temp_actrvariables, time, freeing=False) extra_time = utilities.calculate_setting_time(updated) time += extra_time yield Event(roundtime(time), name, self._UNKNOWN) if self.model_parameters['production_compilation']: RHSdict = otherchunk._asdict() RHSdict = {item[0]: item[1] for item in RHSdict.items()} self.current_slotvals[name] = RHSdict updated.create(otherchunk, list(self.dm.values())[0], temp_actrvariables) created_elem = list(updated)[0] updated.state = updated._FREE yield Event(roundtime(time), name, "CREATED A CHUNK: %s" % str(created_elem)) elif isinstance(updated, vision.VisualLocation): extra_time = utilities.calculate_setting_time(updated) time += extra_time #0 ms to create chunk in location (pop-up effect) yield Event(roundtime(time), name, self._UNKNOWN) chunk, stim = updated.find(otherchunk, actrvariables=temp_actrvariables, extra_tests=self.extra_tests.get(name, {})) #extra_time currently ignored if chunk: yield from self.clear(name, updated, None, temp_actrvariables, time, freeing=False) updated.add(chunk, stim, time) updated.state = updated._FREE else: updated.state = updated._ERROR yield Event(roundtime(time), name, "ENCODED LOCATION:'%s'" % str(chunk)) elif isinstance(updated, vision.Visual): mod_attr_val = {x[0]: utilities.check_bound_vars(temp_actrvariables, x[1]) for x in otherchunk.removeunused()} if (not mod_attr_val['cmd'].values) or mod_attr_val['cmd'].values not in utilities.CMDVISUAL: raise ACTRError("Visual module received no command or an invalid command: '%s'. The valid commands are: '%s'" % (mod_attr_val['cmd'].values, utilities.CMDVISUAL)) if mod_attr_val['cmd'].values == utilities.CMDMOVEATTENTION: ret = yield from self.visualshift(name, updated, otherchunk, temp_actrvariables, time) return ret #visual action returns value, namely, its continuation method elif mod_attr_val['cmd'].values == utilities.CMDCLEAR: updated.stop_automatic_buffering() updated.state = updated._FREE yield Event(roundtime(time), name, "VISUAL STOPPED FROM AUTOMATIC BUFFERING AT ITS CURRENT FOCUS") elif isinstance(updated, motor.Motor): ret = yield from self.motorset(name, updated, otherchunk, temp_actrvariables, time) return ret #motor action returns value, namely, its continuation method else: yield from self.retrieve(name, updated, otherchunk, temp_actrvariables, time)
def create(self, otherchunk, harvest=None, actrvariables=None): """ Create (aka set) a chunk in goal buffer. """ try: mod_attr_val = { x[0]: utilities.check_bound_vars(actrvariables, x[1]) for x in otherchunk.removeunused() } #creates dict of attr-val pairs according to otherchunk except utilities.ACTRError as arg: raise utilities.ACTRError( "Setting the buffer using the chunk '%s' is impossible; %s" % (otherchunk, arg)) new_chunk = chunks.Chunk(otherchunk.typename, **mod_attr_val) #creates new chunk self.add(new_chunk, 0, harvest) #put chunk using add
def shift(self, otherchunk, harvest=None, actrvariables=None): """ Return a chunk, time needed to attend and shift eye focus to the chunk, and the landing site of eye mvt. """ if actrvariables == None: actrvariables = {} try: mod_attr_val = {x[0]: utilities.check_bound_vars(actrvariables, x[1]) for x in otherchunk.removeunused()} #creates dict of attr-val pairs according to otherchunk except ACTRError as arg: raise ACTRError("The chunk '%s' is not defined correctly; %s" % (otherchunk, arg)) vis_delay = None for each in self.environment.stimulus: try: if self.environment.stimulus[each]['position'] == (float(mod_attr_val['screen_pos'].screen_x), float(mod_attr_val['screen_pos'].screen_y)): mod_attr_val['value'] = self.environment.stimulus[each]['text'] vis_delay = self.environment.stimulus[each].get('vis_delay') except (AttributeError, KeyError): raise ACTRError("The chunk in the visual buffer is not defined correctly. It is not possible to move attention.") new_chunk = chunks.Chunk(self._VISUAL, **mod_attr_val) #creates new chunk if new_chunk.cmd not in utilities.CMDVISUAL: raise ACTRError("Visual module received an invalid command: '%s'. The valid commands are: '%s'" % (new_chunk.cmd, utilities.CMDVISUAL)) if new_chunk.cmd == utilities.CMDMOVEATTENTION and self.model_parameters['emma']: angle_distance = utilities.calculate_visual_angle(self.current_focus, [float(new_chunk.screen_pos.screen_x), float(new_chunk.screen_pos.screen_y)], self.environment.size, self.environment.simulated_screen_size, self.environment.viewing_distance) encoding_time = utilities.calculate_delay_visual_attention(angle_distance=angle_distance, K=self.model_parameters["eye_mvt_scaling_parameter"], k=self.model_parameters['eye_mvt_angle_parameter'], emma_noise=self.model_parameters['emma_noise'], vis_delay=vis_delay) preparation_time = utilities.calculate_preparation_time(emma_noise=self.model_parameters['emma_noise']) execution_time = utilities.calculate_execution_time(angle_distance, emma_noise=self.model_parameters['emma_noise']) landing_site = utilities.calculate_landing_site([float(new_chunk.screen_pos.screen_x), float(new_chunk.screen_pos.screen_y)], angle_distance, emma_landing_site_noise=self.model_parameters['emma_landing_site_noise']) elif new_chunk.cmd == utilities.CMDMOVEATTENTION and not self.model_parameters['emma']: encoding_time = 0.085 preparation_time = 0 execution_time = 0.085 landing_site = (float(new_chunk.screen_pos.screen_x), float(new_chunk.screen_pos.screen_y)) else: raise ACTRError("Visual module received an invalid command: '%s'. The only valid command currently is: %s" % (new_chunk.cmd, utilities.CMDMOVEATTENTION)) return new_chunk, (encoding_time, preparation_time, execution_time), landing_site
def shift(self, otherchunk, harvest=None, actrvariables=None, model_parameters=None): """ Return a chunk, time needed to attend and shift eye focus to the chunk, and the landing site of eye mvt. """ if model_parameters == None: model_parameters = {} model_parameters = model_parameters.copy() model_parameters.update(self.model_parameters) if actrvariables == None: actrvariables = {} try: mod_attr_val = {x[0]: utilities.check_bound_vars(actrvariables, x[1]) for x in otherchunk.removeunused()} except ACTRError as arg: raise ACTRError("Shifting towards the chunk '%s' is impossible; %s" % (otherchunk, arg)) vis_delay = None for each in self.environment.stimulus: try: if self.environment.stimulus[each]['position'] == (float(mod_attr_val['screen_pos'].values.screen_x.values), float(mod_attr_val['screen_pos'].values.screen_y.values)): mod_attr_val['value'] = self.environment.stimulus[each]['text'] vis_delay = self.environment.stimulus[each].get('vis_delay') except (AttributeError, KeyError): raise ACTRError("The chunk in the visual buffer is not defined correctly. It is not possible to move attention.") new_chunk = chunks.Chunk(self._VISUAL, **mod_attr_val) #creates new chunk if model_parameters['emma']: angle_distance = utilities.calculate_visual_angle(self.environment.current_focus, [float(new_chunk.screen_pos.values.screen_x.values), float(new_chunk.screen_pos.values.screen_y.values)], self.environment.size, self.environment.simulated_screen_size, self.environment.viewing_distance) encoding_time = utilities.calculate_delay_visual_attention(angle_distance=angle_distance, K=model_parameters["eye_mvt_scaling_parameter"], k=model_parameters['eye_mvt_angle_parameter'], emma_noise=model_parameters['emma_noise'], vis_delay=vis_delay) preparation_time = utilities.calculate_preparation_time(emma_noise=model_parameters['emma_noise'], emma_preparation_time=model_parameters['emma_preparation_time']) execution_time = utilities.calculate_execution_time(angle_distance, emma_noise=model_parameters['emma_noise']) landing_site = utilities.calculate_landing_site([float(new_chunk.screen_pos.values.screen_x.values), float(new_chunk.screen_pos.values.screen_y.values)], angle_distance, emma_landing_site_noise=model_parameters['emma_landing_site_noise']) elif not model_parameters['emma']: encoding_time = 0.085 preparation_time = 0 execution_time = 0.085 landing_site = (float(new_chunk.screen_pos.values.screen_x.values), float(new_chunk.screen_pos.values.screen_y.values)) return new_chunk, (encoding_time, preparation_time, execution_time), landing_site
def shift(self, otherchunk, harvest=None, actrvariables=None, model_parameters=None): """ Return a chunk, time needed to attend and shift eye focus to the chunk, and the landing site of eye mvt. """ if model_parameters == None: model_parameters = {} model_parameters = model_parameters.copy() model_parameters.update(self.model_parameters) if actrvariables == None: actrvariables = {} try: mod_attr_val = {x[0]: utilities.check_bound_vars(actrvariables, x[1]) for x in otherchunk.removeunused()} except ACTRError as arg: raise ACTRError("Shifting towards the chunk '%s' is impossible; %s" % (otherchunk, arg)) vis_delay = None for each in self.environment.stimulus: try: if self.environment.stimulus[each]['position'] == (float(mod_attr_val['screen_pos'].values.screen_x.values), float(mod_attr_val['screen_pos'].values.screen_y.values)): mod_attr_val['value'] = self.environment.stimulus[each]['text'] vis_delay = self.environment.stimulus[each].get('vis_delay') except (AttributeError, KeyError): raise ACTRError("The chunk in the visual buffer is not defined correctly. It is not possible to move attention.") new_chunk = chunks.Chunk(self._VISUAL, **mod_attr_val) #creates new chunk if model_parameters['emma']: angle_distance = utilities.calculate_visual_angle(self.environment.current_focus, [float(new_chunk.screen_pos.values.screen_x.values), float(new_chunk.screen_pos.values.screen_y.values)], self.environment.size, self.environment.simulated_screen_size, self.environment.viewing_distance) encoding_time = utilities.calculate_delay_visual_attention(angle_distance=angle_distance, K=model_parameters["eye_mvt_scaling_parameter"], k=model_parameters['eye_mvt_angle_parameter'], emma_noise=model_parameters['emma_noise'], vis_delay=vis_delay) preparation_time = utilities.calculate_preparation_time(emma_noise=model_parameters['emma_noise']) execution_time = utilities.calculate_execution_time(angle_distance, emma_noise=model_parameters['emma_noise']) landing_site = utilities.calculate_landing_site([float(new_chunk.screen_pos.values.screen_x.values), float(new_chunk.screen_pos.values.screen_y.values)], angle_distance, emma_landing_site_noise=model_parameters['emma_landing_site_noise']) elif not model_parameters['emma']: encoding_time = 0.085 preparation_time = 0 execution_time = 0.085 landing_site = (float(new_chunk.screen_pos.values.screen_x.values), float(new_chunk.screen_pos.values.screen_y.values)) return new_chunk, (encoding_time, preparation_time, execution_time), landing_site
def create(self, otherchunk, actrvariables=None): """ Create (aka set) a chunk for manual control. The chunk is returned (and could be used by device or external environment). """ if actrvariables == None: actrvariables = {} try: mod_attr_val = {x[0]: utilities.check_bound_vars(actrvariables, x[1]) for x in otherchunk.removeunused()} #creates dict of attr-val pairs according to otherchunk except ACTRError as arg: raise ACTRError("Setting the chunk '%s' in the manual buffer is impossible; %s" % (otherchunk, arg)) new_chunk = chunks.Chunk(self._MANUAL, **mod_attr_val) #creates new chunk if new_chunk.cmd.values not in utilities.CMDMANUAL: raise ACTRError("Motor module received an invalid command: '%s'. The valid commands are: '%s'" % (new_chunk.cmd.values, utilities.CMDMANUAL)) if new_chunk.cmd.values == utilities.CMDPRESSKEY: pressed_key = new_chunk.key.values.upper() #change key into upper case mod_attr_val["key"] = pressed_key new_chunk = chunks.Chunk(self._MANUAL, **mod_attr_val) #creates new chunk if pressed_key not in self.LEFT_HAND and new_chunk.key.values not in self.RIGHT_HAND: raise ACTRError("Motor module received an invalid key: %s" % pressed_key) return new_chunk
def retrieve(self, time, otherchunk, actrvariables, buffers, extra_tests, model_parameters): """ Retrieve a chunk from declarative memory that matches otherchunk. """ model_parameters = model_parameters.copy() model_parameters.update(self.model_parameters) if actrvariables == None: actrvariables = {} try: mod_attr_val = {x[0]: utilities.check_bound_vars(actrvariables, x[1], negative_impossible=False) for x in otherchunk.removeunused()} except utilities.ACTRError as arg: raise utilities.ACTRError("Retrieving the chunk '%s' is impossible; %s" % (otherchunk, arg)) chunk_tobe_matched = chunks.Chunk(otherchunk.typename, **mod_attr_val) max_A = float("-inf") #collect the subset of dm that is useful (only chunks that match the searched chunk will be used if len(chunk_tobe_matched.removeunused()) == 0 or ( model_parameters["subsymbolic"] and model_parameters["partial_matching"]): used_dm = self.dm else: used_dm = {} for x in chunk_tobe_matched.removeunused(): temp_chunk = chunks.Chunk(typename=getattr(chunk_tobe_matched, "typename"), **{x[0]: x[1]}) temp_data = {} for x in self.dm._data: if temp_chunk <= x: temp_data.update(self.dm._data[x]) #update used_dm with found chunks (either by creating it, if it is empty, or by intersecting with already present chunks) if not used_dm: used_dm = temp_data elif len(used_dm) <= len(temp_data): temp_data2 = {} for i in used_dm: if i in temp_data: temp_data2[i] = temp_data[i] used_dm = temp_data2 elif len(temp_data) < len(used_dm): temp_data2 = {} for i in temp_data: if i in used_dm: temp_data2[i] = used_dm[i] used_dm = temp_data2 retrieved = None #loop through this subset and check activation for chunk in used_dm: try: if extra_tests["recently_retrieved"] == False or extra_tests["recently_retrieved"] == 'False': if self.__finst and chunk in self.recent: continue else: if self.__finst and chunk not in self.recent: continue except KeyError: pass if model_parameters["subsymbolic"]: #if subsymbolic, check activation A_pm = 0 if model_parameters["partial_matching"]: A_pm = chunk_tobe_matched.match(chunk, partialmatching=True, mismatch_penalty=model_parameters["mismatch_penalty"]) else: if not chunk_tobe_matched <= chunk: continue try: A_bll = utilities.baselevel_learning(time, self.dm[chunk], model_parameters["baselevel_learning"], model_parameters["decay"], self.dm.activations.get(chunk), optimized_learning=model_parameters["optimized_learning"]) #bll except UnboundLocalError: continue if math.isnan(A_bll): raise utilities.ACTRError("The following chunk cannot receive base activation: %s. The reason is that one of its traces did not appear in a past moment." % chunk) try: A_sa = utilities.spreading_activation(chunk, buffers, self.dm, model_parameters["buffer_spreading_activation"], model_parameters["strength_of_association"], model_parameters["spreading_activation_restricted"], model_parameters["association_only_from_chunks"]) except IndexError: A_sa = float(0) inst_noise = utilities.calculate_instantaneous_noise(model_parameters["instantaneous_noise"]) A = A_bll + A_sa + A_pm + inst_noise #chunk.activation is the manually specified activation, potentially used by the modeller if utilities.retrieval_success(A, model_parameters["retrieval_threshold"]) and max_A < A: self.spreading_activation = A_sa max_A = A self.activation = max_A retrieved = chunk extra_time = utilities.retrieval_latency(A, model_parameters["latency_factor"], model_parameters["latency_exponent"]) if model_parameters["activation_trace"]: print("(Partially) matching chunk:", chunk) print("Base level learning:", A_bll) print("Spreading activation", A_sa) print("Partial matching", A_pm) print("Noise:", inst_noise) print("Total activation", A) print("Time to retrieve", extra_time) else: #otherwise, just standard time for rule firing, so no extra calculation needed if chunk_tobe_matched <= chunk and self.dm[chunk][0] != time: #the second condition ensures that the chunk that was created are not retrieved at the same time retrieved = chunk extra_time = model_parameters["rule_firing"] if not retrieved: self.activation, self.spreading_activation = None, None if model_parameters["subsymbolic"]: extra_time = utilities.retrieval_latency(model_parameters["retrieval_threshold"], model_parameters["latency_factor"], model_parameters["latency_exponent"]) else: extra_time = model_parameters["rule_firing"] if self.__finst: self.recent.append(retrieved) if self.__finst < len(self.recent): self.recent.popleft() return retrieved, extra_time
def retrieve(self, time, otherchunk, actrvariables, buffers, extra_tests, model_parameters): """ Retrieve a chunk from declarative memory that matches otherchunk. """ model_parameters = model_parameters.copy() model_parameters.update(self.model_parameters) if actrvariables == None: actrvariables = {} try: mod_attr_val = { x[0]: utilities.check_bound_vars(actrvariables, x[1]) for x in otherchunk.removeunused() } except utilities.ACTRError as arg: raise utilities.ACTRError( "The chunk '%s' is not defined correctly; %s" % (otherchunk, arg)) chunk_tobe_matched = chunks.Chunk(otherchunk.typename, **mod_attr_val) max_A = float("-inf") retrieved = None for chunk in self.dm: try: if extra_tests["recently_retrieved"] == False or extra_tests[ "recently_retrieved"] == 'False': if self.__finst and chunk in self.recent: continue else: if self.__finst and chunk not in self.recent: continue except KeyError: pass if model_parameters[ "subsymbolic"]: #if subsymbolic, check activation A_pm = 0 if model_parameters["partial_matching"]: A_pm = chunk_tobe_matched.match( chunk, partialmatching=True, mismatch_penalty=model_parameters["mismatch_penalty"]) else: if not chunk_tobe_matched <= chunk: continue if chunk in self.dm.activations: A_bll = utilities.baselevel_learning( time, self.dm[chunk], model_parameters["baselevel_learning"], model_parameters["decay"], self.dm.activations[chunk], optimized_learning=model_parameters[ "optimized_learning"]) #bll else: A_bll = utilities.baselevel_learning( time, self.dm[chunk], model_parameters["baselevel_learning"], model_parameters["decay"], optimized_learning=model_parameters[ "optimized_learning"]) #bll A_sa = utilities.spreading_activation( chunk, buffers, self.dm, model_parameters["buffer_spreading_activation"], model_parameters["strength_of_association"], model_parameters["spreading_activation_restricted"], model_parameters["association_only_from_chunks"]) inst_noise = utilities.calculate_instantanoues_noise( model_parameters["instantaneous_noise"]) A = A_bll + A_sa + A_pm + inst_noise #chunk.activation is the manually specified activation, potentially used by the modeller if utilities.retrieval_success( A, model_parameters["retrieval_threshold"]) and max_A < A: max_A = A self.activation = max_A retrieved = chunk extra_time = utilities.retrieval_latency( A, model_parameters["latency_factor"], model_parameters["latency_exponent"]) if model_parameters["activation_trace"]: print("(Partially) matching chunk:", chunk) print("Base level learning:", A_bll) print("Spreading activation", A_sa) print("Partial matching", A_pm) print("Noise:", inst_noise) print("Total activation", A) print("Time to retrieve", extra_time) else: #otherwise, just standard time for rule firing if chunk_tobe_matched <= chunk: retrieved = chunk extra_time = model_parameters["rule_firing"] if not retrieved: if model_parameters["subsymbolic"]: extra_time = utilities.retrieval_latency( model_parameters["retrieval_threshold"], model_parameters["latency_factor"], model_parameters["latency_exponent"]) else: extra_time = model_parameters["rule_firing"] if self.__finst: self.recent.append(retrieved) if self.__finst < len(self.recent): self.recent.popleft() return retrieved, extra_time
def find(self, otherchunk, actrvariables=None, extra_tests=None): """ Set a chunk in vision based on what is on the screen. """ if extra_tests == None: extra_tests = {} if actrvariables == None: actrvariables = {} try: mod_attr_val = {x[0]: utilities.check_bound_vars(actrvariables, x[1]) for x in otherchunk.removeunused()} except utilities.ACTRError as arg: raise utilities.ACTRError("The chunk '%s' is not defined correctly; %s" % (otherchunk, arg)) chunk_used_for_search = chunks.Chunk(utilities.VISUALLOCATION, **mod_attr_val) found = None found_stim = None closest = float("inf") x_closest = float("inf") y_closest = float("inf") current_x = None current_y = None for each in self.environment.stimulus: position = (int(self.environment.stimulus[each]['position'][0]), int(self.environment.stimulus[each]['position'][1])) try: #checks absolute position if chunk_used_for_search.screen_x and int(chunk_used_for_search.screen_x) != position[0]: continue except (TypeError, ValueError): pass try: #checks absolute position if chunk_used_for_search.screen_y and int(chunk_used_for_search.screen_y) != position[1]: continue except (TypeError, ValueError): pass try: #checks on x and y relative positions if chunk_used_for_search.screen_x[0] == utilities.VISIONSMALLER and int(chunk_used_for_search.screen_x[1:]) <= position[0]: continue elif chunk_used_for_search.screen_x[0] == utilities.VISIONGREATER and int(chunk_used_for_search.screen_x[1:]) >= position[0]: continue except (TypeError, IndexError): pass try: #checks on x and y relative positions if chunk_used_for_search.screen_y[0] == utilities.VISIONSMALLER and int(chunk_used_for_search.screen_y[1:]) <= position[1]: continue elif chunk_used_for_search.screen_y[0] == utilities.VISIONGREATER and int(chunk_used_for_search.screen_y[1:]) >= position[1]: continue except (TypeError, IndexError): pass try: #checks on x and y absolute positions if chunk_used_for_search.screen_x == utilities.VISIONLOWEST and current_x != None and position[0] > current_x: continue elif chunk_used_for_search.screen_x == utilities.VISIONHIGHEST and current_x != None and position[0] < current_x: continue except TypeError: pass try: #checks on x and y absolute positions if chunk_used_for_search.screen_y == utilities.VISIONLOWEST and current_y != None and position[1] > current_y: continue elif chunk_used_for_search.screen_y == utilities.VISIONHIGHEST and current_y != None and position[1] < current_y: continue except TypeError: pass try: if extra_tests["attended"] == False or extra_tests["attended"] == 'False': if self.finst and self.environment.stimulus[each] in self.recent: continue else: if self.finst and self.environment.stimulus[each] not in self.recent: continue except KeyError: pass try: #checks on closest if (chunk_used_for_search.screen_x == utilities.VISIONCLOSEST or chunk_used_for_search.screen_y == utilities.VISIONCLOSEST) and utilities.calculate_pythagorian_distance(self.environment.current_focus, position) > closest: continue except TypeError: pass try: #checks on onewayclosest if (chunk_used_for_search.screen_x == utilities.VISIONONEWAYCLOSEST) and utilities.calculate_onedimensional_distance(self.environment.current_focus, position, horizontal=True) > x_closest: continue except TypeError: pass try: #checks on onewayclosest if (chunk_used_for_search.screen_y == utilities.VISIONONEWAYCLOSEST) and utilities.calculate_onedimensional_distance(self.environment.current_focus, position, horizontal=False) > y_closest: continue except TypeError: pass found_stim = self.environment.stimulus[each] visible_chunk = chunks.makechunk(nameofchunk="vis1", typename="_visuallocation", **{key: each[key] for key in self.environment.stimulus[each] if key != 'position' and key != 'text' and key != 'vis_delay'}) if visible_chunk <= chunk_used_for_search: temp_dict = visible_chunk._asdict() temp_dict.update({"screen_x":position[0], "screen_y":position[1]}) found = chunks.Chunk(utilities.VISUALLOCATION, **temp_dict) current_x = position[0] current_y = position[1] closest = utilities.calculate_pythagorian_distance(self.environment.current_focus, position) x_closest = utilities.calculate_onedimensional_distance(self.environment.current_focus, position, horizontal=True) y_closest = utilities.calculate_onedimensional_distance(self.environment.current_focus, position, horizontal=False) return found, found_stim
def find(self, otherchunk, actrvariables=None, extra_tests=None): """ Set a chunk in vision based on what is on the screen. """ if extra_tests == None: extra_tests = {} if actrvariables == None: actrvariables = {} try: mod_attr_val = {x[0]: utilities.check_bound_vars(actrvariables, x[1], negative_impossible=False) for x in otherchunk.removeunused()} except utilities.ACTRError as arg: raise utilities.ACTRError("The chunk '%s' is not defined correctly; %s" % (otherchunk, arg)) chunk_used_for_search = chunks.Chunk(utilities.VISUALLOCATION, **mod_attr_val) found = None found_stim = None closest = float("inf") x_closest = float("inf") y_closest = float("inf") current_x = None current_y = None for each in self.environment.stimulus: #extra test applied first try: if extra_tests["attended"] == False or extra_tests["attended"] == 'False': if self.finst and self.environment.stimulus[each] in self.recent: continue else: if self.finst and self.environment.stimulus[each] not in self.recent: continue except KeyError: pass #check value in text; in principle, you can search based on any value, so this is more powerful than actual visual search if chunk_used_for_search.value != chunk_used_for_search.EmptyValue() and chunk_used_for_search.value.values != self.environment.stimulus[each]["text"]: continue position = (int(self.environment.stimulus[each]['position'][0]), int(self.environment.stimulus[each]['position'][1])) #check absolute position; exception on AttributeError is to avoid the case in which the slot has empty value (in that case, the attribute "values" is undefined) try: if chunk_used_for_search.screen_x.values and int(chunk_used_for_search.screen_x.values) != position[0]: continue except (TypeError, ValueError, AttributeError): pass try: if chunk_used_for_search.screen_y.values and int(chunk_used_for_search.screen_y.values) != position[1]: continue except (TypeError, ValueError, AttributeError): pass #check on x and y relative positions try: if chunk_used_for_search.screen_x.values[0] == utilities.VISIONSMALLER and int(chunk_used_for_search.screen_x.values[1:]) <= position[0]: continue elif chunk_used_for_search.screen_x.values[0] == utilities.VISIONGREATER and int(chunk_used_for_search.screen_x.values[1:]) >= position[0]: continue except (TypeError, IndexError, AttributeError): pass try: if chunk_used_for_search.screen_y.values[0] == utilities.VISIONSMALLER and int(chunk_used_for_search.screen_y.values[1:]) <= position[1]: continue elif chunk_used_for_search.screen_y.values[0] == utilities.VISIONGREATER and int(chunk_used_for_search.screen_y.values[1:]) >= position[1]: continue except (TypeError, IndexError, AttributeError): pass #check on x and y absolute positions try: if chunk_used_for_search.screen_x.values == utilities.VISIONLOWEST and current_x != None and position[0] > current_x: continue elif chunk_used_for_search.screen_x.values == utilities.VISIONHIGHEST and current_x != None and position[0] < current_x: continue except (TypeError, AttributeError): pass try: if chunk_used_for_search.screen_y.values == utilities.VISIONLOWEST and current_y != None and position[1] > current_y: continue elif chunk_used_for_search.screen_y.values == utilities.VISIONHIGHEST and current_y != None and position[1] < current_y: continue except (TypeError, AttributeError): pass #check on closest try: if (chunk_used_for_search.screen_x.values == utilities.VISIONCLOSEST or chunk_used_for_search.screen_y.values == utilities.VISIONCLOSEST) and utilities.calculate_pythagorian_distance(self.environment.current_focus, position) > closest: continue except (TypeError, AttributeError): pass #check on onewayclosest try: if (chunk_used_for_search.screen_x.values == utilities.VISIONONEWAYCLOSEST) and utilities.calculate_onedimensional_distance(self.environment.current_focus, position, horizontal=True) > x_closest: continue except (TypeError, AttributeError): pass try: if (chunk_used_for_search.screen_y.values == utilities.VISIONONEWAYCLOSEST) and utilities.calculate_onedimensional_distance(self.environment.current_focus, position, horizontal=False) > y_closest: continue except (TypeError, AttributeError): pass found_stim = self.environment.stimulus[each] visible_chunk = chunks.makechunk(nameofchunk="vis1", typename="_visuallocation", **{key: each[key] for key in self.environment.stimulus[each] if key != 'position' and key != 'text' and key != 'vis_delay'}) if visible_chunk <= chunk_used_for_search: temp_dict = visible_chunk._asdict() temp_dict.update({"screen_x":position[0], "screen_y":position[1]}) found = chunks.Chunk(utilities.VISUALLOCATION, **temp_dict) current_x = position[0] current_y = position[1] closest = utilities.calculate_pythagorian_distance(self.environment.current_focus, position) x_closest = utilities.calculate_onedimensional_distance(self.environment.current_focus, position, horizontal=True) y_closest = utilities.calculate_onedimensional_distance(self.environment.current_focus, position, horizontal=False) return found, found_stim
def retrieve(self, time, otherchunk, actrvariables, buffers, extra_tests, model_parameters): """ Retrieve a chunk from declarative memory that matches otherchunk. """ model_parameters = model_parameters.copy() model_parameters.update(self.model_parameters) if actrvariables == None: actrvariables = {} try: mod_attr_val = {x[0]: utilities.check_bound_vars(actrvariables, x[1], negative_impossible=False) for x in otherchunk.removeunused()} except utilities.ACTRError as arg: raise utilities.ACTRError("Retrieving the chunk '%s' is impossible; %s" % (otherchunk, arg)) chunk_tobe_matched = chunks.Chunk(otherchunk.typename, **mod_attr_val) max_A = float("-inf") retrieved = None for chunk in self.dm: try: if extra_tests["recently_retrieved"] == False or extra_tests["recently_retrieved"] == 'False': if self.__finst and chunk in self.recent: continue else: if self.__finst and chunk not in self.recent: continue except KeyError: pass if model_parameters["subsymbolic"]: #if subsymbolic, check activation A_pm = 0 if model_parameters["partial_matching"]: A_pm = chunk_tobe_matched.match(chunk, partialmatching=True, mismatch_penalty=model_parameters["mismatch_penalty"]) else: if not chunk_tobe_matched <= chunk: continue try: A_bll = utilities.baselevel_learning(time, self.dm[chunk], model_parameters["baselevel_learning"], model_parameters["decay"], self.dm.activations.get(chunk), optimized_learning=model_parameters["optimized_learning"]) #bll except UnboundLocalError: continue if math.isnan(A_bll): raise utilities.ACTRError("The following chunk cannot receive base activation: %s. The reason is that one of its traces did not appear in a past moment." % chunk) A_sa = utilities.spreading_activation(chunk, buffers, self.dm, model_parameters["buffer_spreading_activation"], model_parameters["strength_of_association"], model_parameters["spreading_activation_restricted"], model_parameters["association_only_from_chunks"]) inst_noise = utilities.calculate_instantaneous_noise(model_parameters["instantaneous_noise"]) A = A_bll + A_sa + A_pm + inst_noise #chunk.activation is the manually specified activation, potentially used by the modeller if utilities.retrieval_success(A, model_parameters["retrieval_threshold"]) and max_A < A: max_A = A self.activation = max_A retrieved = chunk extra_time = utilities.retrieval_latency(A, model_parameters["latency_factor"], model_parameters["latency_exponent"]) if model_parameters["activation_trace"]: print("(Partially) matching chunk:", chunk) print("Base level learning:", A_bll) print("Spreading activation", A_sa) print("Partial matching", A_pm) print("Noise:", inst_noise) print("Total activation", A) print("Time to retrieve", extra_time) else: #otherwise, just standard time for rule firing, so no extra calculation needed if chunk_tobe_matched <= chunk and self.dm[chunk][0] != time: #the second condition ensures that the chunk that was created are not retrieved at the same time retrieved = chunk extra_time = model_parameters["rule_firing"] if not retrieved: if model_parameters["subsymbolic"]: extra_time = utilities.retrieval_latency(model_parameters["retrieval_threshold"], model_parameters["latency_factor"], model_parameters["latency_exponent"]) else: extra_time = model_parameters["rule_firing"] if self.__finst: self.recent.append(retrieved) if self.__finst < len(self.recent): self.recent.popleft() return retrieved, extra_time