add ObservableAgentDataSet for reactive observable agent tracking
Summary
This PR adds ObservableAgentDataSet, a reactive dataset that subscribes to observable agent properties using the mesa_signals system. Unlike AgentDataSet, which gathers values when .data is accessed, ObservableAgentDataSet
maintains an active view by subscribing to ObservableSignals.CHANGED for the specified fields. The dataset updates its internal rows whenever an observable emits a change signal. Calling .data returns this maintained view instead of recomputing values each time.
Part of the improvements suggested in #3341
Motive
The current datasets follow a pull-based pattern where values are collected when .data is accessed. With the introduction of the signals system and Observable descriptors, agents can emit signals when their state changes. This makes it possible to maintain datasets reactively rather than recomputing snapshots repeatedly.
ObservableAgentDataSet provides this push-based alternative by subscribing to observable fields and updating its cached view when changes occur.
Implementation
The new dataset subscribes to ObservableSignals.CHANGED for specified observable fields on agents, maintains a cached dataset view (_view) containing rows for each agent, updates the relevant row when a signal is emitted and returns this active view when .data is accessed.
A convenience method was also added to DataRegistry
DataRegistry.track_agents_observable(...)
This creates and registers an ObservableAgentDataSet for the specified agents and fields.
Usage Examples
class MyAgent(Agent, HasEmitters):
wealth = Observable()
dataset = model.data_registry.track_agents_observable(
model.agents,
"wealth_data",
fields="wealth",
)
data = dataset.data
The dataset updates automatically when an agent's observable field changes.
Performance benchmarks:
| Model | Size | Init time [95% CI] | Run time [95% CI] |
|---|---|---|---|
| BoltzmannWealth | small | 🔵 -0.9% [-1.3%, -0.6%] | 🔵 -0.3% [-0.5%, -0.1%] |
| BoltzmannWealth | large | 🔵 +1.5% [+0.8%, +2.0%] | 🔵 -0.8% [-1.7%, -0.2%] |
| Schelling | small | 🔵 +0.6% [+0.3%, +1.0%] | 🔵 +0.2% [+0.0%, +0.3%] |
| Schelling | large | 🔵 +1.4% [+1.2%, +1.7%] | 🔴 +3.4% [+3.0%, +3.9%] |
| WolfSheep | small | 🔵 -0.5% [-0.7%, -0.3%] | 🔵 +0.8% [+0.6%, +0.9%] |
| WolfSheep | large | 🔵 -0.2% [-1.2%, +0.6%] | 🔵 -0.0% [-0.8%, +0.7%] |
| SugarscapeG1mt | small | 🔵 -0.2% [-0.6%, +0.2%] | 🔵 +0.4% [+0.3%, +0.6%] |
| SugarscapeG1mt | large | 🔵 -1.0% [-1.3%, -0.7%] | 🔵 -0.3% [-0.5%, -0.1%] |
| BoidFlockers | small | 🔵 +1.4% [+1.1%, +1.6%] | 🔵 -0.1% [-0.2%, +0.0%] |
| BoidFlockers | large | 🔵 +2.0% [+1.6%, +2.3%] | 🔵 +0.3% [+0.1%, +0.4%] |
Hi @souro26 If this is ready, would you mind sharing some performance benchmarks by trying it on existing examples?
sure I'll post some benchmarks very soon
I ran a benchmark using the Boltzmann Wealth example comparing AgentDataSet and ObservableAgentDataSet. The benchmark measures initialization and step execution time while accessing .data each step.
50 agents, 100 steps
AgentDataSet: 23 ms
ObservableAgentDataSet: 13 ms
100 agents, 100 steps
AgentDataSet: 60 ms
ObservableAgentDataSet: 32 ms
500 agents, 100 steps
AgentDataSet: 303 ms
ObservableAgentDataSet: 146 ms
In this workload where the dataset is accessed every step, the observable dataset reduces step execution time by around 40 to 50%. Initialization overhead is small and mainly comes from signal subscription setup.
Self-review
- Implemented
ObservableAgentDataSetas a push-based alternative toAgentDataSet. Instead of recomputing values on .data access, it subscribes toObservableSignals.CHANGEDand incrementally updates a cached view. - Internal structure keeps
_rows_by_agentfor O(1) updates and_viewfor stable iteration or output. - Dataset builds an initial snapshot on initialization, then updates rows when observables emit change signals.
- Signals from agents not tracked by the dataset are ignored and subscriptions are cleaned up in close().