Skip to content

Commit

Permalink
Reward tasklinks like tasks, for #77
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrock4t committed Jan 31, 2024
1 parent 1e6dfd2 commit 5698837
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
5 changes: 4 additions & 1 deletion pynars/NARS/DataStructures/_py/Link.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 9 additions & 4 deletions pynars/NARS/DataStructures/_py/Memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pynars/Narsese/_py/Task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down

0 comments on commit 5698837

Please sign in to comment.