fix(emperor-dilemma): replace relative imports and fix unseeded random calls
Summary
Fixes two bugs preventing the Emperor's Dilemma example from running, as reported in #337.
Bug 1 — Relative imports break Solara module resolution
app.py used from .model import EmperorModel and model.py used from .agents import EmperorAgent. Solara loads example modules in a flat namespace and cannot resolve relative imports. Fixed by switching to absolute imports (from emperor_dilemma.model import ...), consistent with how other examples in this repo are structured (e.g. bank_reserves).
Bug 2 — Module-level random bypasses the seeded RNG
model.py imported the stdlib random module and called random.sample() and random.uniform() during agent initialisation. This means the rng parameter passed to EmperorModel() — and exposed as a slider in the UI — had no effect on the initial believer placement or conviction values, making runs non-reproducible. Fixed by replacing both calls with self.random.sample() and self.random.uniform().
Test plan
- [ ]
solara run app.pystarts withoutImportError - [ ] Agents render on the grid with correct colors (comply/enforce states)
- [ ] Changing the Random Seed slider and clicking Reset produces a different initial configuration
- [ ] Line plot shows Compliance / Enforcement / False Enforcement over time
🤖 Generated with Claude Code
Hey @abhinavk0220 I verify that the issue you're trying to solve is still there (Python 3.12.13 and Mesa 4.0.0a0). I reviewed your PR, and here are some comments that can enhance it:
- Currently, in
examples/emperor_dilemma/README.mdwe havesolara run app.pyas the run command. This implicitly assumes that the user is running the example when the working directory isexamples/emperor_dilemma. Therefore, your fix will result inModuleNotFoundError: No module named 'emperor_dilemma'. My suggestion is to move theagents.pyandmodel.pyinto an insider directoryemperor_dilemma(similar tobank_reserves). - nit: make atomic-scoped PRs. The two bugs being solved by this PR do not live in the same scope and could be two separate valid PR.
The rest looks good to me.