Skip to content

Working Cycle Metric #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 13, 2024
5 changes: 5 additions & 0 deletions pynars/ConsolePlus.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ def toggle_silent() -> None:
else "closed"
}.''')

@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.''')

@cmd_register(('volume'), (int, 100))
def volume(vol:int) -> None:
Expand Down
22 changes: 21 additions & 1 deletion pynars/NARS/Control/Reasoner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import Callable, List, Tuple, Union
import pynars.NARS.Operation as Operation
from pynars import Global
from time import time
import time
from pynars.NAL.Functions.Tools import project_truth, project
from ..GlobalEval import GlobalEval

Expand Down Expand Up @@ -50,6 +50,12 @@ 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

def reset(self):
self.memory.reset()
self.overall_experience.reset()
Expand All @@ -76,6 +82,7 @@ def input_narsese(self, text, go_cycle: bool = False) -> Tuple[bool, Union[Task,
return success, task, task_overflow

def cycle(self):
start_cycle_time_in_seconds = time.time()
"""Everything to do by NARS in a single working cycle"""
Global.States.reset()
tasks_derived: List[Task] = []
Expand Down Expand Up @@ -107,6 +114,19 @@ def cycle(self):
thresh_complexity = 20
tasks_derived = [
task for task in tasks_derived if task.term.complexity <= thresh_complexity]

"""done with cycle"""
# record some metrics
total_cycle_duration_in_seconds = time.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

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

Expand Down