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 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], 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