Adding weakref by default to Agents #2763
Replies: 3 comments 8 replies
-
It's worth exploring further. However, there are at least two issues to be aware of.
I am curious to see a benchmark for this idea on your virus model to check the overhead. Just to get a sense of point 1. Because of point 2, I am inclined to also improve the best practices documentation with a separate entry on agents, memory, weakrefs, and agentset. This would add value regardless of point 1. Next to using weakrefs, another option is to rely on |
Beta Was this translation helpful? Give feedback.
-
@Sahil-Chhoker @quaquel For now, what would you think about adding weakrefs by default only to direct references (or as an option when making a model and building our agent classes) ? If the overhead is alright, would it be an interesting feature, or not enough added value alone ? |
Beta Was this translation helpful? Give feedback.
-
After a bit of experimenting to make simple direct referencing weakref, I get this :
Overhead takes a big hit in all models, and I don't think that I can optimize it much more (though I will try) so I don't think that it's a good idea to implement is as a default feature. The question is, can it still be useful to implement it as an option passed on as a boolean when creating an agent ? |
Beta Was this translation helpful? Give feedback.
-
Hey everyone ! Recently, while building a model, I had quite a bit of trouble with referencing : I set the target attribute of one of my agents to another agent, but when the second was removed, it was still referenced in target. @quaquel explained to me weakref and how it could be of use in my case. After some thinking, I think that it could benefit to almost all cases.
The goal of this discussion is to study weather or not it could/should be incorporated directly into the Agent class, either as a permanent feature, either as an option that someone could toggle at the creation of their new agents. After some reseach, what I found was that I should be possible to complete the dunder methods that are in charge of the referencing : it could be done by overriding both
__setattr__
and__getattribute__
methods so the user can access attributes without even having to know if they're weak refs.Besides eliminating circular referencing issues and allowing automatic cleanup, this allows a more efficient memory management. There are also a few features that come directly with weak referencing that could be useful to statistics (for the agg method, hello @EwoutH) like ref counts, or the ability to naturally execute some code when a ref is about to be removed, etc.
All in all, I think that could be a useful feature, and I'm willing to put some work into it. What do you guys think ? Do you have some ideas ?
Beta Was this translation helpful? Give feedback.
All reactions