-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: Support adaptive Voronoi space with agent-based clustering #2989
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
base: main
Are you sure you want to change the base?
Conversation
|
Performance benchmarks:
|
|
This is quite cool. Thanks, also for documenting it well and the visualization. I think the big question now is, how does this fit in the bigger picture? Is this mainly a way to aggregate data? Or do agents also make decisions based on their cluster or cluster values? What are the use cases? Building some example models with this might help with that. |
@EwoutH, I see this being used for both.
I'll work on a simple 'Cultural Segregation' example model to demonstrate how agents can interact with these emergent regions and use the cell's aggregate values to drive their behavior. I'll update the Draft PR once that's ready. |
|
@EwoutH, kindly review it now. |
|
Sorry I'm not going to be able to move this fast, since this is quite a large PR with a feature being added to our stable user API. That takes some consideration of complexity, proportionality, use cases. We just took a sprint to get 3.4.0 out, so it might take a while. Sorry. |
I understand. |
Summary
Fixes #2961
This PR introduces the
AdaptiveVoronoiSpaceclass, a dynamic spatial component for Mesa that reconfigures its Voronoi regions based on real-time agent density and distribution. By leveraging K-Means clustering, the space allows regional boundaries to emerge organically from agent positions rather than relying on a static grid.Motive
Traditional spatial models often use fixed environments. However, many social and biological phenomena, such as urban growth, cultural regionalization, and economic market territories involve boundaries that shift as agents move. This feature provides a native way to study these emergent spatial patterns, enabling agents to sense and react to local regions that are themselves defined by the collective distribution of agents.
Implementation
The feature is built as a subclass of
ContinuousSpaceto ensure high performance and compatibility with existing Mesa utilities.ContinuousSpaceto reuse optimized NumPy position caches (_agent_points).sklearn.cluster.KMeansto find centroids based on current agent coordinates.scipy.spatial.cKDTreeforO(logN)region membership lookups, avoiding the need for complex polygon geometry calculations.rebuild_voronoi()method that allows developers to control the frequency of spatial reconfiguration to balance accuracy and performance.Usage Examples
Developers can initialize the space by specifying the desired number of dynamic clusters.
The provided visualization script uses
scipy.spatial.voronoi_plot_2dto display the emergent cells.The script generates 5 distinct blobs of agents and uses the adaptive logic to find the centroids and boundaries.
Additional Notes
scikit-learnandscipy. I have added these to a new adaptive optional dependency group inpyproject.tomlto keep the core installation lightweight.tests/test_space_adaptive.pywith complete coverage of clustering logic, neighbor retrieval, and error handling for low agent counts.I have added a full advanced example in
mesa/examples/advanced/cultural_segregation/that demonstrates a generative feedback loop:Voronoi boundaries are updated every step based on where agents have migrated.
Agents analyze their Voronoi cell's average cultural opinion and decide whether to settle or move to find a more compatible region.