mesa-examples icon indicating copy to clipboard operation
mesa-examples copied to clipboard

fix(emperor-dilemma): replace relative imports and fix unseeded random calls

Open abhinavk0220 opened this issue 7 months ago • 1 comments

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.py starts without ImportError
  • [ ] 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

abhinavk0220 avatar Apr 14 '26 16:04 abhinavk0220

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.md we have solara run app.py as the run command. This implicitly assumes that the user is running the example when the working directory is examples/emperor_dilemma. Therefore, your fix will result in ModuleNotFoundError: No module named 'emperor_dilemma'. My suggestion is to move the agents.py and model.py into an insider directory emperor_dilemma (similar to bank_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.

sadrasabouri avatar May 26 '26 00:05 sadrasabouri