Add support for dynamic discrete spaces#2755
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
Performance benchmarks:
|
|
The increase in init_time is due to the clearing of non-existing caches on all |
I am not asking for the complete implementation, but can you give me just a initial idea of how can the |
In |
|
But won't that eliminate the caching entirely from cell? |
|
No, but why do you think that it might? |
EwoutH
left a comment
There was a problem hiding this comment.
This is really cool, and a remarkably clean PR!
I am not sure about the current structure, I will do some individual research later. Thanks again for these discussions, learning a lot from these. |
tpike3
left a comment
There was a problem hiding this comment.
This is great, I think we can worry about decreasing the init later. I know we have at least one user who needs this functionality.
|
I'll expand the docstrings a bit more probably tomorrow and merge this afterwards. |
|
Performance benchmarks:
|
Currently, all
DiscreteSpacesubclasses rely on@cacheand@cached_propertyfor neighborhood-related functionality. This is critical for performance. However, if you want to represent a dynamically changing discrete space (e.g., a dynamic network), this is currently not possible. See ##2754 for more detailsAPI
This PR adds explicit support for changing discrete spaces during the simulation. It adds 4 new methods to
DiscreteSpacewhich means these are available to all subclasses. These methods areadd_cell,remove_cell,add_connection,remove_connection. With these, you can make any modification to a discrete space that you want. For example, it allows you to dynamically change a network or impose a barrier in an orthogonal grid by removing connections between specific cells.Implementation details
This adds support for adding and removing cells and connections to discrete spaces. The critical issue is that discrete spaces relies on caching neighborhoods for substantial performance gains.
Cellalready has aconnectanddisconnectmethod, so clearing the relevant cell caches is placed here. The new methods (add_cell,remove_cell,add_connection,remove_connection) are added toDiscreteSpace, and the relevant cache (all_cells) is cleared here as well.The clearing of caches on
connectanddisconnectadds some additional overhead when constructing a discrete space. It might be possible to reduce this overhead by checking if the cache is already active (long story short, this can be done by checking ifself.__dict__["name of cached property"]exists, but this adds code complexity).