Skip to content

Clean up pd_grid example activation logic#3349

Merged
EwoutH merged 1 commit intomainfrom
pd_grid_cleanup
Feb 19, 2026
Merged

Clean up pd_grid example activation logic#3349
EwoutH merged 1 commit intomainfrom
pd_grid_cleanup

Conversation

@EwoutH
Copy link
Copy Markdown
Member

@EwoutH EwoutH commented Feb 19, 2026

Summary

Cleans up the pd_grid example by separating agent logic from activation control.

Motive

PDAgent.step() contained a check against model.activation_order to decide whether to call self.advance(). This couples the agent to the model's activation regime. Agents shouldn't need to know how they're being activated.

Implementation

  • Removed the if activation_order != "Simultaneous": self.advance() check from PDAgent.step(), so step() only computes next_move.
  • Updated PdGrid.step() to use method chaining and lambdas for clear activation semantics:
    • Sequential: do(lambda a: (a.step(), a.advance()))
    • Random: shuffle_do(lambda a: (a.step(), a.advance()))
    • Simultaneous: do("step").do("advance")

Additional Notes

increment_score still checks activation_order to decide whether to read move or next_move from neighbors. This is inherent to the simultaneous game semantics, not an activation concern.

If we find this too ugly:

do(lambda a: (a.step(), a.advance()))

We might update do and shuffle_do to allow taking multiple method names or callables:

# For each agent, call step, call advance, then the next agent
do("step", "advance")

Move activation control fully into the model by removing the activation_order check from PDAgent.step(). Use method chaining and lambdas to express the three activation regimes clearly.
@EwoutH EwoutH added the example Changes the examples or adds to them. label Feb 19, 2026
Copy link
Copy Markdown
Member

@quaquel quaquel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine to me.

I don't see a need to change do. It is a very particular use case in this example so I am fine with the lambda.

At some point, I want to update this example drawing on my version of this model that I use in teaching which has more strategies and adds evolution.

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.4% [-0.2%, +1.1%] 🔵 -2.1% [-2.4%, -1.8%]
BoltzmannWealth large 🔵 +2.6% [+1.4%, +3.7%] 🔴 +12.7% [+9.0%, +16.2%]
Schelling small 🔵 +1.3% [+0.7%, +1.9%] 🔵 +2.1% [+1.8%, +2.4%]
Schelling large 🔵 -2.5% [-3.7%, -1.4%] 🟢 -7.7% [-11.0%, -4.6%]
WolfSheep small 🔵 -3.5% [-4.0%, -3.0%] 🔵 -1.3% [-1.6%, -1.1%]
WolfSheep large 🟢 -5.9% [-7.7%, -4.0%] 🟢 -8.2% [-10.6%, -5.6%]
BoidFlockers small 🔵 -2.3% [-2.7%, -1.9%] 🔵 -1.7% [-2.0%, -1.5%]
BoidFlockers large 🔵 -2.3% [-2.5%, -2.0%] 🔵 -1.9% [-2.1%, -1.7%]

@EwoutH EwoutH merged commit 746f1e4 into main Feb 19, 2026
13 of 14 checks passed
EwoutH added a commit that referenced this pull request Mar 15, 2026
Move activation control fully into the model by removing the activation_order check from PDAgent.step(). Use method chaining and lambdas to express the three activation regimes clearly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

example Changes the examples or adds to them.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants