Skip to content

Commit

Permalink
Change item level properly when changing Concept budget
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrock4t committed Jan 18, 2024
1 parent 58a68fb commit 40cc2cb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
34 changes: 21 additions & 13 deletions pynars/NARS/DataStructures/_py/Concept.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Tuple, Type, List, Union

from pynars.NAL.Functions import Or
from pynars.NAL.Functions.Tools import calculate_solution_quality, distribute_budget_among_links
from pynars.NAL.Functions.BudgetFunctions import Budget_merge
from pynars.Narsese import Belief, Task, Item, Budget, Sentence, Term, Task, Judgement, Goal
Expand Down Expand Up @@ -139,22 +141,28 @@ def accept(self, task: Task, concepts: Bag=None, conceptualize: bool=True):
self._build_task_links(concepts, task)
self._build_term_links(concepts, task, budget)

def update_priority(self, p):
def update_priority(self, p, concepts: Bag):
concepts.take_by_key(key=self,remove=True)
self.budget.priority = Or(self.budget.priority, p)
concepts.put(item=self)

def update_durability(self, d):
def update_durability(self, d, concepts: Bag):
concepts.take_by_key(key=self, remove=True)
self.budget.durability = (Config.concept_update_durability_weight * d
+ (1-Config.concept_update_durability_weight)*self.budget.durability)
concepts.put(item=self)

def update_quality(self, q):
def update_quality(self, q, concepts: Bag):
concepts.take_by_key(key=self, remove=True)
self.budget.quality = (Config.concept_update_quality_weight * q
+ (1-Config.concept_update_quality_weight)*self.budget.quality)
concepts.put(item=self)

def _build_task_links(self, concepts: Bag, task: Task):
''''''
budget = task.budget
task_link = TaskLink(self, task, budget, True, index=[])
self._insert_task_link(task_link)
self._insert_task_link(task_link, concepts)
if self.term.is_atom: return
sub_budget = budget.distribute(self.term.count()-1) # TODO: It seems that the budget is not the same with that in OpenNARS 3.0.4/3.1.0. Check here.
for term in self.term.components:
Expand All @@ -165,7 +173,7 @@ def _build_task_links(self, concepts: Bag, task: Task):
indices = Link.get_index(self.term, term)
for index in indices:
task_link = TaskLink(concept, task, sub_budget, index=index)
concept._insert_task_link(task_link)
concept._insert_task_link(task_link, concepts)

def _build_term_links(self, concepts: Bag, task: Task, budget: Budget):
'''
Expand All @@ -192,24 +200,24 @@ def _build_term_links(self, concepts: Bag, task: Task, budget: Budget):

indices = Link.get_index(self.term, term)
for index in indices:
self._insert_term_link(TermLink(self, sub_concept, sub_budget, False, index=index))
sub_concept._insert_term_link(TermLink(sub_concept, self, sub_budget, True, index=index))
self._insert_term_link(TermLink(self, sub_concept, sub_budget, False, index=index), concepts)
sub_concept._insert_term_link(TermLink(sub_concept, self, sub_budget, True, index=index), concepts)

sub_concept._build_term_links(concepts, task, sub_budget)


def _insert_task_link(self, task_link: TaskLink):
def _insert_task_link(self, task_link: TaskLink, concepts: Bag):
self.task_links.put(task_link)
# update the concept's budget using the link's budget
self.update_priority(task_link.budget.priority)
self.update_durability(task_link.budget.durability)
self.update_priority(task_link.budget.priority, concepts)
self.update_durability(task_link.budget.durability, concepts)
# TODO: more handling. see OpenNARS 3.1.0 Concept.java line 318~366.

def _insert_term_link(self, term_link: TermLink):
def _insert_term_link(self, term_link: TermLink, concepts: Bag):
self.term_links.put(term_link)
# update the concept's budget using the link's budget
self.update_priority(term_link.budget.priority)
self.update_durability(term_link.budget.durability)
self.update_priority(term_link.budget.priority, concepts)
self.update_durability(term_link.budget.durability, concepts)
# TODO: more handling. see OpenNARS 3.1.0 Concept.java line 318~366.

@classmethod
Expand Down
8 changes: 4 additions & 4 deletions pynars/NARS/DataStructures/_py/Memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def accept(self, task: Task):

def _accept_judgement(self, task: Task, concept: Concept):
''''''
belief_revised = None
belief_revised_task = None
answers = { Question: [], Goal: [] }
if Enable.operation: raise # InternalExperienceBuffer.handleOperationFeedback(task, nal);
if Enable.anticipation: raise # ProcessAnticipation.confirmAnticipation(task, concept, nal);
Expand All @@ -124,7 +124,7 @@ def _accept_judgement(self, task: Task, concept: Concept):
}
'''
raise
belief_revised = local__revision(task, belief) # TODO: handling the stamps
belief_revised_task: Task = local__revision(task, belief) # TODO: handling the stamps
# reduce priority by achieving level
task.reduce_budget_by_achieving_level(belief)

Expand All @@ -147,9 +147,9 @@ def _accept_judgement(self, task: Task, concept: Concept):
if goal_answer is not None: answers[Goal] = [goal_answer]

# Modify the concept's Budget using the belief
if belief_revised is not None: concept.update_quality(belief_revised.sharpness)
if belief_revised_task is not None: concept.update_quality(belief_revised_task.sentence.sharpness, concepts=self.concepts)

return belief_revised, answers
return belief_revised_task, answers

def _accept_question(self, task: Task, concept: Concept):
''''''
Expand Down
4 changes: 2 additions & 2 deletions pynars/Narsese/_py/Sentence.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def directness(self):

@property
def sharpness(self):
if self.truth_value is None: return None
else: return 2 * abs(self.truth_value.e - 0.5)
if self.truth is None: return None
else: return 2 * abs(self.truth.e - 0.5)

# @property
# def temporal_order(self):
Expand Down

0 comments on commit 40cc2cb

Please sign in to comment.