Skip to content
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

Improve notebook controller API #114

Merged
merged 8 commits into from
Nov 26, 2024

Conversation

corentinlger
Copy link
Collaborator

@corentinlger corentinlger commented Nov 25, 2024

Description

Improve notebook controller API on various points:

  • Only execute routines / behaviors of existing entities in the controller run
  • Add helper attributes to access existing / non-existing agents
  • Add a catch_error flag to either automatically catch errors for routines or not (could be renamed debug_mode or something similar). You can launch it like that with the run function of the controller:
controller.run(catch_errors=False) #default is set to True
  • Add locking mechanism to routine handler to prevent modifying the routines while they are called (e.g removing a routine during a routine step, resulting in an error)

While updating the new eating function, I got this (old is the old eating routine function, new the one I did, and Clem the one you did):

import timeit

from functools import partial

num_iterations = 1000

new = timeit.timeit(new, number=num_iterations)
old = timeit.timeit(old, number=num_iterations)
clem = timeit.timeit(clem, number=num_iterations)

print(f"Old took {old:.6f} seconds")
print(f"New took {new:.6f} seconds")
print(f"Clem took {clem:.6f} seconds")

Session 5:

Old took 0.806134 seconds
New took 1.754245 seconds
Clem took 1.760777 seconds

Session neuroevo_ABC:

Old took 0.043899 seconds
New took 0.124735 seconds
Clem took 0.116994 seconds

Seems like the old function works better, can you also try this on your computer ?

@corentinlger corentinlger merged commit 9741618 into main Nov 26, 2024
2 checks passed
Copy link
Collaborator

@clement-moulin-frier clement-moulin-frier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! See my comments in the code. Also:

  • could be renamed debug_mode or something similar: Yes
  • Seems like the old function works better, can you also try this on your computer ? : Can you send me the code to run?

@@ -127,7 +126,9 @@ def __init__(self, config):
self.active_behaviors = {}
self.eating_range = 10
self.diet = []
# TODO : see if we keep both
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need It I think. Is has_eaten_since(time=1) equivalent to the previous has_eaten()?
Maybe also avoid the work "meal", I'd like to keep a low-level biological flavor in this library. Maybe time_since_last_consumption?

# execute behaviors of agents
if entity.etype == EntityType.AGENT:
entity.behave(self.time)
# TODO: see if we do this in a dedicated function or not
# increment time since last meal for all alive agents
entity.time_since_last_meal += 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be inside the routine instead?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general we should avoid specific mechanisms within the generic run loop, they should all be in routines


:return: True if the agent has eaten since the given time, False otherwise
"""
return self.time_since_last_meal < time
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double check this will work as expected (init of the variable, comparison operator..)


# finally stop the simulation
self.stop()
self.pause()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To discuss


def stop(self):
"""Stop the simulation
"""Stop the simulation and detach all routines
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also detach all behaviors?


def eating_routine_fn(controller):
"""make agents of the simulation eating the entities in their diet
def eating_routine_classic(controller):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could rename it eating_range_routine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants