diff --git a/pynars/ConsolePlus.py b/pynars/ConsolePlus.py index 9f5b4ffb..67bea909 100644 --- a/pynars/ConsolePlus.py +++ b/pynars/ConsolePlus.py @@ -220,6 +220,12 @@ def toggle_silent() -> None: else "closed" }.''') +@cmd_register('cycles') +def cycles(*args: List[str]) -> None: + '''Prints the "average cycles per second" metric''' + if(current_NARS_interface.reasoner.cycles_count == 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 {int(1 // current_NARS_interface.reasoner.avg_cycle_duration)} based on the last {current_NARS_interface.reasoner.cycles_count} cycles. Last cycle took {current_NARS_interface.reasoner.last_cycle_duration:.6f} seconds.''') @cmd_register(('volume'), (int, 100)) def volume(vol:int) -> None: diff --git a/pynars/NARS/Control/Reasoner.py b/pynars/NARS/Control/Reasoner.py index fb6d1028..7d535756 100644 --- a/pynars/NARS/Control/Reasoner.py +++ b/pynars/NARS/Control/Reasoner.py @@ -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: @@ -50,6 +49,11 @@ def __init__(self, n_memory, capacity, config='./config.json', nal_rules={1, 2, self.u_top_level_attention = 0.5 + # metrics + self.cycles_count = 0 + self.last_cycle_duration = 0 + self.avg_cycle_duration = 0 + def reset(self): self.memory.reset() self.overall_experience.reset() @@ -76,6 +80,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() """Everything to do by NARS in a single working cycle""" Global.States.reset() tasks_derived: List[Task] = [] @@ -107,9 +112,14 @@ def cycle(self): thresh_complexity = 20 tasks_derived = [ task for task in tasks_derived if task.term.complexity <= thresh_complexity] + + """done with cycle""" + 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 consider(self, tasks_derived: List[Task]): """ Consider a Concept in the Memory @@ -272,3 +282,11 @@ def register_operator(self, name_operator: str, callback: Callable): Operation.register(op, callback) return op return None + + 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 + # calculate average + self.cycles_count += 1 + self.avg_cycle_duration += (self.last_cycle_duration - self.avg_cycle_duration) / self.cycles_count