The Emperor's Dilemma was broken in two ways — one obvious, one silent #464
abhinavk0220
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Why the Emperor's Dilemma model matters — and what was quietly breaking it
The Emperor's Dilemma is one of the more intellectually rich models in this repo. It simulates how an unpopular norm spreads and persists in a society — the gap between what people privately believe and what they publicly perform. It's the kind of model that makes people stop and think, "wait, this explains something real."
Which is exactly why it bothered me that it was silently broken.
I spent some time tracing through the code (issue #337) and found two bugs that together made the model impossible to run and impossible to trust even if you worked around the crash.
Bug 1 — It wouldn't start at all
app.pyimported its model withfrom .model import EmperorModel.model.pyimported its agents withfrom .agents import EmperorAgent. These are relative imports — they only work when Python knows the package context. Solara loads example modules in a flat namespace, so it simply couldn't resolve them. Runningsolara run app.pythrew anImportErrorbefore a single agent was created.The fix is straightforward: switch to absolute imports (
from emperor_dilemma.model import ...), the same pattern used in every other working example in this repo.Bug 2 — The seed slider was decorative
This one is subtler, and I think more important.
model.pyhadimport randomat the top. During initialisation, it calledrandom.sample()to place true believers andrandom.uniform()to assign conviction values. The model exposes anrngparameter — there's even a seed input in the UI so users can reproduce runs.But
self.random(Mesa's seeded RNG) and the stdlibrandommodule are completely separate. Every time you ran the model, even with the same seed, the initial agent placement was different. The seed slider did nothing for the two most important initialisation decisions in the model.For a model that studies norm diffusion and social tipping points — where the spatial distribution of true believers is everything — this is not a cosmetic bug. It means no two runs were actually comparable.
Fixed in PR #463 by replacing both
random.*calls withself.random.*.A pattern worth watching
This is the second example in a short span (after Bank Reserves, PR #461) where I've found that the stdlib
randommodule snuck in alongside Mesa's own RNG. It's an easy mistake —random.sampleandself.random.samplelook nearly identical — but the consequence is that reproducibility, one of the core values of agent-based modelling, is silently undermined.It might be worth a note in the contribution guidelines or the review checklist (#390): grep for bare
random.calls in model files and flag them for review.Would love to hear if others have spotted this pattern elsewhere in the repo.
— Abhinav
Beta Was this translation helpful? Give feedback.
All reactions