-
Notifications
You must be signed in to change notification settings - Fork 929
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
Add agent state management system with discrete & continuous states #2547
base: main
Are you sure you want to change the base?
Conversation
…ates Adds a minimal state management system to Mesa that supports: - Discrete states with explicit value changes - Continuous states that update based on elapsed time - Composite states derived from other states - Integration with Mesa's Agent class via StateAgent This provides a clean API for managing agent states in simulations while handling both discrete events and continuous changes efficiently.
This comment was marked as off-topic.
This comment was marked as off-topic.
I updated the PR to:
The API is now much cleaner! |
I haven't seen the discussion and haven't looked at the implementation yet, but this reads fantastic! The API seems simple and powerful. Really looking forward to how this evolved, but congratulations already! |
Thanks! Only thing I'm a bit worried about is performance. Don't make things states that don't need to be a state I guess. |
The next thing is that I would like to do is implement triggers for state changes:
These thresholds can be read (Boolean value) but could also emit an event to an event scheduler. This would tightly integrate with potentially Mesa Observables and DEVS, so we need a bit of coordination on this. |
I also looked a bit into how it could integrate with Mesa Observables. I have a rough Proof of Concept here: @quaquel I would love to hear your take on it. |
There is a lot going on here. I need time to look at it closely and make up my mind. Given that is experimental, I believe we can push foward regardless. However, some quick reactions
|
Thanks for thinking critically about it. The main advantage is to decouple state updates from the main, fixed timestep, agent logic. Especially if you have a DEVS scheduler, you can now update states continuously, instead of them being literal step functions that get updated only on the whole time steps. With the thresholds, it also allows events to fire exactly upon reaching a certain state. It does add complexity and maybe makes some things more implicit/hidden. I would like to explore where this is worth it. |
@projectmesa/maintainers and others, I could use some additional eyes on this.
And any other suggestions ideas. I mainly need to know if this is an idea worth pushing further, or not. |
I am with @quaquel on all comments, but I want to +1 -- it is experimental - so I am less hesitant to stop integration. My first concern was -- how this helps or hinders performance .... I saw the comment on this as well. This is something we should evaluate. The question is -- evaluate before or after integration into experimental? Overall though -- this is potentially very interesting and exciting! |
The basic version of purely automatic state updates based on elapsed time does not seem particularly useful because it adds little. I see two possible directions to make it more useful. One option is to tie it to The other option just occurred to me but might require some further testing. In short, we might be able to bridge between mesa-frames and normal mesa with dedicated state descriptors. Let me explain: both property layers and the new style continuous space use objects with attributes that are views into an underlying numpy array. That is, the state data is stored not inside the agent attribute but is part of a larger numpy array. What if, in, say, the Boltzmann Wealth Model, If I have an hour somewhere, I can probably code up a proof of principle of this idea. Let me know if you want it.
As indicated before, I don't necessarily like the implicit nature of state updates as presented here. But if tied to a reactive paradigm, I think this can be overcome. If we go down option two first (so numpy array based collective state), there is some further design work required. The critical thing to handle carefully is the addition and removal of agents. I found a clever solution with numpy for continuous space (see
Given 1 and 2, I think some further work might be required before there is a need to discuss the imlementation details |
This PR builds on discussion #2529.
Summary
This PR adds a new state management framework to Mesa, enabling both discrete and continuous state management in agents. It provides a clean, minimal API for managing agent states that can change both discretely (at specific moments) and continuously (over time), as well as composed states that derive their values from other states.
Motive
Agent-based models often need to track various agent states that change in different ways. While discrete state changes are straightforward to implement, continuous changes (like energy depletion, resource growth) are more challenging to model efficiently. Current approaches often require manual calculations each timestep or complex event scheduling.
This framework addresses several common challenges:
Implementation
The implementation adds four new classes:
State
: Base class defining the state interfaceDiscreteState
: For states that change at specific momentsContinuousState
: For states that change continuously over timeCompositeState
: For states derived from other statesAnd a mixin class:
StateAgent
: Extension of Mesa's Agent class with state management capabilitiesKey implementation details:
agent.some_state
.Usage Examples
Here are the key API examples:
Here are three small, complete examples showing different use cases:
Additional Notes
Future Enhancements:
Related Work
To review
self.energy
in an Agent, like we recently added to the PropertyLayer Cell Space integration (see Add full support for property layers to cell spaces #2512 (comment))