adamamer20 / mesa-frames

Extension of mesa for performance and scalability
https://adamamer20.github.io/mesa-frames/api
MIT License
6 stars 1 forks source link

Refactoring mesa.Agent, mesa.AgentSet, mesa.Model -> AgentSetDF, AgentsDF, ModelDF #8

Closed adamamer20 closed 1 week ago

adamamer20 commented 5 months ago

1) Agents of the model are included in a unique container class AgentContainer, in its agents attribute. Agents of the same type are stored in a DataFrame in the AgentSet class. Multiple types (all agents of the model) are stored in a list of AgentSets. This avoids having many missing values (which would occupy memory) if Agents of different type were stored in the same Dataframe. 2) Went with encapsulation, avoiding extensions and subclassing because they didn't always work well with storing additional attributes and it wasn't easy to extend the created classes by subclassing (as it's often done with base mesa.Agent). 3) All operations are mutable: better aligment with base mesa API. Possibility of functional programming with the use of fast copy methods. 4) All methods and dunder methods that can act on a subset of agents support a custom mask = "active" that operates only on active agents. The private attribute is a mask (pl.Expr, pl.Series, pd.Series). The public property is self.active_agents. If you wanted to remove the agents you could simply use the "discard" or "remove" methods. 5) Dunder methods to facilitate interaction introduced on top. Maybe can be added also to base mesa. 6) pandas and polars are currently implemented in two different classes. Creating a single AgentSet class and use backend as environment variable or a method .to like Pytorch is not currently possible because type hinting is only static (see discussion #12). However having an abstract AgentSet class makes it easy to extend to other backends with relative ease.

rht commented 5 months ago

All operations are inplace (no immutability): if we wanted to keep immutability, there couldn't be method chaining with encapsulation (since methods would have to return a pd.Dataframe) If we were to return an AgentSet. I think it also aligns well with base mesa API.

Not sure if Polars supports inplace operations. Needs to check the documentation.

EwoutH commented 3 months ago

I like it on a high-level! Especially the active agents concept looks interesting.

Are there any parts that significantly differ from regular mesa?

Is there anything that you are in doubt of or would like to have a more in-depth review of?

adamamer20 commented 1 month ago

@rht i'm a bit behind the schedule as i'm working only during the evenings because i still have some exams left. from mid june i will be completely free and i will give my complete attention to development!

rht commented 1 month ago

No worries, looking forward it.

adamamer20 commented 1 week ago

@rht I have made the last commit. For me it's ready to merge, let me know what you think about it. I went back to inplace operations because the API would be a bit more complex and too different from base mesa. I tried implementing a unique AgentSet class but type hinting becomes fairly complicated, especially with subclassing (see #12). Maybe in the future.

rht commented 1 week ago

Great progress so far!

adamamer20 commented 1 week ago

Thank you!