diff --git a/Config.py b/Config.py index 40d8071..08caa2c 100644 --- a/Config.py +++ b/Config.py @@ -35,7 +35,6 @@ """ TRUTH_EPSILON = 0.01 # minimum size that truth-value can be incremented/decremented MAX_EVIDENTIAL_BASE_LENGTH = 20000 # maximum IDs to store documenting evidential base -MAX_INTERACTED_SENTENCES_LENGTH = 20000 """ Default Input Task Values @@ -51,6 +50,7 @@ DEFAULT_QUESTION_PRIORITY = 0.9 DEFAULT_QUESTION_DURABILITY = 0.9 +DEFAULT_GOAL_FREQUENCY = 1.0 DEFAULT_GOAL_CONFIDENCE = 0.9 DEFAULT_GOAL_PRIORITY = 0.9 DEFAULT_GOAL_DURABILITY = 0.9 diff --git a/NALGrammar.py b/NALGrammar.py index ce0514c..712857b 100644 --- a/NALGrammar.py +++ b/NALGrammar.py @@ -66,11 +66,6 @@ def new_sentence_from_string(cls, sentence_string: str): punctuation = NALSyntax.Punctuation.get_punctuation(punctuation_str) assert (punctuation is not None), punctuation_str + " is not punctuation." - # todo add support for goals - assert ( - punctuation == NALSyntax.Punctuation.Judgment or punctuation == NALSyntax.Punctuation.Question), " Currently only accepting Judgments and Questions." - - # Find statement copula, subject string, and predicate string statement = Statement.from_string(sentence_string[start_idx:end_idx + 1]) @@ -80,20 +75,28 @@ def new_sentence_from_string(cls, sentence_string: str): middle_truth_val_idx = sentence_string.find(NALSyntax.StatementSyntax.TruthValDivider.value) end_truth_val_idx = sentence_string.rfind(NALSyntax.StatementSyntax.TruthValMarker.value) - no_truth_value_found = start_truth_val_idx == -1 or end_truth_val_idx == -1 or start_truth_val_idx == end_truth_val_idx - if no_truth_value_found: - # No truth value, use default truth value - truth_value = TruthValue(Config.DEFAULT_JUDGMENT_FREQUENCY, Config.DEFAULT_JUDGMENT_CONFIDENCE) - else: + truth_value_found = not (start_truth_val_idx == -1 or end_truth_val_idx == -1 or start_truth_val_idx == end_truth_val_idx) + freq = None + conf = None + if truth_value_found: # Parse truth value from string freq = float(sentence_string[start_truth_val_idx + 1:middle_truth_val_idx]) conf = float(sentence_string[middle_truth_val_idx + 1:end_truth_val_idx]) - truth_value = TruthValue(freq, conf) if punctuation == NALSyntax.Punctuation.Judgment: - sentence = Judgment(statement, truth_value) + if freq is None: + # No truth value, use default truth value + freq = Config.DEFAULT_JUDGMENT_FREQUENCY + conf = Config.DEFAULT_JUDGMENT_CONFIDENCE + sentence = Judgment(statement, TruthValue(freq, conf)) elif punctuation == NALSyntax.Punctuation.Question: sentence = Question(statement) + elif punctuation == NALSyntax.Punctuation.Goal: + if freq is None: + # No truth value, use default truth value + freq = Config.DEFAULT_GOAL_FREQUENCY + conf = Config.DEFAULT_GOAL_CONFIDENCE + sentence = Goal(statement, DesireValue(freq,conf)) else: assert False,"Error: No Punctuation!" @@ -128,7 +131,6 @@ def may_interact(cls,j1,j2): """ if j1 is None or j2 is None: return False if j1.stamp.id == j2.stamp.id: return False - if j1 in j2.stamp.interacted_sentences: return False # don't need to check the inverse, since they are added mutually if j1 in j2.stamp.evidential_base or j2 in j1.stamp.evidential_base: return False if j1.stamp.evidential_base.has_evidential_overlap(j2.stamp.evidential_base): return False return True @@ -285,7 +287,7 @@ def __init__(self, self_sentence, occurrence_time=None): self.occurrence_time = occurrence_time self.sentence = self_sentence self.evidential_base = EvidentialBase(self_sentence=self_sentence) - self.interacted_sentences = [] # list of sentence this sentence has already interacted with + self.from_conversion = False # is this sentence derived from Conversion? def get_tense(self): if self.occurrence_time is None: @@ -299,15 +301,6 @@ def get_tense(self): elif self.occurrence_time > current_cycle: return NALSyntax.Tense.Future - def mutually_add_to_interacted_sentences(self, other_sentence): - self.interacted_sentences.append(other_sentence) - if len(self.interacted_sentences) > Config.MAX_INTERACTED_SENTENCES_LENGTH: - self.interacted_sentences.pop(0) - - other_sentence.stamp.interacted_sentences.append(self.sentence) - if len(other_sentence.stamp.interacted_sentences) > Config.MAX_INTERACTED_SENTENCES_LENGTH: - other_sentence.stamp.interacted_sentences.pop(0) - class EvidentialBase: """ Stores history of how the sentence was derived diff --git a/NALInferenceRules.py b/NALInferenceRules.py index 3bb558c..1aedf64 100644 --- a/NALInferenceRules.py +++ b/NALInferenceRules.py @@ -256,6 +256,9 @@ def Revision(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence): assert ( j1.statement.term.get_formatted_string() == j2.statement.term.get_formatted_string()), "Cannot revise sentences for 2 different statements" + #todo handle occurrence_time + occurrence_time = None + # Get Truth Value (wp1, w1, wn1), (wp2, w2, wn2) = getevidence_from2sentences(j1, j2) result_truth = F_Revision(wp1=wp1,wn1=wn1,wp2=wp2,wn2=wn2) @@ -297,7 +300,7 @@ def Choice(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence): subjpred2 = j2.subject_predicate # Truth Value - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) # Make the choice if subjpred1 == subjpred2: @@ -423,6 +426,7 @@ def Conversion(j: NALGrammar.Sentence): # merge in the parent sentence's evidential base result.stamp.evidential_base.merge_sentence_evidential_base_into_self(j) + result.stamp.from_conversion = True return result @@ -556,7 +560,7 @@ def IntensionalImage(j: NALGrammar.Sentence): """ ====================================== - ==== (Strong Syllogistic Rules) ==== + ==== (First-order and Higher-order Syllogism) ==== ====================================== """ @@ -585,13 +589,13 @@ def Deduction(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence): j1.statement.get_predicate_term(), j1.statement.get_copula()) - - if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment: + result_truth= None + if j1.punctuation == NALSyntax.Punctuation.Judgment: # Get Truth Value - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) result_truth = F_Deduction(f1,c1,f2,c2) result = NALGrammar.Judgment(result_statement, result_truth) - elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question: + elif j1.punctuation == NALSyntax.Punctuation.Question: result = NALGrammar.Question(result_statement) # merge in the parent sentences' evidential bases @@ -654,12 +658,12 @@ def Analogy(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence): assert(False), "Error: Invalid inputs to nal_analogy: " + j1.get_formatted_string() + " and " + j2.get_formatted_string() result = None - if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment: + if j1.punctuation == NALSyntax.Punctuation.Judgment: # Get Truth Value - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) result_truth = F_Analogy(f1, c1, f2, c2) result = NALGrammar.Judgment(result_statement, result_truth) - elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question: + elif j1.punctuation == NALSyntax.Punctuation.Question: result = NALGrammar.Question(result_statement) # merge in the parent sentences' evidential bases @@ -719,13 +723,13 @@ def Resemblance(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence): assert ( False), "Error: Invalid inputs to nal_resemblance: " + j1.get_formatted_string() + " and " + j2.get_formatted_string() - if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment: + if j1.punctuation == NALSyntax.Punctuation.Judgment: # Truth Value - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) result_truth = F_Resemblance(f1,c1,f2,c2) result = NALGrammar.Judgment(result_statement, result_truth) - elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question: + elif j1.punctuation == NALSyntax.Punctuation.Question: result = NALGrammar.Question(result_statement) # merge in the parent sentences' evidential bases @@ -734,14 +738,6 @@ def Resemblance(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence): return result - -""" - ====================================== - ++++ (Weak Syllogistic Rules) ++++ - ====================================== -""" - - def Abduction(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence): """ Abduction (Weak syllogism) @@ -770,13 +766,13 @@ def Abduction(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence): j1.statement.get_subject_term(), j1.statement.get_copula()) - if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment: + if j1.punctuation == NALSyntax.Punctuation.Judgment: # Get Truth Value - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) result_truth = F_Abduction(f1,c1,f2,c2) result = NALGrammar.Judgment(result_statement, result_truth) - elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question: + elif j1.punctuation == NALSyntax.Punctuation.Question: result = NALGrammar.Question(result_statement) # merge in the parent sentences' evidential bases @@ -814,7 +810,7 @@ def Induction(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence): if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment: # Get Truth Value - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) result_truth = F_Induction(f1,c1,f2,c2) result = NALGrammar.Judgment(result_statement, result_truth) @@ -856,7 +852,7 @@ def Exemplification(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence): if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment: # Get Truth Value - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) result_truth = F_Exemplification(f1,c1,f2,c2) result = NALGrammar.Judgment(result_statement, result_truth) elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question: @@ -907,7 +903,7 @@ def Comparison(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence): if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment: # Get Truth Value - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) result_truth = F_Comparison(f1,c1,f2,c2) result = NALGrammar.Judgment(result_statement, result_truth) @@ -983,7 +979,7 @@ def IntensionalIntersectionOrDisjunction(j1, j2): if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment: # Get Truth Value - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) result_truth = F_Intersection(f1,c1,f2,c2) result = NALGrammar.Judgment(result_statement, result_truth) @@ -1003,7 +999,7 @@ def IntensionalIntersectionOrDisjunction(j1, j2): if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment: # Get Truth Value - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) result_truth = F_Union(f1,c1,f2,c2) result = NALGrammar.Judgment(result_statement, result_truth) @@ -1076,7 +1072,7 @@ def ExtensionalIntersectionOrConjunction(j1, j2): if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment: # Get Truth Value - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) result_truth = F_Union(f1,c1,f2,c2) result = NALGrammar.Judgment(result_statement, result_truth) elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question: @@ -1094,7 +1090,7 @@ def ExtensionalIntersectionOrConjunction(j1, j2): if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment: # Get Truth Value - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) result_truth = F_Intersection(f1,c1,f2,c2) result = NALGrammar.Judgment(result_statement, result_truth) elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question: @@ -1136,7 +1132,7 @@ def IntensionalDifference(j1, j2): if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment: # Get Truth Value - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) result_truth = F_Difference(f1,c1,f2,c2) result = NALGrammar.Judgment(result_statement, result_truth) elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question: @@ -1190,7 +1186,7 @@ def ExtensionalDifference(j1, j2): if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment: # Get Truth Value - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) result_truth = F_Difference(f1,c1,f2,c2) result = NALGrammar.Judgment(result_statement, result_truth) elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question: @@ -1227,7 +1223,7 @@ def Temporal_Induction(j1: NALGrammar.Judgment, j2: NALGrammar.Judgment): :- or Sentence (S =/> P ) :- or Sentence (P =/> S ) """ - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) j1_statement_term = j1.statement.term j2_statement_term = j2.statement.term @@ -1270,7 +1266,7 @@ def Temporal_Comparison(j1: NALGrammar.Judgment, j2: NALGrammar.Judgment): :- or Sentence (S P ) :- or Sentence (P S ) """ - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) j1_statement_term = j1.statement.term j2_statement_term = j2.statement.term @@ -1300,7 +1296,6 @@ def Temporal_Comparison(j1: NALGrammar.Judgment, j2: NALGrammar.Judgment): ====================================== """ - def get_truthvalue_from_evidence(wp, w): """ Input: @@ -1340,7 +1335,7 @@ def get_confidence_from_evidence(w): """ return w / (w + Config.k) -def gettruthvalues_from2sentences(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence): +def getevidentialvalues_from2sentences(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence): """ Input: j1: Statement @@ -1349,10 +1344,10 @@ def gettruthvalues_from2sentences(j1: NALGrammar.Sentence, j2: NALGrammar.Senten Returns: f1, c1, f2, c2 """ - return gettruthvalues_fromsentence(j1), gettruthvalues_fromsentence(j2) + return getevidentialvalues_fromsentence(j1), getevidentialvalues_fromsentence(j2) -def gettruthvalues_fromsentence(j: NALGrammar.Sentence): +def getevidentialvalues_fromsentence(j: NALGrammar.Sentence): """ Input: j: Statement @@ -1371,5 +1366,5 @@ def getevidence_from2sentences(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence) Returns: w1+, w1, w1-, w2+, w2, w2- """ - (f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2) + (f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2) return get_evidence_fromfreqconf(f1, c1), get_evidence_fromfreqconf(f2, c2) diff --git a/NARS.py b/NARS.py index 74bb9e3..da61ab1 100644 --- a/NARS.py +++ b/NARS.py @@ -171,7 +171,7 @@ def Consider(self): if concept_to_consider is not None: if len(concept_to_consider.belief_table) > 0: sentence = concept_to_consider.belief_table.peek() # get most confident belief - self.process_judgment_sentence(sentence) + self.process_sentence(sentence) # decay priority concept_item.decay() @@ -186,11 +186,15 @@ def process_task(self, task: NARSDataStructures.Task): """ NARSDataStructures.assert_task(task) - if task.sentence.punctuation == NALSyntax.Punctuation.Question \ - or NALGrammar.VariableTerm.QUERY_SYM in str(task.sentence.statement.term): - self.process_question(task) - elif task.sentence.punctuation == NALSyntax.Punctuation.Judgment: + punctuation = task.sentence.punctuation + + if punctuation == NALSyntax.Punctuation.Judgment: self.process_judgment_task(task) + elif punctuation == NALSyntax.Punctuation.Question \ + or NALGrammar.VariableTerm.QUERY_SYM in str(task.sentence.statement.term): + self.process_question_task(task) + elif punctuation == NALSyntax.Punctuation.Goal: + self.process_goal_task(task) def process_judgment_task(self, task: NARSDataStructures.Task): """ @@ -205,24 +209,22 @@ def process_judgment_task(self, task: NARSDataStructures.Task): # get terms from sentence statement_term = j1.statement.term - # get (or create if necessary) statement concept, and sub-term concepts recursively - statement_concept = self.memory.peek_concept(statement_term) - if statement_term.contains_variable(): return #todo handle variables - + + statement_concept = None if task.needs_initial_processing: """ Initial Processing Revise this judgment with the most confident belief, then insert it into the belief table """ - #if task.is_from_input: derived_sentences = NARSInferenceEngine.do_inference_one_premise(j1) for derived_sentence in derived_sentences: self.experience_task_buffer.put(NARSDataStructures.Task(derived_sentence)) - # revise the judgment - self.process_judgment_sentence(j1, statement_concept) + # get (or create if necessary) statement concept, and sub-term concepts recursively + statement_concept = self.memory.peek_concept(statement_term) + # add the judgment itself into concept's belief table statement_concept.belief_table.put(j1) task.needs_initial_processing = False @@ -232,54 +234,12 @@ def process_judgment_task(self, task: NARSDataStructures.Task): Do local/forward inference on a related belief """ - self.process_judgment_sentence(j1) - - - def process_judgment_sentence(self, j1, related_concept=None): - """ - Processes a Judgment Sentence with a belief from a related concept. - - :param j1 - judgment sentence to process - :param related_concept - (Optional) concept to process the judgment with - - #todo handle variables - #todo handle tenses - """ - statement_term = j1.statement.term - # get (or create if necessary) statement concept, and sub-term concepts recursively - statement_concept = self.memory.peek_concept(statement_term) - - if statement_term.contains_variable(): return - j2 = None - if related_concept is None: - number_of_attempts = 0 - while j2 is None and number_of_attempts < Config.NUMBER_OF_ATTEMPTS_TO_SEARCH_FOR_SEMANTICALLY_RELATED_BELIEF: # try searching a maximum of 3 concepts - related_concept = self.memory.get_semantically_related_concept(statement_concept) - if related_concept is None: - return # no related concepts! Should never happen, the concept is always semantically related to itself - - # check for a belief we can interact with + pass - for (belief, confidence) in related_concept.belief_table: - if NALGrammar.Sentence.may_interact(j1,belief): - j2 = belief # belief can interact with j1 - break + # revise the judgment + self.process_sentence(j1, statement_concept) - number_of_attempts += 1 - if j2 is None: print (related_concept.get_formatted_string() + ' concept was no good') - else: - for (belief, confidence) in related_concept.belief_table: - if NALGrammar.Sentence.may_interact(j1, belief): - j2 = belief # belief can interact with j1 - break - - if j2 is None: return # done if can't interact - - derived_sentences = NARSInferenceEngine.do_semantic_inference_two_premise(j1, j2) - for derived_sentence in derived_sentences: - self.experience_task_buffer.put(NARSDataStructures.Task(derived_sentence)) - - def process_question(self, task): + def process_question_task(self, task): """ Process a Narsese question task @@ -316,20 +276,91 @@ def process_question(self, task): # do inference between question and a related belief j1 = task.sentence - # get a related concept - related_concept = self.memory.get_semantically_related_concept(statement_concept) - if related_concept is None: return # no related concepts! + self.process_sentence(j1) + + def process_goal_task(self, task: NARSDataStructures.Task): + """ + Processes a Narsese Goal Task + + :param Goal Task to process + """ + NARSDataStructures.assert_task(task) + + j1 = task.sentence + + # get terms from sentence + statement_term = j1.statement.term + + if statement_term.contains_variable(): return # todo handle variables + + statement_concept = None + if task.needs_initial_processing: + """ + Initial Processing + + Revise this judgment with the most confident belief, then insert it into the belief table + """ + # derived_sentences = NARSInferenceEngine.do_inference_one_premise(j1) + # for derived_sentence in derived_sentences: + # self.experience_task_buffer.put(NARSDataStructures.Task(derived_sentence)) + + # get (or create if necessary) statement concept, and sub-term concepts recursively + statement_concept = self.memory.peek_concept(statement_term) + + # add the judgment itself into concept's desire table + statement_concept.desire_table.put(j1) + task.needs_initial_processing = False + else: + """ + Continued processing + + Do local/forward inference on a related belief + """ + pass + + self.process_sentence(j1, statement_concept) + + + def process_sentence(self, j1, related_concept=None): + """ + Processes a Judgment Sentence with a belief from a related concept. + + :param j1 - judgment sentence to process + :param related_concept - (Optional) concept to process the judgment with + + #todo handle variables + #todo handle tenses + """ + statement_term = j1.statement.term + # get (or create if necessary) statement concept, and sub-term concepts recursively + statement_concept = self.memory.peek_concept(statement_term) - # check for a belief we can interact with + if statement_term.contains_variable(): return j2 = None - for (belief,confidence) in related_concept.belief_table: - if NALGrammar.Sentence.may_interact(j1,belief): - j2 = belief # belief can interact with j1. - break + if related_concept is None: + number_of_attempts = 0 + while j2 is None and number_of_attempts < Config.NUMBER_OF_ATTEMPTS_TO_SEARCH_FOR_SEMANTICALLY_RELATED_BELIEF: # try searching a maximum of 3 concepts + related_concept = self.memory.get_semantically_related_concept(statement_concept) + if related_concept is None: + return # no related concepts! Should never happen, the concept is always semantically related to itself + + # check for a belief we can interact with + + for (belief, confidence) in related_concept.belief_table: + if NALGrammar.Sentence.may_interact(j1,belief): + j2 = belief # belief can interact with j1 + break + + number_of_attempts += 1 + if j2 is None: print (related_concept.get_formatted_string() + ' concept was no good') + else: + for (belief, confidence) in related_concept.belief_table: + if NALGrammar.Sentence.may_interact(j1, belief): + j2 = belief # belief can interact with j1 + break if j2 is None: return # done if can't interact derived_sentences = NARSInferenceEngine.do_semantic_inference_two_premise(j1, j2) - # add all derived tasks to the buffer for derived_sentence in derived_sentences: self.experience_task_buffer.put(NARSDataStructures.Task(derived_sentence)) diff --git a/NARSDataStructures.py b/NARSDataStructures.py index 3fb6dbd..048ba61 100644 --- a/NARSDataStructures.py +++ b/NARSDataStructures.py @@ -107,7 +107,10 @@ def __init__(self, object, id): priority = None quality = None if isinstance(object, Task): - priority = 0.950 + if isinstance( object.sentence, NALGrammar.Judgment): + priority = object.sentence.value.confidence + else: + priority = 0.95 quality = 0.010 elif isinstance(object, NARSMemory.Concept): priority = 0.990 / object.term.syntactic_complexity diff --git a/NARSGUI.py b/NARSGUI.py index 78d0116..e5bf7aa 100644 --- a/NARSGUI.py +++ b/NARSGUI.py @@ -371,18 +371,6 @@ def listbox_sentence_item_click_callback(event, iterable_with_sentences): lambda event: listbox_sentence_item_click_callback(event, sentence_from_iterable.stamp.evidential_base)) - # Interacted sentences listbox - label = tk.Label(item_info_window, text="Sentence Interacted Sentences", font=('bold')) - label.grid(row=4, column=2, columnspan=2) - - interacted_sentences_listbox = tk.Listbox(item_info_window, height=object_listbox_height, - width=object_listbox_width, font=('', 8)) - interacted_sentences_listbox.grid(row=5, column=2, columnspan=2) - for sentence in sentence_from_iterable.stamp.interacted_sentences: - interacted_sentences_listbox.insert(tk.END, str(sentence)) - interacted_sentences_listbox.bind("<>", - lambda event: listbox_sentence_item_click_callback(event, - sentence_from_iterable.stamp.interacted_sentences)) def listbox_datastructure_item_click_callback(event): @@ -504,19 +492,6 @@ def listbox_datastructure_item_click_callback(event): evidential_base_listbox.bind("<>", lambda event: listbox_sentence_item_click_callback(event, object.sentence.stamp.evidential_base)) - # Interacted sentences listbox - label = tk.Label(item_info_window, text="Sentence Interacted Sentences", font=('bold')) - label.grid(row=3, column=2, columnspan=2) - - interacted_sentences_listbox = tk.Listbox(item_info_window, height=object_listbox_height, - width=object_listbox_width, font=('', 8)) - interacted_sentences_listbox.grid(row=4, column=2, columnspan=2) - for sentence in object.sentence.stamp.interacted_sentences: - interacted_sentences_listbox.insert(tk.END, str(sentence)) - interacted_sentences_listbox.bind("<>", - lambda event: listbox_sentence_item_click_callback(event, - object.sentence.stamp.interacted_sentences)) - def toggle_pause(event=None): """ diff --git a/NARSInferenceEngine.py b/NARSInferenceEngine.py index 55db849..4d03200 100644 --- a/NARSInferenceEngine.py +++ b/NARSInferenceEngine.py @@ -249,8 +249,6 @@ def do_semantic_inference_two_premise(j1: NALGrammar.Sentence, j2: NALGrammar.Se =============================================== =============================================== """ - # mark sentences as interacted with each other - j1.stamp.mutually_add_to_interacted_sentences(j2) return derived_sentences @@ -272,8 +270,7 @@ def do_temporal_inference_two_premise(A: NALGrammar.Sentence, B: NALGrammar.Sent =============================================== =============================================== """ - # mark sentences as interacted with each other - A.stamp.mutually_add_to_interacted_sentences(B) + return derived_sentences @@ -295,7 +292,8 @@ def do_inference_one_premise(j): derived_sentences.append(derived_sentence) # Conversion (P --> S) - if not NALSyntax.Copula.is_symmetric(j.statement.get_copula()) \ + if not j.stamp.from_conversion \ + and not NALSyntax.Copula.is_symmetric(j.statement.get_copula()) \ and j.value.frequency > 0: derived_sentence = NALInferenceRules.Conversion(j) print_inference_rule(inference_rule="Conversion") diff --git a/TestCases/InferenceTests.py b/TestCases/InferenceTests.py index a23ba53..f5893ff 100644 --- a/TestCases/InferenceTests.py +++ b/TestCases/InferenceTests.py @@ -41,14 +41,14 @@ def run_test(input_judgment_q, input_question_q, output_q, debug=False): InputBuffer.add_input_sentence(input_judgment_q.get()) # process judgments - Global.Global.NARS.do_working_cycles(100) + Global.Global.NARS.do_working_cycles(200) # feed in questions while input_question_q.qsize() > 0: InputBuffer.add_input_sentence(input_question_q.get()) # process questions - Global.Global.NARS.do_working_cycles(100) + Global.Global.NARS.do_working_cycles(50) sys.stdout = sys.__stdout__