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

Cell space: step() function for cells #2562

Open
wang-boyu opened this issue Dec 22, 2024 · 1 comment
Open

Cell space: step() function for cells #2562

wang-boyu opened this issue Dec 22, 2024 · 1 comment

Comments

@wang-boyu
Copy link
Member

What's the problem this feature will solve?

Instead of writing vectorized operations of property layer, users could write step() functions for individual cells, similar to how NetLogo handles its patches, or how Mesa-Geo currently manages its Cell class. In a way Cells act like agents with changing states, while they can't move.

For example in our urban growth gis example, users can define how individual Cell should update itself: https://github.com/projectmesa/mesa-examples/blob/0c93dc50f2c9cbcaf1c98287c6beca85fa6c6233/gis/urban_growth/urban_growth/space.py#L49

This is much slower than vectorized operations on the entire property layer at once: https://github.com/projectmesa/mesa-examples/blob/0c93dc50f2c9cbcaf1c98287c6beca85fa6c6233/examples/conways_game_of_life_fast/model.py#L43-L47

However, it might be easier to write in certain cases, especially when it's difficult to vectorize model logic.

Describe the solution you'd like

Perhaps have something similar to how AgentSet activites agents through its do, shuffle_do and other methods, in CellCollection class to activate cells' step() and other functions. A difference could be that agents are indexed by unique_id, whereas cells are indexed by (x, y) or (row, col) indices. Both AgentSet and CellCollection already have a select() method with similar API:

mesa/mesa/agent.py

Lines 200 to 206 in 393f6a0

def select(
self,
filter_func: Callable[[Agent], bool] | None = None,
at_most: int | float = float("inf"),
inplace: bool = False,
agent_type: type[Agent] | None = None,
) -> AgentSet:

def select(
self,
filter_func: Callable[[T], bool] | None = None,
at_most: int | float = float("inf"),
):

Similarly for property layer:

def select_cells(self, condition: Callable, return_list=True):

Hence I think that a property layer can be viewed as a collection of its cells, i.e., a CellCollection. For example, AgentSet has an aggregation function:

def agg(self, attribute: str, func: Callable) -> Any:

and similarly for property layer:

def aggregate(self, operation: Callable):

There are set_cells, select_cells and modify_cells methods in property layer, although here cells do not link to the Cell class that is mainly agent containers with neighborhood definitions.

@quaquel
Copy link
Member

quaquel commented Dec 22, 2024

This is already possible in the new style cell space by using a FixedAgent.

I would be hesitant to make a cell, which encapsulates connection and has a collection of agents, itself an agent or even agent-like. To me, this violates the separation of concerns.

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

No branches or pull requests

2 participants