Skip to content

Commit

Permalink
keep a running average of cycle duration
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrock4t committed May 2, 2024
1 parent 2b2fd70 commit 83e8275
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
3 changes: 3 additions & 0 deletions pynars/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class Config:

maximum_evidental_base_length = 20000

# cycle metrics
cycle_durations_window_length = 100

@classmethod
def check(cls):
'''Check if each parameter is valid'''
Expand Down
5 changes: 3 additions & 2 deletions pynars/ConsolePlus.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,9 @@ def toggle_silent() -> None:
@cmd_register('cycles')
def cycles(*args: List[str]) -> None:
'''Prints the "average cycles per second" metric'''
current_NARS_interface.print_output(
type=PrintType.INFO, content=f'''Cycles per second is {current_NARS_interface.reasoner.cycles_per_second}. Last cycle took {current_NARS_interface.reasoner.last_cycle_duration:f} seconds.''')
if(len(current_NARS_interface.reasoner.cycles_durations_window) == 0): current_NARS_interface.print_output(type=PrintType.INFO, content="No cycles have been run yet.")
else: current_NARS_interface.print_output(
type=PrintType.INFO, content=f'''The average cycles per second is {1 / current_NARS_interface.reasoner.avg_cycle_duration} based on the last {len(current_NARS_interface.reasoner.cycles_durations_window)} cycles. Last cycle took {current_NARS_interface.reasoner.last_cycle_duration:.32f} seconds.''')

@cmd_register(('volume'), (int, 100))
def volume(vol:int) -> None:
Expand Down
34 changes: 20 additions & 14 deletions pynars/NARS/Control/Reasoner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from ..GlobalEval import GlobalEval



class Reasoner:

def __init__(self, n_memory, capacity, config='./config.json', nal_rules={1, 2, 3, 4, 5, 6, 7, 8, 9}) -> None:
Expand Down Expand Up @@ -51,10 +50,10 @@ def __init__(self, n_memory, capacity, config='./config.json', nal_rules={1, 2,
self.u_top_level_attention = 0.5

# metrics
self.cycles_per_second_timer = 0
self.cycles_per_second_counter = 0
self.cycles_per_second = 0
self.last_cycle_duration = 0
self.cycles_durations_window = []
self.cycles_duration_window_sum = 0
self.avg_cycle_duration = 0

def reset(self):
self.memory.reset()
Expand Down Expand Up @@ -116,20 +115,27 @@ def cycle(self):
task for task in tasks_derived if task.term.complexity <= thresh_complexity]

"""done with cycle"""
# record some metrics
total_cycle_duration_in_seconds = time() - start_cycle_time_in_seconds
self.last_cycle_duration = total_cycle_duration_in_seconds
self.cycles_per_second_timer += total_cycle_duration_in_seconds
self.cycles_per_second_counter += 1
if self.cycles_per_second_timer > 1:
# 1 second has passed
self.cycles_per_second = self.cycles_per_second_counter # store the result
self.cycles_per_second_timer = 0 # reset the timer
self.cycles_per_second_counter = 0 # reset the counter
self.do_cycle_metrics(start_cycle_time_in_seconds)

return tasks_derived, judgement_revised, goal_revised, answers_question, answers_quest, (
task_operation_return, task_executed)

def do_cycle_metrics(self, start_cycle_time_in_seconds: float):
# record some metrics
total_cycle_duration_in_seconds = time() - start_cycle_time_in_seconds
self.last_cycle_duration = total_cycle_duration_in_seconds # store the cycle duration
# put it in with the others to find the avg duration
self.cycles_durations_window.append(total_cycle_duration_in_seconds)
self.cycles_duration_window_sum += total_cycle_duration_in_seconds

# we only want to keep track of a certain number of cycles, so remove old cycles
if len(self.cycles_durations_window) > Config.Config.cycle_durations_window_length:

self.cycles_duration_window_sum -= self.cycles_durations_window[0]
self.cycles_durations_window.pop(0)

self.avg_cycle_duration = self.cycles_duration_window_sum / len(self.cycles_durations_window) # avg seconds per 1 cycle

def consider(self, tasks_derived: List[Task]):
"""
Consider a Concept in the Memory
Expand Down

0 comments on commit 83e8275

Please sign in to comment.