From 569883752e25043d6fc76bef369db15814e68cbe Mon Sep 17 00:00:00 2001 From: ccrock4t <15344554+ccrock4t@users.noreply.github.com> Date: Wed, 31 Jan 2024 12:20:01 -0500 Subject: [PATCH] Reward tasklinks like tasks, for #77 --- pynars/NARS/DataStructures/_py/Link.py | 5 ++++- pynars/NARS/DataStructures/_py/Memory.py | 13 +++++++++---- pynars/Narsese/_py/Task.py | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/pynars/NARS/DataStructures/_py/Link.py b/pynars/NARS/DataStructures/_py/Link.py index 6daaaf7..51bc822 100644 --- a/pynars/NARS/DataStructures/_py/Link.py +++ b/pynars/NARS/DataStructures/_py/Link.py @@ -239,12 +239,15 @@ def __init__(self, source: 'Concept', target: 'Concept', budget: Budget, copy_bu def set_type(self, source_is_component=True, type: LinkType=None): Link.set_type(self, source_is_component, type, enable_transform=True) if not self.is_valid: self.type = None + + def reward_budget(self, reward: float): + self.budget.priority = Or(self.budget.priority, reward) @property def is_valid(self) -> bool: return self.type in ( LinkType.SELF, - LinkType.COMPOUND, + LinkType.COMPOUND, LinkType.COMPOUND_STATEMENT, LinkType.COMPOUND_CONDITION, LinkType.TRANSFORM, diff --git a/pynars/NARS/DataStructures/_py/Memory.py b/pynars/NARS/DataStructures/_py/Memory.py index fcd8a35..827fb9b 100644 --- a/pynars/NARS/DataStructures/_py/Memory.py +++ b/pynars/NARS/DataStructures/_py/Memory.py @@ -311,8 +311,11 @@ def _solve_judgement(self, belief_task: Task, concept: Concept): answer = solution_question(question_task, belief_task) new_answer = question.best_answer if old_answer != new_answer: - # the belief is a better answer to the question, so reward it - belief_task.reward_budget_priority(question_task.achieving_level()) + # the belief is a better answer to the question, so reward the Task and tasklinks + reward = question_task.achieving_level() + belief_task.reward_budget(reward) + for task_link in concept.task_links: + task_link.reward_budget(reward) if answer is not None: answers.append(answer) # 2. try to solve wh-questions sub_terms = belief_task.term.sub_terms @@ -393,8 +396,10 @@ def _solve_goal(self, goal_task: Task, concept: Concept, task_link: TaskLink=Non if belief_task is None or belief_task == old_best: return tasks, None elif belief_task != old_best: - belief_task.reward_budget_priority(goal_task.achieving_level()) - + reward = goal_task.achieving_level() + belief_task.reward_budget(reward) + for task_link in concept.task_links: + task_link.reward_budget(reward) if old_best is not None: quality_new = calculate_solution_quality(goal_task.sentence, belief_task.sentence, True) diff --git a/pynars/Narsese/_py/Task.py b/pynars/Narsese/_py/Task.py index d54e5f8..4c16e70 100644 --- a/pynars/Narsese/_py/Task.py +++ b/pynars/Narsese/_py/Task.py @@ -40,7 +40,7 @@ def achieving_level(self, previous_belief: Truth=None): else: raise f'Invalid type! {type(self.sentence)}' - def reward_budget_priority(self, reward: float): + def reward_budget(self, reward: float): self.budget.priority = NAL.Functions.Or(self.budget.priority, reward) def reduce_budget_by_achieving_level(self, belief_selected: Union[Type['Belief'], None]):