Skip to content

Making Model reactive #3211

@quaquel

Description

@quaquel

As part of various ongoing conversations, it has been suggested to make the Model reactive. Specifically, the idea is that model.time becomes observable and that whenever agents are registered or deregistered, also a signal is emitted.

I started some pathfinding for this.

  1. Making time observable is trivial. Just let Model extend HasObservables, and declare time as observable is all that is needed.
  2. Making agent registration and deregistration is not straightforward. Mesa signals are built around class-level declarations that stuff is observable. But for agents, this is currently not easy to do. It is possible to hack around this by just adding "agents" to self.observables with 2 possible signals (AGENT_ADDED and AGENT_REMOVED). However, a cleaner solution would be nice.
  3. We recently shifted to using enums for declaring signal types. But the typing for this is tricky. For example, self.notify specifies signal_type to be either a SignalType or a str, but for the model.agent stuff, I declare a new model-specific ModelSignals enum, which breaks typing.
  4. The performance overhead is not bad. Testing locally, I get the numbers below. The init times, I wouldn't worry about much. The main conversation will focus on the wolf-sheep runtime. Here, we see a clear performance loss at runtime. This is due to agents dying and being born, which call register_agent and deregister_agent, which in turn trigger the emit logic. Some further profiling is probably needed because the overhead is larger than I expected. Remember, in these benchmarks, nothing is listening to these signals.
Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔴 +14.5% [+13.0%, +15.9%] 🔴 +3.6% [+3.1%, +4.2%]
BoltzmannWealth large 🔴 +17.8% [+16.0%, +19.3%] 🔵 +1.8% [-2.9%, +6.3%]
Schelling small 🔴 +7.8% [+7.0%, +8.5%] 🔵 +0.1% [-0.8%, +1.1%]
Schelling large 🔴 +7.5% [+4.9%, +9.8%] 🔵 +1.2% [-3.8%, +6.1%]
WolfSheep small 🔴 +21.5% [+20.8%, +22.2%] 🔴 +4.3% [+3.6%, +5.0%]
WolfSheep large 🔴 +27.1% [+25.1%, +29.1%] 🔴 +16.3% [+13.3%, +19.1%]
BoidFlockers small 🔴 +19.0% [+18.0%, +19.9%] 🔵 +2.4% [+1.6%, +3.2%]
BoidFlockers large 🔴 +18.5% [+17.7%, +19.2%] 🔵 +1.8% [+1.0%, +2.7%]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions