Skip to content

Commit

Permalink
Merge pull request #105 from bowen-xu/running-mean
Browse files Browse the repository at this point in the history
calculate running mean of cycle duration
  • Loading branch information
ccrock4t authored May 3, 2024
2 parents 7c7b8c4 + eec912c commit 3f231d7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
4 changes: 2 additions & 2 deletions pynars/ConsolePlus.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ def toggle_silent() -> None:
@cmd_register('cycles')
def cycles(*args: List[str]) -> None:
'''Prints the "average cycles per second" metric'''
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.")
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 {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.''')
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:
Expand Down
18 changes: 4 additions & 14 deletions pynars/NARS/Control/Reasoner.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ 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.cycles_durations_window = []
self.cycles_duration_window_sum = 0
self.avg_cycle_duration = 0

def reset(self):
Expand Down Expand Up @@ -288,15 +287,6 @@ 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

# calculate average
self.cycles_count += 1
self.avg_cycle_duration += (self.last_cycle_duration - self.avg_cycle_duration) / self.cycles_count

0 comments on commit 3f231d7

Please sign in to comment.