Skip to content

Add support for dynamic discrete spaces#2755

Merged
quaquel merged 18 commits intomesa:mainfrom
quaquel:dynamic_discretespace
Apr 20, 2025
Merged

Add support for dynamic discrete spaces#2755
quaquel merged 18 commits intomesa:mainfrom
quaquel:dynamic_discretespace

Conversation

@quaquel
Copy link
Copy Markdown
Member

@quaquel quaquel commented Apr 14, 2025

Currently, all DiscreteSpace subclasses rely on @cache and @cached_property for 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 details

API

This PR adds explicit support for changing discrete spaces during the simulation. It adds 4 new methods to DiscreteSpace which means these are available to all subclasses. These methods are add_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. Cell already has a connect and disconnect method, so clearing the relevant cell caches is placed here. The new methods (add_cell, remove_cell, add_connection, remove_connection) are added to DiscreteSpace, and the relevant cache (all_cells) is cleared here as well.

The clearing of caches on connect and disconnect adds 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 if self.__dict__["name of cached property"] exists, but this adds code complexity).

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 14, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@quaquel quaquel added the enhancement Release notes label label Apr 14, 2025
@quaquel quaquel linked an issue Apr 14, 2025 that may be closed by this pull request
@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔴 +29.6% [+28.5%, +30.7%] 🔵 +0.5% [+0.3%, +0.6%]
BoltzmannWealth large 🔴 +36.1% [+17.7%, +45.5%] 🔵 +0.8% [+0.0%, +1.4%]
Schelling small 🔴 +55.1% [+54.8%, +55.4%] 🔵 +0.3% [+0.1%, +0.5%]
Schelling large 🔴 +58.0% [+55.5%, +61.2%] 🔵 -1.5% [-2.0%, -1.0%]
WolfSheep small 🔴 +24.7% [+24.2%, +25.3%] 🔵 -0.3% [-0.5%, -0.1%]
WolfSheep large 🔴 +27.8% [+27.3%, +28.2%] 🔴 +4.5% [+3.6%, +5.4%]
BoidFlockers small 🔵 +1.6% [+1.1%, +2.1%] 🔵 -0.8% [-1.0%, -0.6%]
BoidFlockers large 🔵 +1.3% [+0.9%, +1.7%] 🔵 -0.5% [-0.7%, -0.2%]

@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Apr 14, 2025

The increase in init_time is due to the clearing of non-existing caches on all cell.connect calls when instantiating the spaces. It's possible to resolve this but the numbers or tiny and it adds code complexity if we want to do that.

@quaquel quaquel mentioned this pull request Apr 14, 2025
@Sahil-Chhoker
Copy link
Copy Markdown
Collaborator

The increase in init_time is due to the clearing of non-existing caches on all cell.connect calls when instantiating the spaces. It's possible to resolve this but the numbers or tiny and it adds code complexity if we want to do that.

I am not asking for the complete implementation, but can you give me just a initial idea of how can the init time be reduced?

@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Apr 16, 2025

I am not asking for the complete implementation, but can you give me just a initial idea of how can the init time be reduced?

In Cell, there are 3 places were caching happens: neighborhood, _neighborhood, and get_neighborhood. These are tied together with neighborhood as the basis. neighborhood is a cached property. The cached value is stored in self.__dict__["neighborhood"]. So, if this field thas not exist, the other caches also won't exist and thus won't have to be cleared. No idea how much runtime it would save.

@Sahil-Chhoker
Copy link
Copy Markdown
Collaborator

But won't that eliminate the caching entirely from cell?

@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Apr 16, 2025

No, but why do you think that it might?

Copy link
Copy Markdown
Member

@EwoutH EwoutH left a comment

Choose a reason for hiding this comment

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

This is really cool, and a remarkably clean PR!

@Sahil-Chhoker
Copy link
Copy Markdown
Collaborator

No, but why do you think that it might?

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.

Copy link
Copy Markdown
Member

@tpike3 tpike3 left a comment

Choose a reason for hiding this comment

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

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.

@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Apr 17, 2025

I'll expand the docstrings a bit more probably tomorrow and merge this afterwards.

@quaquel quaquel added the trigger-benchmarks Special label that triggers the benchmarking CI label Apr 20, 2025
@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔴 +30.6% [+28.8%, +32.6%] 🔵 +0.0% [-0.2%, +0.2%]
BoltzmannWealth large 🔴 +57.3% [+38.1%, +93.7%] 🔵 -4.1% [-6.1%, -2.0%]
Schelling small 🔴 +48.4% [+48.0%, +48.8%] 🔵 +0.3% [-0.0%, +0.6%]
Schelling large 🔴 +42.0% [+34.4%, +46.6%] 🔵 -2.6% [-5.7%, +0.1%]
WolfSheep small 🔴 +18.7% [+18.3%, +19.1%] 🔵 -0.5% [-0.8%, -0.3%]
WolfSheep large 🔴 +19.8% [+19.1%, +20.5%] 🟢 -5.3% [-7.1%, -3.6%]
BoidFlockers small 🔵 -1.9% [-2.6%, -1.2%] 🔵 +0.2% [+0.1%, +0.3%]
BoidFlockers large 🔵 -0.7% [-1.2%, -0.3%] 🔵 +0.3% [+0.0%, +0.5%]

@quaquel quaquel merged commit a62f75e into mesa:main Apr 20, 2025
13 checks passed
@quaquel quaquel removed the trigger-benchmarks Special label that triggers the benchmarking CI label Apr 20, 2025
@quaquel quaquel deleted the dynamic_discretespace branch December 30, 2025 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dynamic Discrete Spaces

4 participants